Skip to content

Commit a7d10f0

Browse files
committed
test(service): Update tests of all services
1 parent 8d06d3c commit a7d10f0

6 files changed

+23
-90
lines changed
283 KB
Loading

resources/my-giraffe.jpeg

32.7 KB
Loading

test/unit/test_assistant_v1.py

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def test_create_counterexample():
3737
content_type='application/json')
3838
authenticator = BasicAuthenticator('username', 'password')
3939
service = ibm_watson.AssistantV1(version='2017-02-03', authenticator=authenticator)
40+
service.set_service_url(base_url)
4041
counterexample = service.create_counterexample(
4142
workspace_id='boguswid', text='I want financial advice today.').get_result()
4243
assert len(responses.calls) == 1

test/unit/test_discovery_v1.py

+8-68
Original file line numberDiff line numberDiff line change
@@ -372,55 +372,6 @@ def test_query_2(cls):
372372
assert called_url.path == test_url.path
373373
assert len(responses.calls) == 1
374374

375-
@classmethod
376-
@responses.activate
377-
def test_query_relations(cls):
378-
discovery_url = urljoin(
379-
base_discovery_url,
380-
'environments/envid/collections/collid/query_relations')
381-
382-
responses.add(
383-
responses.POST,
384-
discovery_url,
385-
body="{\"body\": \"hello\"}",
386-
status=200,
387-
content_type='application/json')
388-
389-
authenticator = BasicAuthenticator('username', 'password')
390-
discovery = ibm_watson.DiscoveryV1('2018-08-13', authenticator=authenticator)
391-
392-
discovery.query_relations('envid', 'collid', count=10)
393-
called_url = urlparse(responses.calls[0].request.url)
394-
test_url = urlparse(discovery_url)
395-
assert called_url.netloc == test_url.netloc
396-
assert called_url.path == test_url.path
397-
assert len(responses.calls) == 1
398-
399-
400-
@classmethod
401-
@responses.activate
402-
def test_query_entities(cls):
403-
discovery_url = urljoin(
404-
base_discovery_url,
405-
'environments/envid/collections/collid/query_entities')
406-
407-
responses.add(
408-
responses.POST,
409-
discovery_url,
410-
body="{\"body\": \"hello\"}",
411-
status=200,
412-
content_type='application/json')
413-
414-
authenticator = BasicAuthenticator('username', 'password')
415-
discovery = ibm_watson.DiscoveryV1('2018-08-13', authenticator=authenticator)
416-
417-
discovery.query_entities('envid', 'collid', count={'count': 10})
418-
called_url = urlparse(responses.calls[0].request.url)
419-
test_url = urlparse(discovery_url)
420-
assert called_url.netloc == test_url.netloc
421-
assert called_url.path == test_url.path
422-
assert len(responses.calls) == 1
423-
424375
@classmethod
425376
@responses.activate
426377
def test_query_notices(cls):
@@ -523,17 +474,6 @@ def test_document(cls):
523474

524475
authenticator = BasicAuthenticator('username', 'password')
525476
discovery = ibm_watson.DiscoveryV1('2018-08-13', authenticator=authenticator)
526-
html_path = os.path.join(os.getcwd(), 'resources', 'simple.html')
527-
with open(html_path) as fileinfo:
528-
conf_id = discovery.test_configuration_in_environment(environment_id='envid',
529-
configuration_id='bogus',
530-
file=fileinfo)
531-
assert conf_id is not None
532-
conf_id = discovery.test_configuration_in_environment(environment_id='envid',
533-
file=fileinfo)
534-
assert conf_id is not None
535-
536-
assert len(responses.calls) == 2
537477

538478
add_doc_url = urljoin(base_discovery_url,
539479
'environments/envid/collections/collid/documents')
@@ -580,46 +520,46 @@ def test_document(cls):
580520
file=fileinfo)
581521
assert conf_id is not None
582522

583-
assert len(responses.calls) == 3
523+
assert len(responses.calls) == 1
584524

585525
discovery.get_document_status(environment_id='envid',
586526
collection_id='collid',
587527
document_id='docid')
588528

589-
assert len(responses.calls) == 4
529+
assert len(responses.calls) == 2
590530

591531
discovery.update_document(environment_id='envid',
592532
collection_id='collid',
593533
document_id='docid')
594534

595-
assert len(responses.calls) == 5
535+
assert len(responses.calls) == 3
596536

597537
discovery.update_document(environment_id='envid',
598538
collection_id='collid',
599539
document_id='docid')
600540

601-
assert len(responses.calls) == 6
541+
assert len(responses.calls) == 4
602542

603543
discovery.delete_document(environment_id='envid',
604544
collection_id='collid',
605545
document_id='docid')
606546

607-
assert len(responses.calls) == 7
547+
assert len(responses.calls) == 5
608548

609549
conf_id = discovery.add_document(environment_id='envid',
610550
collection_id='collid',
611551
file=io.StringIO(u'my string of file'),
612552
filename='file.txt')
613553

614-
assert len(responses.calls) == 8
554+
assert len(responses.calls) == 6
615555

616556
conf_id = discovery.add_document(environment_id='envid',
617557
collection_id='collid',
618558
file=io.StringIO(u'<h1>my string of file</h1>'),
619559
filename='file.html',
620560
file_content_type='application/html')
621561

622-
assert len(responses.calls) == 9
562+
assert len(responses.calls) == 7
623563

624564
conf_id = discovery.add_document(environment_id='envid',
625565
collection_id='collid',
@@ -628,7 +568,7 @@ def test_document(cls):
628568
file_content_type='application/html',
629569
metadata=io.StringIO(u'{"stuff": "woot!"}'))
630570

631-
assert len(responses.calls) == 10
571+
assert len(responses.calls) == 8
632572

633573

634574
@classmethod

test/unit/test_natural_language_understanding.py

+6-13
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ def test_version_date(self):
6262
NaturalLanguageUnderstandingV1() # pylint: disable=E1120
6363
authenticator = BasicAuthenticator('username', 'password')
6464
nlu = NaturalLanguageUnderstandingV1(version='2016-01-23',
65-
url='http://bogus.com',
6665
authenticator=authenticator)
6766
assert nlu
6867

@@ -72,38 +71,35 @@ def test_missing_credentials(self):
7271
with pytest.raises(ValueError):
7372
NaturalLanguageUnderstandingV1(version='2016-01-23')
7473
with pytest.raises(ValueError):
75-
NaturalLanguageUnderstandingV1(version='2016-01-23', url='http://bogus.com')
74+
NaturalLanguageUnderstandingV1(version='2016-01-23')
7675

7776
def test_analyze_throws(self):
7877
authenticator = BasicAuthenticator('username', 'password')
7978
nlu = NaturalLanguageUnderstandingV1(version='2016-01-23',
80-
url='http://bogus.com',
8179
authenticator=authenticator)
8280
with pytest.raises(ValueError):
8381
nlu.analyze(None, text="this will not work")
8482

8583
@responses.activate
8684
def test_text_analyze(self):
87-
nlu_url = "http://bogus.com/v1/analyze"
85+
nlu_url = "https://gateway.watsonplatform.net/natural-language-understanding/api/v1/analyze"
8886
responses.add(responses.POST, nlu_url,
8987
body="{\"resulting_key\": true}", status=200,
9088
content_type='application/json')
9189
authenticator = BasicAuthenticator('username', 'password')
9290
nlu = NaturalLanguageUnderstandingV1(version='2016-01-23',
93-
url='http://bogus.com',
9491
authenticator=authenticator)
9592
nlu.analyze(Features(sentiment=SentimentOptions()), text="hello this is a test")
9693
assert len(responses.calls) == 1
9794

9895
@responses.activate
9996
def test_html_analyze(self):
100-
nlu_url = "http://bogus.com/v1/analyze"
97+
nlu_url = "https://gateway.watsonplatform.net/natural-language-understanding/api/v1/analyze"
10198
responses.add(responses.POST, nlu_url,
10299
body="{\"resulting_key\": true}", status=200,
103100
content_type='application/json')
104101
authenticator = BasicAuthenticator('username', 'password')
105102
nlu = NaturalLanguageUnderstandingV1(version='2016-01-23',
106-
url='http://bogus.com',
107103
authenticator=authenticator)
108104
nlu.analyze(Features(sentiment=SentimentOptions(),
109105
emotion=EmotionOptions(document=False)),
@@ -112,13 +108,12 @@ def test_html_analyze(self):
112108

113109
@responses.activate
114110
def test_url_analyze(self):
115-
nlu_url = "http://bogus.com/v1/analyze"
111+
nlu_url = "https://gateway.watsonplatform.net/natural-language-understanding/api/v1/analyze"
116112
responses.add(responses.POST, nlu_url,
117113
body="{\"resulting_key\": true}", status=200,
118114
content_type='application/json')
119115
authenticator = BasicAuthenticator('username', 'password')
120116
nlu = NaturalLanguageUnderstandingV1(version='2016-01-23',
121-
url='http://bogus.com',
122117
authenticator=authenticator)
123118
nlu.analyze(Features(sentiment=SentimentOptions(),
124119
emotion=EmotionOptions(document=False)),
@@ -128,26 +123,24 @@ def test_url_analyze(self):
128123

129124
@responses.activate
130125
def test_list_models(self):
131-
nlu_url = "http://bogus.com/v1/models"
126+
nlu_url = "https://gateway.watsonplatform.net/natural-language-understanding/api/v1/models"
132127
responses.add(responses.GET, nlu_url, status=200,
133128
body="{\"resulting_key\": true}",
134129
content_type='application/json')
135130
authenticator = BasicAuthenticator('username', 'password')
136131
nlu = NaturalLanguageUnderstandingV1(version='2016-01-23',
137-
url='http://bogus.com',
138132
authenticator=authenticator)
139133
nlu.list_models()
140134
assert len(responses.calls) == 1
141135

142136
@responses.activate
143137
def test_delete_model(self):
144138
model_id = "invalid_model_id"
145-
nlu_url = "http://bogus.com/v1/models/" + model_id
139+
nlu_url = "https://gateway.watsonplatform.net/natural-language-understanding/api/v1/models/" + model_id
146140
responses.add(responses.DELETE, nlu_url, status=200,
147141
body="{}", content_type='application/json')
148142
authenticator = BasicAuthenticator('username', 'password')
149143
nlu = NaturalLanguageUnderstandingV1(version='2016-01-23',
150-
url='http://bogus.com',
151144
authenticator=authenticator)
152145
nlu.delete_model(model_id)
153146
assert len(responses.calls) == 1

test/unit/test_personality_insights_v3.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ def test_plain_to_json():
1313
authenticator = BasicAuthenticator('username', 'password')
1414
personality_insights = ibm_watson.PersonalityInsightsV3('2016-10-20', authenticator=authenticator)
1515

16-
with open(os.path.join(os.path.dirname(__file__), '../../resources/personality-v3-expect1.txt')) as expect_file:
16+
with open(os.path.join(os.path.dirname(__file__), '../../resources/personality-v3-expect1.txt'), 'r') as expect_file:
1717
profile_response = expect_file.read()
1818

1919
responses.add(responses.POST, profile_url,
2020
body=profile_response, status=200,
2121
content_type='application/json')
2222

23-
with open(os.path.join(os.path.dirname(__file__), '../../resources/personality-v3.txt')) as personality_text:
23+
with open(os.path.join(os.path.dirname(__file__), '../../resources/personality-v3.txt'), 'rb') as personality_text:
2424
response = personality_insights.profile(
2525
personality_text, 'application/json', content_type='text/plain;charset=utf-8').get_result()
2626

@@ -36,14 +36,14 @@ def test_json_to_json():
3636
authenticator = BasicAuthenticator('username', 'password')
3737
personality_insights = ibm_watson.PersonalityInsightsV3('2016-10-20', authenticator=authenticator)
3838

39-
with open(os.path.join(os.path.dirname(__file__), '../../resources/personality-v3-expect2.txt')) as expect_file:
39+
with open(os.path.join(os.path.dirname(__file__), '../../resources/personality-v3-expect2.txt'), 'r') as expect_file:
4040
profile_response = expect_file.read()
4141

4242
responses.add(responses.POST, profile_url,
4343
body=profile_response, status=200,
4444
content_type='application/json')
4545

46-
with open(os.path.join(os.path.dirname(__file__), '../../resources/personality-v3.json')) as personality_text:
46+
with open(os.path.join(os.path.dirname(__file__), '../../resources/personality-v3.json'), 'rb') as personality_text:
4747
response = personality_insights.profile(
4848
personality_text, accept='application/json',
4949
content_type='application/json',
@@ -64,14 +64,14 @@ def test_json_to_csv():
6464
authenticator = BasicAuthenticator('username', 'password')
6565
personality_insights = ibm_watson.PersonalityInsightsV3('2016-10-20', authenticator=authenticator)
6666

67-
with open(os.path.join(os.path.dirname(__file__), '../../resources/personality-v3-expect3.txt')) as expect_file:
67+
with open(os.path.join(os.path.dirname(__file__), '../../resources/personality-v3-expect3.txt'), 'r') as expect_file:
6868
profile_response = expect_file.read()
6969

7070
responses.add(responses.POST, profile_url,
7171
body=profile_response, status=200,
7272
content_type='text/csv')
7373

74-
with open(os.path.join(os.path.dirname(__file__), '../../resources/personality-v3.json')) as personality_text:
74+
with open(os.path.join(os.path.dirname(__file__), '../../resources/personality-v3.json'), 'rb') as personality_text:
7575
personality_insights.profile(
7676
personality_text,
7777
'text/csv',
@@ -94,15 +94,14 @@ def test_plain_to_json_es():
9494
authenticator = BasicAuthenticator('username', 'password')
9595
personality_insights = ibm_watson.PersonalityInsightsV3('2016-10-20', authenticator=authenticator)
9696

97-
with codecs.open(os.path.join(os.path.dirname(__file__), '../../resources/personality-v3-expect4.txt'), \
98-
encoding='utf-8') as expect_file:
97+
with codecs.open(os.path.join(os.path.dirname(__file__), '../../resources/personality-v3-expect4.txt'), 'r') as expect_file:
9998
profile_response = expect_file.read()
10099

101100
responses.add(responses.POST, profile_url,
102101
body=profile_response, status=200,
103102
content_type='application/json')
104103

105-
with open(os.path.join(os.path.dirname(__file__), '../../resources/personality-v3-es.txt')) as personality_text:
104+
with open(os.path.join(os.path.dirname(__file__), '../../resources/personality-v3-es.txt'), 'rb') as personality_text:
106105
response = personality_insights.profile(
107106
personality_text,
108107
'application/json',

0 commit comments

Comments
 (0)