def __show_interface(self, database: Database) -> bool: """ Function shows a command line for this app. On receiving a command from user, it passes query to Database entity, which tries to proceed it. If Database got an Error, function prints this error. In other case, it prints a result. On \\q command from user, it simply returns False. :param database: Database :return: bool """ command = input(self.__present_command_line(database.get_name())) if command == '\q': return False database.proceed_query(command) if database.has_error(): print(database.get_error()) return True if database.get_query().get_aggregate_function() != '': print(database.get_query().get_result()[0]) else: for row in database.get_query().get_result(): join_str = ', ' print(join_str.join(row)) return True
def handle(self, path: str) -> Database: """ Method receives path as a string and tries to reach this path. If path is incorrect, it returns Database entity with error message. In case of succeed, function goes through each file and tries to pass it through a factory, that returns reader or fails if no suitable reader found for this specific file. :param path: str :return: Database """ if not os.path.isdir(path): database = Database('', Query()) database.set_error('Path incorrect') return database database_name = os.path.basename(os.path.normpath(path)) database = Database(database_name, Query()) files = os.listdir(path) for file in files: table_parts = file.split('.') try: table = self.__reader_factory.create(table_parts).read( path + '/' + file, table_parts[0]) database.add_table(table) except: pass return database
def test_init(self, input): database = Database('db_name', Query()) controller = Controller() with patch('sys.stdout', new=StringIO()) as fake_out: controller.init(database) self.assertEqual(fake_out.getvalue(), 'Tables found: 0\n')
def test_database_proceed_filled_query_string_with_table_exist_fills_query_table_field( self): query = Query() database = Database('database_name', query) database.add_table(Table('drivers', [])) database.proceed_query('SELECT company, country FROM drivers;') self.assertEqual(database.get_query().get_table(), 'drivers')
def test_init_with_database(self, input): query = Query() query.get_aggregate_function = MagicMock(return_value='COUNT(*)') query.get_result = MagicMock(return_value=[3]) table = Table('drivers', [ ['id', 'first_name', 'last_name', 'country', 'year_of_birth', 'company'], ['1', 'John', 'Carmack', 'USA', '1970', 'id Software'], ['2', 'Jonn', 'Romero', 'USA', '1967', 'id Software'], ['3', 'Elon', 'Musk', 'USA', '1971', 'Tesla'], ['4', 'Damon', 'Hill', 'UK', '1960', 'Williams']]) database = Database('database_name', query) database.add_table(table) controller = Controller() with patch('sys.stdout', new=StringIO()) as fake_out: controller.init(database) self.assertEqual(fake_out.getvalue(), 'Tables found: 1\ndrivers\n')
def test_database_default(self): query = Query() database = Database('database_name', query) self.assertEqual(database.get_name(), 'database_name') self.assertEqual(database.get_tables(), {}) self.assertEqual(database.get_error(), '') self.assertEqual(database.has_error(), False) self.assertEqual(database.get_query(), query)
def __present_interface(self, database: Database) -> None: """ Auxiliary function, aimed to show a quantity of tables found, and list them. :param database: Database :return: """ print(f'Tables found: {len(database.get_tables())}') keys = database.get_tables().keys() for key in keys: print(key)
def test_it_fills_aggregate_function_field(self): query = Query() query.set_aggregate_function = MagicMock() database = Database('database_name', query) database.add_table(Table('drivers', [])) database.proceed_query('SELECT COUNT(*) FROM drivers;') query.set_aggregate_function.assert_called_once_with('COUNT(*)')
def test_it_fails_if_field_not_exist(self): query = Query() table = Table('drivers', [[ 'id', 'first_name', 'last_name', 'country', 'year_of_birth', 'company' ], ['1', 'John', 'Carmack', 'USA', '1970', 'id Software' ], ['2', 'Jonn', 'Romero', 'USA', '1967', 'id Software' ], ['3', 'Elon', 'Musk', 'USA', '1971', 'Tesla' ], ['4', 'Damon', 'Hill', 'UK', '1960', 'Williams']]) database = Database('database_name', query) database.add_table(table) database.proceed_query('SELECT *, id_name FROM drivers;') self.assertEqual(database.get_query().get_select_expression(), []) self.assertEqual(database.get_error(), 'Field not exist')
def test_it_shows_error_if_aggregate_function_witout_if_statement(self): query = Query() table = Table('drivers', [[ 'id', 'first_name', 'last_name', 'country', 'year_of_birth', 'company' ], ['1', 'John', 'Carmack', 'USA', '1970', 'id Software' ], ['2', 'Jonn', 'Romero', 'USA', '1967', 'id Software' ], ['3', 'Elon', 'Musk', 'USA', '1971', 'Tesla' ], ['4', 'Damon', 'Hill', 'UK', '1960', 'Williams']]) database = Database('database_name', query) database.add_table(table) database.proceed_query('SELECT COUNT(*) FROM drivers;') self.assertEqual(database.get_query().get_select_expression(), []) self.assertEqual( database.get_error(), 'Query contains aggregate function, but WHERE statement missed')
def test_it_parses_complicated_where_statement_with_lots_of_spaces(self): query = Query() table = Table('drivers', [[ 'id', 'first_name', 'last_name', 'country', 'year_of_birth', 'company' ], ['1', 'John', 'Carmack', 'USA', '1970', 'id Software' ], ['2', 'Jonn', 'Romero', 'USA', '1967', 'id Software' ], ['3', 'Elon', 'Musk', 'USA', '1971', 'Tesla' ], ['4', 'Damon', 'Hill', 'UK', '1960', 'Williams']]) database = Database('database_name', query) database.add_table(table) database.proceed_query( "SELECT first_name, country FROM drivers WHERE country = 'USA' OR company = 'sdf';" ) self.assertEqual(database.get_query().get_select_expression(), ['first_name', 'country']) self.assertEqual(database.get_query().get_where_clause(), [('', 'country', 'USA'), ('OR', 'company', 'sdf')])
def test_it_parses_simple_where_statement_with_non_existed_field(self): query = Query() table = Table('drivers', [[ 'id', 'first_name', 'last_name', 'country', 'year_of_birth', 'company' ], ['1', 'John', 'Carmack', 'USA', '1970', 'id Software' ], ['2', 'Jonn', 'Romero', 'USA', '1967', 'id Software' ], ['3', 'Elon', 'Musk', 'USA', '1971', 'Tesla' ], ['4', 'Damon', 'Hill', 'UK', '1960', 'Williams']]) database = Database('database_name', query) database.add_table(table) database.proceed_query( "SELECT first_name FROM drivers WHERE if1 = 'USA';") self.assertEqual(database.get_error(), 'Column from where_expression not found')
def test_database_proceed_filled_query_string_with_table_not_exist(self): query = Query() database = Database('database_name', query) database.add_table(Table('drivers', [])) database.proceed_query('SELECT company, country FROM drivers1;') self.assertEqual(database.get_error(), 'Table not exist')
def test_database_proceed_empty_query_string(self): query = Query() database = Database('database_name', query) database.proceed_query('') self.assertEqual(database.get_error(), 'Wrong query syntax, please try again')
def test_database_setters(self): query = Query() query.set_table('tada') table = Table('table_name', []) error_text = 'some error' database = Database('database_name', query) database.add_table(table) database.set_error(error_text) self.assertEqual(database.get_name(), 'database_name') self.assertEqual(database.get_tables(), {'table_name': table}) self.assertEqual(database.get_error(), error_text) self.assertEqual(database.has_error(), True) self.assertEqual(database.get_query(), query)