def test_anonymize_database(query_factory, execution, simple_strategy, simple_strategy_update_fake_column, simple_strategy_update, simple_strategy_trunc, simple_strategy_delete): manager = Mock() manager.attach_mock(execution, "execution") provider = PostgreSqlProvider("1.2.3.4", "root", "password", "db_name", seed_rows=200) provider.anonymize_database(simple_strategy) seed_table_qualifier_map = ["_pynonymizer_seed_fake_data", {"user_name": simple_strategy_update_fake_column}] # get_create_seed_table( seed table name, qualifier map) query_factory.get_create_seed_table.assert_called_once_with(*seed_table_qualifier_map) # seed table should be called with the qual map 150 times query_factory.get_insert_seed_row.assert_has_calls( ([call(*seed_table_qualifier_map)] * 200) ) query_factory.get_truncate_table.assert_called_once_with(simple_strategy_trunc) query_factory.get_delete_table.assert_called_once_with(simple_strategy_delete) query_factory.get_update_table.assert_called_once_with("_pynonymizer_seed_fake_data", simple_strategy_update) query_factory.get_drop_seed_table.assert_called_once_with("_pynonymizer_seed_fake_data") # anonymize basic order checks ix_create_seed = manager.mock_calls.index(call.execution.PSqlCmdRunner().db_execute(query_factory.get_create_seed_table())) ix_insert_seed_first = manager.mock_calls.index(call.execution.PSqlCmdRunner().db_execute(query_factory.get_insert_seed_row())) ix_insert_seed_last = list_rindex(manager.mock_calls, call.execution.PSqlCmdRunner().db_execute(query_factory.get_insert_seed_row())) ix_trunc_table = manager.mock_calls.index(call.execution.PSqlCmdRunner().db_execute(query_factory.get_truncate_table())) ix_update_table = manager.mock_calls.index(call.execution.PSqlCmdRunner().db_execute(query_factory.get_update_table())) ix_drop_seed = manager.mock_calls.index(call.execution.PSqlCmdRunner().db_execute(query_factory.get_drop_seed_table())) assert ix_create_seed < ix_insert_seed_first assert ix_insert_seed_last < ix_trunc_table assert ix_insert_seed_last < ix_update_table assert ix_update_table < ix_drop_seed
def test_anonymize(connect, provider, simple_strategy, simple_strategy_fake_generator): provider.anonymize_database(simple_strategy) execute_calls = connect().execute.mock_calls ix_create_seed = execute_calls.index(call('CREATE TABLE [_pynonymizer_seed_fake_data]([user_name] VARCHAR(MAX));')) ix_insert_seed_first = execute_calls.index(call('INSERT INTO [_pynonymizer_seed_fake_data]([user_name]) VALUES ( ?);', ['TEST_VALUE'])) ix_insert_seed_last = list_rindex(execute_calls, call('INSERT INTO [_pynonymizer_seed_fake_data]([user_name]) VALUES ( ?);', ['TEST_VALUE']) ) ix_trunc_table = execute_calls.index(call('TRUNCATE TABLE [truncate_table];')) ix_delete_table = execute_calls.index(call('DELETE FROM [delete_table];')) ix_update_table_1 = execute_calls.index(call("SET ANSI_WARNINGS off; UPDATE [update_table_where_3] SET [column1] = ( SELECT CONCAT(NEWID(), '@', NEWID(), '.com') ),[column2] = ( SELECT NEWID() ) WHERE BANANAS < 5; SET ANSI_WARNINGS on;")) ix_update_table_2 = execute_calls.index(call('SET ANSI_WARNINGS off; UPDATE [update_table_where_3] SET [column3] = ( SELECT TOP 1 [user_name] FROM [_pynonymizer_seed_fake_data] WHERE [update_table_where_3].[column3] LIKE \'%\' OR [update_table_where_3].[column3] IS NULL ORDER BY NEWID()) WHERE BANANAS < 3; SET ANSI_WARNINGS on;')) ix_update_table_3 = execute_calls.index(call("SET ANSI_WARNINGS off; UPDATE [update_table_where_3] SET [column4] = (''); SET ANSI_WARNINGS on;")) ix_drop_seed = execute_calls.index(call('DROP TABLE IF EXISTS [_pynonymizer_seed_fake_data];')) # seed table create needs to happen before inserting data assert ix_create_seed < ix_insert_seed_first # last insert of seed data before update anonymize starts assert ix_insert_seed_last < ix_trunc_table assert ix_insert_seed_last < ix_delete_table assert ix_insert_seed_last < ix_update_table_1 assert ix_insert_seed_last < ix_update_table_2 assert ix_insert_seed_last < ix_update_table_3 # update anonymize should all be before seed table drop assert ix_update_table_1 < ix_drop_seed assert ix_update_table_2 < ix_drop_seed assert ix_update_table_3 < ix_drop_seed