Bump required SQLA to 0.6.
[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].name, u'Bulbasaur', u'Type + id returns the right result')
70
71 results = lookup.lookup(u'1', valid_types=['pokemon'])
72 assert_equal(results[0].name, u'Bulbasaur', u'valid_types works as well as type: prefix')
73
74 def test_fuzzy_lookup():
75 tests = [
76 # Regular English names
77 (u'chamander', u'Charmander'),
78 (u'pokeball', u'Poké Ball'),
79
80 # Names with squiggles in them
81 (u'farfetchd', u"Farfetch'd"),
82 (u'porygonz', u'Porygon-Z'),
83
84 # Sufficiently long foreign names
85 (u'カクレオ', u'Kecleon'),
86 (u'Yamikrasu', u'Murkrow'),
87 ]
88
89 for misspelling, name in tests:
90 results = lookup.lookup(misspelling)
91 first_result = results[0]
92 assert_equal(first_result.object.name, name,
93 u'Simple misspellings are corrected')
94
95 results = lookup.lookup(u'Nidoran')
96 top_names = [_.object.name for _ in results[0:2]]
97 assert_true(u'Nidoran♂' in top_names, u'Nidoran♂ is a top result for "Nidoran"')
98 assert_true(u'Nidoran♀' in top_names, u'Nidoran♀ is a top result for "Nidoran"')
99
100 def test_wildcard_lookup():
101 tests = [
102 (u'pokemon:*meleon', u'Charmeleon'),
103 (u'item:master*', u'Master Ball'),
104 (u'ee?ee', u'Eevee'),
105 ]
106
107 for wildcard, name in tests:
108 results = lookup.lookup(wildcard)
109 first_result = results[0]
110 assert_equal(first_result.object.name, name,
111 u'Wildcards work correctly')
112
113 def test_random_lookup():
114 for _ in xrange(5):
115 results = lookup.lookup(u'random')
116 assert_equal(len(results), 1, u'Random returns one result')
117
118 for table_name in [u'pokemon', u'moves', u'items', u'abilities', u'types']:
119 results = lookup.lookup(u'random', valid_types=[table_name])
120 assert_equal(len(results), 1, u'Constrained random returns one result')
121 assert_equal(results[0].object.__tablename__, table_name,
122 u'Constrained random returns result from the right table')