Beispiel #1
0
def test_anonymize_database_unsupported_table_strategy(query_factory, execution):
    with pytest.raises(UnsupportedTableStrategyError) as e_info:
        provider = PostgreSqlProvider("1.2.3.4", "root", "password", "db_name")
        database_strategy = DatabaseStrategy([
                Mock(table_name="table1", strategy_type="DEFINITELY_NOT_A_SUPPORTED_STRATEGY_TYPE"),
        ])
        provider.anonymize_database(database_strategy)
Beispiel #2
0
    def parse_config(self, raw_config):
        """
        parse a configuration dict into a DatabaseStrategy.
        :param raw_config:
        :return:
        """
        # Deepcopy raw_config to avoid normalization mutability issues
        config = StrategyParser.__normalize_config(deepcopy(raw_config))
        table_strategies = []
        for table_config in config["tables"]:
            table_strategies.append(self.__parse_table(table_config))

        before_scripts = None
        after_scripts = None
        try:
            scripts = config["scripts"]

            if "before" in scripts:
                before_scripts = scripts["before"]

            if "after" in scripts:
                after_scripts = scripts["after"]
        except KeyError:
            pass

        return DatabaseStrategy(table_strategies=table_strategies,
                                before_scripts=before_scripts,
                                after_scripts=after_scripts)
Beispiel #3
0
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
Beispiel #4
0
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
Beispiel #5
0
    def parse_config(self, raw_config, locale_override=None):
        """
        parse a configuration dict into a DatabaseStrategy.
        :param raw_config:
        :return:
        """
        config = StrategyParser.__normalize_table_list(deepcopy(raw_config))

        locale = config.get("locale", "en_GB")
        if locale_override:
            locale = locale_override

        providers = config.get("providers", [])
        self.fake_seeder = FakeColumnGenerator(locale=locale,
                                               providers=providers)

        table_strategies = [
            self.__parse_table(table_config)
            for table_config in config["tables"]
        ]

        before_scripts = None
        after_scripts = None
        try:
            scripts = config["scripts"]

            if "before" in scripts:
                before_scripts = scripts["before"]

            if "after" in scripts:
                after_scripts = scripts["after"]
        except KeyError:
            pass

        return DatabaseStrategy(
            table_strategies=table_strategies,
            before_scripts=before_scripts,
            after_scripts=after_scripts,
        )
Beispiel #6
0
def simple_strategy(simple_strategy_trunc, simple_strategy_update,
                    simple_strategy_delete):
    return DatabaseStrategy([
        simple_strategy_trunc, simple_strategy_update, simple_strategy_delete
    ])