def test_clear_database(self): member_db = MemberDatabase(self.db, "?", "member_table_v") member_db.create() member_db.add(b'test_data', "John Smith", "*****@*****.**", datetime.now() + timedelta(days = 1)) member_db.add(b'othe_data', "Crystal Meth", "*****@*****.**", datetime.now() + timedelta(days = 1)) member_db.clear() self.assertEqual(len(member_db.list()), 0)
class TestDoorConnectionUpdate(unittest.TestCase): db_path_local = u'/tmp/test_door_connection_update_local.db' db_path_remote = u'/tmp/test_door_connection_update_remote.db' def setUp(self): self.db_local = sqlite3.connect(self.db_path_local) self.member_db_local = MemberDatabase(self.db_local, "?") self.member_db_local.create() self.db_remote = sqlite3.connect(self.db_path_remote) self.member_db_remote = MemberDatabase(self.db_remote, "?") self.member_db_remote.create() self.member_db_remote.add(b'test_data', u'John Smith', u'*****@*****.**', datetime.now() + timedelta(days=1)) self.member_db_remote.add(b'dope_data', u'Crystal Meth', u'*****@*****.**', datetime.now() + timedelta(days=1)) self.door_connection = DoorConnection(self.member_db_local, self.member_db_remote) def tearDown(self): self.db_local.close() self.db_remote.close() remove(self.db_path_local) remove(self.db_path_remote) def test_update_duplicates(self): self.door_connection.update() self.assertEqual(len(self.member_db_local.list()), 2) self.assertTrue(self.member_db_local.have(b'test_data')) self.assertTrue(self.member_db_local.have(b'dope_data'))
class TestDoorConnectionRecover(unittest.TestCase): db_path_local = u'/tmp/test_door_connection_check_recover_local.db' def setUp(self): config = {} with open(u'test_db.json') as config_file: config = json.load(config_file) self.db_local = sqlite3.connect(self.db_path_local) self.member_db_local = None self.has_mysql = True self.db_remote = None self.member_db_remote = None self.door_connection = None try: self.db_remote = mysql.connector.connect(**config) except: self.has_mysql = False print(u'MySQL DB Connection Failure') self.member_db_remote = MemberDatabase(self.db_remote, u'%s') if self.has_mysql: self.member_db_remote.create() self.member_db_remote.add(b'test_data', u'John Smith', u'*****@*****.**', datetime.now() + timedelta(days=1)) self.member_db_remote.add(b'dope_data', u'Crystal Meth', u'*****@*****.**', datetime.now() + timedelta(days=1)) self.member_db_local = MemberDatabase(self.db_local, u'?') self.member_db_local.create() self.door_connection = DoorConnection(self.member_db_local, self.member_db_remote) def tearDown(self): self.db_local.close() remove(self.db_path_local) def test_mysql_remote_verifies(self): if self.has_mysql: self.assertTrue(self.door_connection.check_request(b'test_data')) self.assertTrue(self.door_connection.check_request(b'dope_data')) self.member_db_remote.destroy() self.db_remote.close() def test_local_verifies_with_broken_mysql_remote(self): if self.has_mysql: self.door_connection.update() self.member_db_remote.destroy() self.db_remote.close() self.assertTrue(self.door_connection.check_request(b'test_data')) self.assertTrue(self.door_connection.check_request(b'dope_data')) def test_checking_syncs_mysql(self): if self.has_mysql: self.assertTrue(self.door_connection.check_request(b'test_data')) self.assertTrue(self.door_connection.check_request(b'dope_data')) self.member_db_remote.destroy() self.db_remote.close() self.assertTrue(self.door_connection.check_request(b'test_data')) self.assertTrue(self.door_connection.check_request(b'dope_data'))
def test_use_resource(self): cur = self.db1.cursor() cur.execute(""" CREATE TABLE member_table_v ( hash CHAR(24), name TEXT, email VARCHAR(254), expiration_date DATE, resource VARCHAR(255)); """) cur.execute(""" INSERT INTO member_table_v (hash, name, email, expiration_date, resource) VALUES ( ?, 'John Smith', '*****@*****.**', ?, 'door' ), ( ?, 'John Smith', '*****@*****.**', ?, 'laser' ), ( ?, 'Crystal Meth', '*****@*****.**', ?, 'door' ); """, ( MemberDatabase.hash(b'test_data'), datetime.now() + timedelta(days = 1), MemberDatabase.hash(b'test_data'), datetime.now() + timedelta(days = 1), MemberDatabase.hash(b'othe_data'), datetime.now() + timedelta(days = 1), )) self.db1.commit() # Use laser resource member_db1a = MemberDatabase(self.db1, "?", "member_table_v") member_db1a.use_resource('laser') member_db2 = MemberDatabase(self.db2, "?", "member_table_v") member_db2.create() member_db2.mimic(member_db1a) self.assertTrue(member_db2.have_current(b'test_data')) self.assertFalse(member_db2.have_current(b'othe_data')) # Use door resource member_db1b = MemberDatabase(self.db1, "?", "member_table_v") member_db1b.use_resource('door') member_db3 = MemberDatabase(self.db3, "?", "member_table_v") member_db3.create() member_db3.mimic(member_db1b) self.assertTrue(member_db3.have_current(b'test_data')) self.assertTrue(member_db3.have_current(b'othe_data'))
def test_mimic_database(self): member_db1 = MemberDatabase(self.db1, "?", "member_table_v") member_db1.create() member_db1.add(b'test_data', "John Smith", "*****@*****.**", datetime.now() + timedelta(days = 1)) member_db1.add(b'othe_data', "Crystal Meth", "*****@*****.**", datetime.now() + timedelta(days = 1)) self.db1.commit() # member_db2 = MemberDatabase(self.db2, "?", "member_table_v") member_db2.create() member_db2.mimic(member_db1) self.assertEqual(len(member_db2.list()), 2)
class TestMemberDatabaseAdd(unittest.TestCase): db_path = "/tmp/test_member_database_add.db" def setUp(self): self.db = sqlite3.connect(self.db_path) self.member_db = MemberDatabase(self.db, "?", "member_table_v") self.member_db.create() def tearDown(self): # close the connection and delete the object self.db.close() remove(self.db_path) def test_add_member_does_not_fail(self): self.member_db.add(b'test_data', "John Smith", "*****@*****.**", datetime.now() + timedelta(days = 1))
class TestMemberDatabaseList(unittest.TestCase): db_path = "/tmp/test_member_database_list.db" def setUp(self): self.db = sqlite3.connect(self.db_path) self.member_db = MemberDatabase(self.db, "?", "member_table_v") self.member_db.create() self.member_db.add(b'test_data', "John Smith", "*****@*****.**", datetime.now() + timedelta(days = 1)) self.member_db.add(b'othe_data', "Crystal Meth", "*****@*****.**", datetime.now() + timedelta(days = 1)) def tearDown(self): # close the connection and delete the object self.db.close() remove(self.db_path) def test_list_contains_users(self): member_list = self.member_db.list() self.assertEqual(len(member_list), 2)
class TestMemberDatabaseHaveCurrent(unittest.TestCase): db_path = "/tmp/test_member_database_add.db" def setUp(self): self.db = sqlite3.connect(self.db_path) self.member_db = MemberDatabase(self.db, "?", "member_table_v") self.member_db.create() self.member_db.add(b'test_data', "John Smith", "*****@*****.**", datetime.now() + timedelta(days = 1)) def tearDown(self): # close the connection and delete the object self.db.close() remove(self.db_path) def test_checks_member_existence(self): self.assertTrue(self.member_db.have_current(b'test_data')) def test_checks_member_non_existence(self): self.assertFalse(self.member_db.have_current(b'bad_test_data'))
class TestMemberDatabaseDestroy(unittest.TestCase): db_path = "/tmp/test_member_database_destroy.db" def setUp(self): self.db = sqlite3.connect(self.db_path) self.member_db = MemberDatabase(self.db, "?", "member_table_v") def tearDown(self): # close the connection and delete the object self.db.close() remove(self.db_path) def test_creation_destruction_cycles_work(self): # Check that database creation doesn't cause an error self.member_db.create() self.member_db.destroy() self.member_db.create() self.member_db.destroy()
class TestDoorConnectionCheckRequest(unittest.TestCase): db_path_local = u'/tmp/test_door_connection_check_request_local.db' db_path_remote = u'/tmp/test_door_connection_check_request_remote.db' def setUp(self): self.db_local = sqlite3.connect(self.db_path_local) self.member_db_local = MemberDatabase(self.db_local, "?") self.member_db_local.create() self.db_remote = sqlite3.connect(self.db_path_remote) self.member_db_remote = MemberDatabase(self.db_remote, u'?') self.member_db_remote.create() self.member_db_remote.add(b'test_data', u'John Smith', u'*****@*****.**', datetime.now() + timedelta(days=1)) self.member_db_remote.add(b'dope_data', u'Crystal Meth', u'*****@*****.**', datetime.now() + timedelta(days=1)) self.door_connection = DoorConnection(self.member_db_local, self.member_db_remote) def tearDown(self): self.db_local.close() self.db_remote.close() remove(self.db_path_local) remove(self.db_path_remote) def test_remote_verifies(self): self.assertTrue(self.door_connection.check_request(b'test_data')) self.assertTrue(self.door_connection.check_request(b'dope_data')) def test_local_verifies_with_broken_remote(self): self.door_connection.update() self.db_remote.close() self.assertTrue(self.door_connection.check_request(b'test_data')) self.assertTrue(self.door_connection.check_request(b'dope_data')) def test_checking_syncs(self): self.assertTrue(self.door_connection.check_request(b'test_data')) self.assertTrue(self.door_connection.check_request(b'dope_data')) self.db_remote.close() self.assertTrue(self.door_connection.check_request(b'test_data')) self.assertTrue(self.door_connection.check_request(b'dope_data'))
class TestMemberDatabaseCreate(unittest.TestCase): db_path = "/tmp/test_member_database_create.db" def setUp(self): self.db = sqlite3.connect(self.db_path) self.member_db = MemberDatabase(self.db, "?", "member_table_v") def tearDown(self): # close the connection and delete the object self.db.close() remove(self.db_path) def test_can_create(self): # Check that database creation doesn't cause an error self.member_db.create() def test_double_creation_fails(self): # Check that trying to create an existing database throws an error self.member_db.create() with self.assertRaises(self.db.OperationalError): self.member_db.create()