From c9f5749611becf9e914a069f8bfd2723d5641b8b Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Sat, 2 Jun 2018 12:40:55 -0500 Subject: [PATCH] Added unit tests for GeoffreyDatabase --- DatabaseModels.py | 1 + Geoffrey.py | 1 + test_geoffreyDatabase.py | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 test_geoffreyDatabase.py diff --git a/DatabaseModels.py b/DatabaseModels.py index e7cce13..8ac7e2d 100644 --- a/DatabaseModels.py +++ b/DatabaseModels.py @@ -36,6 +36,7 @@ class GeoffreyDatabase: def combine_filter(self, filter_value): return sqlalchemy.sql.expression.and_(filter_value[0]) + class TunnelDirection(enum.Enum): North = 'green' East = 'blue' diff --git a/Geoffrey.py b/Geoffrey.py index 68f933a..e2802d9 100644 --- a/Geoffrey.py +++ b/Geoffrey.py @@ -115,6 +115,7 @@ async def deletebase(ctx, name: str): except DeleteEntryError: await bot.say('{}, you do not have a base named "{}".'.format(ctx.message.author.mention, name)) + @bot.command(pass_context=True) async def findbasearound(ctx, x_pos: int, z_pos: int, * args): ''' diff --git a/test_geoffreyDatabase.py b/test_geoffreyDatabase.py new file mode 100644 index 0000000..1da116b --- /dev/null +++ b/test_geoffreyDatabase.py @@ -0,0 +1,40 @@ +from unittest import TestCase +from DatabaseModels import GeoffreyDatabase +from DatabaseModels import Location +from BotErrors import * + + +class TestGeoffreyDatabase(TestCase): + def setUp(self): + self.database = GeoffreyDatabase('sqlite:///:memory:') + self.loc = Location('test', 1, 2, 3, 'owner', ['Green', 0]) + + def test_add_object(self): + self.database.add_object(self.loc) + + loc2 = self.database.query_by_filter(Location, Location.owner == 'owner')[0] + + self.assertEqual(self.loc.id, loc2.id) + + def test_query_by_filter(self): + expr = (Location.owner == 'owner') & (Location.x == 0) + loc2 = self.database.query_by_filter(Location, expr) + self.assertEqual(len(loc2), 0) + + def test_delete_entry(self): + self.database.add_object(self.loc) + expr = (Location.owner == 'owner') & (Location.name == 'test') + self.database.delete_entry(Location, expr) + + expr = (Location.owner == 'owner') & (Location.x == 0) + loc2 = self.database.query_by_filter(Location, expr) + + self.assertEqual(len(loc2), 0) + + self.assertRaises(DeleteEntryError, self.database.delete_entry, Location, expr) + + + + + +