a5c30c408d41bfea5010820d34c252a118c227be
[zzz-pokedex.git] / pokedex / tests / test_lookup.py
1 # encoding: utf8
2 from nose.tools import *
3 import unittest
4
5 from pokedex.lookup import PokedexLookup
6
7 lookup = None
8
9 def setup():
10 # Recreate data
11 global lookup
12 lookup = PokedexLookup()
13 lookup.rebuild_index()
14
15 def test_exact_lookup():
16 tests = [
17 # Simple lookups
18 (u'Eevee', 'pokemon', 133),
19 (u'Scratch', 'moves', 10),
20 (u'Master Ball', 'items', 1),
21 (u'normal', 'types', 1),
22 (u'Run Away', 'abilities', 50),
23
24 # Funny characters
25 (u'Mr. Mime', 'pokemon', 122),
26 (u"Farfetch'd", 'pokemon', 83),
27 (u'Poké Ball', 'items', 4),
28
29 # Forms
30 (u'Rotom', 'pokemon', 479),
31 (u'Wash Rotom', 'pokemon_forms',10059),
32 (u'East Shellos', 'pokemon_forms',10039),
33
34 # Other languages
35 (u'イーブイ', 'pokemon', 133),
36 (u'Iibui', 'pokemon', 133),
37 (u'Eievui', 'pokemon', 133),
38 (u'이브이', 'pokemon', 133),
39 (u'伊布', 'pokemon', 133),
40 (u'Evoli', 'pokemon', 133),
41 ]
42
43 for input, table, id in tests:
44 results = lookup.lookup(input)
45 assert_equal(len(results), 1, u"'%s' returns one result" % input)
46 assert_equal(results[0].exact, True, u"'%s' match exactly" % input)
47
48 row = results[0].object
49 assert_equal(row.__tablename__, table, u"'%s' is in the right table" % input)
50 assert_equal(row.id, id, u"'%s' returns the right id" % input)
51
52
53 def test_id_lookup():
54 results = lookup.lookup(u'1')
55 assert_true(len(results) >= 5, u'At least five things have id 1')
56 assert_true(all(_.object.id == 1 for _ in results),
57 u'All results have id 1')
58
59 def test_multi_lookup():
60 results = lookup.lookup(u'Metronome')
61 assert_equal(len(results), 2, u'Two things called "Metronome"')
62 assert_true(results[0].exact, u'Metronome matches are exact')
63
64
65 def test_type_lookup():
66 results = lookup.lookup(u'pokemon:1')
67 assert_equal(results[0].object.__tablename__, 'pokemon',
68 u'Type restriction works correctly')
69 assert_equal(len(results), 1, u'Only one id result when type is specified')
70 assert_equal(results[0].object.name, u'Bulbasaur',
71 u'Type + id returns the right result')
72
73 results = lookup.lookup(u'1', valid_types=['pokemon'])
74 assert_equal(results[0].object.name, u'Bulbasaur',
75 u'valid_types works as well as type: prefix')
76
77 def test_language_lookup():
78 # There are two objects named "charge": the move Charge, and the move
79 # Tackle, which is called "Charge" in French.
80 results = lookup.lookup(u'charge')
81 assert_true(len(results) > 1, u'There are multiple "charge"s')
82
83 results = lookup.lookup(u'@fr:charge')
84 assert_equal(results[0].iso639, u'fr', u'Language restriction works correctly')
85 assert_equal(len(results), 1, u'Only one "charge" result when language is specified')
86 assert_equal(results[0].object.name, u'Tackle',
87 u'Language + vague name returns the right result')
88
89 results = lookup.lookup(u'charge', valid_types=['@fr'])
90 assert_equal(results[0].object.name, u'Tackle',
91 u'valid_types works as well as @lang: prefix')
92
93 results = lookup.lookup(u'@fr,move:charge')
94 assert_equal(results[0].object.name, u'Tackle',
95 u'Languages and types both work together')
96
97 results = lookup.lookup(u'@fr:charge', valid_types=['move'])
98 assert_equal(results[0].object.name, u'Tackle',
99 u'valid_types and language prefixes get along')
100
101 def test_fuzzy_lookup():
102 tests = [
103 # Regular English names
104 (u'chamander', u'Charmander'),
105 (u'pokeball', u'Poké Ball'),
106
107 # Names with squiggles in them
108 (u'farfetchd', u"Farfetch'd"),
109 (u'porygonz', u'Porygon-Z'),
110
111 # Sufficiently long foreign names
112 (u'カクレオ', u'Kecleon'),
113 (u'Yamikrasu', u'Murkrow'),
114 ]
115
116 for misspelling, name in tests:
117 results = lookup.lookup(misspelling)
118 first_result = results[0]
119 assert_equal(first_result.object.name, name,
120 u'Simple misspellings are corrected')
121
122 results = lookup.lookup(u'Nidoran')
123 top_names = [_.object.name for _ in results[0:2]]
124 assert_true(u'Nidoran♂' in top_names, u'Nidoran♂ is a top result for "Nidoran"')
125 assert_true(u'Nidoran♀' in top_names, u'Nidoran♀ is a top result for "Nidoran"')
126
127 def test_wildcard_lookup():
128 tests = [
129 (u'pokemon:*meleon', u'Charmeleon'),
130 (u'item:master*', u'Master Ball'),
131 (u'ee?ee', u'Eevee'),
132 ]
133
134 for wildcard, name in tests:
135 results = lookup.lookup(wildcard)
136 first_result = results[0]
137 assert_equal(first_result.object.name, name,
138 u'Wildcards work correctly')
139
140 def test_random_lookup():
141 for _ in xrange(5):
142 results = lookup.lookup(u'random')
143 assert_equal(len(results), 1, u'Random returns one result')
144
145 for table_name in [u'pokemon', u'moves', u'items', u'abilities', u'types']:
146 results = lookup.lookup(u'random', valid_types=[table_name])
147 assert_equal(len(results), 1, u'Constrained random returns one result')
148 assert_equal(results[0].object.__tablename__, table_name,
149 u'Constrained random returns result from the right table')
150
151 def test_crash_empty_prefix():
152 """Searching for ':foo' used to crash, augh!"""
153 results = lookup.lookup(u'Eevee')
154 assert_equal(results[0].object.name, u'Eevee', u'Empty prefix dun crash')