2009-08-24 03:43:39 +00:00
|
|
|
# encoding: utf8
|
|
|
|
from nose.tools import *
|
|
|
|
import unittest
|
|
|
|
|
2010-03-29 05:14:27 +00:00
|
|
|
from pokedex.lookup import PokedexLookup
|
2009-08-24 03:43:39 +00:00
|
|
|
|
2010-03-29 05:14:27 +00:00
|
|
|
lookup = None
|
|
|
|
|
|
|
|
def setup():
|
|
|
|
# Recreate data
|
|
|
|
global lookup
|
2010-05-13 05:36:12 +00:00
|
|
|
lookup = PokedexLookup()
|
|
|
|
lookup.rebuild_index()
|
2009-08-24 03:43:39 +00:00
|
|
|
|
|
|
|
def test_exact_lookup():
|
|
|
|
tests = [
|
|
|
|
# Simple lookups
|
|
|
|
(u'Eevee', 'pokemon', 133),
|
|
|
|
(u'Scratch', 'moves', 10),
|
|
|
|
(u'Master Ball', 'items', 1),
|
|
|
|
(u'normal', 'types', 1),
|
|
|
|
(u'Run Away', 'abilities', 50),
|
|
|
|
|
|
|
|
# Funny characters
|
|
|
|
(u'Mr. Mime', 'pokemon', 122),
|
|
|
|
(u"Farfetch'd", 'pokemon', 83),
|
|
|
|
(u'Poké Ball', 'items', 4),
|
|
|
|
|
|
|
|
# Forms
|
|
|
|
(u'Rotom', 'pokemon', 479),
|
|
|
|
(u'Wash Rotom', 'pokemon', 504),
|
|
|
|
|
|
|
|
# Other languages
|
|
|
|
(u'イーブイ', 'pokemon', 133),
|
|
|
|
(u'Iibui', 'pokemon', 133),
|
|
|
|
(u'Eievui', 'pokemon', 133),
|
|
|
|
(u'이브이', 'pokemon', 133),
|
|
|
|
(u'伊布', 'pokemon', 133),
|
|
|
|
(u'Evoli', 'pokemon', 133),
|
|
|
|
]
|
|
|
|
|
|
|
|
for input, table, id in tests:
|
2010-03-29 05:14:27 +00:00
|
|
|
results = lookup.lookup(input)
|
2009-08-24 03:43:39 +00:00
|
|
|
assert_equal(len(results), 1, u"'%s' returns one result" % input)
|
|
|
|
assert_equal(results[0].exact, True, u"'%s' match exactly" % input)
|
|
|
|
|
|
|
|
row = results[0].object
|
|
|
|
assert_equal(row.__tablename__, table, u"'%s' is in the right table" % input)
|
|
|
|
assert_equal(row.id, id, u"'%s' returns the right id" % input)
|
|
|
|
|
|
|
|
|
|
|
|
def test_id_lookup():
|
2010-03-29 05:14:27 +00:00
|
|
|
results = lookup.lookup(u'1')
|
2009-08-24 03:43:39 +00:00
|
|
|
assert_true(len(results) >= 5, u'At least five things have id 1')
|
|
|
|
assert_true(all(_.object.id == 1 for _ in results),
|
|
|
|
u'All results have id 1')
|
|
|
|
|
|
|
|
def test_multi_lookup():
|
2010-03-29 05:14:27 +00:00
|
|
|
results = lookup.lookup(u'Metronome')
|
2009-08-24 03:43:39 +00:00
|
|
|
assert_equal(len(results), 2, u'Two things called "Metronome"')
|
|
|
|
assert_true(results[0].exact, u'Metronome matches are exact')
|
|
|
|
|
|
|
|
|
|
|
|
def test_type_lookup():
|
2010-03-29 05:14:27 +00:00
|
|
|
results = lookup.lookup(u'pokemon:1')
|
2009-08-24 03:43:39 +00:00
|
|
|
assert_equal(results[0].object.__tablename__, 'pokemon',
|
|
|
|
u'Type restriction works correctly')
|
|
|
|
assert_equal(len(results), 1, u'Only one id result when type is specified')
|
|
|
|
assert_equal(results[0].name, u'Bulbasaur', u'Type + id returns the right result')
|
|
|
|
|
2010-03-29 05:14:27 +00:00
|
|
|
results = lookup.lookup(u'1', valid_types=['pokemon'])
|
2009-08-24 03:43:39 +00:00
|
|
|
assert_equal(results[0].name, u'Bulbasaur', u'valid_types works as well as type: prefix')
|
|
|
|
|
|
|
|
def test_fuzzy_lookup():
|
|
|
|
tests = [
|
|
|
|
# Regular English names
|
|
|
|
(u'chamander', u'Charmander'),
|
|
|
|
(u'pokeball', u'Poké Ball'),
|
|
|
|
|
|
|
|
# Names with squiggles in them
|
|
|
|
(u'farfetchd', u"Farfetch'd"),
|
|
|
|
(u'porygonz', u'Porygon-Z'),
|
|
|
|
|
|
|
|
# Sufficiently long foreign names
|
|
|
|
(u'カクレオ', u'Kecleon'),
|
|
|
|
(u'Yamikrasu', u'Murkrow'),
|
|
|
|
]
|
|
|
|
|
|
|
|
for misspelling, name in tests:
|
2010-03-29 05:14:27 +00:00
|
|
|
results = lookup.lookup(misspelling)
|
2009-08-24 03:43:39 +00:00
|
|
|
first_result = results[0]
|
|
|
|
assert_equal(first_result.object.name, name,
|
|
|
|
u'Simple misspellings are corrected')
|
|
|
|
|
2010-03-29 05:14:27 +00:00
|
|
|
results = lookup.lookup(u'Nidoran')
|
2009-08-24 03:43:39 +00:00
|
|
|
top_names = [_.object.name for _ in results[0:2]]
|
|
|
|
assert_true(u'Nidoran♂' in top_names, u'Nidoran♂ is a top result for "Nidoran"')
|
|
|
|
assert_true(u'Nidoran♀' in top_names, u'Nidoran♀ is a top result for "Nidoran"')
|
2009-08-25 05:04:55 +00:00
|
|
|
|
|
|
|
def test_wildcard_lookup():
|
|
|
|
tests = [
|
|
|
|
(u'pokemon:*meleon', u'Charmeleon'),
|
|
|
|
(u'item:master*', u'Master Ball'),
|
|
|
|
(u'ee?ee', u'Eevee'),
|
|
|
|
]
|
|
|
|
|
|
|
|
for wildcard, name in tests:
|
2010-03-29 05:14:27 +00:00
|
|
|
results = lookup.lookup(wildcard)
|
2009-08-25 05:04:55 +00:00
|
|
|
first_result = results[0]
|
|
|
|
assert_equal(first_result.object.name, name,
|
|
|
|
u'Wildcards work correctly')
|
2009-08-25 05:59:39 +00:00
|
|
|
|
|
|
|
def test_random_lookup():
|
|
|
|
for _ in xrange(5):
|
2010-03-29 05:14:27 +00:00
|
|
|
results = lookup.lookup(u'random')
|
2009-08-25 05:59:39 +00:00
|
|
|
assert_equal(len(results), 1, u'Random returns one result')
|
|
|
|
|
|
|
|
for table_name in [u'pokemon', u'moves', u'items', u'abilities', u'types']:
|
2010-03-29 05:14:27 +00:00
|
|
|
results = lookup.lookup(u'random', valid_types=[table_name])
|
2009-08-25 05:59:39 +00:00
|
|
|
assert_equal(len(results), 1, u'Constrained random returns one result')
|
|
|
|
assert_equal(results[0].object.__tablename__, table_name,
|
|
|
|
u'Constrained random returns result from the right table')
|