Пример #1
0
def test_get_update_table_unsupported_column_type(
        update_table_strategy_unknown):
    """
    get_update_table should raise UnsupportedColumnStrategyError if presented with an unsupported column type
    """
    with pytest.raises(UnsupportedColumnStrategyError):
        query_factory.get_update_table("seed_table",
                                       update_table_strategy_unknown)
Пример #2
0
def test_get_update_table_literal(literal_strategy):

    result_queries = query_factory.get_update_table("seed_table", UpdateColumnsTableStrategy("anon_table", [
        LiteralUpdateColumnStrategy("literal_column", "RANDOM()")
    ]))

    assert result_queries == [
        "UPDATE anon_table AS \"updatetarget\" SET \"literal_column\" = RANDOM();"
    ]
Пример #3
0
    def anonymize_database(self, database_strategy):
        """
        Anonymize a restored database using the passed database strategy
        :param database_strategy: a strategy.DatabaseStrategy configuration
        :return:
        """
        qualifier_map = database_strategy.fake_update_qualifier_map

        if len(qualifier_map) > 0:
            self.logger.info("creating seed table with %d columns",
                             len(qualifier_map))
            create_seed_table_sql = query_factory.get_create_seed_table(
                SEED_TABLE_NAME, qualifier_map)
            self.__runner.db_execute(create_seed_table_sql)

            self.logger.info("Inserting seed data")
            self.__seed(qualifier_map)

        self.__run_scripts(database_strategy.before_scripts, "before")

        table_strategies = database_strategy.table_strategies
        self.logger.info("Anonymizing %d tables", len(table_strategies))

        with tqdm(desc="Anonymizing database",
                  total=len(table_strategies)) as progressbar:
            for table_strategy in table_strategies:
                if table_strategy.strategy_type == TableStrategyTypes.TRUNCATE:
                    progressbar.set_description("Truncating {}".format(
                        table_strategy.qualified_name))
                    self.__runner.db_execute(
                        query_factory.get_truncate_table(table_strategy))

                elif table_strategy.strategy_type == TableStrategyTypes.DELETE:
                    progressbar.set_description("Deleting {}".format(
                        table_strategy.qualified_name))
                    self.__runner.db_execute(
                        query_factory.get_delete_table(table_strategy))

                elif table_strategy.strategy_type == TableStrategyTypes.UPDATE_COLUMNS:
                    progressbar.set_description("Anonymizing {}".format(
                        table_strategy.qualified_name))
                    statements = query_factory.get_update_table(
                        SEED_TABLE_NAME, table_strategy)
                    self.__runner.db_execute(statements)

                else:
                    raise UnsupportedTableStrategyError(table_strategy)

                progressbar.update()

        self.__run_scripts(database_strategy.after_scripts, "after")

        self.logger.info("dropping seed table")
        self.__runner.db_execute(
            query_factory.get_drop_seed_table(SEED_TABLE_NAME))
Пример #4
0
def test_get_update_table_literal(literal_strategy):

    result_queries = query_factory.get_update_table(
        "seed_table",
        UpdateColumnsTableStrategy(
            "anon_table", [LiteralUpdateColumnStrategy("literal_column", "RANDOM()")]
        ),
    )

    assert result_queries == [
        'UPDATE "anon_table" AS "updatetarget" SET "literal_column" = RANDOM();'
    ]
Пример #5
0
def test_get_update_table_fake_column(column_strategy_list):
    update_table_all = query_factory.get_update_table("seed_table", UpdateColumnsTableStrategy("anon_table", column_strategy_list))

    assert update_table_all == [
            "UPDATE anon_table AS \"updatetarget\" SET "
            "\"test_column1\" = ( SELECT first_name FROM seed_table ORDER BY RANDOM(), MD5(\"updatetarget\"::text) LIMIT 1),"
            "\"test_column2\" = ( SELECT last_name FROM seed_table ORDER BY RANDOM(), MD5(\"updatetarget\"::text) LIMIT 1),"
            "\"test_column7\" = ( SELECT user_id::UUID FROM seed_table ORDER BY RANDOM(), MD5(\"updatetarget\"::text) LIMIT 1),"
            "\"test_column3\" = (''),"
            "\"test_column4\" = ( SELECT md5(random()::text) ORDER BY MD5(\"updatetarget\"::text) LIMIT 1),"
            "\"test_column5\" = ( SELECT CONCAT(md5(random()::text), '@', md5(random()::text), '.com') ORDER BY MD5(\"updatetarget\"::text) LIMIT 1),"
            "\"test_column6\" = RANDOM();"
            ]
Пример #6
0
def test_get_update_table_fake_column(column_strategy_list):
    update_table_all = query_factory.get_update_table(
        "seed_table",
        UpdateColumnsTableStrategy("anon_table", column_strategy_list))

    assert update_table_all == [
        "UPDATE anon_table AS \"updatetarget\" SET "
        "test_column1 = ( SELECT first_name FROM seed_table WHERE \"updatetarget\"=\"updatetarget\" ORDER BY RANDOM() LIMIT 1),"
        "test_column2 = ( SELECT last_name FROM seed_table WHERE \"updatetarget\"=\"updatetarget\" ORDER BY RANDOM() LIMIT 1),"
        "test_column3 = (''),"
        "test_column4 = ( SELECT md5(random()::text) WHERE \"updatetarget\"=\"updatetarget\" ),"
        "test_column5 = ( SELECT CONCAT(md5(random()::text), '@', md5(random()::text), '.com') WHERE \"updatetarget\"=\"updatetarget\" ),"
        "test_column6 = RANDOM();"
    ]
Пример #7
0
def test_get_update_table_fake_column(column_strategy_list):
    update_table_all = query_factory.get_update_table(
        "seed_table", UpdateColumnsTableStrategy("anon_table", column_strategy_list)
    )

    assert update_table_all == [
        'UPDATE "anon_table" AS "updatetarget" SET '
        '"test_column1" = ( SELECT "first_name" FROM "seed_table" ORDER BY RANDOM(), MD5("updatetarget"::text) LIMIT 1),'
        '"test_column2" = ( SELECT "last_name" FROM "seed_table" ORDER BY RANDOM(), MD5("updatetarget"::text) LIMIT 1),'
        '"test_column7" = ( SELECT "user_id"::UUID FROM "seed_table" ORDER BY RANDOM(), MD5("updatetarget"::text) LIMIT 1),'
        "\"test_column3\" = (''),"
        '"test_column4" = ( SELECT md5(random()::text) ORDER BY MD5("updatetarget"::text) LIMIT 1),'
        "\"test_column5\" = ( SELECT CONCAT(md5(random()::text), '@', md5(random()::text), '.com') ORDER BY MD5(\"updatetarget\"::text) LIMIT 1),"
        '"test_column6" = RANDOM();'
    ]