-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathtest_rosette_api.py
More file actions
708 lines (533 loc) · 22.3 KB
/
test_rosette_api.py
File metadata and controls
708 lines (533 loc) · 22.3 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
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
# -*- coding: utf-8 -*-
"""
Copyright (c) 2014-2024 Basis Technology Corporation.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# To run tests, run `py.test test_rosette_api.py`
import json
import sys
import platform
import pook
import pytest
from rosette.api import (AddressSimilarityParameters,
API,
DocumentParameters,
NameTranslationParameters,
NameSimilarityParameters,
NameDeduplicationParameters,
RecordSimilarityParameters,
RosetteException)
_ISPY3 = sys.version_info[0] == 3
def get_base_url():
return "https://analytics.babelstreet.com/rest/"
@pytest.fixture
def json_response():
""" fixture to return info body"""
body = json.dumps({'name': 'Babel Street Analytics',
'versionChecked': True})
return body
@pytest.fixture
def api():
""" fixture to return api key"""
tmp_api = API('bogus_key')
return tmp_api
@pytest.fixture
def json_409():
""" fixture to return 409 body"""
body = json.dumps({'code': 'incompatibleClientVersion',
'message': 'the version of client library used'
' is not compatible with this server',
'versionChecked': True})
return body
@pytest.fixture
def doc_params():
""" fixture to return basic DocumentParameters"""
params = DocumentParameters()
params['content'] = 'Sample test string'
return params
@pytest.fixture
def doc_map():
""" fixture for a simple map of doc request """
return {'content': 'Simple test string'}
def test_option_get_set_clear(api):
"""Tests the get/set/clear methods"""
api.set_option('test', 'foo')
assert api.get_option('test') == 'foo'
api.clear_options()
assert api.get_option('test') is None
def test_option_clear_single_option(api):
"""Test the clear single option"""
api.set_option('test', 'foo')
assert api.get_option('test') == 'foo'
api.set_option('test', None)
assert api.get_option('test') is None
def test_url_parameter_getsetclear(api):
"""Tests get/set/clear url parameter"""
api.set_url_parameter('test', 'foo')
assert api.get_url_parameter('test') == 'foo'
api.clear_url_parameters()
assert api.get_url_parameter('test') is None
def test_url_parameter_clear_single(api):
"""Test the clearing of a single url parameter"""
api.set_url_parameter('test', 'foo')
assert api.get_url_parameter('test') == 'foo'
api.set_url_parameter('test', None)
assert api.get_url_parameter('test') is None
def test_custom_header_props(api):
"""Test custom header get/set/clear"""
key = 'X-BabelStreetAPI-Test'
value = 'foo'
api.set_custom_headers(key, value)
assert value == api.get_custom_headers()[key]
api.clear_custom_headers()
assert len(api.get_custom_headers()) == 0
def test_invalid_header(api):
"""Test for invalid header"""
key = 'test'
value = 'foo'
api.set_custom_headers(key, value)
with pytest.raises(RosetteException) as e_rosette:
api.info()
assert e_rosette.value.status == 'badHeader'
def test_user_agent(api):
""" Test user agent """
value = ("Babel-Street-Analytics-API-Python/"
+ api.get_binding_version() + "/" + platform.python_version())
assert value == api.get_user_agent_string()
@pook.on
def test_ping_pook(api, json_response):
pook.get(url=get_base_url() + "v1/ping",
response_json=json_response,
reply=200)
result = api.ping()
assert result["name"] == "Babel Street Analytics"
@pook.on
def test_info(api, json_response):
pook.get(url=get_base_url() + "v1/info",
response_json=json_response,
reply=200)
result = api.info()
assert result["name"] == "Babel Street Analytics"
@pook.on
def test_for_409(api, json_409):
pook.get(url=get_base_url() + "v1/info",
response_json=json_409,
reply=409)
with pytest.raises(RosetteException) as e_rosette:
result = api.info()
assert e_rosette.value.status == 'incompatibleClientVersion'
@pook.on
@pytest.mark.parametrize("header_key",
['x-rosetteapi-concurrency',
'x-babelstreetapi-concurrency'])
def test_the_max_pool_size_header(json_response, doc_params, header_key):
pook.post(url=get_base_url() + "v1/language",
response_json=json_response,
reply=200,
response_headers={header_key: 5})
api = API('bogus_key')
assert api.get_pool_size() == 1
result = api.language(doc_params)
assert result["name"] == "Babel Street Analytics"
assert api.get_pool_size() == 5
api.set_pool_size(11)
assert api.get_pool_size() == 11
@pook.on
def test_the_max_pool_size_both(json_response, doc_params):
pook.post(url=get_base_url() + "v1/language",
response_json=json_response,
reply=200,
response_headers={'x-rosetteapi-concurrency': 5,
'x-babelstreetapi-concurrency': 8})
api = API('bogus_key')
assert api.get_pool_size() == 1
result = api.language(doc_params)
assert result["name"] == "Babel Street Analytics"
assert api.get_pool_size() == 8
api.set_pool_size(11)
assert api.get_pool_size() == 11
@pook.on
def test_a_doc_endpoint_fails_on_map(api, json_response, doc_map):
pook.post(url=get_base_url() + "v1/language",
response_json=json_response,
reply=200)
with pytest.raises(RosetteException) as e_rosette:
result = api.language(doc_map)
assert e_rosette.value.status == 'incompatible'
@pook.on
@pytest.mark.parametrize("endpoint",
['categories',
'entities',
'events',
'language',
'morphology/complete',
'morphology/compound-components',
'morphology/han-readings',
'morphology/lemmas',
'morphology/parts-of-speech',
'relationships',
'semantics/similar',
'semantics/vector',
'sentences',
'sentiment',
'syntax/dependencies',
'tokens',
'topics',
'transliteration'])
def test_document_endpoints(api, json_response, doc_params, endpoint):
pook.post(url=get_base_url() + "v1/" + endpoint,
response_json=json_response,
reply=200)
# TODO: Convert to match-case when minimum supported version is 3.10
if endpoint == "categories":
result = api.categories(doc_params)
elif endpoint == "entities":
result = api.entities(doc_params)
elif endpoint == "events":
result = api.events(doc_params)
elif endpoint == "language":
result = api.language(doc_params)
elif endpoint == "morphology/complete":
result = api.morphology(doc_params)
elif endpoint == "morphology/compound-components":
result = api.morphology(doc_params, "compound-components")
elif endpoint == "morphology/han-readings":
result = api.morphology(doc_params, "han-readings")
elif endpoint == "morphology/lemmas":
result = api.morphology(doc_params, "lemmas")
elif endpoint == "morphology/parts-of-speech":
result = api.morphology(doc_params, "parts-of-speech")
elif endpoint == "relationships":
api.set_option('accuracyMode', 'PRECISION')
result = api.relationships(doc_params)
elif endpoint == "semantics/similar":
result = api.similar_terms(doc_params)
elif endpoint == "semantics/vector":
result = api.semantic_vectors(doc_params)
elif endpoint == "sentences":
result = api.sentences(doc_params)
elif endpoint == "sentiment":
result = api.sentiment(doc_params)
elif endpoint == "syntax/dependencies":
result = api.syntax_dependencies(doc_params)
elif endpoint == "tokens":
result = api.tokens(doc_params)
elif endpoint == "topics":
result = api.topics(doc_params)
elif endpoint == "transliteration":
result = api.transliteration(doc_params)
else:
raise Exception("Unknown endpoint.")
assert result["name"] == "Babel Street Analytics"
@pook.on
def test_the_multipart_operation(api, json_response, doc_params, tmpdir):
pook.post(url=get_base_url() + "v1/sentiment",
response_json=json_response,
reply=200)
tmp_file = tmpdir.mkdir("sub").join("testfile.txt")
tmp_file.write(json_response)
doc_params.load_document_file = tmp_file
result = api.sentiment(doc_params)
assert result["name"] == "Babel Street Analytics"
@pook.on
def test_incompatible_type(api, json_response):
pook.post(url=get_base_url() + "v1/sentences",
response_json=json_response,
reply=200)
params = NameTranslationParameters()
params["name"] = "some data to translate"
params["entityType"] = "PERSON"
params["targetLanguage"] = "eng"
params["targetScript"] = "Latn"
# oops, called sentences
with pytest.raises(RosetteException) as e_rosette:
api.sentences(params)
@pook.on
def test_the_name_translation_endpoint(api, json_response):
pook.post(url=get_base_url() + "v1/name-translation",
response_json=json_response,
reply=200)
params = NameTranslationParameters()
params["name"] = "some data to translate"
params["entityType"] = "PERSON"
params["targetLanguage"] = "eng"
params["targetScript"] = "Latn"
result = api.name_translation(params)
assert result["name"] == "Babel Street Analytics"
@pook.on
def test_the_name_requests_with_text(api, json_response):
pook.post(url=get_base_url() + "v1/name-similarity",
response_json=json_response,
reply=200)
with pytest.raises(RosetteException) as e_rosette:
result = api.name_similarity("should fail")
assert e_rosette.value.status == 'incompatible'
with pytest.raises(RosetteException) as e_rosette:
result = api.name_translation("should fail")
assert e_rosette.value.status == 'incompatible'
with pytest.raises(RosetteException) as e_rosette:
result = api.name_deduplication("should fail")
assert e_rosette.value.status == 'incompatible'
with pytest.raises(RosetteException) as e_rosette:
result = api.address_similarity("should fail")
assert e_rosette.value.status == 'incompatible'
with pytest.raises(RosetteException) as e_rosette:
result = api.record_similarity("should fail")
assert e_rosette.value.status == 'incompatible'
@pook.on
def test_the_name_similarity_single_parameters(api, json_response):
pook.post(url=get_base_url() + "v1/name-similarity",
response_json=json_response,
reply=200)
matched_name_data1 = "John Mike Smith"
matched_name_data2 = "John Joe Smith"
params = NameSimilarityParameters()
params["name1"] = {"text": matched_name_data1}
params["name2"] = {"text": matched_name_data2}
params["parameters"] = {"conflictScore": "0.9"}
result = api.name_similarity(params)
assert result["name"] == "Babel Street Analytics"
@pook.on
def test_the_name_similarity_multiple_parameters(api, json_response):
pook.post(url=get_base_url() + "v1/name-similarity",
response_json=json_response,
reply=200)
matched_name_data1 = "John Mike Smith"
matched_name_data2 = "John Joe Smith"
params = NameSimilarityParameters()
params["name1"] = {"text": matched_name_data1}
params["name2"] = {"text": matched_name_data2}
params["parameters"] = {"conflictScore": "0.9", "deletionScore": "0.5"}
result = api.name_similarity(params)
assert result["name"] == "Babel Street Analytics"
@pook.on
def test_the_name_similarity_endpoint(api, json_response):
pook.post(url=get_base_url() + "v1/name-similarity",
response_json=json_response,
reply=200)
matched_name_data1 = "Michael Jackson"
matched_name_data2 = "迈克尔·杰克逊"
params = NameSimilarityParameters()
params["name1"] = {
"text": matched_name_data1,
"language": "eng",
"entityType": "PERSON"}
params["name2"] = {"text": matched_name_data2, "entityType": "PERSON"}
result = api.name_similarity(params)
assert result["name"] == "Babel Street Analytics"
@pook.on
def test_name_deduplication_parameters(api, json_response):
pook.post(url=get_base_url() + "v1/name-deduplication",
response_json=json_response,
reply=200)
params = NameDeduplicationParameters()
with pytest.raises(RosetteException) as e_rosette:
api.name_deduplication(params)
assert e_rosette.value.status == 'missingParameter'
assert (e_rosette.value.message ==
'Required Name De-Duplication parameter is missing: names')
params["names"] = ["John Smith", "Johnathon Smith", "Fred Jones"]
result = api.name_deduplication(params)
assert result["name"] == "Babel Street Analytics"
@pook.on
def test_the_name_deduplication_endpoint(api, json_response):
pook.post(url=get_base_url() + "v1/name-deduplication",
response_json=json_response,
reply=200)
dedup_list = ["John Smith", "Johnathon Smith", "Fred Jones"]
threshold = 0.75
params = NameDeduplicationParameters()
params["names"] = dedup_list
params["threshold"] = threshold
result = api.name_deduplication(params)
assert result["name"] == "Babel Street Analytics"
@pook.on
def test_for_404(api):
pook.get(url=get_base_url() + "v1/info",
response_json={'message': 'not found'},
reply=404)
with pytest.raises(RosetteException) as e_rosette:
api.info()
assert e_rosette.value.status == 404
assert e_rosette.value.message == 'not found'
@pook.on
def test_both_content_and_content_uri(api, json_response, doc_params):
pook.post(url=get_base_url() + "v1/entities",
response_json=json_response,
reply=200)
doc_params['contentUri'] = 'https://example.com'
with pytest.raises(RosetteException) as e_rosette:
api.entities(doc_params)
assert e_rosette.value.status == 'badArgument'
assert (e_rosette.value.message ==
'Cannot supply both Content and ContentUri')
@pook.on
def test_for_no_content_or_content_uri(api, json_response, doc_params):
pook.post(url=get_base_url() + "v1/entities",
response_json=json_response,
reply=200)
doc_params['content'] = None
with pytest.raises(RosetteException) as e_rosette:
api.entities(doc_params)
assert e_rosette.value.status == 'badArgument'
assert (e_rosette.value.message ==
'Must supply one of Content or ContentUri')
@pook.on
def test_for_address_similarity_required_parameters(api, json_response):
pook.post(url=get_base_url() + "v1/address-similarity",
response_json=json_response,
reply=200)
params = AddressSimilarityParameters()
with pytest.raises(RosetteException) as e_rosette:
api.address_similarity(params)
assert e_rosette.value.status == 'missingParameter'
assert (e_rosette.value.message ==
'Required Address Similarity parameter is missing: address1')
params["address1"] = {"houseNumber": "1600",
"road": "Pennsylvania Ave NW",
"city": "Washington",
"state": "DC",
"postCode": "20500"}
with pytest.raises(RosetteException) as e_rosette:
api.address_similarity(params)
assert e_rosette.value.status == 'missingParameter'
assert (e_rosette.value.message ==
'Required Address Similarity parameter is missing: address2')
params["address2"] =\
{"text": "160 Pennsilvana Avenue, Washington, D.C., 20500"}
result = api.address_similarity(params)
assert result["name"] == "Babel Street Analytics"
@pook.on
def test_for_address_similarity_optional_parameters(api, json_response):
pook.post(url=get_base_url() + "v1/address-similarity",
response_json=json_response,
reply=200)
params = AddressSimilarityParameters()
params["address1"] = {"houseNumber": "1600",
"road": "Pennsylvania Ave NW",
"city": "Washington",
"state": "DC",
"postCode": "20500"}
params["address2"] =\
{"text": "160 Pennsilvana Avenue, Washington, D.C., 20500"}
params["parameters"] = {"houseNumberAddressFieldWeight": "0.9"}
result = api.address_similarity(params)
assert result["name"] == "Babel Street Analytics"
@pook.on
def test_for_name_similarity_required_parameters(api, json_response):
pook.post(url=get_base_url() + "v1/name-similarity",
response_json=json_response,
reply=200)
matched_name_data1 = "Michael Jackson"
matched_name_data2 = "迈克尔·杰克逊"
params = NameSimilarityParameters()
with pytest.raises(RosetteException) as e_rosette:
api.name_similarity(params)
assert e_rosette.value.status == 'missingParameter'
assert (e_rosette.value.message ==
'Required Name Similarity parameter is missing: name1')
params["name1"] = {
"text": matched_name_data1,
"language": "eng",
"entityType": "PERSON"}
with pytest.raises(RosetteException) as e_rosette:
api.name_similarity(params)
assert e_rosette.value.status == 'missingParameter'
assert (e_rosette.value.message ==
'Required Name Similarity parameter is missing: name2')
params["name2"] = {"text": matched_name_data2, "entityType": "PERSON"}
result = api.name_similarity(params)
assert result["name"] == "Babel Street Analytics"
@pook.on
def test_for_name_translation_required_parameters(api, json_response):
pook.post(url=get_base_url() + "v1/name-translation",
response_json=json_response,
reply=200)
params = NameTranslationParameters()
params["entityType"] = "PERSON"
params["targetScript"] = "Latn"
with pytest.raises(RosetteException) as e_rosette:
api.name_translation(params)
assert e_rosette.value.status == 'missingParameter'
assert (e_rosette.value.message ==
'Required Name Translation parameter is missing: name')
params["name"] = "some data to translate"
with pytest.raises(RosetteException) as e_rosette:
api.name_translation(params)
assert e_rosette.value.status == 'missingParameter'
assert (e_rosette.value.message ==
'Required Name Translation parameter is missing: targetLanguage')
params["targetLanguage"] = "eng"
result = api.name_translation(params)
assert result["name"] == "Babel Street Analytics"
@pook.on
def test_the_deprecated_endpoints(api, json_response, doc_params):
# TEXT_EMBEDDING calls SEMANTIC_VECTORS
pook.post(url=get_base_url() + "v1/semantics/vector",
response_json=json_response,
reply=200)
result = api.text_embedding(doc_params)
assert result["name"] == "Babel Street Analytics"
# MATCHED_NAME calls NAME_SIMILARITY
pook.post(url=get_base_url() + "v1/name-similarity",
response_json=json_response,
reply=200)
name_similarity_params = NameSimilarityParameters()
name_similarity_params["name1"] = {
"text": "Michael Jackson",
"language": "eng",
"entityType": "PERSON"}
name_similarity_params["name2"] =\
{"text": "迈克尔·杰克逊", "entityType": "PERSON"}
result = api.matched_name(name_similarity_params)
assert result["name"] == "Babel Street Analytics"
# TRANSLATED_NAME calls NAME_TRANSLATION
pook.post(url=get_base_url() + "v1/name-translation",
response_json=json_response,
reply=200)
name_translation_params = NameTranslationParameters()
name_translation_params["entityType"] = "PERSON"
name_translation_params["targetScript"] = "Latn"
name_translation_params["name"] = "some data to translate"
name_translation_params["targetLanguage"] = "eng"
result = api.translated_name(name_translation_params)
assert result["name"] == "Babel Street Analytics"
@pook.on
def test_the_record_similarity_endpoint(api, json_response):
pook.post(url=get_base_url() + "v1/record-similarity",
response_json=json_response,
reply=200)
params = RecordSimilarityParameters()
params["fields"] = {}
params["properties"] = {}
params["records"] = {}
result = api.record_similarity(params)
assert result["name"] == "Babel Street Analytics"
@pook.on
def test_for_record_similarity_required_parameters(api, json_response):
pook.post(url=get_base_url() + "v1/record-similarity",
response_json=json_response,
reply=200)
params = RecordSimilarityParameters()
with pytest.raises(RosetteException) as e_rosette:
api.record_similarity(params)
assert e_rosette.value.status == 'missingParameter'
assert (e_rosette.value.message ==
'Required Record Similarity parameter is missing: records')
params["records"] = {}
with pytest.raises(RosetteException) as e_rosette:
api.record_similarity(params)
assert e_rosette.value.status == 'missingParameter'
assert (e_rosette.value.message ==
'Required Record Similarity parameter is missing: fields')
params["fields"] = {}
result = api.record_similarity(params)
assert result["name"] == "Babel Street Analytics"