Skip to content

Commit c0c1c2d

Browse files
committed
more tests
1 parent 2aef099 commit c0c1c2d

File tree

5 files changed

+57
-30
lines changed

5 files changed

+57
-30
lines changed

tests/ManagedCode.Presidio.Anonymizer.Tests/BatchAnonymizerEngineTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ public sealed class BatchAnonymizerEngineTests
1010
public void AnonymizeDictReplacesValues()
1111
{
1212
var texts = new[] { "John", "Jill", "Jack" };
13-
var recognizerResults = new[] { PersonEntity, PersonEntity, PersonEntity };
13+
14+
_ = new[] { PersonEntity, PersonEntity, PersonEntity };
1415

1516
var engine = new BatchAnonymizerEngine();
1617
var input = new[]

tests/ManagedCode.Presidio.Anonymizer.Tests/DeanonymizeEngineTests.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void EncryptThenDecryptRoundTrips()
8282
var anonymizerEngine = new AnonymizerEngine();
8383
var anonymized = anonymizerEngine.Anonymize(
8484
text,
85-
recognizerResults,
85+
ToCore(recognizerResults),
8686
new Dictionary<string, OperatorConfig>
8787
{
8888
["PERSON"] = new OperatorConfig("encrypt", new Dictionary<string, object?>
@@ -94,7 +94,7 @@ public void EncryptThenDecryptRoundTrips()
9494
var deanonymizeEngine = new DeanonymizeEngine();
9595
var deanonymized = deanonymizeEngine.Deanonymize(
9696
anonymized.Text!,
97-
anonymized.Items,
97+
anonymized.Items.ToList(),
9898
new Dictionary<string, OperatorConfig>
9999
{
100100
["PERSON"] = new OperatorConfig("decrypt", new Dictionary<string, object?>
@@ -139,7 +139,7 @@ public void CustomOperatorRoundTrip()
139139
anonymizer.AddAnonymizer(typeof(ReverserAnonymizeOperator));
140140
var anonymized = anonymizer.Anonymize(
141141
text,
142-
recognizerResults,
142+
ToCore(recognizerResults),
143143
new Dictionary<string, OperatorConfig>
144144
{
145145
["WORD"] = new OperatorConfig("Reverser")
@@ -151,7 +151,7 @@ public void CustomOperatorRoundTrip()
151151
deanonymizer.AddDeanonymizer(typeof(ReverserDeanonymizeOperator));
152152
var restored = deanonymizer.Deanonymize(
153153
anonymized.Text!,
154-
anonymized.Items,
154+
anonymized.Items.ToList(),
155155
new Dictionary<string, OperatorConfig>
156156
{
157157
["WORD"] = new OperatorConfig("Reverser")
@@ -160,6 +160,11 @@ public void CustomOperatorRoundTrip()
160160
Assert.Equal(text, restored.Text);
161161
}
162162

163+
private static IReadOnlyCollection<ManagedCode.Presidio.Core.RecognizerResult> ToCore(IEnumerable<RecognizerResult> results)
164+
{
165+
return results.Select(result => result.ToCore()).ToList();
166+
}
167+
163168
private sealed class CustomDeanonymizer : Operator
164169
{
165170
public override string Operate(string text, IDictionary<string, object?>? parameters = null) => text;

tests/ManagedCode.Presidio.Anonymizer.Tests/KeepOperatorTests.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,30 @@ public sealed class KeepOperatorTests
77
[Fact]
88
public void OperateReturnsOriginalText()
99
{
10-
var op = new KeepOperator();
11-
var output = op.Operate("original", new Dictionary<string, object?>());
10+
var output = new KeepOperator().Operate("original", new Dictionary<string, object?>());
11+
12+
Assert.Equal("original", output);
13+
}
14+
15+
[Fact]
16+
public void DeanonymizeVariantAlsoReturnsOriginalText()
17+
{
18+
var output = new DeanonymizeKeepOperator().Operate("original", new Dictionary<string, object?>());
1219

1320
Assert.Equal("original", output);
1421
}
1522

1623
[Fact]
1724
public void ValidateDoesNotThrow()
1825
{
19-
var op = new KeepOperator();
26+
Assert.Null(Record.Exception(() => new KeepOperator().Validate(new Dictionary<string, object?>())));
27+
Assert.Null(Record.Exception(() => new DeanonymizeKeepOperator().Validate(new Dictionary<string, object?>())));
28+
}
2029

21-
var exception = Record.Exception(() => op.Validate(new Dictionary<string, object?>()));
22-
Assert.Null(exception);
30+
[Fact]
31+
public void OperatorNamesMatchPythonBehaviour()
32+
{
33+
Assert.Equal("keep", new KeepOperator().OperatorName);
34+
Assert.Equal("deanonymize_keep", new DeanonymizeKeepOperator().OperatorName);
2335
}
2436
}

tests/ManagedCode.Presidio.Anonymizer.Tests/RedactOperatorTests.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,24 @@ namespace ManagedCode.Presidio.Anonymizer.Tests;
55
public sealed class RedactOperatorTests
66
{
77
[Fact]
8-
public void OperateReturnsEmptyString()
8+
public void OperateReturnsEmptyStringRegardlessOfParameters()
99
{
10-
var op = new RedactOperator();
11-
var output = op.Operate("secret", new Dictionary<string, object?>());
10+
var output = new RedactOperator().Operate("secret", new Dictionary<string, object?>());
11+
Assert.Equal(string.Empty, output);
1212

13+
output = new RedactOperator().Operate("secret", new Dictionary<string, object?> { ["new_value"] = string.Empty });
1314
Assert.Equal(string.Empty, output);
1415
}
1516

1617
[Fact]
1718
public void ValidateDoesNothing()
1819
{
19-
var op = new RedactOperator();
20-
var exception = Record.Exception(() => op.Validate(new Dictionary<string, object?>()));
20+
Assert.Null(Record.Exception(() => new RedactOperator().Validate(new Dictionary<string, object?>())));
21+
}
2122

22-
Assert.Null(exception);
23+
[Fact]
24+
public void OperatorNameIsRedact()
25+
{
26+
Assert.Equal("redact", new RedactOperator().OperatorName);
2327
}
2428
}

tests/ManagedCode.Presidio.Anonymizer.Tests/ReplaceOperatorTests.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ public sealed class ReplaceOperatorTests
77
[Fact]
88
public void OperateUsesProvidedReplacement()
99
{
10-
var op = new ReplaceOperator();
11-
var output = op.Operate(
10+
var output = new ReplaceOperator().Operate(
1211
"secret",
1312
new Dictionary<string, object?>
1413
{
@@ -19,30 +18,36 @@ public void OperateUsesProvidedReplacement()
1918
Assert.Equal("replacement", output);
2019
}
2120

22-
[Fact]
23-
public void OperateFallsBackToEntityPlaceholder()
21+
[Theory]
22+
[InlineData("", "<>")]
23+
[InlineData("PHONE_NUMBER", "<PHONE_NUMBER>")]
24+
public void OperateFallsBackToEntityPlaceholder(string entityType, string expected)
2425
{
25-
var op = new ReplaceOperator();
26-
var output = op.Operate(
27-
"secret",
28-
new Dictionary<string, object?>
29-
{
30-
["entity_type"] = "PERSON",
31-
});
26+
var parameters = new Dictionary<string, object?>
27+
{
28+
["entity_type"] = entityType,
29+
};
3230

33-
Assert.Equal("<PERSON>", output);
31+
var output = new ReplaceOperator().Operate("secret", parameters);
32+
33+
Assert.Equal(expected, output);
3434
}
3535

3636
[Fact]
3737
public void ValidateRejectsNonStringReplacement()
3838
{
39-
var op = new ReplaceOperator();
4039
var parameters = new Dictionary<string, object?>
4140
{
4241
[ReplaceOperator.NewValueKey] = 123,
4342
};
4443

45-
var exception = Assert.Throws<InvalidParamException>(() => op.Validate(parameters));
44+
var exception = Assert.Throws<InvalidParamException>(() => new ReplaceOperator().Validate(parameters));
4645
Assert.Equal("Invalid parameter value for new_value. Expecting 'string', but got 'number'.", exception.Message);
4746
}
47+
48+
[Fact]
49+
public void OperatorNameIsReplace()
50+
{
51+
Assert.Equal("replace", new ReplaceOperator().OperatorName);
52+
}
4853
}

0 commit comments

Comments
 (0)