summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
4a32d3d)
Test suite no longer reloads the entire database. Takes too long.
Factored out some magic numbers in lookup().
__all__ = ['open_index', 'lookup']
__all__ = ['open_index', 'lookup']
+INTERMEDIATE_LOOKUP_RESULTS = 25
+MAX_LOOKUP_RESULTS = 10
+
# Dictionary of table name => table class.
# Need the table name so we can get the class from the table name after we
# retrieve something from the index
# Dictionary of table name => table class.
# Need the table name so we can get the class from the table name after we
# retrieve something from the index
# provided
valid_types = prefixes
# provided
valid_types = prefixes
- # If the input provided is a number, match it as an id. Otherwise, name.
- # Term objects do an exact match, so we don't have to worry about a query
- # parser tripping on weird characters in the input
- if rx_is_number.match(name):
+ # Do different things depending what the query looks like
+ # Note: Term objects do an exact match, so we don't have to worry about a
+ # query parser tripping on weird characters in the input
+ if '*' in name or '?' in name:
+ exact_only = True
+ query = whoosh.query.Wildcard(u'name', name)
+ elif rx_is_number.match(name):
# Don't spell-check numbers!
exact_only = True
query = whoosh.query.Term(u'row_id', name)
# Don't spell-check numbers!
exact_only = True
query = whoosh.query.Term(u'row_id', name)
searcher.weighting = LanguageWeighting() # XXX kosher? docs say search()
# takes a weighting kw but it
# certainly does not
searcher.weighting = LanguageWeighting() # XXX kosher? docs say search()
# takes a weighting kw but it
# certainly does not
- results = searcher.search(query)
+ results = searcher.search(query, limit=INTERMEDIATE_LOOKUP_RESULTS)
# Look for some fuzzy matches if necessary
if not exact_only and not results:
exact = False
results = []
# Look for some fuzzy matches if necessary
if not exact_only and not results:
exact = False
results = []
- for suggestion in speller.suggest(name, 25):
+ for suggestion in speller.suggest(name, INTERMEDIATE_LOOKUP_RESULTS):
query = whoosh.query.Term('name', suggestion)
results.extend(searcher.search(query))
query = whoosh.query.Term('name', suggestion)
results.extend(searcher.search(query))
# should have more than 10 here and lost a few. The speller returns 25 to
# give us some padding, and should avoid that problem. Not a big deal if
# we lose the 25th-most-likely match anyway.
# should have more than 10 here and lost a few. The speller returns 25 to
# give us some padding, and should avoid that problem. Not a big deal if
# we lose the 25th-most-likely match anyway.
+ return objects[:MAX_LOOKUP_RESULTS]
def setup():
# Reload data just in case
session = connect()
def setup():
# Reload data just in case
session = connect()
- load(session, verbose=False, drop_tables=True)
open_index(session=session, recreate=True)
def teardown():
open_index(session=session, recreate=True)
def teardown():
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"')
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"')
+
+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:
+ results = pokedex.lookup.lookup(wildcard)
+ first_result = results[0]
+ assert_equal(first_result.object.name, name,
+ u'Wildcards work correctly')