Updated tests for Commands and refactored the location lookup logic

doc_update
Joey Hines 2018-09-20 09:20:30 -05:00
parent 63970bdbef
commit 2aad1a3344
4 changed files with 98 additions and 63 deletions

View File

@ -16,6 +16,24 @@ class Commands:
return player 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): def register(self, player_name, discord_uuid):
session = self.interface.database.Session() session = self.interface.database.Session()
@ -77,14 +95,8 @@ class Commands:
player = self.get_player(session, discord_uuid, mc_uuid) player = self.get_player(session, discord_uuid, mc_uuid)
if location_name is None: if location_name is None:
location_list = self.interface.find_location_by_owner(session, player) loc = self.get_location(session, player, name=location_name)
location_name = loc.name
if len(location_list) == 0:
raise NoLocationsInDatabase
if len(location_list) > 1:
raise EntryNameNotUniqueError
location_name = location_list[0].name
tunnel = self.interface.add_tunnel(session, player, tunnel_direction, tunnel_number, location_name) tunnel = self.interface.add_tunnel(session, player, tunnel_direction, tunnel_number, location_name)
tunnel_info = tunnel.__str__() tunnel_info = tunnel.__str__()
@ -123,21 +135,14 @@ class Commands:
return loc_list 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() session = self.interface.database.Session()
try: try:
player = self.get_player(session, discord_uuid, mc_uuid) 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: shop = self.get_location(session, player, shop_name, Shop)
if len(shop_list) == 1:
shop_name = shop_list[0].name
elif len(shop_list) == 0:
raise NoLocationsInDatabase
else:
raise LocationInitError
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__() item_listing_str = item_listing.__str__()
finally: finally:
session.close() session.close()
@ -190,17 +195,7 @@ class Commands:
try: try:
player = self.get_player(session, discord_uuid=discord_uuid, mc_uuid=mc_uuid) player = self.get_player(session, discord_uuid=discord_uuid, mc_uuid=mc_uuid)
if loc_name is None: location = self.get_location(session, player, loc_name)
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.x = x location.x = x
location.z = z location.z = z
@ -255,17 +250,7 @@ class Commands:
try: try:
player = self.get_player(session, discord_uuid=discord_uuid, mc_uuid=mc_uuid) player = self.get_player(session, discord_uuid=discord_uuid, mc_uuid=mc_uuid)
if loc_name is None: location = self.get_location(session, player, loc_name)
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.name = new_name location.name = new_name
loc_str = location.__str__() loc_str = location.__str__()
@ -290,21 +275,7 @@ class Commands:
try: try:
player = self.get_player(session, discord_uuid=discord_uuid, mc_uuid=mc_uuid) player = self.get_player(session, discord_uuid=discord_uuid, mc_uuid=mc_uuid)
if shop_name is None: shop = self.get_location(session, player, shop_name, Shop)
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
expr = (ItemListing.name == item) & (ItemListing.shop == shop) expr = (ItemListing.name == item) & (ItemListing.shop == shop)
self.interface.database.delete_entry(session, ItemListing, expr) self.interface.database.delete_entry(session, ItemListing, expr)

View File

@ -133,7 +133,7 @@ class Add_Commands:
item_name)) item_name))
except NoLocationsInDatabase: except NoLocationsInDatabase:
await ctx.send('{}, you don\'t have any shops in the database.'.format(ctx.message.author.mention)) 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.' await ctx.send('{}, you have more than one shop in the database, please specify a shop name.'
.format(ctx.message.author.mention)) .format(ctx.message.author.mention))
except LocationLookUpError: except LocationLookUpError:

View File

@ -47,7 +47,7 @@ class Delete_Commands:
await ctx.send('{}, **{}** has been removed from the inventory of **{}**.'. await ctx.send('{}, **{}** has been removed from the inventory of **{}**.'.
format(ctx.message.author.mention, item, shop_name)) format(ctx.message.author.mention, item, shop_name))
except LocationLookUpError: except NoLocationsInDatabase:
if shop is None: if shop is None:
await ctx.send('{}, you do have any shops in the database.'.format(ctx.message.author.mention)) await ctx.send('{}, you do have any shops in the database.'.format(ctx.message.author.mention))
else: else:

View File

@ -1,5 +1,4 @@
import os import os
import time
from unittest import TestCase from unittest import TestCase
from Commands import * from Commands import *
@ -22,7 +21,47 @@ class TestCommands(TestCase):
player = self.commands.get_player(session, discord_uuid='143072699567177728') player = self.commands.get_player(session, discord_uuid='143072699567177728')
self.assertEqual(player.name, 'BirbHD') 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): def test_register(self):
self.commands.register('BirbHD', '143072699567177728') self.commands.register('BirbHD', '143072699567177728')
@ -35,6 +74,8 @@ class TestCommands(TestCase):
player_name = self.commands.register('BirbHD', '143072699567177728') player_name = self.commands.register('BirbHD', '143072699567177728')
base = self.commands.add_base(0, 0, discord_uuid='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: if player_name not in base:
self.fail() self.fail()
else: else:
@ -44,6 +85,8 @@ class TestCommands(TestCase):
player_name = self.commands.register('BirbHD', '143072699567177728') player_name = self.commands.register('BirbHD', '143072699567177728')
shop = self.commands.add_shop(0, 0, discord_uuid='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: if player_name not in shop:
self.fail() self.fail()
else: else:
@ -52,6 +95,7 @@ class TestCommands(TestCase):
def test_addtunnel(self): def test_addtunnel(self):
self.commands.register('BirbHD', '143072699567177728') 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 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', tunnel2 = self.commands.add_tunnel("East", 50, location_name='test_shop',
discord_uuid='143072699567177728') discord_uuid='143072699567177728')
@ -62,6 +106,9 @@ class TestCommands(TestCase):
self.assertRaises(LocationHasTunnelError, self.commands.add_tunnel, "East", 50, self.assertRaises(LocationHasTunnelError, self.commands.add_tunnel, "East", 50,
location_name='test_shop', discord_uuid='143072699567177728') location_name='test_shop', discord_uuid='143072699567177728')
self.assertRaises(EntryNameNotUniqueError, self.commands.add_tunnel, "East", 50,
discord_uuid='143072699567177728')
def test_find(self): def test_find(self):
self.commands.register('BirbHD', '143072699567177728') self.commands.register('BirbHD', '143072699567177728')
self.commands.add_shop(0, 0, shop_name='frick', discord_uuid='143072699567177728') self.commands.add_shop(0, 0, shop_name='frick', discord_uuid='143072699567177728')
@ -95,6 +142,9 @@ class TestCommands(TestCase):
def test_additem(self): def test_additem(self):
self.commands.register('BirbHD', '143072699567177728') 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') self.commands.add_shop(0, 0, discord_uuid='143072699567177728')
result = self.commands.add_item('dirt', 5, 5, None, discord_uuid='143072699567177728') result = self.commands.add_item('dirt', 5, 5, None, discord_uuid='143072699567177728')
@ -139,7 +189,7 @@ class TestCommands(TestCase):
else: else:
self.fail() self.fail()
def test_tunnel(self): def test_add_tunnel(self):
self.commands.register('BirbHD', '143072699567177728') self.commands.register('BirbHD', '143072699567177728')
self.assertRaises(NoLocationsInDatabase, self.commands.add_tunnel, "soUTH", 50, None, self.assertRaises(NoLocationsInDatabase, self.commands.add_tunnel, "soUTH", 50, None,
@ -156,6 +206,21 @@ class TestCommands(TestCase):
else: else:
self.fail() 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): def test_edit_name(self):
self.commands.register('BirbHD', '143072699567177728') 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 shop', discord_uuid='143072699567177728')
@ -191,7 +256,7 @@ class TestCommands(TestCase):
self.commands.delete(name='test shop', discord_uuid='143072699567177728') 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') discord_uuid='143072699567177728')
def test_edit_tunnel(self): def test_edit_tunnel(self):
@ -218,7 +283,6 @@ class TestCommands(TestCase):
self.assertRaises(ItemNotFound, self.commands.selling, 'dirt') self.assertRaises(ItemNotFound, self.commands.selling, 'dirt')
self.commands.add_shop(0, 0, shop_name='test shop2', discord_uuid='143072699567177728') self.commands.add_shop(0, 0, shop_name='test shop2', discord_uuid='143072699567177728')
self.assertRaises(EntryNameNotUniqueError, self.commands.delete_item, 'wood', None, self.assertRaises(EntryNameNotUniqueError, self.commands.delete_item, 'wood', None,
discord_uuid='143072699567177728') discord_uuid='143072699567177728')
@ -226,7 +290,7 @@ class TestCommands(TestCase):
self.commands.delete('test shop', discord_uuid='143072699567177728') self.commands.delete('test shop', discord_uuid='143072699567177728')
self.commands.delete('test shop2', 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') discord_uuid='143072699567177728')
def test_me(self): def test_me(self):