From 95ee2046715024e7eedf663e09161933689a7aef Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Sat, 24 Oct 2015 13:28:24 -0700 Subject: Initial commit. --- tests/__init__.py | 0 tests/test_canarydb.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/test_canarydb.py (limited to 'tests') diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_canarydb.py b/tests/test_canarydb.py new file mode 100644 index 0000000..5a0fc23 --- /dev/null +++ b/tests/test_canarydb.py @@ -0,0 +1,66 @@ +import unittest +import tempfile, shutil +import datetime + +from emailcanary import canarydb + +class TestCanaryDB(unittest.TestCase): + def setUp(self): + self.tempdir = tempfile.mkdtemp() + self.db = canarydb.CanaryDB(self.tempdir + "canary.db") + + def tearDown(self): + self.db.close() + shutil.rmtree(self.tempdir) + + def testPingCheckPong(self): + address = "test@example.com" + time = datetime.datetime(2015, 10, 24, 9, 00) + uuid = "1234" + expectedDelta = datetime.datetime.now() - time + + # Record a Ping + self.db.ping(address, time, uuid) + + # Check for missing pongs + missing = self.db.get_missing_pongs() + + self.assertEqual(1, len(missing)) + firstMissing = missing[0] + self.assertEqual(3, len(firstMissing)) + self.assertEqual(uuid, firstMissing[0]) + self.assertEqual(address, firstMissing[1]) + delta = firstMissing[2].total_seconds() - expectedDelta.total_seconds() + self.assertTrue(delta <= 10) + + # Record a pong + pongtime = datetime.datetime(2015, 10, 24, 9, 05) + self.db.pong(address, pongtime, uuid) + + # Check for missing pongs + missing = self.db.get_missing_pongs() + self.assertEqual(0, len(missing)) + + def testCloseReopen(self): + address = "test@example.com" + time = datetime.datetime(2015, 10, 24, 9, 00) + uuid = "1234" + expectedDelta = datetime.datetime.now() - time + + # Record a Ping + self.db.ping(address, time, uuid) + + # Close, Reopen + self.db.close() + self.db = canarydb.CanaryDB(self.tempdir + "canary.db") + + # Check for missing pongs + missing = self.db.get_missing_pongs() + + self.assertEqual(1, len(missing)) + firstMissing = missing[0] + self.assertEqual(3, len(firstMissing)) + self.assertEqual(uuid, firstMissing[0]) + self.assertEqual(address, firstMissing[1]) + delta = firstMissing[2].total_seconds() - expectedDelta.total_seconds() + self.assertTrue(delta <= 10) -- cgit v1.2.3