class TestUnpickler(TestCase): def setUp(self): self.local = DBLocal() self.local.connect() self.local.create_table() self.maxDiff = None def tearDown(self): self.local.close() self.local = None def test_unpickle_dict(self): expected = { 1: { "1ID": "A23", "Gender": "Male", "Age": 22, "Sales": 2445, "BMI": "normal", "salary": 20, "Birthday": "24/06/1995" }, 2: { "IhD": "A2f3", "Gender": "Male", "Age": 23, "Sales": 2565, "BMI": "normal", "salary": 20, "Birthday": "24/06/1995" }, 3: { "IjD": "Aa23", "Gender": "Female", "Age": 25, "Sales": 25, "BMI": "normal", "salary": 20, "Birthday": "24/06/1995" }, 4: { "IgD": "A23", "Gender": "Female", "Age": 26, "Sales": 225, "BMI": "normal", "salary": 20, "Birthday": "24/06/1995" } } pickle = Pickler.pickle_dictionary_values(expected) self.local.insert_dictionary(pickle) result = Unpickler.unpickle_dictionary(self.local.get_db()) self.assertEqual(expected, result)
def setUp(self): self.local = DBLocal() self.local.connect() self.local.create_table() self.maxDiff = None
def __init__(self): self.local = DBLocal() self.remote = DBRemote() self.local_connection = None self.remote_connection = None
class DatabaseHandler: def __init__(self): self.local = DBLocal() self.remote = DBRemote() self.local_connection = None self.remote_connection = None # A really, really, really bad decorator that can get fixed in ass2 //Wesley def local_decorator(f): def wrapper(*args): db = args[0].local_connection args[0].local.connect(db) # if args[0].local_connection == ":memory:": args[0].local.create_table() r = f(*args) args[0].local.commit() args[0].local.close() return r return wrapper # Part 2 of really, really, badness //Wesley def remote_decorator(f): def wrapper(*args): db = args[0].remote_connection args[0].remote.connect(db["host"], db["user"], db["password"], db["db"]) args[0].remote.create_table() r = f(*args) args[0].remote.commit() args[0].remote.close() return r return wrapper # Wesley def set_local(self, connection=":memory:"): self.local_connection = connection # Wesley def set_remote(self, host, user, password, db): self.remote_connection = { "host": host, "user": user, "password": password, "db": db } # Wesley @local_decorator def insert_local_dict(self, dictionary): """Insert values into both the local and remote""" pickled = Pickler.pickle_dictionary_values(dictionary) self.local.insert_dictionary(pickled) # print(self.local.get_db()) # Wesley @remote_decorator def insert_remote_dict(self, dictionary): """Insert values into both the local and remote""" pickled = Pickler.pickle_dictionary_values(dictionary) self.remote.insert_dictionary(pickled) # print(self.remote.get_db()) # Wesley @local_decorator def get_local(self): unpickle = Unpickler.unpickle_dictionary(self.local.get_db()) return unpickle # print(unpickle) # Wesley @remote_decorator def get_remote(self): unpickle = Unpickler.unpickle_dictionary(self.remote.get_db()) return unpickle # print(unpickle) # Wesley @local_decorator def drop_local_table(self): self.local.drop_table() # Wesley @remote_decorator def drop_remote_table(self): self.remote.drop_table()
def setUp(self): self.db = DBLocal() self.db.connect("TestLocal") self.db.create_table()
class TestLocal(TestCase): # Wesley def setUp(self): self.db = DBLocal() self.db.connect("TestLocal") self.db.create_table() # Wesley def tearDown(self): self.db.drop_table() self.db.close() self.db = None # Wesley def test_db_insert_values(self): """Create a table, insert data and check the return data is what was inserted""" expected = [(1, 'dskjhflkasdjlfkj23543'), (2, 'dskjhflkasdjlasdfdsafkj23543')] self.db.insert_record("dskjhflkasdjlfkj23543") self.db.insert_record("dskjhflkasdjlasdfdsafkj23543") self.db.commit() result = self.db.get_db() self.assertEqual(expected, result) def test_db_get(self): """Getting an empty database""" expected = list() result = self.db.get_db() self.assertEqual(expected, result) def test_db_insert_dict(self): """Insert a dictionary into the database""" expected = [(1, 'dskjhflkasdjlfkj23543'), (2, "dskjhflkasdjlasdfdsafkj23543")] data = {1: 'dskjhflkasdjlfkj23543', 2: "dskjhflkasdjlasdfdsafkj23543"} self.db.insert_dictionary(data) result = self.db.get_db() self.assertEqual(expected, result) def test_db_update(self): """Insert a dictionary into the database""" expected = [(1, 'dskjhflkasdjlfkj23543'), (2, "changed")] data = {1: 'dskjhflkasdjlfkj23543', 2: "dskjhflkasdjlasdfdsafkj23543"} self.db.insert_dictionary(data) self.db.update_record(2, "changed") result = self.db.get_db() self.assertEqual(expected, result) def test_db_delete(self): """Insert a dictionary into the database""" expected = [(1, 'dskjhflkasdjlfkj23543')] data = {1: 'dskjhflkasdjlfkj23543', 2: "dskjhflkasdjlasdfdsafkj23543"} self.db.insert_dictionary(data) self.db.delete_record("2") result = self.db.get_db() self.assertEqual(expected, result)
class DatabaseHandler: def __init__(self): self.local = DBLocal() self.remote = DBRemote() self.local_connection = None self.remote_connection = None def local_decorator(f): def wrapper(*args): db = args[0].local_connection args[0].local.connect(db) args[0].local.create_table() r = f(*args) args[0].local.commit() args[0].local.close() return r return wrapper def remote_decorator(f): def wrapper(*args): db = args[0].remote_connection args[0].remote.connect(db["host"], db["user"], db["password"], db["db"]) args[0].remote.create_table() r = f(*args) args[0].remote.commit() args[0].remote.close() return r return wrapper def set_local(self, connection=":memory:"): self.local_connection = connection def set_remote(self, host, user, password, db): self.remote_connection = { "host": host, "user": user, "password": password, "db": db } @local_decorator def insert_local_dict(self, dictionary): """Insert values into both the local and remote""" pickled = Pickler.pickle_dictionary_values(dictionary) self.local.insert_dictionary(pickled) @remote_decorator def insert_remote_dict(self, dictionary): """Insert values into both the local and remote""" pickled = Pickler.pickle_dictionary_values(dictionary) self.remote.insert_dictionary(pickled) @local_decorator def get_local(self): unpickle = Pickler.unpickle_dictionary(self.local.get_db()) return unpickle @remote_decorator def get_remote(self): unpickle = Pickler.unpickle_dictionary(self.remote.get_db()) return unpickle @local_decorator def drop_local_table(self): self.local.drop_table() @remote_decorator def drop_remote_table(self): self.remote.drop_table()