def testRowShouldNotExistInTable(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        sut.row_should_not_exist_in_table('employee', "surname='Doe'")
    def testTableMustBeEmpty(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        sut.table_must_be_empty('employee')
    def testOpenConnection(self):
        dbDriver = DBDriverMock()
        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        self.assertTrue(dbDriver.connection.isConnected,
                        'Disconnected from database but should be connected.')
    def testCheckIfNotExistsInDatabase(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        sut.check_if_not_exists_in_database('select * from employee')
    def testRowsCountIsZero(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        sut.rows_count_is_0('select * from employee')
Example #6
0
    def testOpenConnection(self):
        dbDriver = DBDriverMock()
        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        self.assertTrue(dbDriver.connection.isConnected,
                        'Disconnected from database but should be connected.')
    def testCloseConnection(self):
        dbDriver = DBDriverMock()
        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        sut.disconnect_from_database()

        self.assertFalse(dbDriver.connection.isConnected,
                         'Connected to database but should be disconnected.')
    def testCheckIfExistsInDatabase(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        responses = {'select * from employee': [(0, 'John', 'Doe')]}

        dbDriver.connection.cursor().setQueryResponses(responses)

        sut.check_if_exists_in_database('select * from employee')
    def testTableMustContainNumberOfRows(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        responses = {"select * from employee": [(0, 'John', 'Doe'),
                                                (1, 'Jane', 'Doe')]}

        dbDriver.connection.cursor().setQueryResponses(responses)

        sut.table_must_contain_number_of_rows('employee', 2)
    def testRowCountIsLessThanX(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        responses = {'select * from employee': [(0, 'John', 'Doe'),
                                                (1, 'Jane', 'Doe')]}

        dbDriver.connection.cursor().setQueryResponses(responses)

        sut.row_count_is_less_than_x('select * from employee', 3)
    def testCheckContentForRowIdentifiedByRownum(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        responses = {'select id,name,surname from employee':
                     [(0, 'John', 'Doe'), (1, 'Jane', 'Doe')]}

        dbDriver.connection.cursor().setQueryResponses(responses)

        sut.check_content_for_row_identified_by_rownum(
            ['id', 'name', 'surname'], [0, 'John', 'Doe'], 'employee', 1)
    def testRowsCount(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        responses = {'select * from employee': [(0, 'John', 'Doe'),
                                                (1, 'Jane', 'Doe')]}

        dbDriver.connection.cursor().setQueryResponses(responses)

        self.assertTrue(sut.rows_count('select * from employee') == 2,
                        'Incorrect rows count.')
    def testTableMustExist(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        responses = {("SELECT * FROM information_schema.tables WHERE "
                      "table_name='employee'"):
                     [('someDbName', 'public', 'employee', 'BASE TABLE',
                       None, None, None, None, None, 'YES', 'NO', None)]}

        dbDriver.connection.cursor().setQueryResponses(responses)

        sut.table_must_exist('employee')
    def testVerifyNumberOfRowsMatchingWhere(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        responses = {"select * from employee where surname='Doe'":
                     [(0, 'John', 'Doe'),
                      (1, 'Jane', 'Doe')]}

        dbDriver.connection.cursor().setQueryResponses(responses)

        sut.verify_number_of_rows_matching_where('employee',
                                                 "surname='Doe'", 2)
    def testQuery(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        responses = {'select * from employee': [(0, 'John', 'Doe'),
                                                (1, 'Jane', 'Doe')]}

        dbDriver.connection.cursor().setQueryResponses(responses)

        result = sut.query('select * from employee')

        self.assertTrue(responses['select * from employee'] == result,
                        'Query was performed with fail.')
Example #16
0
    def testRowsCount(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        responses = {
            'select * from employee': [(0, 'John', 'Doe'), (1, 'Jane', 'Doe')]
        }

        dbDriver.connection.cursor().setQueryResponses(responses)

        self.assertTrue(
            sut.rows_count('select * from employee') == 2,
            'Incorrect rows count.')
    def testExecuteSql(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        self.assertFalse(dbDriver.connection.cursor().wasExecuted or
                         dbDriver.connection.wasCommitted,
                         'Incorrect driver initial state.')

        sut.execute_sql('select 1 from 1')

        self.assertTrue(dbDriver.connection.cursor().wasExecuted and
                        dbDriver.connection.wasCommitted,
                        'Query was not performed and committed.')
Example #18
0
    def testQuery(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        responses = {
            'select * from employee': [(0, 'John', 'Doe'), (1, 'Jane', 'Doe')]
        }

        dbDriver.connection.cursor().setQueryResponses(responses)

        result = sut.query('select * from employee')

        self.assertTrue(responses['select * from employee'] == result,
                        'Query was performed with fail.')
    def testReadSingleValueFromTable(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        responses = {"select name from employee where surname='Doe'":
                         [('John',)]}

        dbDriver.connection.cursor().setQueryResponses(responses)

        result = sut.read_single_value_from_table('employee', 'name',
                                                  "surname='Doe'")

        self.assertTrue(responses[("select name from employee "
                                   "where surname='Doe'")][0] ==
                        result, 'Read Single Value was performed with fail.')
Example #20
0
    def testReadSingleValueFromTable(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        responses = {
            "select name from employee where surname='Doe'": [('John', )]
        }

        dbDriver.connection.cursor().setQueryResponses(responses)

        result = sut.read_single_value_from_table('employee', 'name',
                                                  "surname='Doe'")

        self.assertTrue(
            responses[("select name from employee "
                       "where surname='Doe'")][0] == result,
            'Read Single Value was performed with fail.')
Example #21
0
    def testTableMustBeEmpty(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        sut.table_must_be_empty('employee')
Example #22
0
    def testRowsCountIsZero(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        sut.rows_count_is_0('select * from employee')
Example #23
0
    def testCheckIfNotExistsInDatabase(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        sut.check_if_not_exists_in_database('select * from employee')
Example #24
0
    def testRowShouldNotExistInTable(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        sut.row_should_not_exist_in_table('employee', "surname='Doe'")
Example #25
0
    def testCloseAllConnections(self):
        dbDriver1 = DBDriverMock()
        dbDriver2 = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver1, 'someDbName1', 'someUsername',
                                'somePassword', 'someHost', '7777')

        sut.connect_to_database(dbDriver2, 'someDbName2', 'someUsername',
                                'somePassword', 'someHost', '7777')

        sut.disconnect_from_all_databases()

        self.assertFalse(
            dbDriver1.connection.isConnected
            or dbDriver2.connection.isConnected,
            'Connected to database but should be disconnected.')
Example #26
0
    def testCloseConnection(self):
        dbDriver = DBDriverMock()
        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        sut.disconnect_from_database()

        self.assertFalse(dbDriver.connection.isConnected,
                         'Connected to database but should be disconnected.')
Example #27
0
    def testCheckIfExistsInDatabase(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        responses = {'select * from employee': [(0, 'John', 'Doe')]}

        dbDriver.connection.cursor().setQueryResponses(responses)

        sut.check_if_exists_in_database('select * from employee')
Example #28
0
    def testDbElementsAreEqual(self):
        dbDriver1 = DBDriverMock()
        dbDriver2 = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver1, 'someDbName1', 'someUsername',
                                'somePassword', 'someHost', '7777', 'db1')

        sut.connect_to_database(dbDriver2, 'someDbName2', 'someUsername',
                                'somePassword', 'someHost', '7777', 'db2')

        responses = {
            'select * from employee': [(0, 'John', 'Doe'), (1, 'Jane', 'Doe')]
        }

        dbDriver1.connection.cursor().setQueryResponses(responses)
        dbDriver2.connection.cursor().setQueryResponses(responses)

        sut.db_elements_are_equal('select * from employee', 'db1', 'db2')
    def testCloseAllConnections(self):
        dbDriver1 = DBDriverMock()
        dbDriver2 = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver1, 'someDbName1', 'someUsername',
                                'somePassword', 'someHost', '7777')

        sut.connect_to_database(dbDriver2, 'someDbName2', 'someUsername',
                                'somePassword', 'someHost', '7777')

        sut.disconnect_from_all_databases()

        self.assertFalse(dbDriver1.connection.isConnected or
                         dbDriver2.connection.isConnected,
                         'Connected to database but should be disconnected.')
Example #30
0
    def testRowCountIsLessThanX(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        responses = {
            'select * from employee': [(0, 'John', 'Doe'), (1, 'Jane', 'Doe')]
        }

        dbDriver.connection.cursor().setQueryResponses(responses)

        sut.row_count_is_less_than_x('select * from employee', 3)
Example #31
0
    def testTableMustContainNumberOfRows(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        responses = {
            "select * from employee": [(0, 'John', 'Doe'), (1, 'Jane', 'Doe')]
        }

        dbDriver.connection.cursor().setQueryResponses(responses)

        sut.table_must_contain_number_of_rows('employee', 2)
    def testDbElementsAreEqual(self):
        dbDriver1 = DBDriverMock()
        dbDriver2 = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver1, 'someDbName1', 'someUsername',
                                'somePassword', 'someHost', '7777', 'db1')

        sut.connect_to_database(dbDriver2, 'someDbName2', 'someUsername',
                                'somePassword', 'someHost', '7777', 'db2')

        responses = {'select * from employee': [(0, 'John', 'Doe'),
                                                (1, 'Jane', 'Doe')]}

        dbDriver1.connection.cursor().setQueryResponses(responses)
        dbDriver2.connection.cursor().setQueryResponses(responses)

        sut.db_elements_are_equal('select * from employee', 'db1', 'db2')
Example #33
0
    def testSetCurrentDatabase(self):
        dbDriver1 = DBDriverMock()
        dbDriver2 = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver1, 'someDbName1', 'someUsername',
                                'somePassword', 'someHost', '7777', 'db1')

        sut.connect_to_database(dbDriver2, 'someDbName2', 'someUsername',
                                'somePassword', 'someHost', '7777', 'db2')

        self.assertTrue(
            id(sut._connectionCache.current.connection) == id(
                dbDriver2.connection),
            'Current database is db1 but should be db2.')

        sut.set_current_database('db1')

        self.assertTrue(
            id(sut._connectionCache.current.connection) == id(
                dbDriver1.connection),
            'Current database is db2 but should be db1.')
Example #34
0
    def testCheckContentForRowIdentifiedByRownum(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        responses = {
            'select id,name,surname from employee': [(0, 'John', 'Doe'),
                                                     (1, 'Jane', 'Doe')]
        }

        dbDriver.connection.cursor().setQueryResponses(responses)

        sut.check_content_for_row_identified_by_rownum(
            ['id', 'name', 'surname'], [0, 'John', 'Doe'], 'employee', 1)
Example #35
0
    def testVerifyNumberOfRowsMatchingWhere(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        responses = {
            "select * from employee where surname='Doe'": [(0, 'John', 'Doe'),
                                                           (1, 'Jane', 'Doe')]
        }

        dbDriver.connection.cursor().setQueryResponses(responses)

        sut.verify_number_of_rows_matching_where('employee', "surname='Doe'",
                                                 2)
    def testSetCurrentDatabase(self):
        dbDriver1 = DBDriverMock()
        dbDriver2 = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver1, 'someDbName1', 'someUsername',
                                'somePassword', 'someHost', '7777', 'db1')

        sut.connect_to_database(dbDriver2, 'someDbName2', 'someUsername',
                                'somePassword', 'someHost', '7777', 'db2')

        self.assertTrue(id(sut._connectionCache.current.connection) ==
                        id(dbDriver2.connection),
                        'Current database is db1 but should be db2.')

        sut.set_current_database('db1')

        self.assertTrue(id(sut._connectionCache.current.connection) ==
                        id(dbDriver1.connection),
                        'Current database is db2 but should be db1.')
Example #37
0
    def testTableMustExist(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        responses = {
            ("SELECT * FROM information_schema.tables WHERE "
             "table_name='employee'"):
            [('someDbName', 'public', 'employee', 'BASE TABLE', None, None,
              None, None, None, 'YES', 'NO', None)]
        }

        dbDriver.connection.cursor().setQueryResponses(responses)

        sut.table_must_exist('employee')
Example #38
0
    def testExecuteSql(self):
        dbDriver = DBDriverMock()

        sut = Pydblibrary()

        sut.connect_to_database(dbDriver, 'someDbName', 'someUsername',
                                'somePassword', 'someHost', '7777')

        self.assertFalse(
            dbDriver.connection.cursor().wasExecuted
            or dbDriver.connection.wasCommitted,
            'Incorrect driver initial state.')

        sut.execute_sql('select 1 from 1')

        self.assertTrue(
            dbDriver.connection.cursor().wasExecuted
            and dbDriver.connection.wasCommitted,
            'Query was not performed and committed.')