?info now gives priority to complete matches to a location name

doc_update
Joey Hines 2018-09-26 08:48:23 -05:00
parent bd3c6fa0af
commit 6d9c262433
3 changed files with 36 additions and 1 deletions

View File

@ -199,7 +199,8 @@ class Commands:
def info(self, location_name):
session = self.interface.database.Session()
try:
loc = self.interface.find_location_by_name(session, location_name)[0].full_str(self.bot_config)
loc = self.interface.find_location_by_name_closest_match(session,
location_name).full_str(self.bot_config)
finally:
session.close()

View File

@ -70,6 +70,25 @@ class DatabaseInterface:
expr = loc_type.name.ilike('%{}%'.format(name))
return self.database.query_by_filter(session, loc_type, expr)
def find_location_by_name_closest_match(self, session, name, loc_type=Location):
expr = loc_type.name.ilike('%{}%'.format(name))
loc_list = self.database.query_by_filter(session, loc_type, expr)
if len(loc_list) == 1:
loc = loc_list[0]
else:
expr = loc_type.name.ilike(name)
loc_list = self.database.query_by_filter(session, loc_type, expr)
if len(loc_list) > 1:
raise EntryNameNotUniqueError
elif len(loc_list) == 0:
raise LocationLookUpError
else:
loc = loc_list[0]
return loc
def find_location_by_owner(self, session, owner, loc_type=Location):
expr = loc_type.owner == owner
return self.database.query_by_filter(session, loc_type, expr)

View File

@ -6,6 +6,7 @@ from BotConfig import *
zerohd = 'BirbHD'
class TestGeoffreyDatabase(TestCase):
def setUp(self):
path = os.path.dirname(os.path.abspath(__file__))
@ -224,6 +225,20 @@ class TestGeoffreyDatabase(TestCase):
self.assertEqual(loc_list[0].name, loc.name)
def test_find_matching_location_by_name(self):
owner = self.add_player()
loc = self.add_loc(owner)
loc_list = self.interface.find_location_by_name_closest_match(self.session, 'test')
self.assertEqual(loc_list.name, loc.name)
shop = self.interface.add_loc(self.session, owner, 'tes', 1, 3, "nether", loc_type=Shop)
loc_list = self.interface.find_location_by_name_closest_match(self.session, shop.name)
self.assertEqual(loc_list.name, shop.name)
def test_search_all(self):
owner = self.add_player()
self.add_loc(owner)