Overhauled CLI. #180
[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(recreate=True)
13
14 def test_exact_lookup():
15 tests = [
16 # Simple lookups
17 (u'Eevee', 'pokemon', 133),
18 (u'Scratch', 'moves', 10),
19 (u'Master Ball', 'items', 1),
20 (u'normal', 'types', 1),
21 (u'Run Away', 'abilities', 50),
22
23 # Funny characters
24 (u'Mr. Mime', 'pokemon', 122),
25 (u"Farfetch'd", 'pokemon', 83),
26 (u'Poké Ball', 'items', 4),
27
28 # Forms
29 (u'Rotom', 'pokemon', 479),
30 (u'Wash Rotom', 'pokemon', 504),
31
32 # Other languages
33 (u'イーブイ', 'pokemon', 133),
34 (u'Iibui', 'pokemon', 133),
35 (u'Eievui', 'pokemon', 133),
36 (u'이브이', 'pokemon', 133),
37 (u'伊布', 'pokemon', 133),
38 (u'Evoli', 'pokemon', 133),
39 ]
40
41 for input, table, id in tests:
42 results = lookup.lookup(input)
43 assert_equal(len(results), 1, u"'%s' returns one result" % input)
44 assert_equal(results[0].exact, True, u"'%s' match exactly" % input)
45
46 row = results[0].object
47 assert_equal(row.__tablename__, table, u"'%s' is in the right table" % input)
48 assert_equal(row.id, id, u"'%s' returns the right id" % input)
49
50
51 def test_id_lookup():
52 results = lookup.lookup(u'1')
53 assert_true(len(results) >= 5, u'At least five things have id 1')
54 assert_true(all(_.object.id == 1 for _ in results),
55 u'All results have id 1')
56
57 def test_multi_lookup():
58 results = lookup.lookup(u'Metronome')
59 assert_equal(len(results), 2, u'Two things called "Metronome"')
60 assert_true(results[0].exact, u'Metronome matches are exact')
61
62
63 def test_type_lookup():
64 results = lookup.lookup(u'pokemon:1')
65 assert_equal(results[0].object.__tablename__, 'pokemon',
66 u'Type restriction works correctly')
67 assert_equal(len(results), 1, u'Only one id result when type is specified')
68 assert_equal(results[0].name, u'Bulbasaur', u'Type + id returns the right result')
69
70 results = lookup.lookup(u'1', valid_types=['pokemon'])
71 assert_equal(results[0].name, u'Bulbasaur', u'valid_types works as well as type: prefix')
72
73 def test_fuzzy_lookup():
74 tests = [
75 # Regular English names
76 (u'chamander', u'Charmander'),
77 (u'pokeball', u'Poké Ball'),
78
79 # Names with squiggles in them
80 (u'farfetchd', u"Farfetch'd"),
81 (u'porygonz', u'Porygon-Z'),
82
83 # Sufficiently long foreign names
84 (u'カクレオ', u'Kecleon'),
85 (u'Yamikrasu', u'Murkrow'),
86 ]
87
88 for misspelling, name in tests:
89 results = lookup.lookup(misspelling)
90 first_result = results[0]
91 assert_equal(first_result.object.name, name,
92 u'Simple misspellings are corrected')
93
94 results = lookup.lookup(u'Nidoran')
95 top_names = [_.object.name for _ in results[0:2]]
96 assert_true(u'Nidoran♂' in top_names, u'Nidoran♂ is a top result for "Nidoran"')
97 assert_true(u'Nidoran♀' in top_names, u'Nidoran♀ is a top result for "Nidoran"')
98
99 def test_wildcard_lookup():
100 tests = [
101 (u'pokemon:*meleon', u'Charmeleon'),
102 (u'item:master*', u'Master Ball'),
103 (u'ee?ee', u'Eevee'),
104 ]
105
106 for wildcard, name in tests:
107 results = lookup.lookup(wildcard)
108 first_result = results[0]
109 assert_equal(first_result.object.name, name,
110 u'Wildcards work correctly')
111
112 def test_random_lookup():
113 for _ in xrange(5):
114 results = lookup.lookup(u'random')
115 assert_equal(len(results), 1, u'Random returns one result')
116
117 for table_name in [u'pokemon', u'moves', u'items', u'abilities', u'types']:
118 results = lookup.lookup(u'random', valid_types=[table_name])
119 assert_equal(len(results), 1, u'Constrained random returns one result')
120 assert_equal(results[0].object.__tablename__, table_name,
121 u'Constrained random returns result from the right table')