Exemplo n.º 1
0
 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(*)')
Exemplo n.º 2
0
    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
Exemplo n.º 3
0
 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')
Exemplo n.º 4
0
 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'])
Exemplo n.º 5
0
 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')])
Exemplo n.º 6
0
 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')
Exemplo n.º 7
0
 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')
Exemplo n.º 8
0
 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')
Exemplo n.º 9
0
 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')