-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
test_recognize_pii_entities.py
745 lines (637 loc) · 30.1 KB
/
test_recognize_pii_entities.py
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import os
import pytest
import platform
import functools
import json
from azure.core.exceptions import HttpResponseError, ClientAuthenticationError
from azure.core.credentials import AzureKeyCredential
from testcase import TextAnalyticsTest, TextAnalyticsPreparer
from testcase import TextAnalyticsClientPreparer as _TextAnalyticsClientPreparer
from devtools_testutils import recorded_by_proxy
from azure.ai.textanalytics import (
TextAnalyticsClient,
TextDocumentInput,
VERSION,
TextAnalyticsApiVersion,
PiiEntityDomain,
PiiEntityCategory
)
# pre-apply the client_cls positional argument so it needn't be explicitly passed below
# the first one
TextAnalyticsClientPreparer = functools.partial(_TextAnalyticsClientPreparer, TextAnalyticsClient)
class TestRecognizePIIEntities(TextAnalyticsTest):
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_no_single_input(self, client):
with pytest.raises(TypeError):
response = client.recognize_pii_entities("hello world")
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_all_successful_passing_dict(self, client):
docs = [{"id": "1", "text": "My SSN is 859-98-0987."},
{"id": "2", "text": "Your ABA number - 111000025 - is the first 9 digits in the lower left hand corner of your personal check."},
{"id": "3", "text": "Is 998.214.865-68 your Brazilian CPF number?"}]
response = client.recognize_pii_entities(docs, show_stats=True)
assert response[0].entities[0].text == "859-98-0987"
# assert response[0].entities[0].category == "USSocialSecurityNumber"
assert response[1].entities[0].text == "111000025"
# assert response[1].entities[0].category == "ABA Routing Number" # Service is currently returning PhoneNumber here
# commenting out brazil cpf, currently service is not returning it
# assert response[2].entities[0].text == "998.214.865-68"
# assert response[2].entities[0].category == "Brazil CPF Number"
for doc in response:
assert doc.id is not None
assert doc.statistics is not None
for entity in doc.entities:
assert entity.text is not None
assert entity.category is not None
assert entity.offset is not None
assert entity.confidence_score is not None
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_all_successful_passing_text_document_input(self, client):
docs = [
TextDocumentInput(id="1", text="My SSN is 859-98-0987."),
TextDocumentInput(id="2", text="Your ABA number - 111000025 - is the first 9 digits in the lower left hand corner of your personal check."),
TextDocumentInput(id="3", text="Is 998.214.865-68 your Brazilian CPF number?")
]
response = client.recognize_pii_entities(docs, show_stats=True)
assert response[0].entities[0].text == "859-98-0987"
# assert response[0].entities[0].category == "USSocialSecurityNumber"
assert response[1].entities[0].text == "111000025"
# assert response[1].entities[0].category == "ABA Routing Number" # Service is currently returning PhoneNumber here
# commenting out brazil cpf, currently service is not returning it
# assert response[2].entities[0].text == "998.214.865-68"
# assert response[2].entities[0].category == "Brazil CPF Number"
for doc in response:
assert doc.id is not None
assert doc.statistics is not None
for entity in doc.entities:
assert entity.text is not None
assert entity.category is not None
assert entity.offset is not None
assert entity.confidence_score is not None
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_passing_only_string(self, client):
docs = [
"My SSN is 859-98-0987.",
"Your ABA number - 111000025 - is the first 9 digits in the lower left hand corner of your personal check.",
"Is 998.214.865-68 your Brazilian CPF number?",
""
]
response = client.recognize_pii_entities(docs, show_stats=True)
assert response[0].entities[0].text == "859-98-0987"
# assert response[0].entities[0].category == "USSocialSecurityNumber"
assert response[1].entities[0].text == "111000025"
# assert response[1].entities[0].category == "ABA Routing Number" # Service is currently returning PhoneNumber here
# commenting out brazil cpf, currently service is not returning it
# assert response[2].entities[0].text == "998.214.865-68"
# assert response[2].entities[0].category == "Brazil CPF Number"
assert response[3].is_error
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_input_with_some_errors(self, client):
docs = [{"id": "1", "language": "notalanguage", "text": "hola"},
{"id": "2", "text": ""},
{"id": "3", "text": "Is 998.214.865-68 your Brazilian CPF number?"}]
response = client.recognize_pii_entities(docs)
assert response[0].is_error
assert response[1].is_error
# assert not response[2].is_error
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_input_with_all_errors(self, client):
docs = [{"id": "1", "text": ""},
{"id": "2", "language": "Spanish", "text": "Hola"},
{"id": "3", "language": "de", "text": ""}]
response = client.recognize_pii_entities(docs)
assert response[0].is_error
assert response[1].is_error
assert response[2].is_error
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_too_many_documents(self, client):
docs = ["One", "Two", "Three", "Four", "Five", "Six"]
with pytest.raises(HttpResponseError) as excinfo:
client.recognize_pii_entities(docs)
assert excinfo.value.status_code == 400
assert excinfo.value.error.code == "InvalidDocumentBatch"
assert "Batch request contains too many records" in str(excinfo.value)
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_output_same_order_as_input(self, client):
docs = [
TextDocumentInput(id="1", text="one"),
TextDocumentInput(id="2", text="two"),
TextDocumentInput(id="3", text="three"),
TextDocumentInput(id="4", text="four"),
TextDocumentInput(id="5", text="five")
]
response = client.recognize_pii_entities(docs)
for idx, doc in enumerate(response):
assert str(idx + 1) == doc.id
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer(client_kwargs={"textanalytics_test_api_key": ""})
@recorded_by_proxy
def test_empty_credential_class(self, client):
with pytest.raises(ClientAuthenticationError):
response = client.recognize_pii_entities(
["This is written in English."]
)
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer(client_kwargs={"textanalytics_test_api_key": "xxxxxxxxxxxx"})
@recorded_by_proxy
def test_bad_credentials(self, client):
with pytest.raises(ClientAuthenticationError):
response = client.recognize_pii_entities(
["This is written in English."]
)
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_bad_document_input(self, client):
docs = "This is the wrong type"
with pytest.raises(TypeError):
response = client.recognize_pii_entities(docs)
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_mixing_inputs(self, client):
docs = [
{"id": "1", "text": "Microsoft was founded by Bill Gates and Paul Allen."},
TextDocumentInput(id="2", text="I did not like the hotel we stayed at. It was too expensive."),
"You cannot mix string input with the above inputs"
]
with pytest.raises(TypeError):
response = client.recognize_pii_entities(docs)
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_out_of_order_ids(self, client):
docs = [{"id": "56", "text": ":)"},
{"id": "0", "text": ":("},
{"id": "22", "text": ""},
{"id": "19", "text": ":P"},
{"id": "1", "text": ":D"}]
response = client.recognize_pii_entities(docs)
in_order = ["56", "0", "22", "19", "1"]
for idx, resp in enumerate(response):
assert resp.id == in_order[idx]
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_show_stats_and_model_version(self, client):
def callback(response):
assert response is not None
assert response.model_version
assert response.raw_response is not None
assert response.statistics.document_count == 5
assert response.statistics.transaction_count == 4
assert response.statistics.valid_document_count == 4
assert response.statistics.erroneous_document_count == 1
docs = [{"id": "56", "text": ":)"},
{"id": "0", "text": ":("},
{"id": "22", "text": ""},
{"id": "19", "text": ":P"},
{"id": "1", "text": ":D"}]
response = client.recognize_pii_entities(
docs,
show_stats=True,
model_version="latest",
raw_response_hook=callback
)
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_batch_size_over_limit(self, client):
docs = ["hello world"] * 1050
with pytest.raises(HttpResponseError):
response = client.recognize_pii_entities(docs)
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_whole_batch_language_hint(self, client):
def callback(resp):
language_str = "\"language\": \"fr\""
language = resp.http_request.body.count(language_str)
assert language == 3
docs = [
"This was the best day of my life.",
"I did not like the hotel we stayed at. It was too expensive.",
"The restaurant was not as good as I hoped."
]
response = client.recognize_pii_entities(docs, language="fr", raw_response_hook=callback)
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_whole_batch_dont_use_language_hint(self, client):
def callback(resp):
language_str = "\"language\": \"\""
language = resp.http_request.body.count(language_str)
assert language == 3
docs = [
"This was the best day of my life.",
"I did not like the hotel we stayed at. It was too expensive.",
"The restaurant was not as good as I hoped."
]
response = client.recognize_pii_entities(docs, language="", raw_response_hook=callback)
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_per_item_dont_use_language_hint(self, client):
def callback(resp):
language_str = "\"language\": \"\""
language = resp.http_request.body.count(language_str)
assert language == 2
language_str = "\"language\": \"en\""
language = resp.http_request.body.count(language_str)
assert language == 1
docs = [{"id": "1", "language": "", "text": "I will go to the park."},
{"id": "2", "language": "", "text": "I did not like the hotel we stayed at."},
{"id": "3", "text": "The restaurant had really good food."}]
response = client.recognize_pii_entities(docs, raw_response_hook=callback)
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_whole_batch_language_hint_and_obj_input(self, client):
def callback(resp):
language_str = "\"language\": \"de\""
language = resp.http_request.body.count(language_str)
assert language == 3
docs = [
TextDocumentInput(id="1", text="I should take my cat to the veterinarian."),
TextDocumentInput(id="4", text="Este es un document escrito en Español."),
TextDocumentInput(id="3", text="猫は幸せ"),
]
response = client.recognize_pii_entities(docs, language="de", raw_response_hook=callback)
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_whole_batch_language_hint_and_obj_per_item_hints(self, client):
def callback(resp):
language_str = "\"language\": \"es\""
language = resp.http_request.body.count(language_str)
assert language == 2
language_str = "\"language\": \"en\""
language = resp.http_request.body.count(language_str)
assert language == 1
docs = [
TextDocumentInput(id="1", text="I should take my cat to the veterinarian.", language="es"),
TextDocumentInput(id="2", text="Este es un document escrito en Español.", language="es"),
TextDocumentInput(id="3", text="猫は幸せ"),
]
response = client.recognize_pii_entities(docs, language="en", raw_response_hook=callback)
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_whole_batch_language_hint_and_dict_per_item_hints(self, client):
def callback(resp):
language_str = "\"language\": \"es\""
language = resp.http_request.body.count(language_str)
assert language == 2
language_str = "\"language\": \"en\""
language = resp.http_request.body.count(language_str)
assert language == 1
docs = [{"id": "1", "language": "es", "text": "I will go to the park."},
{"id": "2", "language": "es", "text": "I did not like the hotel we stayed at."},
{"id": "3", "text": "The restaurant had really good food."}]
response = client.recognize_pii_entities(docs, language="en", raw_response_hook=callback)
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer(client_kwargs={"default_language": "es"})
@recorded_by_proxy
def test_client_passed_default_language_hint(self, client):
def callback(resp):
language_str = "\"language\": \"es\""
language = resp.http_request.body.count(language_str)
assert language == 3
def callback_2(resp):
language_str = "\"language\": \"en\""
language = resp.http_request.body.count(language_str)
assert language == 3
docs = [{"id": "1", "text": "I will go to the park."},
{"id": "2", "text": "I did not like the hotel we stayed at."},
{"id": "3", "text": "The restaurant had really good food."}]
response = client.recognize_pii_entities(docs, raw_response_hook=callback)
response = client.recognize_pii_entities(docs, language="en", raw_response_hook=callback_2)
response = client.recognize_pii_entities(docs, raw_response_hook=callback)
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_invalid_language_hint_method(self, client):
response = client.recognize_pii_entities(
["This should fail because we're passing in an invalid language hint"], language="notalanguage"
)
assert response[0].error.code == 'UnsupportedLanguageCode'
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_invalid_language_hint_docs(self, client):
response = client.recognize_pii_entities(
[{"id": "1", "language": "notalanguage", "text": "This should fail because we're passing in an invalid language hint"}]
)
assert response[0].error.code == 'UnsupportedLanguageCode'
@TextAnalyticsPreparer()
@recorded_by_proxy
def test_rotate_subscription_key(self, textanalytics_test_endpoint, textanalytics_test_api_key):
credential = AzureKeyCredential(textanalytics_test_api_key)
client = TextAnalyticsClient(textanalytics_test_endpoint, credential)
docs = [{"id": "1", "text": "I will go to the park."},
{"id": "2", "text": "I did not like the hotel we stayed at."},
{"id": "3", "text": "The restaurant had really good food."}]
response = client.recognize_pii_entities(docs)
assert response is not None
credential.update("xxx") # Make authentication fail
with pytest.raises(ClientAuthenticationError):
response = client.recognize_pii_entities(docs)
credential.update(textanalytics_test_api_key) # Authenticate successfully again
response = client.recognize_pii_entities(docs)
assert response is not None
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_user_agent(self, client):
def callback(resp):
assert "azsdk-python-ai-textanalytics/{} Python/{} ({})".format(
VERSION, platform.python_version(), platform.platform()) in \
resp.http_request.headers["User-Agent"]
docs = [{"id": "1", "text": "I will go to the park."},
{"id": "2", "text": "I did not like the hotel we stayed at."},
{"id": "3", "text": "The restaurant had really good food."}]
response = client.recognize_pii_entities(docs, raw_response_hook=callback)
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_document_attribute_error_no_result_attribute(self, client):
docs = [{"id": "1", "text": ""}]
response = client.recognize_pii_entities(docs)
# Attributes on DocumentError
assert response[0].is_error
assert response[0].id == "1"
assert response[0].error is not None
# Result attribute not on DocumentError, custom error message
try:
entities = response[0].entities
except AttributeError as custom_error:
assert custom_error.args[0] == \
'\'DocumentError\' object has no attribute \'entities\'. ' \
'The service was unable to process this document:\nDocument Id: 1\nError: ' \
'InvalidDocument - Document text is empty.\n'
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_document_attribute_error_nonexistent_attribute(self, client):
docs = [{"id": "1", "text": ""}]
response = client.recognize_pii_entities(docs)
# Attribute not found on DocumentError or result obj, default behavior/message
try:
entities = response[0].attribute_not_on_result_or_error
except AttributeError as default_behavior:
assert default_behavior.args[0] == '\'DocumentError\' object has no attribute \'attribute_not_on_result_or_error\''
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_bad_model_version_error(self, client):
docs = [{"id": "1", "language": "english", "text": "I did not like the hotel we stayed at."}]
try:
result = client.recognize_pii_entities(docs, model_version="bad")
except HttpResponseError as err:
assert err.error.code == "ModelVersionIncorrect"
assert err.error.message is not None
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_document_errors(self, client):
text = ""
for _ in range(5121):
text += "x"
docs = [{"id": "1", "text": ""},
{"id": "2", "language": "english", "text": "I did not like the hotel we stayed at."},
{"id": "3", "text": text}]
doc_errors = client.recognize_pii_entities(docs)
assert doc_errors[0].error.code == "InvalidDocument"
assert doc_errors[0].error.message is not None
assert doc_errors[1].error.code == "UnsupportedLanguageCode"
assert doc_errors[1].error.message is not None
assert doc_errors[2].error.code == "InvalidDocument"
assert doc_errors[2].error.message is not None
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_document_warnings(self, client):
# No warnings actually returned for recognize_pii_entities. Will update when they add
docs = [
{"id": "1", "text": "This won't actually create a warning :'("},
]
result = client.recognize_pii_entities(docs)
for doc in result:
doc_warnings = doc.warnings
assert len(doc_warnings) == 0
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_not_passing_list_for_docs(self, client):
docs = {"id": "1", "text": "hello world"}
with pytest.raises(TypeError) as excinfo:
client.recognize_pii_entities(docs)
assert "Input documents cannot be a dict" in str(excinfo.value)
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_missing_input_records_error(self, client):
docs = []
with pytest.raises(ValueError) as excinfo:
client.recognize_pii_entities(docs)
assert "Input documents can not be empty or None" in str(excinfo.value)
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_passing_none_docs(self, client):
with pytest.raises(ValueError) as excinfo:
client.recognize_pii_entities(None)
assert "Input documents can not be empty or None" in str(excinfo.value)
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_duplicate_ids_error(self, client):
# Duplicate Ids
docs = [{"id": "1", "text": "hello world"},
{"id": "1", "text": "I did not like the hotel we stayed at."}]
try:
result = client.recognize_pii_entities(docs)
except HttpResponseError as err:
assert err.error.code == "InvalidDocument"
assert err.error.message is not None
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_batch_size_over_limit_error(self, client):
# Batch size over limit
docs = ["hello world"] * 1001
try:
response = client.recognize_pii_entities(docs)
except HttpResponseError as err:
assert err.error.code == "InvalidDocumentBatch"
assert err.error.message is not None
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_pass_cls(self, client):
def callback(pipeline_response, deserialized, _):
return "cls result"
res = client.recognize_pii_entities(
documents=["Test passing cls to endpoint"],
cls=callback
)
assert res == "cls result"
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_language_kwarg_english(self, client):
def callback(response):
language_str = "\"language\": \"en\""
assert response.http_request.body.count(language_str) == 1
assert response.model_version is not None
assert response.statistics is not None
res = client.recognize_pii_entities(
documents=["Bill Gates is the CEO of Microsoft."],
model_version="latest",
show_stats=True,
language="en",
raw_response_hook=callback
)
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_redacted_text(self, client):
result = client.recognize_pii_entities(["My SSN is 859-98-0987."])
assert "My SSN is ***********." == result[0].redacted_text
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_phi_domain_filter(self, client):
# without the domain filter, this should return two entities: Microsoft as an org,
# and the phone number. With the domain filter, it should only return one.
result = client.recognize_pii_entities(
["I work at Microsoft and my phone number is 333-333-3333"],
domain_filter=PiiEntityDomain.PROTECTED_HEALTH_INFORMATION
)
assert len(result[0].entities) == 2
microsoft = list(filter(lambda x: x.text == "Microsoft", result[0].entities))[0]
phone = list(filter(lambda x: x.text == "333-333-3333", result[0].entities))[0]
assert phone.category == "PhoneNumber"
assert microsoft.category == "Organization"
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_categories_filter(self, client):
result = client.recognize_pii_entities(
["My name is Inigo Montoya, my SSN in 243-56-0987 and my phone number is 333-3333."],
)
# assert len(result[0].entities) == 3 FIXME service returning entity for "333-3333" and "333-3333."
result = client.recognize_pii_entities(
["My name is Inigo Montoya, my SSN in 243-56-0987 and my phone number is 333-3333."],
categories_filter=[PiiEntityCategory.US_SOCIAL_SECURITY_NUMBER]
)
assert len(result[0].entities) == 1
entity = result[0].entities[0]
assert entity.category == PiiEntityCategory.US_SOCIAL_SECURITY_NUMBER.value
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer()
@recorded_by_proxy
def test_categories_filter_with_domain_filter(self, client):
# Currently there seems to be no effective difference with or without the PHI domain filter.
result = client.recognize_pii_entities(
["My name is Inigo Montoya, my SSN in 243-56-0987 and my phone number is 333-3333."],
categories_filter=[PiiEntityCategory.US_SOCIAL_SECURITY_NUMBER],
domain_filter=PiiEntityDomain.PROTECTED_HEALTH_INFORMATION
)
assert len(result[0].entities) == 1
entity = result[0].entities[0]
assert entity.category == PiiEntityCategory.US_SOCIAL_SECURITY_NUMBER.value
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer(client_kwargs={"api_version": TextAnalyticsApiVersion.V3_1})
@recorded_by_proxy
def test_default_string_index_type_is_UnicodeCodePoint(self, client):
def callback(response):
assert response.http_request.query["stringIndexType"] == "UnicodeCodePoint"
res = client.recognize_pii_entities(
documents=["Hello world"],
raw_response_hook=callback
)
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer(client_kwargs={"api_version": TextAnalyticsApiVersion.V2022_05_01})
@recorded_by_proxy
def test_default_string_index_type_UnicodeCodePoint_body_param(self, client):
def callback(response):
assert json.loads(response.http_request.body)['parameters']["stringIndexType"] == "UnicodeCodePoint"
res = client.recognize_pii_entities(
documents=["Hello world"],
raw_response_hook=callback
)
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer(client_kwargs={"api_version": TextAnalyticsApiVersion.V3_1})
@recorded_by_proxy
def test_explicit_set_string_index_type(self, client):
def callback(response):
assert response.http_request.query["stringIndexType"] == "TextElement_v8"
res = client.recognize_pii_entities(
documents=["Hello world"],
string_index_type="TextElement_v8",
raw_response_hook=callback
)
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer(client_kwargs={"api_version": TextAnalyticsApiVersion.V2022_05_01})
@recorded_by_proxy
def test_explicit_set_string_index_type_body_param(self, client):
def callback(response):
assert json.loads(response.http_request.body)['parameters']["stringIndexType"] == "TextElements_v8"
res = client.recognize_pii_entities(
documents=["Hello world"],
string_index_type="TextElement_v8",
raw_response_hook=callback
)
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer(client_kwargs={"api_version": TextAnalyticsApiVersion.V3_1})
@recorded_by_proxy
def test_disable_service_logs(self, client):
def callback(resp):
assert resp.http_request.query['loggingOptOut']
client.recognize_pii_entities(
documents=["Test for logging disable"],
disable_service_logs=True,
raw_response_hook=callback,
)
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer(client_kwargs={"api_version": TextAnalyticsApiVersion.V2022_05_01})
@recorded_by_proxy
def test_disable_service_logs_body_param(self, client):
def callback(resp):
assert json.loads(resp.http_request.body)['parameters']['loggingOptOut']
client.recognize_pii_entities(
documents=["Test for logging disable"],
disable_service_logs=True,
raw_response_hook=callback,
)
@TextAnalyticsPreparer()
@TextAnalyticsClientPreparer(client_kwargs={"api_version": "v3.0"})
def test_pii_entities_multiapi_validate_v3_0(self, **kwargs):
client = kwargs.pop("client")
with pytest.raises(ValueError) as e:
client.recognize_pii_entities(
documents=["Test"]
)
assert str(e.value) == "'TextAnalyticsClient.recognize_pii_entities' is not available in API version v3.0. " \
"Use service API version v3.1 or newer."