ibm_watson 2.1.3 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,217 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require("json")
4
- require_relative("./../test_helper.rb")
5
- require("webmock/minitest")
6
-
7
- WebMock.disable_net_connect!(allow_localhost: true)
8
-
9
- # Unit tests for the Tone Analyzer V3 Service
10
- class ToneAnalyzerV3Test < Minitest::Test
11
- def test_tone
12
- tone_response = JSON.parse(File.read(Dir.getwd + "/resources/tone-v3-expect1.json"))
13
- headers = {
14
- "Content-Type" => "application/json"
15
- }
16
- expected_response = IBMWatson::DetailedResponse.new(status: 200, headers: headers, body: tone_response)
17
- tone_text = File.read(Dir.getwd + "/resources/personality.txt")
18
- stub_request(:post, "https://api.us-south.tone-analyzer.watson.cloud.ibm.com/v3/tone?version=2017-09-21")
19
- .with(
20
- body: tone_text,
21
- headers: {
22
- "Accept" => "application/json",
23
- "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
24
- "Content-Type" => "application/json",
25
- "Host" => "api.us-south.tone-analyzer.watson.cloud.ibm.com"
26
- }
27
- ).to_return(status: 200, body: tone_response.to_json, headers: headers)
28
- authenticator = IBMWatson::Authenticators::BasicAuthenticator.new(
29
- username: "username",
30
- password: "password"
31
- )
32
- service = IBMWatson::ToneAnalyzerV3.new(
33
- version: "2017-09-21",
34
- authenticator: authenticator
35
- )
36
- service_response = service.tone(tone_input: tone_text, content_type: "application/json")
37
- assert_equal(expected_response.status, service_response.status)
38
- assert_equal(expected_response.result, service_response.result)
39
- expected_response.headers.each_key do |key|
40
- assert(service_response.headers.key?(key))
41
- assert(expected_response.headers[key] == service_response.headers[key])
42
- end
43
- end
44
-
45
- def test_tone_with_args
46
- tone_response = JSON.parse(File.read(Dir.getwd + "/resources/tone-v3-expect1.json"))
47
- headers = {
48
- "Content-Type" => "application/json"
49
- }
50
- tone_text = File.read(Dir.getwd + "/resources/personality.txt")
51
- expected_response = IBMWatson::DetailedResponse.new(status: 200, headers: headers, body: tone_response)
52
- stub_request(:post, "https://api.us-south.tone-analyzer.watson.cloud.ibm.com/v3/tone?sentences=false&version=2017-09-21")
53
- .with(
54
- body: tone_text,
55
- headers: {
56
- "Accept" => "application/json",
57
- "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
58
- "Content-Type" => "application/json",
59
- "Host" => "api.us-south.tone-analyzer.watson.cloud.ibm.com"
60
- }
61
- ).to_return(status: 200, body: tone_response.to_json, headers: headers)
62
- authenticator = IBMWatson::Authenticators::BasicAuthenticator.new(
63
- username: "username",
64
- password: "password"
65
- )
66
- service = IBMWatson::ToneAnalyzerV3.new(
67
- version: "2017-09-21",
68
- authenticator: authenticator
69
- )
70
- service_response = service.tone(tone_input: tone_text, content_type: "application/json", sentences: false)
71
- assert_equal(expected_response.status, service_response.status)
72
- assert_equal(expected_response.result, service_response.result)
73
- expected_response.headers.each_key do |key|
74
- assert(service_response.headers.key?(key))
75
- assert(expected_response.headers[key] == service_response.headers[key])
76
- end
77
- end
78
-
79
- def test_tone_chat
80
- tone_response = JSON.parse(File.read(Dir.getwd + "/resources/tone-v3-expect2.json"))
81
- headers = {
82
- "Content-Type" => "application/json"
83
- }
84
- expected_response = IBMWatson::DetailedResponse.new(body: tone_response, status: 200, headers: headers)
85
- stub_request(:post, "https://api.us-south.tone-analyzer.watson.cloud.ibm.com/v3/tone_chat?version=2017-09-21")
86
- .with(
87
- body: "{\"utterances\":[{\"text\":\"I am very happy\",\"user\":\"glenn\"}]}",
88
- headers: {
89
- "Accept" => "application/json",
90
- "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
91
- "Content-Type" => "application/json",
92
- "Host" => "api.us-south.tone-analyzer.watson.cloud.ibm.com"
93
- }
94
- ).to_return(status: 200, body: tone_response.to_json, headers: headers)
95
- authenticator = IBMWatson::Authenticators::BasicAuthenticator.new(
96
- username: "username",
97
- password: "password"
98
- )
99
- service = IBMWatson::ToneAnalyzerV3.new(
100
- version: "2017-09-21",
101
- authenticator: authenticator
102
- )
103
- utterances = [
104
- {
105
- "text" => "I am very happy",
106
- "user" => "glenn"
107
- }
108
- ]
109
- service_response = service.tone_chat(utterances: utterances)
110
- assert_equal(expected_response.status, service_response.status)
111
- assert_equal(expected_response.result, service_response.result)
112
- expected_response.headers.each_key do |key|
113
- assert(service_response.headers.key?(key))
114
- assert(expected_response.headers[key] == service_response.headers[key])
115
- end
116
- end
117
-
118
- def test_error
119
- error_code = 400
120
- error_message = "Invalid Json at line 2, column 12"
121
- headers = {
122
- "Content-Type" => "application/json"
123
- }
124
- tone_response = {
125
- "code" => error_code,
126
- "sub_code" => "C00012",
127
- "error" => error_message
128
- }
129
- text = "Team, I know that times are tough!"
130
- stub_request(:post, "https://api.us-south.tone-analyzer.watson.cloud.ibm.com/v3/tone?version=2017-09-21")
131
- .with(
132
- body: text,
133
- headers: {
134
- "Accept" => "application/json",
135
- "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
136
- "Content-Type" => "application/json",
137
- "Host" => "api.us-south.tone-analyzer.watson.cloud.ibm.com"
138
- }
139
- ).to_return(status: 400, body: tone_response.to_json, headers: headers)
140
- authenticator = IBMWatson::Authenticators::BasicAuthenticator.new(
141
- username: "username",
142
- password: "password"
143
- )
144
- service = IBMWatson::ToneAnalyzerV3.new(
145
- version: "2017-09-21",
146
- authenticator: authenticator
147
- )
148
- begin
149
- service.tone(tone_input: text, content_type: "application/json")
150
- rescue IBMWatson::ApiException => e
151
- assert_equal(error_code, e.code)
152
- assert_equal(error_message, e.error)
153
- assert_equal("C00012", e.info["sub_code"])
154
- end
155
- end
156
-
157
- # Test to ensure that custom headers are sent
158
- def test_tone_with_custom_headers
159
- tone_response = JSON.parse(File.read(Dir.getwd + "/resources/tone-v3-expect1.json"))
160
- headers = {
161
- "Content-Type" => "application/json"
162
- }
163
- tone_text = File.read(Dir.getwd + "/resources/personality.txt")
164
- stub_request(:post, "https://api.us-south.tone-analyzer.watson.cloud.ibm.com/v3/tone?version=2017-09-21")
165
- .with(
166
- body: tone_text,
167
- headers: {
168
- "Accept" => "application/json",
169
- "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
170
- "Content-Type" => "Custom/Type",
171
- "Host" => "api.us-south.tone-analyzer.watson.cloud.ibm.com",
172
- "Custom-Header-One" => "yes"
173
- }
174
- ).to_return(status: 200, body: tone_response.to_json, headers: headers)
175
- authenticator = IBMWatson::Authenticators::BasicAuthenticator.new(
176
- username: "username",
177
- password: "password"
178
- )
179
- service = IBMWatson::ToneAnalyzerV3.new(
180
- version: "2017-09-21",
181
- authenticator: authenticator
182
- )
183
- service_response = service.headers(
184
- "Custom-Header-One" => "yes",
185
- "Content-Type" => "Custom/Type"
186
- ).tone(tone_input: tone_text, content_type: "application/json")
187
- assert_equal(tone_response, service_response.result)
188
- end
189
-
190
- def test_tone_with_application_json
191
- tone_response = JSON.parse(File.read(Dir.getwd + "/resources/tone-v3-expect1.json"))
192
- headers = {
193
- "Content-Type" => "application/json"
194
- }
195
- tone_text = { "text" => "This is the text to be analyzed" }
196
- stub_request(:post, "https://api.us-south.tone-analyzer.watson.cloud.ibm.com/v3/tone?version=2017-09-21")
197
- .with(
198
- body: tone_text,
199
- headers: {
200
- "Accept" => "application/json",
201
- "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
202
- "Content-Type" => "application/json",
203
- "Host" => "api.us-south.tone-analyzer.watson.cloud.ibm.com"
204
- }
205
- ).to_return(status: 200, body: tone_response.to_json, headers: headers)
206
- authenticator = IBMWatson::Authenticators::BasicAuthenticator.new(
207
- username: "username",
208
- password: "password"
209
- )
210
- service = IBMWatson::ToneAnalyzerV3.new(
211
- version: "2017-09-21",
212
- authenticator: authenticator
213
- )
214
- service_response = service.tone(tone_input: tone_text, content_type: "application/json")
215
- assert_equal(tone_response, service_response.result)
216
- end
217
- end
@@ -1,300 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require("json")
4
- require_relative("./../test_helper.rb")
5
- require("webmock/minitest")
6
-
7
- WebMock.disable_net_connect!(allow_localhost: true)
8
-
9
- # Unit tests for the Visual Recognition V3 Service
10
- class VisualRecognitionV3Test < Minitest::Test
11
- def test_get_classifier
12
- authenticator = IBMWatson::Authenticators::BearerTokenAuthenticator.new(
13
- bearer_token: "bogus_access_token"
14
- )
15
- service = IBMWatson::VisualRecognitionV3.new(
16
- version: "2018-03-19",
17
- authenticator: authenticator
18
- )
19
- response = {
20
- "classifier_id" => "bogusnumber",
21
- "name" => "Dog Breeds",
22
- "owner" => "58b61352-678c-44d1-9f40-40edf4ea8d19",
23
- "status" => "failed",
24
- "created" => "2017-08-25T06:39:01.968Z",
25
- "classes" => [
26
- {
27
- "class" => "goldenretriever"
28
- }
29
- ]
30
- }
31
- stub_request(:get, "https://api.us-south.visual-recognition.watson.cloud.ibm.com/v3/classifiers/bogusnumber?version=2018-03-19")
32
- .with(
33
- headers: {
34
- "Accept" => "application/json",
35
- "Host" => "api.us-south.visual-recognition.watson.cloud.ibm.com",
36
- "Authorization" => "Bearer bogus_access_token"
37
- }
38
- ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
39
- service_response = service.get_classifier(
40
- classifier_id: "bogusnumber"
41
- )
42
- assert_equal(response, service_response.result)
43
- end
44
-
45
- def test_delete_classifier
46
- authenticator = IBMWatson::Authenticators::BearerTokenAuthenticator.new(
47
- bearer_token: "bogus_access_token"
48
- )
49
- service = IBMWatson::VisualRecognitionV3.new(
50
- version: "2018-03-19",
51
- authenticator: authenticator
52
- )
53
- stub_request(:delete, "https://api.us-south.visual-recognition.watson.cloud.ibm.com/v3/classifiers/bogusnumber?version=2018-03-19")
54
- .with(
55
- headers: {
56
- "Accept" => "application/json",
57
- "Host" => "api.us-south.visual-recognition.watson.cloud.ibm.com",
58
- "Authorization" => "Bearer bogus_access_token"
59
- }
60
- ).to_return(status: 200, body: { "response" => 200 }.to_json, headers: { "Content-Type" => "applicaton/json" })
61
- service_response = service.delete_classifier(
62
- classifier_id: "bogusnumber"
63
- )
64
- assert_nil(service_response)
65
- end
66
-
67
- def test_list_classifiers
68
- authenticator = IBMWatson::Authenticators::BearerTokenAuthenticator.new(
69
- bearer_token: "bogus_access_token"
70
- )
71
- service = IBMWatson::VisualRecognitionV3.new(
72
- version: "2018-03-19",
73
- authenticator: authenticator
74
- )
75
- response = {
76
- "classifiers" =>
77
- [
78
- {
79
- "classifier_id" => "InsuranceClaims_1362331461",
80
- "name" => "Insurance Claims",
81
- "status" => "ready"
82
- },
83
- {
84
- "classifier_id" => "DogBreeds_1539707331",
85
- "name" => "Dog Breeds",
86
- "status" => "ready"
87
- }
88
- ]
89
- }
90
- stub_request(:get, "https://api.us-south.visual-recognition.watson.cloud.ibm.com/v3/classifiers?version=2018-03-19")
91
- .with(
92
- headers: {
93
- "Accept" => "application/json",
94
- "Host" => "api.us-south.visual-recognition.watson.cloud.ibm.com",
95
- "Authorization" => "Bearer bogus_access_token"
96
- }
97
- ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
98
- service_response = service.list_classifiers
99
- assert_equal(response, service_response.result)
100
- end
101
-
102
- def test_create_classifier
103
- authenticator = IBMWatson::Authenticators::BearerTokenAuthenticator.new(
104
- bearer_token: "bogus_access_token"
105
- )
106
- service = IBMWatson::VisualRecognitionV3.new(
107
- version: "2018-03-19",
108
- authenticator: authenticator
109
- )
110
- response = {
111
- "classifier_id" => "DogBreeds_2014254824",
112
- "name" => "Dog Breeds",
113
- "owner" => "58b61352-678c-44d1-9f40-40edf4ea8d19",
114
- "status" => "failed",
115
- "created" => "2017-08-25T06:39:01.968Z",
116
- "classes" => [{ "class" => "goldenretriever" }]
117
- }
118
- cars = File.open(Dir.getwd + "/resources/cars.zip")
119
- trucks = File.open(Dir.getwd + "/resources/trucks.zip")
120
-
121
- stub_request(:post, "https://api.us-south.visual-recognition.watson.cloud.ibm.com/v3/classifiers?version=2018-03-19")
122
- .with(
123
- headers: {
124
- "Accept" => "application/json",
125
- "Host" => "api.us-south.visual-recognition.watson.cloud.ibm.com",
126
- "Authorization" => "Bearer bogus_access_token"
127
- }
128
- ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
129
- service_response = service.create_classifier(
130
- name: "Cars vs Trucks",
131
- positive_examples: { cars: cars },
132
- negative_examples: trucks
133
- )
134
- assert_equal(response, service_response.result)
135
-
136
- service_response = service.create_classifier(
137
- name: "Cars vs Trucks",
138
- positive_examples: { cars: "cars" },
139
- negative_examples: "trucks"
140
- )
141
- assert_equal(response, service_response.result)
142
- end
143
-
144
- def test_update_classifier
145
- authenticator = IBMWatson::Authenticators::BearerTokenAuthenticator.new(
146
- bearer_token: "bogus_access_token"
147
- )
148
- service = IBMWatson::VisualRecognitionV3.new(
149
- version: "2018-03-19",
150
- authenticator: authenticator
151
- )
152
- response = {
153
- "classifier_id" => "bogusid",
154
- "name" => "Insurance Claims",
155
- "owner" => "58b61352-678c-44d1-9f40-40edf4ea8d19",
156
- "status" => "ready",
157
- "created" => "2017-07-17T22:17:14.860Z",
158
- "classes" => [
159
- { "class" => "motorcycleaccident" },
160
- { "class" => "flattire" },
161
- { "class" => "brokenwinshield" }
162
- ]
163
- }
164
- stub_request(:post, "https://api.us-south.visual-recognition.watson.cloud.ibm.com/v3/classifiers/bogusid?version=2018-03-19")
165
- .with(
166
- headers: {
167
- "Accept" => "application/json",
168
- "Host" => "api.us-south.visual-recognition.watson.cloud.ibm.com",
169
- "Authorization" => "Bearer bogus_access_token"
170
- }
171
- ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
172
- service_response = service.update_classifier(
173
- classifier_id: "bogusid",
174
- positive_examples: { example: "positive examples classname" },
175
- negative_examples: "negative examples"
176
- )
177
- assert_equal(response, service_response.result)
178
-
179
- service_response = service.update_classifier(
180
- classifier_id: "bogusid",
181
- positive_examples: { example: "positive examples classname" },
182
- negative_examples: "negative examples"
183
- )
184
- assert_equal(response, service_response.result)
185
- end
186
-
187
- def test_classify
188
- authenticator = IBMWatson::Authenticators::BearerTokenAuthenticator.new(
189
- bearer_token: "bogus_access_token"
190
- )
191
- service = IBMWatson::VisualRecognitionV3.new(
192
- version: "2018-03-19",
193
- authenticator: authenticator
194
- )
195
- response = {
196
- "images" =>
197
- [
198
- {
199
- "image" => "test.jpg",
200
- "classifiers" =>
201
- [
202
- {
203
- "classes" =>
204
- [
205
- { "score" => 0.95, "class" => "tiger", "type_hierarchy" => "/animal/mammal/carnivore/feline/big cat/tiger" },
206
- { "score" => 0.997, "class" => "big cat" },
207
- { "score" => 0.998, "class" => "feline" },
208
- { "score" => 0.998, "class" => "carnivore" },
209
- { "score" => 0.998, "class" => "mammal" },
210
- { "score" => 0.999, "class" => "animal" }
211
- ],
212
- "classifier_id" => "default",
213
- "name" => "default"
214
- }
215
- ]
216
- }
217
- ],
218
- "custom_classes" => 0,
219
- "images_processed" => 1
220
- }
221
-
222
- stub_request(:post, "https://api.us-south.visual-recognition.watson.cloud.ibm.com/v3/classify?version=2018-03-19")
223
- .with(
224
- headers: {
225
- "Accept" => "application/json",
226
- "Host" => "api.us-south.visual-recognition.watson.cloud.ibm.com",
227
- "Authorization" => "Bearer bogus_access_token"
228
- }
229
- ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
230
- service_response = service.classify(
231
- url: "http://google.com"
232
- )
233
- assert_equal(response, service_response.result)
234
-
235
- service_response = service.classify(
236
- url: "http://google.com",
237
- classifier_ids: %w[one two three]
238
- )
239
- assert_equal(response, service_response.result)
240
-
241
- service_response = service.classify(
242
- url: "http://google.com",
243
- owners: %w[me IBM]
244
- )
245
- assert_equal(response, service_response.result)
246
-
247
- image_file = File.open(Dir.getwd + "/resources/test.jpg")
248
- service_response = service.classify(
249
- images_file: image_file,
250
- images_filename: "test.jpg"
251
- )
252
- assert_equal(response, service_response.result)
253
-
254
- service_response = service.classify(
255
- images_file: "image_file"
256
- )
257
- assert_equal(response, service_response.result)
258
- end
259
-
260
- def test_delete_user_data
261
- authenticator = IBMWatson::Authenticators::BearerTokenAuthenticator.new(
262
- bearer_token: "bogus_access_token"
263
- )
264
- service = IBMWatson::VisualRecognitionV3.new(
265
- version: "2018-03-19",
266
- authenticator: authenticator
267
- )
268
- stub_request(:delete, "https://api.us-south.visual-recognition.watson.cloud.ibm.com/v3/user_data?customer_id=id&version=2018-03-19")
269
- .with(
270
- headers: {
271
- "Accept" => "application/json",
272
- "Host" => "api.us-south.visual-recognition.watson.cloud.ibm.com",
273
- "Authorization" => "Bearer bogus_access_token"
274
- }
275
- ).to_return(status: 200, body: "", headers: {})
276
- service_response = service.delete_user_data(
277
- customer_id: "id"
278
- )
279
- assert_nil(service_response)
280
- end
281
-
282
- def test_get_core_ml_model
283
- authenticator = IBMWatson::Authenticators::BearerTokenAuthenticator.new(
284
- bearer_token: "bogus_access_token"
285
- )
286
- service = IBMWatson::VisualRecognitionV3.new(
287
- version: "2018-03-19",
288
- authenticator: authenticator
289
- )
290
- stub_request(:get, "https://api.us-south.visual-recognition.watson.cloud.ibm.com/v3/classifiers/classifierid/core_ml_model?version=2018-03-19")
291
- .with(
292
- headers: {
293
- "Authorization" => "Bearer bogus_access_token",
294
- "Host" => "api.us-south.visual-recognition.watson.cloud.ibm.com"
295
- }
296
- ).to_return(status: 200, body: "ml_model", headers: {})
297
- service_response = service.get_core_ml_model(classifier_id: "classifierid")
298
- assert_equal("ml_model", service_response.result)
299
- end
300
- end