def test_set_default_from_dict(): config_dict = { "defaultMechanism": { "mechanism": "suppression", "config": {} }, "mechanismsByTag": {} } validate(instance=config_dict, schema=AnonymizerConfig.schema()) config = AnonymizerConfig(**config_dict) assert type(config.default_mechanism) == SuppressionParameters
def test_pass_custom_length_to_suppression(): config_dict = { "mechanismsByTag": { "tag1": { "mechanism": "suppression", "config": { "customLength": 42 } } } } validate(instance=config_dict, schema=AnonymizerConfig.schema()) config = AnonymizerConfig(**config_dict) assert config.mechanisms_by_tag["tag1"].config.custom_length == 42
def test_detect_mismatched_options(): config_dict = { "mechanismsByTag": { "tag1": { "mechanism": "generalization", "config": { "customLength": 42 } } } } with pytest.raises(SchemaValidationError): validate(instance=config_dict, schema=AnonymizerConfig.schema()) with pytest.raises(PydanticValidationError): a = AnonymizerConfig(**config_dict) print(type(a.mechanisms_by_tag["tag1"]))
def test_json_schema_is_valid(): validate(instance={ "mechanismsByTag": { "tag1": { "mechanism": "suppression", "config": {} } } }, schema=AnonymizerConfig.schema())
def test_creating_config_wo_passing_mechanism_name(): config_dict = { "defaultMechanism": { "config": { "replacement": "<other>" } }, "mechanismsByTag": { "tag1": { "config": { "customLength": 42 } } }, } validate(instance=config_dict, schema=AnonymizerConfig.schema()) config = AnonymizerConfig(**config_dict) assert type(config.mechanisms_by_tag["tag1"]) == SuppressionParameters assert type(config.default_mechanism) == GeneralizationParameters
def test_with_default(piis): config = AnonymizerConfig( default_mechanism=Suppression(), mechanisms_by_tag={ "foo": Suppression(suppression_char="Y", custom_length=3), "bar": Pseudonymization(format_string="Bar {}"), }, ) anonymizer = Anonymizer(config) anonymized_piis = [ AnonymizedPii.from_pii(piis[0], "XXXXX"), AnonymizedPii.from_pii(piis[1], "YYY"), AnonymizedPii.from_pii(piis[2], "Bar 1"), AnonymizedPii.from_pii(piis[3], "Bar 2"), ] assert anonymizer.anonymize_individual(piis[0]) == anonymized_piis[0] assert anonymizer.anonymize(piis[0]) == anonymized_piis[0] assert list(anonymizer.anonymize(piis)) == anonymized_piis
def test_without_default(piis): # + stateful pseudonymization. config = AnonymizerConfig( mechanisms_by_tag={ "foo": SuppressionParameters( config=Suppression(suppression_char="Y", custom_length=3)), "bar": PseudonymizationParameters(config=Pseudonymization( format_string="Bar {}", stateful=True)), }) anonymizer = Anonymizer(config) anonymized_piis = [ AnonymizedPii.from_pii(piis[0]), AnonymizedPii.from_pii(piis[1], "YYY"), AnonymizedPii.from_pii(piis[2], "Bar 1"), AnonymizedPii.from_pii(piis[3], "Bar 1"), ] assert anonymizer.anonymize_individual(piis[0]) == anonymized_piis[0] assert anonymizer.anonymize(piis[0]) == anonymized_piis[0] assert list(anonymizer.anonymize(piis)) == anonymized_piis