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'], {})
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']))
def test_add_index_with_untracked_table(self): """Testing DatabaseState.add_index with untracked table""" database_state = DatabaseState(db_name='default', scan=False) expected_message = ( 'Unable to add index "my_index" to table "my_test_table". The ' 'table is not being tracked in the database state.') with self.assertRaisesMessage(DatabaseStateError, expected_message): database_state.add_index(table_name='my_test_table', index_name='my_index', columns=['col1', 'col2'], 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_add_index_with_untracked_table(self): """Testing DatabaseState.add_index with untracked table""" database_state = DatabaseState(db_name='default', scan=False) expected_message = ( 'Unable to add index "my_index" to table "my_test_table". The ' 'table is not being tracked in the database state.' ) with self.assertRaisesMessage(DatabaseStateError, expected_message): database_state.add_index(table_name='my_test_table', index_name='my_index', columns=['col1', 'col2'], unique=True)
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))
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_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_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 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=False)
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_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_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_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_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_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)