def test_add_index_with_existing_index(self):
        """Testing DatabaseState.add_index with existing index"""
        database_state = DatabaseState(db_name='default', scan=False)
        database_state.add_table('my_test_table')
        database_state.add_index(table_name='my_test_table',
                                 index_name='existing_index',
                                 columns=['col1', 'col2'],
                                 unique=True)

        expected_message = (
            'Unable to add index "existing_index" to table "my_test_table". '
            'This index already exists.'
        )

        with self.assertRaisesMessage(DatabaseStateError, expected_message):
            database_state.add_index(table_name='my_test_table',
                                     index_name='existing_index',
                                     columns=['col1', 'col2'],
                                     unique=True)

        # It's fine if it has a new name.
        database_state.add_index(table_name='my_test_table',
                                 index_name='new_index',
                                 columns=['col1', 'col2'],
                                 unique=True)
    def test_get_index_with_invalid_name(self):
        """Testing DatabaseState.get_index with invalid name"""
        database_state = DatabaseState(db_name='default', scan=False)
        database_state.add_table('my_test_table')

        self.assertIsNone(database_state.get_index(table_name='my_test_table',
                                                   index_name='my_index'),)
    def test_has_table(self):
        """Testing DatabaseState.has_table"""
        database_state = DatabaseState(db_name='default', scan=False)
        self.assertFalse(database_state.has_table('my_test_table'))

        database_state.add_table('my_test_table')
        self.assertTrue(database_state.has_table('my_test_table'))
Ejemplo n.º 4
0
    def test_has_table(self):
        """Testing DatabaseState.has_table"""
        database_state = DatabaseState(db_name='default', scan=False)
        self.assertFalse(database_state.has_table('my_test_table'))

        database_state.add_table('my_test_table')
        self.assertTrue(database_state.has_table('my_test_table'))
Ejemplo n.º 5
0
    def test_get_index_with_invalid_name(self):
        """Testing DatabaseState.get_index with invalid name"""
        database_state = DatabaseState(db_name='default', scan=False)
        database_state.add_table('my_test_table')

        self.assertIsNone(
            database_state.get_index(table_name='my_test_table',
                                     index_name='my_index'), )
    def test_add_table(self):
        """Testing DatabaseState.add_table"""
        database_state = DatabaseState(db_name='default', scan=False)
        database_state.add_table('my_test_table')

        self.assertEqual(database_state._tables['my_test_table'], {
            'indexes': {},
        })
    def test_add_table(self):
        """Testing DatabaseState.add_table"""
        database_state = DatabaseState(db_name='default', scan=False)
        database_state.add_table('my_test_table')

        self.assertEqual(database_state._tables['my_test_table'], {
            'indexes': {},
        })
Ejemplo n.º 8
0
    def test_find_index_with_not_found(self):
        """Testing DatabaseState.find_index with index no found"""
        database_state = DatabaseState(db_name='default', scan=False)
        database_state.add_table('my_test_table')

        index = database_state.find_index(table_name='my_test_table',
                                          columns=['col1', 'col2'],
                                          unique=True)
        self.assertIsNone(index)
    def test_find_index_with_not_found(self):
        """Testing DatabaseState.find_index with index no found"""
        database_state = DatabaseState(db_name='default', scan=False)
        database_state.add_table('my_test_table')

        index = database_state.find_index(table_name='my_test_table',
                                          columns=['col1', 'col2'],
                                          unique=True)
        self.assertIsNone(index)
    def clear_indexes(self):
        """Testing DatabaseState.clear_indexes"""
        database_state = DatabaseState(db_name='default', scan=False)
        database_state.add_table('my_test_table')
        database_state.add_index(table_name='my_test_table',
                                 index_name='my_index',
                                 columns=['col1', 'col2'],
                                 unique=True)

        self.assertEqual(database_state._tables['my_test_table']['indexes'],
                         {})
    def clear_indexes(self):
        """Testing DatabaseState.clear_indexes"""
        database_state = DatabaseState(db_name='default', scan=False)
        database_state.add_table('my_test_table')
        database_state.add_index(table_name='my_test_table',
                                 index_name='my_index',
                                 columns=['col1', 'col2'],
                                 unique=True)

        self.assertEqual(database_state._tables['my_test_table']['indexes'],
                         {})
Ejemplo n.º 12
0
    def test_find_index(self):
        """Testing DatabaseState.find_index"""
        database_state = DatabaseState(db_name='default', scan=False)
        database_state.add_table('my_test_table')
        database_state.add_index(table_name='my_test_table',
                                 index_name='my_index',
                                 columns=['col1', 'col2'])

        index = database_state.find_index(table_name='my_test_table',
                                          columns=['col1', 'col2'])
        self.assertEqual(index,
                         IndexState(name='my_index', columns=['col1', 'col2']))
Ejemplo n.º 13
0
    def test_remove_index_with_invalid_index_name(self):
        """Testing DatabaseState.remove_index with invalid index name"""
        database_state = DatabaseState(db_name='default', scan=False)
        database_state.add_table('my_test_table')

        expected_message = (
            'Unable to remove index "my_index" from table "my_test_table". '
            'The index could not be found.')

        with self.assertRaisesMessage(DatabaseStateError, expected_message):
            database_state.remove_index(table_name='my_test_table',
                                        index_name='my_index',
                                        unique=True)
    def test_get_index(self):
        """Testing DatabaseState.get_index"""
        database_state = DatabaseState(db_name='default', scan=False)
        database_state.add_table('my_test_table')
        database_state.add_index(table_name='my_test_table',
                                 index_name='my_index',
                                 columns=['col1', 'col2'],
                                 unique=True)

        self.assertEqual(
            database_state.get_index(table_name='my_test_table',
                                     index_name='my_index'),
            IndexState(name='my_index', columns=['col1', 'col2'], unique=True))
    def test_remove_index_with_invalid_index_name(self):
        """Testing DatabaseState.remove_index with invalid index name"""
        database_state = DatabaseState(db_name='default', scan=False)
        database_state.add_table('my_test_table')

        expected_message = (
            'Unable to remove index "my_index" from table "my_test_table". '
            'The index could not be found.'
        )

        with self.assertRaisesMessage(DatabaseStateError, expected_message):
            database_state.remove_index(table_name='my_test_table',
                                        index_name='my_index',
                                        unique=True)
    def test_get_index(self):
        """Testing DatabaseState.get_index"""
        database_state = DatabaseState(db_name='default', scan=False)
        database_state.add_table('my_test_table')
        database_state.add_index(table_name='my_test_table',
                                 index_name='my_index',
                                 columns=['col1', 'col2'],
                                 unique=True)

        self.assertEqual(
            database_state.get_index(table_name='my_test_table',
                                     index_name='my_index'),
            IndexState(name='my_index',
                       columns=['col1', 'col2'],
                       unique=True))
Ejemplo n.º 17
0
    def test_find_index_with_unique_true(self):
        """Testing DatabaseState.find_index with unique=True"""
        database_state = DatabaseState(db_name='default', scan=False)
        database_state.add_table('my_test_table')
        database_state.add_index(table_name='my_test_table',
                                 index_name='my_index',
                                 columns=['col1', 'col2'],
                                 unique=True)

        index = database_state.find_index(table_name='my_test_table',
                                          columns=['col1', 'col2'],
                                          unique=True)
        self.assertEqual(
            index,
            IndexState(name='my_index', columns=['col1', 'col2'], unique=True))
Ejemplo n.º 18
0
    def test_remove_index(self):
        """Testing DatabaseState.remove_index"""
        database_state = DatabaseState(db_name='default', scan=False)
        database_state.add_table('my_test_table')
        database_state.add_index(table_name='my_test_table',
                                 index_name='my_index',
                                 columns=['col1', 'col2'])

        database_state.remove_index(table_name='my_test_table',
                                    index_name='my_index')

        self.assertEqual(database_state._tables['my_test_table'], {
            'indexes': {},
            'unique_indexes': {},
        })
    def test_remove_index_with_invalid_index_type(self):
        """Testing DatabaseState.remove_index with invalid index type"""
        database_state = DatabaseState(db_name='default', scan=False)
        database_state.add_table('my_test_table')
        database_state.add_index(table_name='my_test_table',
                                 index_name='my_index',
                                 columns=['col1', 'col2'],
                                 unique=True)

        expected_message = (
            'Unable to remove index "my_index" from table "my_test_table". '
            'The specified index type (unique=False) does not match the '
            'existing type (unique=True).')

        with self.assertRaisesMessage(DatabaseStateError, expected_message):
            database_state.remove_index(table_name='my_test_table',
                                        index_name='my_index',
                                        unique=False)
Ejemplo n.º 20
0
    def test_add_index_with_unique_true(self):
        """Testing DatabaseState.add_index with unique=True"""
        database_state = DatabaseState(db_name='default', scan=False)
        database_state.add_table('my_test_table')
        database_state.add_index(table_name='my_test_table',
                                 index_name='my_index',
                                 columns=['col1', 'col2'],
                                 unique=True)

        self.assertEqual(
            database_state._tables['my_test_table'], {
                'indexes': {},
                'unique_indexes': {
                    'my_index':
                    IndexState(name='my_index',
                               columns=['col1', 'col2'],
                               unique=True),
                },
            })
    def test_remove_index_with_invalid_index_type(self):
        """Testing DatabaseState.remove_index with invalid index type"""
        database_state = DatabaseState(db_name='default', scan=False)
        database_state.add_table('my_test_table')
        database_state.add_index(table_name='my_test_table',
                                 index_name='my_index',
                                 columns=['col1', 'col2'],
                                 unique=True)

        expected_message = (
            'Unable to remove index "my_index" from table "my_test_table". '
            'The specified index type (unique=False) does not match the '
            'existing type (unique=True).'
        )

        with self.assertRaisesMessage(DatabaseStateError, expected_message):
            database_state.remove_index(table_name='my_test_table',
                                        index_name='my_index',
                                        unique=False)
    def test_iter_indexes(self):
        """Testing DatabaseState.iter_indexes"""
        database_state = DatabaseState(db_name='default', scan=False)
        database_state.add_table('my_test_table')
        database_state.add_index(table_name='my_test_table',
                                 index_name='my_index1',
                                 columns=['col1', 'col2'],
                                 unique=True)
        database_state.add_index(table_name='my_test_table',
                                 index_name='my_index2',
                                 columns=['col3'])

        indexes = list(database_state.iter_indexes('my_test_table'))

        self.assertEqual(indexes, [
            IndexState(name='my_index1', columns=['col1', 'col2'],
                       unique=True),
            IndexState(name='my_index2', columns=['col3'], unique=False),
        ])
    def test_iter_indexes(self):
        """Testing DatabaseState.iter_indexes"""
        database_state = DatabaseState(db_name='default', scan=False)
        database_state.add_table('my_test_table')
        database_state.add_index(table_name='my_test_table',
                                 index_name='my_index1',
                                 columns=['col1', 'col2'],
                                 unique=True)
        database_state.add_index(table_name='my_test_table',
                                 index_name='my_index2',
                                 columns=['col3'])

        indexes = list(database_state.iter_indexes('my_test_table'))

        self.assertEqual(
            indexes,
            [
                IndexState(name='my_index1',
                           columns=['col1', 'col2'],
                           unique=True),
                IndexState(name='my_index2',
                           columns=['col3'],
                           unique=False),
            ])
Ejemplo n.º 24
0
    def test_add_index_with_existing_index(self):
        """Testing DatabaseState.add_index with existing index"""
        database_state = DatabaseState(db_name='default', scan=False)
        database_state.add_table('my_test_table')
        database_state.add_index(table_name='my_test_table',
                                 index_name='existing_index',
                                 columns=['col1', 'col2'],
                                 unique=True)

        expected_message = (
            'Unable to add index "existing_index" to table "my_test_table". '
            'This index already exists.')

        with self.assertRaisesMessage(DatabaseStateError, expected_message):
            database_state.add_index(table_name='my_test_table',
                                     index_name='existing_index',
                                     columns=['col1', 'col2'],
                                     unique=True)

        # It's fine if it has a new name.
        database_state.add_index(table_name='my_test_table',
                                 index_name='new_index',
                                 columns=['col1', 'col2'],
                                 unique=True)