def test_table_selects_simple_query_with_one_field(self): table = Table('table_name', [ ['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'] ]) query = Query() query.get_select_expression = MagicMock(return_value=['id']) query.get_aggregate_function = MagicMock(return_value='') query.get_where_clause = MagicMock(return_value=[]) self.assertEqual(table.select_rows(query), [['1'], ['2'], ['3'], ['4']])
def test_table_returns_correct_values(self): table = Table('table_name', [ ['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'] ]) self.assertEqual(table.get_name(), 'table_name') self.assertEqual(table.get_column_names(), ['id', 'first_name', 'last_name', 'country', 'year_of_birth', 'company']) self.assertEqual(table.get_rows(), [['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']])
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_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_it_returns_table(self): reader = CSVReader() 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'] ]) self.assertIsInstance(reader.read('./csvs/drivers.csv', 'drivers'), Table) self.assertEqual('drivers', table.get_name()) self.assertEqual(['id', 'first_name', 'last_name', 'country', 'year_of_birth', 'company'], table.get_column_names()) self.assertEqual([['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']], table.get_rows())
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)
def test_it_fills_select_expression_with_multiple_fields(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, last_name FROM drivers;') self.assertEqual(database.get_query().get_select_expression(), ['first_name', 'last_name'])
def test_it_parses_simple_where_statement_with_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 country = 'USA';") self.assertEqual(database.get_query().get_where_clause(), [('', 'country', 'USA')])
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_second_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 country = 'USA' AND df = 'sdf';" ) self.assertEqual(database.get_error(), 'Column from where_expression not found')
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 read(self, full_path: str, table_name: str) -> Table: file_to_read = open(full_path, encoding='utf-8') csv_data = csv.reader(file_to_read) data_lines = list(csv_data) return Table(table_name.lower(), data_lines)
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 add_table(self, table: Table) -> None: self.__tables[table.get_name()] = table