2018-08-30 16:34:19 +00:00
|
|
|
import os
|
2018-06-02 17:40:55 +00:00
|
|
|
from unittest import TestCase
|
2018-08-12 19:00:04 +00:00
|
|
|
|
|
|
|
from DatabaseInterface import *
|
2018-08-29 21:10:35 +00:00
|
|
|
from BotConfig import *
|
2018-07-22 12:52:49 +00:00
|
|
|
|
2018-09-21 17:22:17 +00:00
|
|
|
zerohd = 'BirbHD'
|
2018-08-30 17:27:05 +00:00
|
|
|
|
2018-09-26 13:48:23 +00:00
|
|
|
|
2018-06-02 17:40:55 +00:00
|
|
|
class TestGeoffreyDatabase(TestCase):
|
|
|
|
def setUp(self):
|
2018-08-30 16:34:19 +00:00
|
|
|
path = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
self.bot_config = get_config('{}/GeoffreyConfig.ini'.format(path))
|
2018-07-21 18:28:31 +00:00
|
|
|
|
2018-08-30 16:34:19 +00:00
|
|
|
self.interface = DatabaseInterface(self.bot_config, True)
|
2018-08-10 02:25:55 +00:00
|
|
|
self.session = self.interface.database.Session()
|
|
|
|
self.interface.database.clear_all(self.session)
|
2018-09-21 17:22:17 +00:00
|
|
|
self.owner = Player(zerohd, '143072699567177728')
|
2018-07-29 13:59:11 +00:00
|
|
|
self.loc = Location('test', 1, 3, self.owner, dimension='Nether')
|
2018-08-30 16:34:19 +00:00
|
|
|
self.tunnel = Tunnel(self.owner, "west", 105, self.loc)
|
2018-07-22 23:56:07 +00:00
|
|
|
|
2018-07-21 18:28:31 +00:00
|
|
|
def tearDown(self):
|
2018-07-29 13:59:11 +00:00
|
|
|
self.session.commit()
|
2018-07-22 01:44:43 +00:00
|
|
|
self.session.close()
|
2018-07-21 18:28:31 +00:00
|
|
|
|
2018-09-25 14:39:30 +00:00
|
|
|
def test_find_top_shops_selling_items(self):
|
|
|
|
owner = self.add_player()
|
2018-09-26 13:53:58 +00:00
|
|
|
shop = self.add_shop(owner)
|
2018-09-25 14:39:30 +00:00
|
|
|
self.interface.add_item(self.session, owner, 'test', 'dirt', 1, 15)
|
|
|
|
self.interface.add_item(self.session, owner, 'test', 'dirt', 1, 5)
|
|
|
|
self.interface.add_loc(self.session, owner, 'test2', 1, 3, "nether", loc_type=Shop)
|
|
|
|
self.interface.add_item(self.session, owner, 'test2', 'dirt', 1, 5)
|
|
|
|
shop_list = self.interface.find_top_shops_selling_item(self.session, "Dirt")
|
|
|
|
|
2018-09-26 13:53:58 +00:00
|
|
|
self.assertEquals(shop_list[0][0].id, shop.id)
|
2018-09-25 14:39:30 +00:00
|
|
|
|
2018-07-22 23:56:07 +00:00
|
|
|
def add_shop(self, player):
|
2018-08-30 17:27:05 +00:00
|
|
|
shop = self.interface.add_loc(self.session, player, 'test', 1, 3, "nether", loc_type=Shop)
|
2018-07-29 13:59:11 +00:00
|
|
|
return shop
|
2018-07-22 23:56:07 +00:00
|
|
|
|
|
|
|
def add_player(self):
|
2018-09-21 17:22:17 +00:00
|
|
|
player = self.interface.add_player(self.session, zerohd, discord_uuid='143072699567177728')
|
2018-07-29 13:59:11 +00:00
|
|
|
return player
|
2018-07-22 23:56:07 +00:00
|
|
|
|
|
|
|
def add_loc(self, player):
|
2018-08-30 17:27:05 +00:00
|
|
|
loc = self.interface.add_loc(self.session, player, 'test', 0, 0, loc_type=Base)
|
2018-07-29 13:59:11 +00:00
|
|
|
return loc
|
2018-07-22 23:56:07 +00:00
|
|
|
|
2018-06-02 17:40:55 +00:00
|
|
|
def test_add_object(self):
|
2018-08-10 02:25:55 +00:00
|
|
|
self.interface.database.add_object(self.session, self.loc)
|
|
|
|
self.interface.database.add_object(self.session, self.owner)
|
|
|
|
self.interface.database.add_object(self.session, self.tunnel)
|
2018-06-02 17:40:55 +00:00
|
|
|
|
2018-09-21 17:22:17 +00:00
|
|
|
uuid = grab_UUID(zerohd)
|
2018-07-08 19:19:40 +00:00
|
|
|
expr = Player.mc_uuid == uuid
|
2018-08-10 02:25:55 +00:00
|
|
|
p = self.interface.database.query_by_filter(self.session, Player, expr)[0]
|
2018-06-23 15:17:51 +00:00
|
|
|
|
|
|
|
expr = Location.owner == p
|
2018-08-10 02:25:55 +00:00
|
|
|
loc2 = self.interface.database.query_by_filter(self.session, Location, expr)[0]
|
2018-06-02 17:40:55 +00:00
|
|
|
|
|
|
|
self.assertEqual(self.loc.id, loc2.id)
|
|
|
|
|
|
|
|
def test_query_by_filter(self):
|
2018-08-10 02:25:55 +00:00
|
|
|
self.interface.database.add_object(self.session, self.loc)
|
|
|
|
self.interface.database.add_object(self.session, self.owner)
|
2018-07-08 19:19:40 +00:00
|
|
|
expr = (Location.owner == self.owner)
|
2018-08-10 02:25:55 +00:00
|
|
|
loc2 = self.interface.database.query_by_filter(self.session, Location, expr)[0]
|
2018-07-08 19:19:40 +00:00
|
|
|
self.assertEqual(loc2.id, self.loc.id)
|
2018-06-02 17:40:55 +00:00
|
|
|
|
|
|
|
def test_delete_entry(self):
|
2018-08-10 02:25:55 +00:00
|
|
|
self.interface.database.add_object(self.session, self.loc)
|
|
|
|
self.interface.database.add_object(self.session, self.owner)
|
2018-07-29 13:59:11 +00:00
|
|
|
self.session.commit()
|
2018-08-11 23:02:50 +00:00
|
|
|
|
2018-07-08 19:19:40 +00:00
|
|
|
expr = Location.owner == self.owner
|
2018-08-10 02:25:55 +00:00
|
|
|
self.interface.database.delete_entry(self.session, Location, expr)
|
2018-06-02 17:40:55 +00:00
|
|
|
|
2018-09-21 17:22:17 +00:00
|
|
|
expr = Player.name == zerohd
|
2018-08-10 02:25:55 +00:00
|
|
|
player = self.interface.database.query_by_filter(self.session, Player, expr)[0]
|
2018-09-21 17:22:17 +00:00
|
|
|
self.assertEqual(player.name, zerohd)
|
2018-07-29 13:59:11 +00:00
|
|
|
|
|
|
|
expr = Location.owner == player
|
|
|
|
|
2018-08-10 02:25:55 +00:00
|
|
|
loc2 = self.interface.database.query_by_filter(self.session, Location, expr)
|
2018-06-02 17:40:55 +00:00
|
|
|
|
|
|
|
self.assertEqual(len(loc2), 0)
|
|
|
|
|
2018-08-10 02:25:55 +00:00
|
|
|
self.assertRaises(DeleteEntryError, self.interface.database.delete_entry, self.session, Location, expr)
|
2018-06-02 17:40:55 +00:00
|
|
|
|
2018-09-19 20:41:32 +00:00
|
|
|
def test_add_player(self):
|
|
|
|
self.add_player()
|
2018-09-21 17:22:17 +00:00
|
|
|
self.assertRaises(PlayerInDBError, self.interface.add_player, self.session, zerohd,
|
2018-09-19 20:41:32 +00:00
|
|
|
discord_uuid='143072699567177728')
|
|
|
|
|
2018-06-30 15:07:56 +00:00
|
|
|
def test_add_shop(self):
|
2018-07-15 18:27:11 +00:00
|
|
|
owner = self.add_player()
|
2018-07-22 23:56:07 +00:00
|
|
|
shop = self.add_shop(owner)
|
2018-06-30 15:07:56 +00:00
|
|
|
|
|
|
|
self.assertEqual(type(shop), Shop)
|
|
|
|
|
2018-08-30 17:27:05 +00:00
|
|
|
shop_list = self.interface.find_location_by_name(self.session, 'test', loc_type=Shop)
|
2018-07-08 19:19:40 +00:00
|
|
|
self.assertEqual(shop_list[0].dimension, shop.dimension)
|
|
|
|
|
2018-06-30 15:07:56 +00:00
|
|
|
def test_add_two_shops(self):
|
2018-07-15 18:27:11 +00:00
|
|
|
owner = self.add_player()
|
2018-08-11 23:02:50 +00:00
|
|
|
self.add_shop(owner)
|
2018-08-30 17:27:05 +00:00
|
|
|
shop2 = self.interface.add_loc(self.session, owner, 'no u', 1, 3, loc_type=Shop)
|
2018-06-30 15:07:56 +00:00
|
|
|
|
2018-08-10 02:25:55 +00:00
|
|
|
loc_list = self.interface.find_location_by_owner(self.session, owner)
|
2018-06-30 15:07:56 +00:00
|
|
|
|
|
|
|
self.assertEqual(loc_list[1].id, shop2.id)
|
|
|
|
|
2018-07-21 01:50:24 +00:00
|
|
|
def test_add_tunnel(self):
|
2018-07-22 23:56:07 +00:00
|
|
|
player = self.add_player()
|
2018-08-30 16:34:19 +00:00
|
|
|
tunnel1 = self.interface.add_tunnel(self.session, player, "South", 155, None)
|
2018-07-21 01:50:24 +00:00
|
|
|
|
2018-09-21 17:22:17 +00:00
|
|
|
tunnel2 = self.interface.find_tunnel_by_owner_name(self.session, zerohd)[0]
|
2018-07-21 01:50:24 +00:00
|
|
|
self.assertEqual(tunnel1, tunnel2)
|
|
|
|
|
2018-09-19 20:41:32 +00:00
|
|
|
self.assertRaises(EntryNameNotUniqueError, self.interface.add_tunnel, self.session, player, "South", 155, None)
|
|
|
|
|
|
|
|
loc = self.add_loc(player)
|
|
|
|
|
|
|
|
tunnel3 = self.interface.add_tunnel(self.session, player, "South", 155, 'test')
|
|
|
|
|
2018-09-21 17:22:17 +00:00
|
|
|
tunnel4 = self.interface.find_tunnel_by_owner_name(self.session, zerohd)[0]
|
2018-09-19 20:41:32 +00:00
|
|
|
self.assertEqual(tunnel3, tunnel4)
|
|
|
|
|
|
|
|
self.assertRaises(LocationHasTunnelError, self.interface.add_tunnel, self.session, player, "South", 155,
|
|
|
|
'test')
|
|
|
|
|
|
|
|
self.assertRaises(LocationLookUpError, self.interface.add_tunnel, self.session, player, "South", 155,
|
|
|
|
'no u')
|
|
|
|
|
2018-06-30 15:07:56 +00:00
|
|
|
def test_add_item(self):
|
2018-07-15 18:27:11 +00:00
|
|
|
owner = self.add_player()
|
2018-07-22 23:56:07 +00:00
|
|
|
self.add_shop(owner)
|
2018-08-10 02:25:55 +00:00
|
|
|
self.interface.add_item(self.session, owner, 'test', 'dirt', 1, 15)
|
2018-06-30 15:07:56 +00:00
|
|
|
|
2018-08-10 02:25:55 +00:00
|
|
|
shops = self.interface.find_shop_selling_item(self.session, 'dirt')
|
2018-07-21 05:45:52 +00:00
|
|
|
self.assertGreater(len(shops), 0)
|
2018-06-30 15:07:56 +00:00
|
|
|
|
2018-09-19 20:41:32 +00:00
|
|
|
self.assertRaises(LocationLookUpError, self.interface.add_item, self.session, owner, 'no u', 'dirt', 1, 15)
|
|
|
|
|
|
|
|
def test_find_player_by_discord_uuid(self):
|
|
|
|
p1 = self.add_player()
|
|
|
|
p2 = self.interface.find_player_by_discord_uuid(self.session, 143072699567177728)
|
|
|
|
|
2018-09-20 17:25:42 +00:00
|
|
|
self.assertEqual(p1, p2)
|
2018-09-19 20:41:32 +00:00
|
|
|
|
|
|
|
self.assertRaises(PlayerNotFound, self.interface.find_player_by_discord_uuid, self.session, 143072698)
|
|
|
|
|
|
|
|
def test_find_player_by_mc_uuid(self):
|
|
|
|
p1 = self.add_player()
|
|
|
|
p2 = self.interface.find_player_by_mc_uuid(self.session, 'fe7e84132570458892032b69ff188bc3')
|
|
|
|
|
2018-09-20 17:25:42 +00:00
|
|
|
self.assertEqual(p1, p2)
|
2018-09-19 20:41:32 +00:00
|
|
|
|
|
|
|
self.assertRaises(PlayerNotFound, self.interface.find_player_by_discord_uuid, self.session, 143072698)
|
|
|
|
|
2018-06-30 15:07:56 +00:00
|
|
|
def test_find_location_by_owner(self):
|
2018-07-15 18:27:11 +00:00
|
|
|
owner = self.add_player()
|
2018-07-22 23:56:07 +00:00
|
|
|
shop = self.add_shop(owner)
|
2018-06-30 15:07:56 +00:00
|
|
|
|
2018-08-10 02:25:55 +00:00
|
|
|
loc_list = self.interface.find_location_by_owner(self.session, owner)
|
2018-06-30 15:07:56 +00:00
|
|
|
|
|
|
|
self.assertEqual(loc_list[0].id, shop.id)
|
|
|
|
|
|
|
|
def test_find_location_by_name_and_owner(self):
|
2018-07-15 18:27:11 +00:00
|
|
|
owner = self.add_player()
|
2018-07-22 23:56:07 +00:00
|
|
|
shop = self.add_shop(owner)
|
2018-06-30 15:07:56 +00:00
|
|
|
|
2018-08-10 02:25:55 +00:00
|
|
|
loc_list = self.interface.find_location_by_name_and_owner(self.session, owner, 'test')
|
2018-06-30 15:07:56 +00:00
|
|
|
|
|
|
|
self.assertEqual(loc_list[0].id, shop.id)
|
|
|
|
|
|
|
|
def test_delete_base(self):
|
2018-07-15 18:27:11 +00:00
|
|
|
owner = self.add_player()
|
2018-07-22 23:56:07 +00:00
|
|
|
self.add_loc(owner)
|
2018-06-30 15:07:56 +00:00
|
|
|
|
2018-08-10 02:25:55 +00:00
|
|
|
self.interface.delete_location(self.session, owner, 'test')
|
2018-06-30 15:07:56 +00:00
|
|
|
|
2018-08-10 02:25:55 +00:00
|
|
|
loc_list = self.interface.find_location_by_name(self.session, 'test')
|
2018-06-30 15:07:56 +00:00
|
|
|
|
|
|
|
self.assertEqual(len(loc_list), 0)
|
|
|
|
|
|
|
|
def test_find_location_around(self):
|
2018-07-15 18:27:11 +00:00
|
|
|
owner = self.add_player()
|
2018-08-11 23:02:50 +00:00
|
|
|
self.add_loc(owner)
|
2018-06-30 15:07:56 +00:00
|
|
|
|
2018-07-15 18:27:11 +00:00
|
|
|
dim = "o"
|
2018-07-15 15:56:56 +00:00
|
|
|
|
2018-08-10 02:25:55 +00:00
|
|
|
loc_list = self.interface.find_location_around(self.session, 100, 100, 100, dim)
|
2018-06-30 15:07:56 +00:00
|
|
|
|
2018-07-22 23:56:07 +00:00
|
|
|
self.assertGreater(len(loc_list), 0)
|
2018-06-30 15:07:56 +00:00
|
|
|
|
2018-08-10 02:25:55 +00:00
|
|
|
loc_list = self.interface.find_location_around(self.session, 200, 200, 100, dim)
|
2018-06-30 15:07:56 +00:00
|
|
|
|
|
|
|
self.assertEqual(len(loc_list), 0)
|
|
|
|
|
2018-08-10 02:25:55 +00:00
|
|
|
loc_list = self.interface.find_location_around(self.session, -100, -100, 100, dim)
|
2018-07-08 19:19:40 +00:00
|
|
|
|
2018-07-22 23:56:07 +00:00
|
|
|
self.assertGreater(len(loc_list), 0)
|
2018-07-08 19:19:40 +00:00
|
|
|
|
2018-08-10 02:25:55 +00:00
|
|
|
loc_list = self.interface.find_location_around(self.session, 100, -100, 100, dim)
|
2018-07-08 19:19:40 +00:00
|
|
|
|
2018-07-22 23:56:07 +00:00
|
|
|
self.assertGreater(len(loc_list), 0)
|
2018-07-08 19:19:40 +00:00
|
|
|
|
2018-08-10 02:25:55 +00:00
|
|
|
loc_list = self.interface.find_location_around(self.session, -100, 100, 100, dim)
|
2018-07-08 19:19:40 +00:00
|
|
|
|
2018-07-22 23:56:07 +00:00
|
|
|
self.assertGreater(len(loc_list), 0)
|
2018-07-08 19:19:40 +00:00
|
|
|
|
2018-08-10 02:25:55 +00:00
|
|
|
loc_list = self.interface.find_location_around(self.session, 50, -50, 100, dim)
|
2018-07-08 19:19:40 +00:00
|
|
|
|
2018-07-22 23:56:07 +00:00
|
|
|
self.assertGreater(len(loc_list), 0)
|
2018-07-08 19:19:40 +00:00
|
|
|
|
2018-07-01 15:10:10 +00:00
|
|
|
def test_find_location_by_name(self):
|
2018-07-15 18:27:11 +00:00
|
|
|
owner = self.add_player()
|
2018-07-22 23:56:07 +00:00
|
|
|
loc = self.add_loc(owner)
|
2018-07-01 15:10:10 +00:00
|
|
|
|
2018-08-10 02:25:55 +00:00
|
|
|
loc_list = self.interface.find_location_by_name(self.session, 'test')
|
2018-07-01 15:10:10 +00:00
|
|
|
|
|
|
|
self.assertEqual(loc_list[0].name, loc.name)
|
|
|
|
|
2018-09-26 13:48:23 +00:00
|
|
|
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)
|
|
|
|
|
2018-07-21 04:44:41 +00:00
|
|
|
def test_search_all(self):
|
|
|
|
owner = self.add_player()
|
2018-08-11 23:02:50 +00:00
|
|
|
self.add_loc(owner)
|
2018-07-21 04:44:41 +00:00
|
|
|
|
2018-09-21 17:22:17 +00:00
|
|
|
loc_list = self.interface.search_all_fields(self.session, zerohd)
|
2018-07-21 04:44:41 +00:00
|
|
|
|
2018-07-22 23:56:07 +00:00
|
|
|
self.assertGreater(len(loc_list), 0)
|
2018-07-21 04:44:41 +00:00
|
|
|
|
2018-06-30 15:07:56 +00:00
|
|
|
def test_wrong_case(self):
|
2018-07-15 18:27:11 +00:00
|
|
|
owner = self.add_player()
|
2018-07-22 23:56:07 +00:00
|
|
|
loc = self.add_loc(owner)
|
2018-06-30 15:07:56 +00:00
|
|
|
|
2018-09-21 17:22:17 +00:00
|
|
|
loc_list = self.interface.find_location_by_owner_name(self.session, zerohd)
|
2018-06-30 15:07:56 +00:00
|
|
|
|
|
|
|
self.assertEqual(loc_list[0].id, loc.id)
|
|
|
|
|
2018-08-30 17:27:05 +00:00
|
|
|
self.interface.add_loc(self.session, owner, 'testshop', 1, 3, 'neThEr', loc_type=Shop)
|
2018-07-01 15:10:10 +00:00
|
|
|
|
2018-08-10 02:25:55 +00:00
|
|
|
self.interface.add_item(self.session, owner, 'testshop', 'dirts', 1, 15)
|
2018-07-01 15:10:10 +00:00
|
|
|
|
2018-08-10 02:25:55 +00:00
|
|
|
shops = self.interface.find_shop_selling_item(self.session, 'diRt')
|
2018-07-01 15:10:10 +00:00
|
|
|
|
2018-07-21 05:45:52 +00:00
|
|
|
self.assertGreater(len(shops), 0)
|
2018-07-01 15:10:10 +00:00
|
|
|
|
2018-08-10 02:25:55 +00:00
|
|
|
loc_list = self.interface.find_location_by_name(self.session, 'TEST')
|
2018-07-01 15:10:10 +00:00
|
|
|
|
|
|
|
self.assertEqual(loc_list[0].name, 'test')
|
|
|
|
|
2018-07-04 00:05:35 +00:00
|
|
|
def test_big_input(self):
|
2018-07-15 18:27:11 +00:00
|
|
|
owner = self.add_player()
|
2018-07-04 00:05:35 +00:00
|
|
|
|
2018-08-30 17:27:05 +00:00
|
|
|
self.assertRaises(DatabaseValueError, self.interface.add_loc, self.session, owner,
|
2018-08-11 23:02:50 +00:00
|
|
|
'TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT'
|
|
|
|
'TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT'
|
2018-08-30 17:27:05 +00:00
|
|
|
'TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT', 0, 0, Shop)
|
2018-07-01 15:10:10 +00:00
|
|
|
|
2018-07-15 15:56:56 +00:00
|
|
|
def test_duplicate_name(self):
|
2018-07-22 23:56:07 +00:00
|
|
|
owner = self.add_player()
|
|
|
|
self.add_loc(owner)
|
2018-07-15 15:56:56 +00:00
|
|
|
|
2018-08-30 17:27:05 +00:00
|
|
|
self.assertRaises(EntryNameNotUniqueError, self.interface.add_loc, self.session,
|
|
|
|
owner, 'test', 0, 0, Shop)
|
2018-06-02 17:40:55 +00:00
|
|
|
|
2018-07-21 14:18:01 +00:00
|
|
|
def test_delete_parent(self):
|
|
|
|
owner = self.add_player()
|
2018-08-11 23:02:50 +00:00
|
|
|
self.add_shop(owner)
|
2018-07-21 14:18:01 +00:00
|
|
|
|
2018-08-10 02:25:55 +00:00
|
|
|
self.interface.add_item(self.session, owner, 'test', 'dirt', 1, 15)
|
2018-07-21 14:18:01 +00:00
|
|
|
|
2018-08-10 02:25:55 +00:00
|
|
|
self.interface.delete_location(self.session, owner, 'test')
|
2018-07-21 14:18:01 +00:00
|
|
|
|
2018-08-10 02:25:55 +00:00
|
|
|
shops = self.interface.find_shop_selling_item(self.session, 'dirt')
|
2018-07-21 18:28:31 +00:00
|
|
|
self.assertEqual(len(shops), 0)
|