forked from watson-developer-cloud/node-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.retrieve_and_rank.v1.js
443 lines (375 loc) · 13.6 KB
/
test.retrieve_and_rank.v1.js
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
'use strict';
var assert = require('assert');
var watson = require('../index');
var nock = require('nock');
var url = require('url');
var fs = require('fs');
var noop = function() {};
describe('retrieve_and_rank', function() {
var clusterId = 'scffffffff_ffff_ffff_ffff_ffffffffffff';
var configName = 'test_config';
var collectionName = 'test_collection';
var serviceUrl = 'http://retrieve-and-rank-service.com/path/to/service';
var solrClustersPath = '/v1/solr_clusters';
var listResponse = 'list response';
var createResponse = 'create response';
var solrClusterPath = '/v1/solr_clusters/' + clusterId;
var pollResponse = 'poll response';
var deleteResponse = 'delete response';
var statsResponse = 'stats response';
var resizeResponse = 'resize response';
var configsPath = '/v1/solr_clusters/' + clusterId + '/config';
var configPath = configsPath + '/' + configName;
var configListResponse = 'config list response';
var configUploadResponse = 'config upload response';
var configGetResponse = 'config get response';
var configDeleteResponse = 'config delete response';
var collectionsPath = '/v1/solr_clusters/' + clusterId + '/solr/admin/collections';
var collectionCreateResponse = 'collection create';
var collectionListResponse = 'collection list';
var collectionDeleteResponse = 'collection delete';
var rankerPath = '/v1/rankers';
var rankerId = 'foo';
var rankPath = rankerPath + '/' + rankerId;
var rankerData = fs.createReadStream(__dirname + '/resources/ranker_test.csv');
var rankerTrainData = fs.createReadStream(__dirname + '/resources/ranker_train.csv');
var createRankerResponse = 'rank created';
var deleteRankerResponse = 'rank deleted';
var getRankerResponse = 'rank status';
var rankResponse = 'ranking';
var service = {
username: 'batman',
password: 'bruce-wayne',
url: serviceUrl,
version: 'v1'
};
var missingParameter = function(err) {
assert.ok((err instanceof Error) && /required parameters/.test(err));
};
var search = watson.retrieve_and_rank(service);
before(function createMockSearchService() {
nock.disableNetConnect();
nock(service.url).persist()
.get(solrClustersPath).reply(200, listResponse)
.post(solrClustersPath).reply(200, createResponse)
.get(solrClusterPath).reply(200, pollResponse)
.delete(solrClusterPath).reply(200, deleteResponse)
.get(solrClusterPath + '/stats').reply(200, statsResponse)
.get(solrClusterPath + '/cluster_size').reply(200, resizeResponse)
.put(solrClusterPath + '/cluster_size').reply(200, resizeResponse)
.get(configsPath).reply(200, configListResponse)
.post(configPath).reply(200, configUploadResponse)
.get(configPath).reply(200, configGetResponse)
.delete(configPath).reply(200, configDeleteResponse)
.post(collectionsPath + '?collection.configName=' + configName+ '&name='+collectionName+'&wt=json&action=CREATE')
.reply(200, collectionCreateResponse)
.get(collectionsPath + '?action=LIST&wt=json')
.reply(200, collectionListResponse)
.post(collectionsPath + '?name=' + collectionName+ '&wt=json&action=DELETE')
.reply(200, collectionDeleteResponse)
.post(rankerPath)
.reply(200, createRankerResponse)
.get(rankerPath)
.reply(200, getRankerResponse)
.delete(rankPath)
.reply(200, deleteRankerResponse)
.post(rankPath)
.reply(200, rankResponse)
.get(rankPath)
.reply(200, getRankerResponse);
});
after(function() {
nock.cleanAll();
});
it('can list Solr clusters', function(done) {
search.listClusters({}, function(error, data) {
assert.equal(data, listResponse);
done(error);
});
});
it('can create a Solr cluster without specified config', function(done) {
search.createCluster({}, function(error, data) {
assert.equal(data, createResponse);
done(error);
});
});
it('sets headers and body of request when creating a Solr cluster based on params', function(done) {
var createParams = {
cluster_size: '2',
cluster_name: 'the_cluster',
some_other_option: 'some_other_value'
};
var response = search.createCluster(createParams, noop);
assert.equal(response.headers['content-type'], 'application/json');
assert.deepEqual(JSON.parse(response.body), createParams);
done();
});
it('can poll a Solr cluster', function(done) {
search.pollCluster({
cluster_id: clusterId
}, function(error, data) {
assert.equal(data, pollResponse);
done();
});
});
it('returns error when cluster_id is not specified on poll request', function() {
search.pollCluster({}, missingParameter);
});
it('can delete a Solr cluster', function(done) {
search.deleteCluster({
cluster_id: clusterId
}, function(error, data) {
assert.equal(data, deleteResponse);
done();
});
});
it('returns error when cluster_id is not specified on delete request', function() {
search.deleteCluster({}, missingParameter);
});
it('can get a Solr clusters stats', function(done) {
search.getClusterStats({
cluster_id: clusterId
}, function(error, data) {
assert.equal(data, statsResponse);
done();
});
});
it('returns error when cluster_id is not specified on stats request', function() {
search.getClusterStats({}, missingParameter);
});
it('can resize a Solr cluster', function(done) {
search.resizeCluster({
cluster_id: clusterId,
cluster_size: 1
}, function(error, data) {
assert.equal(data, resizeResponse);
done();
});
});
it('returns error when cluster_id is not specified on resize request', function() {
search.resizeCluster({cluster_size: 1}, missingParameter);
});
it('returns error when cluster_size is not specified on resize request', function() {
search.resizeCluster({cluster_id: 'any_id'}, missingParameter);
});
it('can get a cluster\'s resize status', function(done) {
search.getResizeStatus({
cluster_id: clusterId
}, function(error, data) {
assert.equal(data, resizeResponse);
done();
});
});
it('returns error when cluster_id is not specified on resize status request', function() {
search.getResizeStatus({}, missingParameter);
});
it('can list Solr configs', function(done) {
search.listConfigs({
cluster_id: clusterId
}, function(error, data) {
assert.equal(data, configListResponse);
done();
});
});
it('returns error when cluster_id is not specified on list config request', function() {
search.listConfigs({}, missingParameter);
});
it('can upload a Solr config', function(done) {
var mockConfigFile = 'test/resources/mock_solr_config_file.zip';
search.uploadConfig({
cluster_id: clusterId,
config_name: configName,
config_zip_path: mockConfigFile
},
function(error, data) {
assert.equal(data, configUploadResponse);
done(error);
});
});
it('sets headers and body of request when uploading a Solr config', function(done) {
var mockConfigFile = 'test/resources/mock_solr_config_file.zip';
var response = search.uploadConfig({
cluster_id: clusterId,
config_name: configName,
config_zip_path: mockConfigFile
}, noop);
assert.equal(response.headers['content-type'], 'application/zip');
done();
});
it('returns error when cluster_id is not specified on upload config request', function() {
search.uploadConfig({}, missingParameter);
});
it('returns error when config_name is not specified on upload config request', function() {
search.uploadConfig({
cluster_id: clusterId
}, missingParameter);
});
it('returns error when config_zip_path is not specified on upload config request', function() {
search.uploadConfig({
cluster_id: clusterId,
config_name: configName
}, missingParameter);
});
it('can get a Solr config', function(done) {
search.getConfig({
cluster_id: clusterId,
config_name: configName
}, function(error, data) {
assert.equal(data, configGetResponse);
done(error);
});
});
it('returns error when cluster_id is not specified on get config request', function() {
search.getConfig({}, missingParameter);
});
it('returns error when config_name is not specified on get config request', function() {
search.getConfig({
cluster_id: clusterId
}, missingParameter);
});
it('can delete a Solr config', function(done) {
search.deleteConfig({
cluster_id: clusterId,
config_name: configName
}, function(error, data) {
assert.equal(data, configDeleteResponse);
done(error);
});
});
it('returns error when cluster_id is not specified on delete config request', function() {
search.deleteConfig({}, missingParameter);
});
it('returns error when config_name is not specified on delete config request', function() {
search.deleteConfig({
cluster_id: clusterId
}, missingParameter);
});
it('can create a Solr collection', function(done) {
search.createCollection({
cluster_id: clusterId,
collection_name: collectionName,
config_name: configName
},
function(error, data) {
assert.equal(data, collectionCreateResponse);
done(error);
});
});
it('returns error when cluster_id is not specified on create collection request', function() {
search.createCollection({}, missingParameter);
});
it('returns error when collection_name is not specified on create collection request', function() {
search.createCollection({
cluster_id: clusterId
}, missingParameter);
});
it('returns error when config_name is not specified on create collection request', function() {
search.createCollection({
cluster_id: clusterId,
collection_name: collectionName
}, missingParameter);
});
it('can list Solr collections', function(done) {
search.listCollections({
cluster_id: clusterId
}, function(error, data) {
assert.equal(data, collectionListResponse);
done(error);
});
});
it('returns error when cluster_id is not specified on list collection request', function() {
search.listCollections({}, missingParameter);
});
it('can delete a Solr collection', function(done) {
search.deleteCollection({
cluster_id: clusterId,
collection_name: collectionName
}, function(error, data) {
assert.equal(data, collectionDeleteResponse);
done(error);
});
});
it('returns error when cluster_id is not specified on delete collection request', function() {
search.deleteCollection({}, missingParameter);
});
it('returns error when collection_name is not specified on delete collection request', function() {
search.deleteCollection({
cluster_id: clusterId
}, missingParameter);
});
it('can create a Solr client using passed in params', function() {
var solrClient = search.createSolrClient({
cluster_id: clusterId,
collection_name: collectionName
});
var parsedUrl = url.parse(serviceUrl);
assert.equal(solrClient.options.host, parsedUrl.hostname);
assert.equal(solrClient.options.port, 443);
assert.equal(solrClient.options.path, parsedUrl.path + '/v1/solr_clusters/' + clusterId + '/solr');
assert.equal(solrClient.options.core, collectionName);
assert.equal(solrClient.options.secure, true);
});
it('returns error when cluster_id is not specified when building Solr client', function() {
assert.throws(
function() {
search.createSolrClient({});
},
/required parameters: cluster_id/
);
});
it('returns error when collection_name is not specified when building Solr client', function() {
assert.throws(
function() {
search.createSolrClient({
cluster_id: clusterId
});
},
/required parameters: collection_name/
);
});
it('generate valid request when creating a ranker', function() {
search.uploadConfig({}, missingParameter);
});
it('check missing parameters', function() {
search.createRanker({}, missingParameter);
search.createRanker(null, missingParameter);
search.rankerStatus({}, missingParameter);
search.rankerStatus(null, missingParameter);
search.deleteRanker({}, missingParameter);
search.deleteRanker(null, missingParameter);
search.rank({ranker_id: 'ranker1'}, missingParameter);
search.rank({ranker_id: 'ranker1', answer_metadata: rankerData }, missingParameter);
});
it('should generate a valid payload when creating a ranker', function(done) {
var req = search.createRanker({
training_data: rankerTrainData,
name: 'ranker1',
language: 'en'
}, function(error, data) {
assert.equal(data, createRankerResponse);
assert.equal(req.method, 'POST');
done(error);
});
});
it('should generate a valid payload when getting the rankers', function(done) {
var req = search.listRankers(null, function(error, data) {
assert.equal(data, getRankerResponse);
assert.equal(req.method, 'GET');
done(error);
});
});
it('should generate a valid payload when getting the ranker status', function(done) {
var req = search.rankerStatus({ranker_id : rankerId}, function(error, data) {
assert.equal(data, getRankerResponse);
assert.equal(req.method, 'GET');
done(error);
});
});
it('should generate a valid payload when deleting a ranker', function(done) {
var req = search.deleteRanker({ranker_id : rankerId}, function(error, data) {
assert.equal(data, deleteRankerResponse);
assert.equal(req.method, 'DELETE');
done(error);
});
});
});