diff --git a/geoffrey/Commands.py b/geoffrey/Commands.py index 9d61aea..9970046 100644 --- a/geoffrey/Commands.py +++ b/geoffrey/Commands.py @@ -16,6 +16,24 @@ class Commands: return player + def get_location(self, session, owner, name=None, loc_type=Location): + if name is None: + loc_list = self.interface.find_location_by_owner(session, owner, loc_type) + if len(loc_list) == 1: + loc = loc_list[0] + elif len(loc_list) == 0: + raise NoLocationsInDatabase + else: + raise EntryNameNotUniqueError + else: + loc_list = self.interface.find_location_by_name_and_owner(session, owner, name, loc_type) + if len(loc_list) == 1: + loc = loc_list[0] + else: + raise LocationLookUpError + + return loc + def register(self, player_name, discord_uuid): session = self.interface.database.Session() @@ -77,14 +95,8 @@ class Commands: player = self.get_player(session, discord_uuid, mc_uuid) if location_name is None: - location_list = self.interface.find_location_by_owner(session, player) - - if len(location_list) == 0: - raise NoLocationsInDatabase - if len(location_list) > 1: - raise EntryNameNotUniqueError - - location_name = location_list[0].name + loc = self.get_location(session, player, name=location_name) + location_name = loc.name tunnel = self.interface.add_tunnel(session, player, tunnel_direction, tunnel_number, location_name) tunnel_info = tunnel.__str__() @@ -123,21 +135,14 @@ class Commands: return loc_list - def add_item(self, item_name, quantity, diamond_price, shop_name, discord_uuid=None, mc_uuid=None): + def add_item(self, item_name, quantity, diamond_price, shop_name=None, discord_uuid=None, mc_uuid=None): session = self.interface.database.Session() try: player = self.get_player(session, discord_uuid, mc_uuid) - shop_list = self.interface.find_location_by_owner(session, player, loc_type=Shop) - if shop_name is None: - if len(shop_list) == 1: - shop_name = shop_list[0].name - elif len(shop_list) == 0: - raise NoLocationsInDatabase - else: - raise LocationInitError + shop = self.get_location(session, player, shop_name, Shop) - item_listing = self.interface.add_item(session, player, shop_name, item_name, diamond_price, quantity) + item_listing = self.interface.add_item(session, player, shop.name, item_name, diamond_price, quantity) item_listing_str = item_listing.__str__() finally: session.close() @@ -190,17 +195,7 @@ class Commands: try: player = self.get_player(session, discord_uuid=discord_uuid, mc_uuid=mc_uuid) - if loc_name is None: - loc_list = self.interface.find_location_by_owner(session, player) - - if len(loc_list) == 0: - raise LocationLookUpError - elif len(loc_list) > 1: - raise EntryNameNotUniqueError - else: - location = loc_list[0] - else: - location = self.interface.find_location_by_name_and_owner(session, player, loc_name)[0] + location = self.get_location(session, player, loc_name) location.x = x location.z = z @@ -255,17 +250,7 @@ class Commands: try: player = self.get_player(session, discord_uuid=discord_uuid, mc_uuid=mc_uuid) - if loc_name is None: - loc_list = self.interface.find_location_by_owner(session, player) - - if len(loc_list) == 0: - raise LocationLookUpError - elif len(loc_list) > 1: - raise EntryNameNotUniqueError - else: - location = loc_list[0] - else: - location = self.interface.find_location_by_name_and_owner(session, player, loc_name)[0] + location = self.get_location(session, player, loc_name) location.name = new_name loc_str = location.__str__() @@ -290,21 +275,7 @@ class Commands: try: player = self.get_player(session, discord_uuid=discord_uuid, mc_uuid=mc_uuid) - if shop_name is None: - shop_list = self.interface.find_location_by_owner(session, player, loc_type=Shop) - - if len(shop_list) == 0: - raise LocationLookUpError - elif len(shop_list) > 1: - raise EntryNameNotUniqueError - else: - shop = shop_list[0] - - else: - try: - shop = self.interface.find_location_by_name_and_owner(session, player, shop_name, loc_type=Shop)[0] - except IndexError: - raise LocationLookUpError + shop = self.get_location(session, player, shop_name, Shop) expr = (ItemListing.name == item) & (ItemListing.shop == shop) self.interface.database.delete_entry(session, ItemListing, expr) diff --git a/geoffrey/cogs/Add_Commands.py b/geoffrey/cogs/Add_Commands.py index 7592791..5517ce5 100644 --- a/geoffrey/cogs/Add_Commands.py +++ b/geoffrey/cogs/Add_Commands.py @@ -133,7 +133,7 @@ class Add_Commands: item_name)) except NoLocationsInDatabase: await ctx.send('{}, you don\'t have any shops in the database.'.format(ctx.message.author.mention)) - except LocationInitError: + except EntryNameNotUniqueError: await ctx.send('{}, you have more than one shop in the database, please specify a shop name.' .format(ctx.message.author.mention)) except LocationLookUpError: diff --git a/geoffrey/cogs/Delete_Commands.py b/geoffrey/cogs/Delete_Commands.py index 955204c..3cb4044 100644 --- a/geoffrey/cogs/Delete_Commands.py +++ b/geoffrey/cogs/Delete_Commands.py @@ -47,7 +47,7 @@ class Delete_Commands: await ctx.send('{}, **{}** has been removed from the inventory of **{}**.'. format(ctx.message.author.mention, item, shop_name)) - except LocationLookUpError: + except NoLocationsInDatabase: if shop is None: await ctx.send('{}, you do have any shops in the database.'.format(ctx.message.author.mention)) else: diff --git a/geoffrey/tests/test_commands.py b/geoffrey/tests/test_commands.py index decd56a..20316ad 100644 --- a/geoffrey/tests/test_commands.py +++ b/geoffrey/tests/test_commands.py @@ -1,5 +1,4 @@ import os -import time from unittest import TestCase from Commands import * @@ -22,7 +21,47 @@ class TestCommands(TestCase): player = self.commands.get_player(session, discord_uuid='143072699567177728') self.assertEqual(player.name, 'BirbHD') - self.session.close() + + self.assertRaises(AttributeError, self.commands.get_player, session) + session.close() + + def test_get_location(self): + session = self.commands.interface.database.Session() + self.commands.interface.add_player(session, 'BirbHD', discord_uuid='143072699567177728') + session.close() + + session = self.commands.interface.database.Session() + + player = self.commands.get_player(session, discord_uuid='143072699567177728') + + self.assertRaises(NoLocationsInDatabase, self.commands.get_location, + session, player, name=None, loc_type=Location) + + session.close() + + self.commands.add_base(0, 0, discord_uuid='143072699567177728') + session = self.commands.interface.database.Session() + + self.commands.get_location(session, player, name=None, loc_type=Location) + + session.close() + + self.commands.add_base(0, 0, base_name='Birb', discord_uuid='143072699567177728') + + session = self.commands.interface.database.Session() + + self.assertRaises(EntryNameNotUniqueError, self.commands.get_location, + session, player, name=None, loc_type=Location) + + self.commands.get_location(session, player, name="Birb", loc_type=Location) + + self.assertRaises(LocationLookUpError, self.commands.get_location, + session, player, name="Henlo", loc_type=Location) + + session.close() + + + def test_register(self): self.commands.register('BirbHD', '143072699567177728') @@ -35,6 +74,8 @@ class TestCommands(TestCase): player_name = self.commands.register('BirbHD', '143072699567177728') base = self.commands.add_base(0, 0, discord_uuid='143072699567177728') + self.assertRaises(EntryNameNotUniqueError, self.commands.add_base, 0, 0, discord_uuid='143072699567177728') + if player_name not in base: self.fail() else: @@ -44,6 +85,8 @@ class TestCommands(TestCase): player_name = self.commands.register('BirbHD', '143072699567177728') shop = self.commands.add_shop(0, 0, discord_uuid='143072699567177728') + self.assertRaises(EntryNameNotUniqueError, self.commands.add_shop, 0, 0, discord_uuid='143072699567177728') + if player_name not in shop: self.fail() else: @@ -52,6 +95,7 @@ class TestCommands(TestCase): def test_addtunnel(self): self.commands.register('BirbHD', '143072699567177728') self.commands.add_shop(0, 0, shop_name='test shop', discord_uuid='143072699567177728') + self.commands.add_shop(0, 0, shop_name='test shop2', discord_uuid='143072699567177728') tunnel2 = self.commands.add_tunnel("East", 50, location_name='test_shop', discord_uuid='143072699567177728') @@ -62,6 +106,9 @@ class TestCommands(TestCase): self.assertRaises(LocationHasTunnelError, self.commands.add_tunnel, "East", 50, location_name='test_shop', discord_uuid='143072699567177728') + self.assertRaises(EntryNameNotUniqueError, self.commands.add_tunnel, "East", 50, + discord_uuid='143072699567177728') + def test_find(self): self.commands.register('BirbHD', '143072699567177728') self.commands.add_shop(0, 0, shop_name='frick', discord_uuid='143072699567177728') @@ -95,6 +142,9 @@ class TestCommands(TestCase): def test_additem(self): self.commands.register('BirbHD', '143072699567177728') + self.assertRaises(NoLocationsInDatabase, self.commands.add_item, 'dirt', 5, 5 + , discord_uuid='143072699567177728') + self.commands.add_shop(0, 0, discord_uuid='143072699567177728') result = self.commands.add_item('dirt', 5, 5, None, discord_uuid='143072699567177728') @@ -139,7 +189,7 @@ class TestCommands(TestCase): else: self.fail() - def test_tunnel(self): + def test_add_tunnel(self): self.commands.register('BirbHD', '143072699567177728') self.assertRaises(NoLocationsInDatabase, self.commands.add_tunnel, "soUTH", 50, None, @@ -156,6 +206,21 @@ class TestCommands(TestCase): else: self.fail() + def test_tunnel(self): + self.commands.register('BirbHD', '143072699567177728') + self.commands.add_shop(0, 0, shop_name='test shop', discord_uuid='143072699567177728') + + self.assertRaises(LocationLookUpError, self.commands.tunnel, 'BirbHD') + + result = self.commands.add_tunnel("WEST", 50, None, discord_uuid='143072699567177728') + + if "West" in result: + pass + else: + self.fail() + + + def test_edit_name(self): self.commands.register('BirbHD', '143072699567177728') self.commands.add_shop(0, 0, shop_name='test shop', discord_uuid='143072699567177728') @@ -191,7 +256,7 @@ class TestCommands(TestCase): self.commands.delete(name='test shop', discord_uuid='143072699567177728') - self.assertRaises(LocationLookUpError, self.commands.edit_pos, 5, 5, None, + self.assertRaises(NoLocationsInDatabase, self.commands.edit_pos, 5, 5, None, discord_uuid='143072699567177728') def test_edit_tunnel(self): @@ -218,7 +283,6 @@ class TestCommands(TestCase): self.assertRaises(ItemNotFound, self.commands.selling, 'dirt') - self.commands.add_shop(0, 0, shop_name='test shop2', discord_uuid='143072699567177728') self.assertRaises(EntryNameNotUniqueError, self.commands.delete_item, 'wood', None, discord_uuid='143072699567177728') @@ -226,7 +290,7 @@ class TestCommands(TestCase): self.commands.delete('test shop', discord_uuid='143072699567177728') self.commands.delete('test shop2', discord_uuid='143072699567177728') - self.assertRaises(LocationLookUpError, self.commands.delete_item, 'wood', None, + self.assertRaises(NoLocationsInDatabase, self.commands.delete_item, 'wood', None, discord_uuid='143072699567177728') def test_me(self):