commit 48d4562f544c0635e0ce75a4151a68ff10c6908f Author: Isis Lovecruft isis@torproject.org Date: Fri Oct 27 23:30:24 2017 +0000
Add unittests for SQL to determine which distributor to allocate to. --- bridgedb/test/test_Storage.py | 69 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 5 deletions(-)
diff --git a/bridgedb/test/test_Storage.py b/bridgedb/test/test_Storage.py index 8b0affd..dffba2b 100644 --- a/bridgedb/test/test_Storage.py +++ b/bridgedb/test/test_Storage.py @@ -1,17 +1,23 @@ #!/usr/bin/env python """Unittests for the :mod:`bridgedb.Storage` module."""
+import os +import threading +import time + from twisted.python import log from twisted.trial import unittest -import bridgedb.Storage as Storage from twisted.internet import reactor from twisted.internet.threads import deferToThread -import os -import threading -from time import sleep + +import bridgedb.Storage as Storage + +from bridgedb.test.util import generateFakeBridges
class DatabaseTest(unittest.TestCase): def setUp(self): + self.fakeBridges = generateFakeBridges() + self.validRings = ['https', 'unallocated', 'email', 'moat'] self.dbfname = 'test-bridgedb.sqlite' Storage.setDBFilename(self.dbfname)
@@ -22,7 +28,7 @@ class DatabaseTest(unittest.TestCase):
def _runAndDie(self, timeout, func): with func(): - sleep(timeout) + time.sleep(timeout)
def _cb_assertTrue(self, result): self.assertTrue(result) @@ -54,3 +60,56 @@ class DatabaseTest(unittest.TestCase): d2.addCallback(self._cb_assertFalse) d2.addErrback(self._eb_Failure) d2.addCallback(self._cb_assertTrue, Storage.getDB(False)) + + def test_insertBridgeAndGetRing_new_bridge(self): + bridge = self.fakeBridges[0] + Storage.initializeDBLock() + with Storage.getDB() as db: + ringname = db.insertBridgeAndGetRing(bridge, 'moat', + time.time(), + self.validRings) + self.assertIn(ringname, self.validRings) + + def test_insertBridgeAndGetRing_already_seen_bridge(self): + bridge = self.fakeBridges[0] + Storage.initializeDBLock() + with Storage.getDB() as db: + ringname = db.insertBridgeAndGetRing(bridge, 'moat', + time.time(), + self.validRings) + self.assertIn(ringname, self.validRings) + ringname = db.insertBridgeAndGetRing(bridge, 'https', + time.time(), + self.validRings) + self.assertIn(ringname, self.validRings) + self.assertEqual(ringname, 'moat') + + def test_getBridgeDistributor_recognised(self): + bridge = self.fakeBridges[0] + Storage.initializeDBLock() + with Storage.getDB() as db: + ringname = db.insertBridgeAndGetRing(bridge, 'moat', + time.time(), + self.validRings) + self.assertIn(ringname, self.validRings) + self.assertEqual(ringname, 'moat') + db.commit() + + with Storage.getDB() as db: + ringname = db.getBridgeDistributor(bridge, self.validRings) + self.assertEqual(ringname, 'moat') + + def test_getBridgeDistributor_unrecognised(self): + bridge = self.fakeBridges[0] + Storage.initializeDBLock() + with Storage.getDB() as db: + ringname = db.insertBridgeAndGetRing(bridge, 'godzilla', + time.time(), + self.validRings) + self.assertIn(ringname, self.validRings) + self.assertEqual(ringname, "unallocated") + db.commit() + + with Storage.getDB() as db: + ringname = db.getBridgeDistributor(bridge, self.validRings) + self.assertEqual(ringname, "unallocated")