-
Notifications
You must be signed in to change notification settings - Fork 995
Expand file tree
/
Copy pathtest_analyzer_engine_provider.py
More file actions
598 lines (452 loc) · 20.8 KB
/
test_analyzer_engine_provider.py
File metadata and controls
598 lines (452 loc) · 20.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
import re
from pathlib import Path
from typing import List
from presidio_analyzer import AnalyzerEngineProvider, RecognizerResult, PatternRecognizer
from presidio_analyzer.nlp_engine import SpacyNlpEngine, NlpArtifacts
from presidio_analyzer.predefined_recognizers import (
AzureAILanguageRecognizer,
CreditCardRecognizer,
SpacyRecognizer,
StanzaRecognizer,
)
import pytest
def get_full_paths(analyzer_yaml, nlp_engine_yaml=None, recognizer_registry_yaml=None):
this_path = Path(__file__).parent.absolute()
analyzer_yaml_path = Path(this_path, analyzer_yaml) if analyzer_yaml else None
nlp_engine_yaml_path = Path(this_path, nlp_engine_yaml) if nlp_engine_yaml else None
recognizer_registry_yaml_path = (
Path(this_path, recognizer_registry_yaml) if recognizer_registry_yaml else None
)
return analyzer_yaml_path, nlp_engine_yaml_path, recognizer_registry_yaml_path
def test_analyzer_engine_provider_default_configuration(mandatory_recognizers):
provider = AnalyzerEngineProvider()
engine = provider.create_engine()
assert engine.supported_languages == ["en"]
assert (
engine.registry.global_regex_flags == re.DOTALL | re.MULTILINE | re.IGNORECASE
)
assert engine.default_score_threshold == 0
names = [recognizer.name for recognizer in engine.registry.recognizers]
for predefined_recognizer in mandatory_recognizers:
assert predefined_recognizer in names
assert "SpacyRecognizer" in names
assert isinstance(engine.nlp_engine, SpacyNlpEngine)
assert engine.nlp_engine.engine_name == "spacy"
def test_analyzer_engine_provider_configuration_file():
test_yaml, _, _ = get_full_paths("conf/test_analyzer_engine.yaml")
provider = AnalyzerEngineProvider(test_yaml)
engine = provider.create_engine()
assert engine.supported_languages == ["de", "en", "es"]
assert engine.default_score_threshold == 0.7
recognizer_registry = engine.registry
assert (
recognizer_registry.global_regex_flags
== re.DOTALL | re.MULTILINE | re.IGNORECASE
)
assert len(recognizer_registry.recognizers) == 8
assert [
recognizer.supported_language
for recognizer in recognizer_registry.recognizers
if recognizer.name == "ItFiscalCodeRecognizer"
] == ["de", "en", "es"]
assert [
recognizer.supported_language
for recognizer in recognizer_registry.recognizers
if recognizer.name == "CreditCardRecognizer"
] == ["en"]
assert [
recognizer.supported_language
for recognizer in recognizer_registry.recognizers
if recognizer.name == "ZipCodeRecognizer"
] == ["de"]
assert [
recognizer.supported_language
for recognizer in recognizer_registry.recognizers
if recognizer.name == "ExampleCustomRecognizer"
] == ["en", "es"]
assert sorted(
[
recognizer.supported_language
for recognizer in recognizer_registry.recognizers
if recognizer.name == "SpacyRecognizer"
]
) == sorted(["en"])
spanish_recognizer = [
recognizer
for recognizer in recognizer_registry.recognizers
if recognizer.name == "ExampleCustomRecognizer"
and recognizer.supported_language == "es"
][0]
assert spanish_recognizer.context == ["tarjeta", "credito"]
assert isinstance(engine.nlp_engine, SpacyNlpEngine)
assert engine.nlp_engine.engine_name == "spacy"
def test_analyzer_engine_provider_defaults(mandatory_recognizers):
provider = AnalyzerEngineProvider()
engine = provider.create_engine()
assert engine.supported_languages == ["en"]
assert engine.default_score_threshold == 0
recognizer_registry = engine.registry
assert (
recognizer_registry.global_regex_flags
== re.DOTALL | re.MULTILINE | re.IGNORECASE
)
assert recognizer_registry.supported_languages == ["en"]
names = [recognizer.name for recognizer in recognizer_registry.recognizers]
for predefined_recognizer in mandatory_recognizers:
assert predefined_recognizer in names
assert isinstance(engine.nlp_engine, SpacyNlpEngine)
assert engine.nlp_engine.engine_name == "spacy"
def test_analyzer_engine_provider_with_files_per_provider():
analyzer_yaml, nlp_engine_yaml, recognizer_registry_yaml = get_full_paths(
"conf/simple_analyzer_engine.yaml",
"conf/default.yaml",
"conf/test_recognizer_registry.yaml",
)
provider = AnalyzerEngineProvider(
analyzer_engine_conf_file=analyzer_yaml,
nlp_engine_conf_file=nlp_engine_yaml,
recognizer_registry_conf_file=recognizer_registry_yaml,
)
analyzer_engine = provider.create_engine()
# assert analyzer instance is correct
assert analyzer_engine.supported_languages == ["en", "es"]
assert analyzer_engine.default_score_threshold == 0.43
# assert nlp engine is correct
nlp_engine = analyzer_engine.nlp_engine
assert isinstance(nlp_engine, SpacyNlpEngine)
assert nlp_engine.nlp is not None
assert nlp_engine.get_supported_languages() == ["en"]
# assert recognizer registry is correct
recognizer_registry = analyzer_engine.registry
assert len(recognizer_registry.recognizers) == 6
assert recognizer_registry.supported_languages == ["en", "es"]
@pytest.mark.skipif(
pytest.importorskip("azure"), reason="Optional dependency not installed"
) # noqa: E501
def test_analyzer_engine_provider_with_azure_ai_language():
analyzer_yaml, _, _ = get_full_paths(
"conf/test_azure_ai_language_reco.yaml",
)
class MockAzureAiLanguageRecognizer(AzureAILanguageRecognizer):
def analyze(
self,
text: str,
entities: List[str] = None,
nlp_artifacts: NlpArtifacts = None,
) -> List[RecognizerResult]:
return [RecognizerResult(entity_type="PERSON", start=0, end=4, score=0.9)]
provider = AnalyzerEngineProvider(analyzer_engine_conf_file=analyzer_yaml)
analyzer_engine = provider.create_engine()
azure_ai_recognizers = [
rec
for rec in analyzer_engine.registry.recognizers
if rec.name == "Azure AI Language PII"
]
assert len(azure_ai_recognizers) == 1
assert len(analyzer_engine.analyze("This is a test", language="en")) > 0
@pytest.mark.skipif(pytest.importorskip("azure"), reason="Optional dependency not installed") # noqa: E501
def test_analyzer_engine_provider_with_ahds():
analyzer_yaml, _, _ = get_full_paths(
"conf/test_ahds_reco.yaml",
)
from presidio_analyzer.predefined_recognizers import AzureHealthDeidRecognizer
class MockAHDSDeidRecognizer(AzureHealthDeidRecognizer):
def analyze(
self,
text: str,
entities: List[str] = None,
nlp_artifacts: NlpArtifacts = None,
) -> List[RecognizerResult]:
return [RecognizerResult(entity_type="PATIENT", start=0, end=4, score=0.9)]
provider = AnalyzerEngineProvider(analyzer_engine_conf_file=analyzer_yaml)
analyzer_engine = provider.create_engine()
ahds_recognizers = [
rec
for rec in analyzer_engine.registry.recognizers
if rec.name == "Azure Health Data Services de-identification"
]
assert len(ahds_recognizers) == 1
assert len(analyzer_engine.analyze("This is a test", language="en")) > 0
def test_analyzer_engine_provider_no_nlp_recognizer():
analyzer_yaml, _, _ = get_full_paths(
"conf/test_nlp_reco_disabled_conf.yaml",
)
provider = AnalyzerEngineProvider(analyzer_engine_conf_file=analyzer_yaml)
analyzer_engine = provider.create_engine()
assert len(analyzer_engine.get_recognizers()) == 1
recognizer = analyzer_engine.get_recognizers()[0]
assert isinstance(recognizer, CreditCardRecognizer)
assert len(analyzer_engine.analyze("My Credit card number is 4917300800000000", language="en")) > 0
def test_analyzer_engine_provider_no_nlp_recognizer_is_added():
analyzer_yaml, _, _ = get_full_paths(
"conf/test_no_nlp_reco_conf.yaml",
)
provider = AnalyzerEngineProvider(analyzer_engine_conf_file=analyzer_yaml)
analyzer_engine = provider.create_engine()
assert len(analyzer_engine.get_recognizers()) == 2
nlp_recognizer = [
rec
for rec in analyzer_engine.get_recognizers()
if isinstance(rec, SpacyRecognizer)
]
assert len(nlp_recognizer) == 1
def test_analyzer_engine_provider_no_nlp_recognizer_is_added_per_language():
analyzer_yaml, _, _ = get_full_paths(
"conf/test_no_nlp_reco_conf_multilingual.yaml",
)
provider = AnalyzerEngineProvider(analyzer_engine_conf_file=analyzer_yaml)
analyzer_engine = provider.create_engine()
assert len(analyzer_engine.get_recognizers()) == 4 # Two CreditCardRecognizers and two SpacyRecognizers
nlp_recognizers = [
rec
for rec in analyzer_engine.get_recognizers()
if isinstance(rec, SpacyRecognizer)
]
assert len(nlp_recognizers) == 2 # one per language
assert set([rec.supported_language for rec in nlp_recognizers]) == {"en", "es"}
def test_analyzer_engine_provider_mismatch_between_nlp_engine_and_nlp_recognizer():
analyzer_yaml, _, _ = get_full_paths(
"conf/test_nlp_reco_does_not_match_engine.yaml",
)
with pytest.raises(ValueError):
provider = AnalyzerEngineProvider(analyzer_engine_conf_file=analyzer_yaml)
analyzer_engine = provider.create_engine()
def test_analyzer_engine_provider_multiple_nlp_recognizers_raises_exception():
analyzer_yaml, _, _ = get_full_paths(
"conf/test_multiple_nlp_recognizers.yaml",
)
with pytest.raises(
ValueError,
match=f"Multiple NLP recognizers for language en found in the configuration. "
f"Please remove the duplicates."):
provider = AnalyzerEngineProvider(analyzer_engine_conf_file=analyzer_yaml)
analyzer_engine = provider.create_engine()
def test_analyzer_engine_provider_no_nlp_engine_or_provider_results_in_default_nlp_recognizer():
analyzer_yaml, _, _ = get_full_paths(
"conf/test_no_nlp_engine.yaml",
)
provider = AnalyzerEngineProvider(analyzer_engine_conf_file=analyzer_yaml)
analyzer_engine = provider.create_engine()
assert len(analyzer_engine.get_recognizers()) == 2 # SpacyRecognizer, CreditCardRecognizer
nlp_recognizer = [
rec
for rec in analyzer_engine.get_recognizers()
if isinstance(rec, SpacyRecognizer)
]
assert len(nlp_recognizer) == 1
@pytest.mark.skip_engine("stanza_en")
def test_analyzer_engine_stanza_without_recognizer_creates_recognizer():
analyzer_yaml, _, _ = get_full_paths(
"conf/test_stanza_without_recognizer.yaml",
)
provider = AnalyzerEngineProvider(analyzer_engine_conf_file=analyzer_yaml)
analyzer_engine = provider.create_engine()
assert (
len(analyzer_engine.get_recognizers()) == 3
) # StanzaRecognizer en, StanzaRecognizer es, CreditCardRecognizer
nlp_recognizers = [
rec
for rec in analyzer_engine.get_recognizers()
if isinstance(rec, StanzaRecognizer)
]
assert len(nlp_recognizers) == 2
supported_languages = {
nlp_recognizers[0].supported_language,
nlp_recognizers[1].supported_language,
}
assert supported_languages == {"en", "es"}
def test_analyzer_engine_provider_one_custom_recognizer():
analyzer_yaml, _, _ = get_full_paths(
"conf/custom_recognizer_yaml.yaml",
)
provider = AnalyzerEngineProvider(analyzer_engine_conf_file=analyzer_yaml)
analyzer_engine = provider.create_engine()
assert len(analyzer_engine.get_recognizers()) == 1
assert analyzer_engine.analyze("My zip code is 12345", language="en")[0].score == pytest.approx(0.4)
def test_analyzer_engine_provider_invalid_analyzer_conf_file():
"""Test that invalid analyzer configuration file path raises error."""
with pytest.raises(ValueError):
AnalyzerEngineProvider(analyzer_engine_conf_file="/nonexistent/path/file.yaml")
def test_analyzer_engine_provider_invalid_nlp_conf_file():
"""Test that invalid NLP engine configuration file path raises error."""
with pytest.raises(ValueError):
AnalyzerEngineProvider(nlp_engine_conf_file="/nonexistent/path/file.yaml")
def test_analyzer_engine_provider_invalid_registry_conf_file():
"""Test that invalid recognizer registry configuration file path raises error."""
with pytest.raises(ValueError):
AnalyzerEngineProvider(recognizer_registry_conf_file="/nonexistent/path/file.yaml")
def test_analyzer_engine_provider_get_configuration_with_nonexistent_file():
"""Test get_configuration falls back to default when file doesn't exist."""
provider = AnalyzerEngineProvider()
# Test with nonexistent file - should fall back to default
config = provider.get_configuration("/tmp/nonexistent_config_file_12345.yaml")
# Should return a valid configuration (the default one)
assert config is not None
assert isinstance(config, dict)
def test_analyzer_engine_provider_get_configuration_with_invalid_yaml():
"""Test get_configuration handles invalid YAML gracefully."""
import tempfile
import os
# Create a temporary file with invalid YAML
with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f:
f.write("invalid: yaml: content: [[[")
temp_file = f.name
try:
provider = AnalyzerEngineProvider()
config = provider.get_configuration(temp_file)
# Should fall back to default configuration
assert config is not None
assert isinstance(config, dict)
finally:
os.unlink(temp_file)
def test_analyzer_engine_provider_get_full_conf_path():
"""Test _get_full_conf_path static method."""
from pathlib import Path
path = AnalyzerEngineProvider._get_full_conf_path()
assert isinstance(path, Path)
assert path.name == "default_analyzer.yaml"
assert path.exists()
def test_analyzer_engine_provider_get_full_conf_path_custom_file():
"""Test _get_full_conf_path with custom filename."""
from pathlib import Path
path = AnalyzerEngineProvider._get_full_conf_path("custom_file.yaml")
assert isinstance(path, Path)
assert path.name == "custom_file.yaml"
def test_analyzer_engine_provider_configuration_property():
"""Test that configuration property is set correctly."""
provider = AnalyzerEngineProvider()
assert provider.configuration is not None
assert isinstance(provider.configuration, dict)
def test_analyzer_engine_provider_nlp_engine_conf_file_property():
"""Test that nlp_engine_conf_file property is stored correctly."""
test_yaml, nlp_yaml, _ = get_full_paths(
"conf/simple_analyzer_engine.yaml",
"conf/default.yaml",
)
provider = AnalyzerEngineProvider(
analyzer_engine_conf_file=test_yaml,
nlp_engine_conf_file=nlp_yaml,
)
assert provider.nlp_engine_conf_file == nlp_yaml
def test_analyzer_engine_provider_recognizer_registry_conf_file_property():
"""Test that recognizer_registry_conf_file property is stored correctly."""
test_yaml, _, registry_yaml = get_full_paths(
"conf/simple_analyzer_engine.yaml",
None,
"conf/test_recognizer_registry.yaml",
)
provider = AnalyzerEngineProvider(
analyzer_engine_conf_file=test_yaml,
recognizer_registry_conf_file=registry_yaml,
)
assert provider.recognizer_registry_conf_file == registry_yaml
def test_analyzer_engine_provider_load_nlp_engine_from_conf():
"""Test _load_nlp_engine with nlp_configuration in analyzer config."""
analyzer_yaml, _, _ = get_full_paths("conf/test_analyzer_engine.yaml")
provider = AnalyzerEngineProvider(analyzer_engine_conf_file=analyzer_yaml)
nlp_engine = provider._load_nlp_engine()
assert nlp_engine is not None
assert nlp_engine.engine_name == "spacy"
def test_analyzer_engine_provider_load_nlp_engine_default():
"""Test _load_nlp_engine falls back to default when no config provided."""
provider = AnalyzerEngineProvider()
nlp_engine = provider._load_nlp_engine()
assert nlp_engine is not None
assert isinstance(nlp_engine, SpacyNlpEngine)
def test_analyzer_engine_provider_load_recognizer_registry_from_embedded_config():
"""Test _load_recognizer_registry with embedded recognizer_registry in config."""
analyzer_yaml, _, _ = get_full_paths("conf/test_analyzer_engine.yaml")
provider = AnalyzerEngineProvider(analyzer_engine_conf_file=analyzer_yaml)
nlp_engine = provider._load_nlp_engine()
registry = provider._load_recognizer_registry(
supported_languages=["en"],
nlp_engine=nlp_engine,
)
assert registry is not None
assert len(registry.recognizers) > 0
def test_analyzer_engine_provider_load_recognizer_registry_default():
"""Test _load_recognizer_registry uses default when no config provided."""
provider = AnalyzerEngineProvider()
nlp_engine = provider._load_nlp_engine()
registry = provider._load_recognizer_registry(
supported_languages=["en"],
nlp_engine=nlp_engine,
)
assert registry is not None
assert len(registry.recognizers) > 0
def test_analyzer_engine_provider_create_engine_with_all_params():
"""Test create_engine with all configuration parameters."""
analyzer_yaml, nlp_yaml, registry_yaml = get_full_paths(
"conf/simple_analyzer_engine.yaml",
"conf/default.yaml",
"conf/test_recognizer_registry.yaml",
)
provider = AnalyzerEngineProvider(
analyzer_engine_conf_file=analyzer_yaml,
nlp_engine_conf_file=nlp_yaml,
recognizer_registry_conf_file=registry_yaml,
)
engine = provider.create_engine()
assert engine is not None
assert engine.nlp_engine is not None
assert engine.registry is not None
assert len(engine.supported_languages) > 0
def test_analyzer_engine_provider_inline_sections_take_priority_over_per_section_files():
"""Test that nlp_configuration / recognizer_registry sections embedded in the
analyzer conf file take priority over separately provided per-section files.
This is the key behaviour that lets a single unified ANALYZER_CONF_FILE drive
both NLP and registry configuration without being silently overridden by
Dockerfile-baked-in default values for NLP_CONF_FILE and
RECOGNIZER_REGISTRY_CONF_FILE.
"""
# test_analyzer_engine.yaml contains both nlp_configuration and
# recognizer_registry sections.
analyzer_yaml, nlp_yaml, registry_yaml = get_full_paths(
"conf/test_analyzer_engine.yaml",
"conf/default.yaml",
"conf/test_recognizer_registry.yaml",
)
provider = AnalyzerEngineProvider(
analyzer_engine_conf_file=analyzer_yaml,
nlp_engine_conf_file=nlp_yaml,
recognizer_registry_conf_file=registry_yaml,
)
engine = provider.create_engine()
# The analyzer yaml's supported_languages + recognizer_registry should prevail.
# test_analyzer_engine.yaml lists de, en, es — not the registry-only en.
assert "de" in engine.supported_languages
assert "en" in engine.supported_languages
assert "es" in engine.supported_languages
# The registry from the inline section has more than the 6 recognizers
# in test_recognizer_registry.yaml, confirming the inline section won.
assert len(engine.registry.recognizers) > 6
def test_analyzer_engine_provider_multiple_languages_support():
"""Test analyzer engine with multiple language support."""
analyzer_yaml, _, _ = get_full_paths("conf/test_analyzer_engine.yaml")
provider = AnalyzerEngineProvider(analyzer_engine_conf_file=analyzer_yaml)
engine = provider.create_engine()
assert "en" in engine.supported_languages
assert "de" in engine.supported_languages
assert "es" in engine.supported_languages
def test_analyzer_engine_provider_default_score_threshold():
"""Test that default_score_threshold is properly set."""
analyzer_yaml, _, _ = get_full_paths("conf/test_analyzer_engine.yaml")
provider = AnalyzerEngineProvider(analyzer_engine_conf_file=analyzer_yaml)
engine = provider.create_engine()
assert engine.default_score_threshold == 0.7
def test_analyzer_engine_provider_with_pathlib_path():
"""Test AnalyzerEngineProvider works with pathlib.Path objects."""
from pathlib import Path
analyzer_yaml, _, _ = get_full_paths("conf/simple_analyzer_engine.yaml")
analyzer_path = Path(analyzer_yaml)
provider = AnalyzerEngineProvider(analyzer_engine_conf_file=analyzer_path)
engine = provider.create_engine()
assert engine is not None
def test_analyzer_engine_provider_configuration_logging(caplog):
"""Test that configuration loading logs appropriate messages."""
import logging
with caplog.at_level(logging.INFO):
provider = AnalyzerEngineProvider()
_ = provider.create_engine()
# Check that some logging occurred
assert len(caplog.records) > 0