def __parse_table(self, table_config): table_strategy = TableStrategyTypes.from_value( table_config.pop("type")) try: if table_strategy == TableStrategyTypes.TRUNCATE: return TruncateTableStrategy(**table_config) elif table_strategy == TableStrategyTypes.DELETE: return DeleteTableStrategy(**table_config) elif table_strategy == TableStrategyTypes.UPDATE_COLUMNS: # update columns supports dict and list columns, so this has to be normalized again during parsing. normalized_columns = StrategyParser.__normalize_update_columns_list( table_config.pop("columns")) parsed_columns = [ self.__parse_update_column(column) for column in normalized_columns ] return UpdateColumnsTableStrategy( column_strategies=parsed_columns, **table_config) else: raise UnknownTableStrategyError(table_config) except TypeError as error: # TypeError can be thrown when the dict args dont match the constructors for the types. We need to re-throw raise ConfigSyntaxError()
def test_before_script_run(query_factory, execution): manager = Mock() manager.attach_mock(execution, "execution") provider = MySqlProvider("1.2.3.4", "root", "password", "db_name") database_strategy = DatabaseStrategy( table_strategies=[TruncateTableStrategy("table1")], before_scripts=["SELECT `before` FROM `before_table`;"], ) provider.anonymize_database(database_strategy) truncate_call_index = manager.mock_calls.index( call.execution.MySqlCmdRunner().db_execute( query_factory.get_truncate_table.return_value ) ) before_script_call_index = manager.mock_calls.index( call.execution.MySqlCmdRunner().db_execute( "SELECT `before` FROM `before_table`;" ) ) # before script should be before any anonymization happens (truncate) assert before_script_call_index < truncate_call_index
def test_after_script_run(query_factory,execution): manager = Mock() manager.attach_mock(execution, "execution") provider = PostgreSqlProvider("1.2.3.4", "root", "password", "db_name") database_strategy = DatabaseStrategy(table_strategies=[ TruncateTableStrategy("table1") ], after_scripts=["SELECT `after` FROM `after_table`;"] ) provider.anonymize_database(database_strategy) truncate_call_index = manager.mock_calls.index(call.execution.PSqlCmdRunner().db_execute(query_factory.get_truncate_table.return_value)) after_script_call_index = manager.mock_calls.index( call.execution.PSqlCmdRunner().db_execute("SELECT `after` FROM `after_table`;") ) # after script should be called after anonymization assert truncate_call_index < after_script_call_index
def simple_strategy_schema_trunc(): return TruncateTableStrategy("truncate_schema_table", schema="schema")
def simple_strategy_trunc(): return TruncateTableStrategy("truncate_table")