Fix a syntax error in Immunity's short effect.
[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', 504),
32
33 # Other languages
34 (u'イーブイ', 'pokemon', 133),
35 (u'Iibui', 'pokemon', 133),
36 (u'Eievui', 'pokemon', 133),
37 (u'이브이', 'pokemon', 133),
38 (u'伊布', 'pokemon', 133),
39 (u'Evoli', 'pokemon', 133),
40 ]
41
42 for input, table, id in tests:
43 results = lookup.lookup(input)
44 assert_equal(len(results), 1, u"'%s' returns one result" % input)
45 assert_equal(results[0].exact, True, u"'%s' match exactly" % input)
46
47 row = results[0].object
48 assert_equal(row.__tablename__, table, u"'%s' is in the right table" % input)
49 assert_equal(row.id, id, u"'%s' returns the right id" % input)
50
51
52 def test_id_lookup():
53 results = lookup.lookup(u'1')
54 assert_true(len(results) >= 5, u'At least five things have id 1')
55 assert_true(all(_.object.id == 1 for _ in results),
56 u'All results have id 1')
57
58 def test_multi_lookup():
59 results = lookup.lookup(u'Metronome')
60 assert_equal(len(results), 2, u'Two things called "Metronome"')
61 assert_true(results[0].exact, u'Metronome matches are exact')
62
63
64 def test_type_lookup():
65 results = lookup.lookup(u'pokemon:1')
66 assert_equal(results[0].object.__tablename__, 'pokemon',
67 u'Type restriction works correctly')
68 assert_equal(len(results), 1, u'Only one id result when type is specified')
69 assert_equal(results[0].object.name, u'Bulbasaur',
70 u'Type + id returns the right result')
71
72 results = lookup.lookup(u'1', valid_types=['pokemon'])
73 assert_equal(results[0].object.name, u'Bulbasaur',
74 u'valid_types works as well as type: prefix')
75
76 def test_language_lookup():
77 # There are two objects named "charge": the move Charge, and the move
78 # Tackle, which is called "Charge" in French.
79 results = lookup.lookup(u'charge')
80 assert_true(len(results) > 1, u'There are multiple "charge"s')
81
82 results = lookup.lookup(u'@fr:charge')
83 assert_equal(results[0].iso639, u'fr', u'Language restriction works correctly')
84 assert_equal(len(results), 1, u'Only one "charge" result when language is specified')
85 assert_equal(results[0].object.name, u'Tackle',
86 u'Language + vague name returns the right result')
87
88 results = lookup.lookup(u'charge', valid_types=['@fr'])
89 assert_equal(results[0].object.name, u'Tackle',
90 u'valid_types works as well as @lang: prefix')
91
92 results = lookup.lookup(u'@fr,move:charge')
93 assert_equal(results[0].object.name, u'Tackle',
94 u'Languages and types both work together')
95
96 results = lookup.lookup(u'@fr:charge', valid_types=['move'])
97 assert_equal(results[0].object.name, u'Tackle',
98 u'valid_types and language prefixes get along')
99
100 def test_fuzzy_lookup():
101 tests = [
102 # Regular English names
103 (u'chamander', u'Charmander'),
104 (u'pokeball', u'Poké Ball'),
105
106 # Names with squiggles in them
107 (u'farfetchd', u"Farfetch'd"),
108 (u'porygonz', u'Porygon-Z'),
109
110 # Sufficiently long foreign names
111 (u'カクレオ', u'Kecleon'),
112 (u'Yamikrasu', u'Murkrow'),
113 ]
114
115 for misspelling, name in tests:
116 results = lookup.lookup(misspelling)
117 first_result = results[0]
118 assert_equal(first_result.object.name, name,
119 u'Simple misspellings are corrected')
120
121 results = lookup.lookup(u'Nidoran')
122 top_names = [_.object.name for _ in results[0:2]]
123 assert_true(u'Nidoran♂' in top_names, u'Nidoran♂ is a top result for "Nidoran"')
124 assert_true(u'Nidoran♀' in top_names, u'Nidoran♀ is a top result for "Nidoran"')
125
126 def test_wildcard_lookup():
127 tests = [
128 (u'pokemon:*meleon', u'Charmeleon'),
129 (u'item:master*', u'Master Ball'),
130 (u'ee?ee', u'Eevee'),
131 ]
132
133 for wildcard, name in tests:
134 results = lookup.lookup(wildcard)
135 first_result = results[0]
136 assert_equal(first_result.object.name, name,
137 u'Wildcards work correctly')
138
139 def test_random_lookup():
140 for _ in xrange(5):
141 results = lookup.lookup(u'random')
142 assert_equal(len(results), 1, u'Random returns one result')
143
144 for table_name in [u'pokemon', u'moves', u'items', u'abilities', u'types']:
145 results = lookup.lookup(u'random', valid_types=[table_name])
146 assert_equal(len(results), 1, u'Constrained random returns one result')
147 assert_equal(results[0].object.__tablename__, table_name,
148 u'Constrained random returns result from the right table')
149
150 def test_crash_empty_prefix():
151 """Searching for ':foo' used to crash, augh!"""
152 results = lookup.lookup(u'Eevee')
153 assert_equal(results[0].object.name, u'Eevee', u'Empty prefix dun crash')