class Controller: def __init__(self): self.db_handler = DatabaseHandler() self.data = None self.filehandler = None self.graph = None def load(self, filename): """ Set the file that will create the filehandler object """ if path.exists(filename): self.filehandler = FileHandler(filename) self.filehandler.set_file_type() return True else: return False def validate(self): """ Read selected file """ # James' changes (13/03) result = self.filehandler.read() self.data = result print(result) def set_local(self, connection): """ Set the local database with a specified name :param connection: :return: """ self.db_handler.set_local(connection) self.db_handler.insert_local_dict(self.data) def set_remote(self, host, user, password, db): """ Set the remote database :param host, user, password, db: :return: """ self.db_handler.set_remote(host, user, password, db) self.db_handler.insert_remote_dict(self.data) def set_graph(self, graph_type, filename): print(graph_type) print(filename) self.graph = Graph() data = self.data self.graph.set_data(data, graph_type, filename) def get_local(self): self.data = self.db_handler.get_local() def get_remote(self): self.data = self.db_handler.get_remote() def set_criteria(self, criteria_1, criteria_2=None): self.graph.set_criteria(criteria_1, criteria_2) def set_keys(self, key_1, key_2=None): self.graph.set_keys(key_1, key_2) def draw(self, x, y, title): self.graph.draw(x, y, title) def check_data(self): if self.data is not None: return True return False # Controller shouldn't run doctests??? # if __name__ == "__main__": # c = Controller() # doctest.testmod()
class TestDBHandler(TestCase): # Wesley def setUp(self): self.handler = DatabaseHandler() self.handler.set_local("TestingPurpose") self.handler.set_remote("localhost", "root", "", "test") # Wesley def tearDown(self): self.handler.drop_local_table() self.handler.drop_remote_table() self.handler = None # Wesley def test_remote_insert_values(self): expected = { 1: { "item": 'dskjhflkasdjlfkj23543' }, 2: { "item2": 'dskjhflkasdjlfkj23543' } } data = { 0: { "item": 'dskjhflkasdjlfkj23543' }, 1: { "item2": 'dskjhflkasdjlfkj23543' } } self.handler.insert_remote_dict(data) result = self.handler.get_remote() self.assertEqual(expected, result) # Wesley def test_local_insert_values(self): expected = { 1: { "item": 'dskjhflkasdjlfkj23543' }, 2: { "item2": 'dskjhflkasdjlfkj23543' } } data = { 0: { "item": 'dskjhflkasdjlfkj23543' }, 1: { "item2": 'dskjhflkasdjlfkj23543' } } self.handler.insert_local_dict(data) result = self.handler.get_local() self.assertEqual(expected, result) # Wesley def test_remote_get(self): expected = { 1: { "item": 'dskjhflkasdjlfkj23543' }, 2: { "item2": 'dskjhflkasdjlfkj23543' } } data = { 0: { "item": 'dskjhflkasdjlfkj23543' }, 1: { "item2": 'dskjhflkasdjlfkj23543' } } self.handler.insert_remote_dict(data) result = self.handler.get_remote() self.assertEqual(expected, result) # Wesley def test_local_get(self): expected = { 1: { "item": 'dskjhflkasdjlfkj23543' }, 2: { "item2": 'dskjhflkasdjlfkj23543' } } data = { 0: { "item": 'dskjhflkasdjlfkj23543' }, 1: { "item2": 'dskjhflkasdjlfkj23543' } } self.handler.insert_local_dict(data) result = self.handler.get_local() self.assertEqual(expected, result)
class Controller: def __init__(self): self.db_handler = DatabaseHandler() self.data = None self.filehandler = None self.graph = None def load(self, filename): """ Set the file that will create the filehandler object """ if path.exists(filename): self.filehandler = self.return_reader(filename) self.filehandler.filename = filename return True else: return False # noinspection PyMethodMayBeStatic def return_reader(self, filename): suffix = PurePosixPath(filename).suffix if suffix == '.csv': return FileTypeCSV() elif suffix == '.xlsx': return FileTypeXLSX() elif suffix == '.txt': return FileTypeTXT() else: print("File extension unknown") def validate(self): """ Read selected file """ result = self.filehandler.template_method() self.data = result print("") print(result) def set_local(self, connection): """ Set the local database with a specified name :param connection: :return: """ self.db_handler.set_local(connection) self.db_handler.insert_local_dict(self.data) def set_remote(self, host, user, password, db): """ Set the remote database :param host, user, password, db: :return: """ self.db_handler.set_remote(host, user, password, db) self.db_handler.insert_remote_dict(self.data) def set_graph(self, graph_type, filename): types = { 'pie': ConcretePie(), 'scatter': ConcreteScatter(), 'bar': ConcreteBar() } self.graph = types[graph_type] self.graph.set_file_name(filename) self.graph.set_graph_type(self.data) def get_local(self): self.data = self.db_handler.get_local() def get_remote(self): self.data = self.db_handler.get_remote() def set_criteria(self, criteria_1, criteria_2=None): self.graph.set_criteria(criteria_1, criteria_2) def set_keys(self, key_1, key_2=None): self.graph.set_keys(key_1, key_2) def draw(self, x, y, title): self.graph.draw(x, y, title) def check_data(self): if self.data is not None: return True return False