ibm_watson 1.0.0.rc3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -104,7 +104,7 @@ if !ENV["DISCOVERY_APIKEY"].nil? && !ENV["DISCOVERY_URL"].nil?
104
104
 
105
105
  fields = @service.list_fields(
106
106
  environment_id: @environment_id,
107
- collection_ids: [new_collection_id]
107
+ collection_ids: [new_collection_id, @collection_id]
108
108
  )
109
109
  refute(fields.nil?)
110
110
 
@@ -80,7 +80,7 @@ if !ENV["SPEECH_TO_TEXT_APIKEY"].nil? && !ENV["SPEECH_TO_TEXT_URL"].nil?
80
80
  content_type: "audio/l16; rate=44100",
81
81
  timestamps: true,
82
82
  word_alternatives_threshold: 0.9,
83
- keywords: %w"[colorado tornado]",
83
+ keywords: %w[colorado tornado],
84
84
  keywords_threshold: 0.5
85
85
  )
86
86
  refute_nil(output.result["results"][0]["alternatives"][0]["transcript"])
@@ -26,19 +26,19 @@ if !ENV["TONE_ANALYZER_APIKEY"].nil? && !ENV["TONE_ANALYZER_URL"].nil?
26
26
  end
27
27
 
28
28
  def test_tone
29
- tone_text = File.read(Dir.getwd + "/resources/personality.txt")
29
+ tone_text = File.read(Dir.getwd + "/resources/tone-example.json")
30
30
  service_response = service.tone(
31
31
  tone_input: tone_text,
32
- content_type: "text/plain"
32
+ content_type: "application/json"
33
33
  )
34
34
  assert((200..299).cover?(service_response.status))
35
35
  end
36
36
 
37
37
  def test_tone_with_args
38
- tone_text = File.read(Dir.getwd + "/resources/personality.txt")
38
+ tone_text = File.read(Dir.getwd + "/resources/tone-example.json")
39
39
  service_response = service.tone(
40
40
  tone_input: tone_text,
41
- content_type: "text/plain",
41
+ content_type: "application/json",
42
42
  sentences: false
43
43
  )
44
44
  assert((200..299).cover?(service_response.status))
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require("json")
4
+ require_relative("./../test_helper.rb")
5
+ require("minitest/hooks/test")
6
+
7
+ if !ENV["VISUAL_RECOGNITION_APIKEY"].nil? && !ENV["VISUAL_RECOGNITION_URL"].nil?
8
+ # Integration tests for the Visual Recognition V3 Service
9
+ class VisualRecognitionV4Test < Minitest::Test
10
+ include Minitest::Hooks
11
+ attr_accessor :service, :classifier_id
12
+ def before_all
13
+ authenticator = IBMWatson::Authenticators::IamAuthenticator.new(
14
+ apikey: ENV["VISUAL_RECOGNITION_APIKEY"]
15
+ )
16
+ @service = IBMWatson::VisualRecognitionV4.new(
17
+ version: "2018-03-19",
18
+ url: ENV["VISUAL_RECOGNITION_URL"],
19
+ authenticator: authenticator
20
+ )
21
+ @collection_id = ENV["VISUAL_RECOGNITION_COLLECTION_ID"]
22
+ @service.add_default_headers(
23
+ headers: {
24
+ "X-Watson-Learning-Opt-Out" => "1",
25
+ "X-Watson-Test" => "1"
26
+ }
27
+ )
28
+ end
29
+
30
+ def test_analyze
31
+ image_file = File.open(Dir.getwd + "/resources/dog.jpg")
32
+ image_file_1 = File.open(Dir.getwd + "/resources/face.jpg")
33
+ result = @service.analyze(
34
+ images_file: [
35
+ {
36
+ "data": image_file,
37
+ "filename": "dog.jpg",
38
+ "content_type": "image/jpeg"
39
+ },
40
+ {
41
+ "data": image_file_1,
42
+ "filename": "face.jpg",
43
+ "content_type": "image/jpeg"
44
+ }
45
+ ],
46
+ collection_ids: [@collection_id],
47
+ features: ["objects"]
48
+ ).result
49
+ assert_equal(2, result["images"].length)
50
+ refute(result.nil?)
51
+ end
52
+
53
+ def test_list_collections
54
+ result = @service.list_collections.result
55
+ refute(result.nil?)
56
+ end
57
+
58
+ def test_get_collection
59
+ result = @service.get_collection(
60
+ collection_id: @collection_id
61
+ ).result
62
+ refute(result.nil?)
63
+ end
64
+
65
+ def test_list_images
66
+ result = @service.list_images(
67
+ collection_id: @collection_id
68
+ ).result
69
+ refute(result.nil?)
70
+ end
71
+ end
72
+ else
73
+ class VisualRecognitionV4Test < Minitest::Test
74
+ def test_missing_credentials_skip_integration
75
+ skip "Skip visual recognition integration tests because credentials have not been provided"
76
+ end
77
+ end
78
+ end
@@ -1029,6 +1029,31 @@ class DiscoveryV1Test < Minitest::Test
1029
1029
  assert_equal({ "received" => "true" }, service_response.result)
1030
1030
  end
1031
1031
 
1032
+ def test_get_autocompletion
1033
+ authenticator = IBMWatson::Authenticators::BasicAuthenticator.new(
1034
+ username: "username",
1035
+ password: "password"
1036
+ )
1037
+ service = IBMWatson::DiscoveryV1.new(
1038
+ version: "2018-03-05",
1039
+ authenticator: authenticator
1040
+ )
1041
+ stub_request(:get, "https://gateway.watsonplatform.net/discovery/api/v1/environments/envid/collections/collid/autocompletion?prefix=hi&version=2018-03-05")
1042
+ .with(
1043
+ headers: {
1044
+ "Accept" => "application/json",
1045
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
1046
+ "Host" => "gateway.watsonplatform.net"
1047
+ }
1048
+ ).to_return(status: 200, body: { "received" => "true" }.to_json, headers: { "Content-Type" => "application/json" })
1049
+ service_response = service.get_autocompletion(
1050
+ environment_id: "envid",
1051
+ collection_id: "collid",
1052
+ prefix: "hi"
1053
+ )
1054
+ assert_equal({ "received" => "true" }, service_response.result)
1055
+ end
1056
+
1032
1057
  def test_list_training_examples
1033
1058
  authenticator = IBMWatson::Authenticators::BasicAuthenticator.new(
1034
1059
  username: "username",
@@ -0,0 +1,412 @@
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 VisualRecognitionV4Test < Minitest::Test
11
+ def test_analyze
12
+ authenticator = IBMWatson::Authenticators::BearerTokenAuthenticator.new(
13
+ bearer_token: "bogus_access_token"
14
+ )
15
+ service = IBMWatson::VisualRecognitionV4.new(
16
+ version: "2018-03-19",
17
+ authenticator: authenticator
18
+ )
19
+ file = File.open(Dir.getwd + "/resources/cnc_test.pdf")
20
+ stub_request(:post, "https://gateway.watsonplatform.net/visual-recognition/api/v4/analyze?version=2018-03-19").with do |req|
21
+ assert_equal(req.headers["Accept"], "application/json")
22
+ assert_match(%r{\Amultipart/form-data}, req.headers["Content-Type"])
23
+ assert_match(/Content-Disposition: form-data/, req.body)
24
+ assert_match(/Content-Disposition: form-data; name="images_file"; filename="dog.jpg"/, req.body)
25
+ end
26
+ service.analyze(
27
+ images_file: [
28
+ {
29
+ "data": file,
30
+ "filename": "dog.jpg",
31
+ "content_type": "image/jpeg"
32
+ }
33
+ ],
34
+ collection_ids: ["collid"],
35
+ features: ["animal"]
36
+ )
37
+ end
38
+
39
+ def test_analyze_url
40
+ authenticator = IBMWatson::Authenticators::BearerTokenAuthenticator.new(
41
+ bearer_token: "bogus_access_token"
42
+ )
43
+ service = IBMWatson::VisualRecognitionV4.new(
44
+ version: "2018-03-19",
45
+ authenticator: authenticator
46
+ )
47
+ stub_request(:post, "https://gateway.watsonplatform.net/visual-recognition/api/v4/analyze?version=2018-03-19").with do |req|
48
+ assert_equal(req.headers["Accept"], "application/json")
49
+ assert_match(%r{\Amultipart/form-data}, req.headers["Content-Type"])
50
+ assert_match(/Content-Disposition: form-data; name="image_url"/, req.body)
51
+ end
52
+ service.analyze(
53
+ image_url: [
54
+ "www.somethingfunny.com"
55
+ ],
56
+ collection_ids: ["collid"],
57
+ features: ["animal"]
58
+ )
59
+ end
60
+
61
+ def test_create_collection
62
+ authenticator = IBMWatson::Authenticators::BearerTokenAuthenticator.new(
63
+ bearer_token: "bogus_access_token"
64
+ )
65
+ service = IBMWatson::VisualRecognitionV4.new(
66
+ version: "2018-03-19",
67
+ authenticator: authenticator
68
+ )
69
+ response = {
70
+ "collection_id" => "collid",
71
+ "name" => "Dog Breeds",
72
+ "owner" => "58b61352-678c-44d1-9f40-40edf4ea8d19",
73
+ "status" => "passed",
74
+ "created" => "2017-08-25T06:39:01.968Z",
75
+ "classes" => [
76
+ {
77
+ "class" => "goldenretriever"
78
+ }
79
+ ]
80
+ }
81
+ stub_request(:post, "https://gateway.watsonplatform.net/visual-recognition/api/v4/collections?version=2018-03-19")
82
+ .with(
83
+ headers: {
84
+ "Accept" => "application/json",
85
+ "Host" => "gateway.watsonplatform.net",
86
+ "Authorization" => "Bearer bogus_access_token"
87
+ }
88
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
89
+ service_response = service.create_collection
90
+ assert_equal(response, service_response.result)
91
+ end
92
+
93
+ def test_list_collections
94
+ authenticator = IBMWatson::Authenticators::BearerTokenAuthenticator.new(
95
+ bearer_token: "bogus_access_token"
96
+ )
97
+ service = IBMWatson::VisualRecognitionV4.new(
98
+ version: "2018-03-19",
99
+ authenticator: authenticator
100
+ )
101
+ response = {
102
+ "collections" => []
103
+ }
104
+ stub_request(:get, "https://gateway.watsonplatform.net/visual-recognition/api/v4/collections?version=2018-03-19")
105
+ .with(
106
+ headers: {
107
+ "Accept" => "application/json",
108
+ "Host" => "gateway.watsonplatform.net",
109
+ "Authorization" => "Bearer bogus_access_token"
110
+ }
111
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
112
+ service_response = service.list_collections
113
+ assert_equal(response, service_response.result)
114
+ end
115
+
116
+ def test_get_collection
117
+ authenticator = IBMWatson::Authenticators::BearerTokenAuthenticator.new(
118
+ bearer_token: "bogus_access_token"
119
+ )
120
+ service = IBMWatson::VisualRecognitionV4.new(
121
+ version: "2018-03-19",
122
+ authenticator: authenticator
123
+ )
124
+ response = {
125
+ "collection_id" => "collid",
126
+ "name" => "Dog Breeds",
127
+ "owner" => "58b61352-678c-44d1-9f40-40edf4ea8d19",
128
+ "status" => "failed",
129
+ "created" => "2017-08-25T06:39:01.968Z",
130
+ "classes" => [
131
+ {
132
+ "class" => "goldenretriever"
133
+ }
134
+ ]
135
+ }
136
+ stub_request(:get, "https://gateway.watsonplatform.net/visual-recognition/api/v4/collections/collid?version=2018-03-19")
137
+ .with(
138
+ headers: {
139
+ "Accept" => "application/json",
140
+ "Host" => "gateway.watsonplatform.net",
141
+ "Authorization" => "Bearer bogus_access_token"
142
+ }
143
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
144
+ service_response = service.get_collection(
145
+ collection_id: "collid"
146
+ )
147
+ assert_equal(response, service_response.result)
148
+ end
149
+
150
+ def test_update_collection
151
+ authenticator = IBMWatson::Authenticators::BearerTokenAuthenticator.new(
152
+ bearer_token: "bogus_access_token"
153
+ )
154
+ service = IBMWatson::VisualRecognitionV4.new(
155
+ version: "2018-03-19",
156
+ authenticator: authenticator
157
+ )
158
+ response = {
159
+ "collection_id" => "collid",
160
+ "name" => "Dog Breeds",
161
+ "owner" => "58b61352-678c-44d1-9f40-40edf4ea8d19",
162
+ "status" => "failed",
163
+ "created" => "2017-08-25T06:39:01.968Z",
164
+ "classes" => [
165
+ {
166
+ "class" => "goldenretriever"
167
+ }
168
+ ]
169
+ }
170
+ stub_request(:post, "https://gateway.watsonplatform.net/visual-recognition/api/v4/collections/collid?version=2018-03-19")
171
+ .with(
172
+ headers: {
173
+ "Accept" => "application/json",
174
+ "Host" => "gateway.watsonplatform.net",
175
+ "Authorization" => "Bearer bogus_access_token"
176
+ }
177
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
178
+ service_response = service.update_collection(
179
+ collection_id: "collid"
180
+ )
181
+ assert_equal(response, service_response.result)
182
+ end
183
+
184
+ def test_delete_collection
185
+ authenticator = IBMWatson::Authenticators::BearerTokenAuthenticator.new(
186
+ bearer_token: "bogus_access_token"
187
+ )
188
+ service = IBMWatson::VisualRecognitionV4.new(
189
+ version: "2018-03-19",
190
+ authenticator: authenticator
191
+ )
192
+ response = {}
193
+ stub_request(:delete, "https://gateway.watsonplatform.net/visual-recognition/api/v4/collections/collid?version=2018-03-19")
194
+ .with(
195
+ headers: {
196
+ "Accept" => "application/json",
197
+ "Host" => "gateway.watsonplatform.net",
198
+ "Authorization" => "Bearer bogus_access_token"
199
+ }
200
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
201
+ service_response = service.delete_collection(
202
+ collection_id: "collid"
203
+ )
204
+ assert_nil(service_response)
205
+ end
206
+
207
+ def test_add_images
208
+ authenticator = IBMWatson::Authenticators::BearerTokenAuthenticator.new(
209
+ bearer_token: "bogus_access_token"
210
+ )
211
+ service = IBMWatson::VisualRecognitionV4.new(
212
+ version: "2018-03-19",
213
+ authenticator: authenticator
214
+ )
215
+ response = {
216
+ "collection_id" => "collid"
217
+ }
218
+ file = File.open(Dir.getwd + "/resources/cnc_test.pdf")
219
+ stub_request(:post, "https://gateway.watsonplatform.net/visual-recognition/api/v4/collections/collid/images?version=2018-03-19")
220
+ .with(
221
+ headers: {
222
+ "Accept" => "application/json",
223
+ "Host" => "gateway.watsonplatform.net",
224
+ "Authorization" => "Bearer bogus_access_token"
225
+ }
226
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
227
+ service_response = service.add_images(
228
+ collection_id: "collid",
229
+ images_file: [
230
+ {
231
+ "data": file,
232
+ "filename": "dog.jpg",
233
+ "content_type": "image/jpeg"
234
+ }
235
+ ]
236
+ )
237
+ assert_equal(response, service_response.result)
238
+ end
239
+
240
+ def test_list_images
241
+ authenticator = IBMWatson::Authenticators::BearerTokenAuthenticator.new(
242
+ bearer_token: "bogus_access_token"
243
+ )
244
+ service = IBMWatson::VisualRecognitionV4.new(
245
+ version: "2018-03-19",
246
+ authenticator: authenticator
247
+ )
248
+ response = {
249
+ "collections" => []
250
+ }
251
+ stub_request(:get, "https://gateway.watsonplatform.net/visual-recognition/api/v4/collections/collid/images?version=2018-03-19")
252
+ .with(
253
+ headers: {
254
+ "Accept" => "application/json",
255
+ "Host" => "gateway.watsonplatform.net",
256
+ "Authorization" => "Bearer bogus_access_token"
257
+ }
258
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
259
+ service_response = service.list_images(
260
+ collection_id: "collid"
261
+ )
262
+ assert_equal(response, service_response.result)
263
+ end
264
+
265
+ def test_get_image_details
266
+ authenticator = IBMWatson::Authenticators::BearerTokenAuthenticator.new(
267
+ bearer_token: "bogus_access_token"
268
+ )
269
+ service = IBMWatson::VisualRecognitionV4.new(
270
+ version: "2018-03-19",
271
+ authenticator: authenticator
272
+ )
273
+ response = {
274
+ "collection_id" => "collid"
275
+ }
276
+ stub_request(:get, "https://gateway.watsonplatform.net/visual-recognition/api/v4/collections/collid/images/imageid?version=2018-03-19")
277
+ .with(
278
+ headers: {
279
+ "Accept" => "application/json",
280
+ "Host" => "gateway.watsonplatform.net",
281
+ "Authorization" => "Bearer bogus_access_token"
282
+ }
283
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
284
+ service_response = service.get_image_details(
285
+ collection_id: "collid",
286
+ image_id: "imageid"
287
+ )
288
+ assert_equal(response, service_response.result)
289
+ end
290
+
291
+ def test_delete_image
292
+ authenticator = IBMWatson::Authenticators::BearerTokenAuthenticator.new(
293
+ bearer_token: "bogus_access_token"
294
+ )
295
+ service = IBMWatson::VisualRecognitionV4.new(
296
+ version: "2018-03-19",
297
+ authenticator: authenticator
298
+ )
299
+ response = {}
300
+ stub_request(:delete, "https://gateway.watsonplatform.net/visual-recognition/api/v4/collections/collid/images/imageid?version=2018-03-19")
301
+ .with(
302
+ headers: {
303
+ "Accept" => "application/json",
304
+ "Host" => "gateway.watsonplatform.net",
305
+ "Authorization" => "Bearer bogus_access_token"
306
+ }
307
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
308
+ service_response = service.delete_image(
309
+ collection_id: "collid",
310
+ image_id: "imageid"
311
+ )
312
+ assert_nil(service_response)
313
+ end
314
+
315
+ def test_get_jpeg_image
316
+ authenticator = IBMWatson::Authenticators::BearerTokenAuthenticator.new(
317
+ bearer_token: "bogus_access_token"
318
+ )
319
+ service = IBMWatson::VisualRecognitionV4.new(
320
+ version: "2018-03-19",
321
+ authenticator: authenticator
322
+ )
323
+ response = {
324
+ "collection_id" => "collid"
325
+ }
326
+ stub_request(:get, "https://gateway.watsonplatform.net/visual-recognition/api/v4/collections/collid/images/imageid/jpeg?version=2018-03-19")
327
+ .with(
328
+ headers: {
329
+ "Host" => "gateway.watsonplatform.net",
330
+ "Authorization" => "Bearer bogus_access_token"
331
+ }
332
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
333
+ service_response = service.get_jpeg_image(
334
+ collection_id: "collid",
335
+ image_id: "imageid"
336
+ )
337
+ assert_equal(response, service_response.result)
338
+ end
339
+
340
+ def test_train
341
+ authenticator = IBMWatson::Authenticators::BearerTokenAuthenticator.new(
342
+ bearer_token: "bogus_access_token"
343
+ )
344
+ service = IBMWatson::VisualRecognitionV4.new(
345
+ version: "2018-03-19",
346
+ authenticator: authenticator
347
+ )
348
+ response = {
349
+ "collection_id" => "collid"
350
+ }
351
+ stub_request(:post, "https://gateway.watsonplatform.net/visual-recognition/api/v4/collections/collid/train?version=2018-03-19")
352
+ .with(
353
+ headers: {
354
+ "Host" => "gateway.watsonplatform.net",
355
+ "Authorization" => "Bearer bogus_access_token"
356
+ }
357
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
358
+ service_response = service.train(
359
+ collection_id: "collid"
360
+ )
361
+ assert_equal(response, service_response.result)
362
+ end
363
+
364
+ def test_add_image_training_data
365
+ authenticator = IBMWatson::Authenticators::BearerTokenAuthenticator.new(
366
+ bearer_token: "bogus_access_token"
367
+ )
368
+ service = IBMWatson::VisualRecognitionV4.new(
369
+ version: "2018-03-19",
370
+ authenticator: authenticator
371
+ )
372
+ response = {
373
+ "collection_id" => "collid"
374
+ }
375
+ stub_request(:post, "https://gateway.watsonplatform.net/visual-recognition/api/v4/collections/collid/images/imageid/training_data?version=2018-03-19")
376
+ .with(
377
+ headers: {
378
+ "Accept" => "application/json",
379
+ "Host" => "gateway.watsonplatform.net",
380
+ "Authorization" => "Bearer bogus_access_token"
381
+ }
382
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
383
+ service_response = service.add_image_training_data(
384
+ collection_id: "collid",
385
+ image_id: "imageid"
386
+ )
387
+ assert_equal(response, service_response.result)
388
+ end
389
+
390
+ def test_delete_user_data
391
+ authenticator = IBMWatson::Authenticators::BearerTokenAuthenticator.new(
392
+ bearer_token: "bogus_access_token"
393
+ )
394
+ service = IBMWatson::VisualRecognitionV4.new(
395
+ version: "2018-03-19",
396
+ authenticator: authenticator
397
+ )
398
+ response = {}
399
+ stub_request(:delete, "https://gateway.watsonplatform.net/visual-recognition/api/v4/user_data?version=2018-03-19&customer_id=customer")
400
+ .with(
401
+ headers: {
402
+ "Accept" => "application/json",
403
+ "Host" => "gateway.watsonplatform.net",
404
+ "Authorization" => "Bearer bogus_access_token"
405
+ }
406
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
407
+ service_response = service.delete_user_data(
408
+ customer_id: "customer"
409
+ )
410
+ assert_nil(service_response)
411
+ end
412
+ end