?info now gives priority to complete matches to a location name
parent
bd3c6fa0af
commit
6d9c262433
|
@ -199,7 +199,8 @@ class Commands:
|
||||||
def info(self, location_name):
|
def info(self, location_name):
|
||||||
session = self.interface.database.Session()
|
session = self.interface.database.Session()
|
||||||
try:
|
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:
|
finally:
|
||||||
session.close()
|
session.close()
|
||||||
|
|
||||||
|
|
|
@ -70,6 +70,25 @@ class DatabaseInterface:
|
||||||
expr = loc_type.name.ilike('%{}%'.format(name))
|
expr = loc_type.name.ilike('%{}%'.format(name))
|
||||||
return self.database.query_by_filter(session, loc_type, expr)
|
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):
|
def find_location_by_owner(self, session, owner, loc_type=Location):
|
||||||
expr = loc_type.owner == owner
|
expr = loc_type.owner == owner
|
||||||
return self.database.query_by_filter(session, loc_type, expr)
|
return self.database.query_by_filter(session, loc_type, expr)
|
||||||
|
|
|
@ -6,6 +6,7 @@ from BotConfig import *
|
||||||
|
|
||||||
zerohd = 'BirbHD'
|
zerohd = 'BirbHD'
|
||||||
|
|
||||||
|
|
||||||
class TestGeoffreyDatabase(TestCase):
|
class TestGeoffreyDatabase(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
path = os.path.dirname(os.path.abspath(__file__))
|
path = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
@ -224,6 +225,20 @@ class TestGeoffreyDatabase(TestCase):
|
||||||
|
|
||||||
self.assertEqual(loc_list[0].name, loc.name)
|
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):
|
def test_search_all(self):
|
||||||
owner = self.add_player()
|
owner = self.add_player()
|
||||||
self.add_loc(owner)
|
self.add_loc(owner)
|
||||||
|
|
Loading…
Reference in New Issue