-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
test.js
778 lines (709 loc) · 24.1 KB
/
test.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
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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
/* globals describe, it */
// to test, first create a search service in azure, and set the url and key here.
var clientConfiguration = {
url: process.env.URL || 'https://xxx.search.windows.net',
key: process.env.KEY || 'your key goes here',
// This API version is required for all tests to pass
version: '2017-11-11-Preview'
}
// You would also need a storage account (fill in the connection string for that account below)
// Please, also create a container named 'azuresearchtest' in that account (can have private access, and be empty)
var storageConnectionString = process.env.CONNECTION_STRING || 'DefaultEndpointsProtocol=https;AccountName=aaa;AccountKey=bbb'
var clientFactory = require('./index')
var client = clientFactory(clientConfiguration)
describe('search service', function () {
this.timeout(5000)
it('creates an index', function (done) {
var schema = {
name: 'myindex',
fields: [
{
name: 'id',
type: 'Edm.String',
searchable: false,
filterable: true,
retrievable: true,
sortable: true,
facetable: true,
key: true
},
{
name: 'description',
type: 'Edm.String',
searchable: true,
filterable: false,
retrievable: true,
sortable: false,
facetable: false,
key: false
},
{
name: 'category',
type: 'Edm.String',
searchable: true,
filterable: false,
retrievable: true,
sortable: false,
facetable: true,
key: false,
analyzer: 'phonetic_area_analyzer'
}
],
'analyzers': [
{
'name': 'phonetic_area_analyzer',
'@odata.type': '#Microsoft.Azure.Search.CustomAnalyzer',
'tokenizer': 'area_standard',
'tokenFilters': ['lowercase', 'asciifolding', 'areas_phonetc']
}
],
'charFilters': [],
'tokenizers': [
{
'name': 'area_standard',
'@odata.type': '#Microsoft.Azure.Search.StandardTokenizerV2'
},
{
'name': 'area_keyword',
'@odata.type': '#Microsoft.Azure.Search.KeywordTokenizerV2'
}
],
'tokenFilters': [
{
'name': 'area_edge',
'@odata.type': '#Microsoft.Azure.Search.EdgeNGramTokenFilterV2',
'minGram': 2,
'maxGram': 50
},
{
'name': 'area_token_edge',
'@odata.type': '#Microsoft.Azure.Search.EdgeNGramTokenFilterV2',
'minGram': 2,
'maxGram': 20
},
{
'name': 'areas_phonetc',
'@odata.type': '#Microsoft.Azure.Search.PhoneticTokenFilter',
'encoder': 'doubleMetaphone'
}
],
suggesters: [
{
name: 'sg',
searchMode: 'analyzingInfixMatching',
sourceFields: ['description', 'id']
}
],
scoringProfiles: [],
defaultScoringProfile: null,
corsOptions: { allowedOrigins: ['*'] }
}
client.createIndex(schema, function (err, data) {
if (err) return done('error returned ' + err.message)
if (!data) return done('data is not defined')
if (data.name !== 'myindex') return done('no index name')
if (data.fields.length !== 3) return done('wrong number of fields')
return done()
})
})
it('get index stats', function (done) {
client.getIndexStats('myindex', function (err, index) {
if (err) return done('error returned')
if (!index) return done('index is null')
if (index.documentCount !== 0) return done('wrong document size')
return done()
})
})
it('runs test analyzer', function (done) {
var data = {
'text': 'Text to analyze',
'analyzer': 'standard'
}
client.testAnalyzer('myindex', data, function (err, tokens) {
if (err) return done('error returned')
if (!tokens) return done('tokens is null')
if (tokens.length === 0) return done('no tokens returned')
return done()
})
})
it('indexes a document', function (done) {
var doc1 = {
'id': 'document1',
'description': 'this is the description of my unique document',
'category': 'mycategory'
}
client.addDocuments('myindex', [doc1], function (err, data) {
if (err) return done('error returned')
if (!data) return done('data is null')
return done()
})
})
it('deletes a document', function (done) {
var doc2 = {
'id': 'document2',
'description': 'this is the description of my document'
}
client.addDocuments('myindex', [doc2], function (err, data) {
if (err) return done('error returned')
if (!data) return done('data is null')
var key = { 'id': 'document2' }
client.deleteDocuments('myindex', [key], function (err, data) {
if (err) return done('error returned')
if (!data) return done('data is null')
return done()
})
})
})
it('updates a document', function (done) {
var doc3 = {
'id': 'document3',
'description': 'this is the description of my document'
}
client.addDocuments('myindex', [doc3], function (err, data) {
if (err) return done('error returned')
if (!data) return done('data is null')
// update description field
doc3.description = 'updated description'
client.updateDocuments('myindex', [doc3], function (err, data) {
if (err) return done('error returned')
if (!data) return done('data is null')
// ensure changes were saved
client.lookup('myindex', 'document3', function (err, results) {
if (err) return done('error returned')
if (!results) return done('results is null')
if (results.description !== doc3.description) return done('document not updated')
return done()
})
})
})
})
it('updates or uploads a document', function (done) {
var doc5 = {
'id': 'document5',
'description': 'this is the description of my document'
}
client.updateOrUploadDocuments('myindex', [doc5], function (err, data) {
if (err) return done('error returned')
if (!data) return done('data is null')
// update description field
doc5.description = 'updated description'
client.updateOrUploadDocuments('myindex', [doc5], function (err, data) {
if (err) return done('error returned')
if (!data) return done('data is null')
// ensure changes were saved
client.lookup('myindex', 'document5', function (err, results) {
if (err) return done('error returned')
if (!results) return done('results is null')
if (results.description !== doc5.description) return done('document not updated')
return done()
})
})
})
})
it('updates a document', function (done) {
var doc3 = {
'id': 'document3',
'description': 'this is the description of my document'
}
client.addDocuments('myindex', [doc3], function (err, data) {
if (err) return done('error returned')
if (!data) return done('data is null')
// update description field
doc3.description = 'updated description'
client.updateDocuments('myindex', [doc3], function (err, data) {
if (err) return done('error returned')
if (!data) return done('data is null')
// ensure changes were saved
client.lookup('myindex', 'document3', function (err, results) {
if (err) return done('error returned')
if (!results) return done('results is null')
if (results.description !== doc3.description) return done('document not updated')
return done()
})
})
})
})
it('lists the indexes', function (done) {
client.listIndexes(function (err, indexes) {
if (err) return done('error returned', err)
if (!Array.isArray(indexes)) return done('indexes is not an array')
var found = false
for (var idx = 0; idx < indexes.length && !found; idx++) {
if (indexes[idx].name === 'myindex') {
found = true
}
}
if (!found) {
return done('Expected index "myindex" was not found')
}
return done()
})
})
it('get an index', function (done) {
client.getIndex('myindex', function (err, index) {
if (err) return done('error returned', err)
if (!index) return done('index is null')
if (index.name !== 'myindex') return done('index has the wrong name')
return done()
})
})
it('searches with no result', function (done) {
client.search('myindex', { search: '1234' }, function (err, results) {
if (err) return done('error returned')
if (!results) return done('results is null')
if (!Array.isArray(results)) return done('results is not an array')
if (results.length !== 0) return done('results is not the right length')
return done()
})
})
it('looks up a document', function (done) {
client.lookup('myindex', 'document1', function (err, results) {
if (err) return done('error returned')
if (!results) return done('results is null')
if (results.id !== 'document1') return done('wrong results')
return done()
})
})
it('counts documents in an index', function (done) {
client.count('myindex', function (err, count) {
if (err) return done('error returned')
// if (count !== 1) return done('wrong results')
return done()
})
})
it('suggestions', function (done) {
client.suggest('myindex', { search: 'doc', suggesterName: 'sg' }, function (err, results) {
if (err) return done('error returned')
if (!results) return done('results is null')
if (!Array.isArray(results)) return done('results is not an array')
return done()
})
})
it('searches', function (done) {
client.search('myindex', { search: 'unique' }, function (err, results) {
if (err) return done('error returned')
if (!results) return done('results is null')
if (!Array.isArray(results)) return done('results is not an array')
if (results.length !== 1) return done('results is not the right length')
if (results[0].id !== 'document1') return done('doc 1 is not returned')
return done()
})
})
it('updates an index', function (done) {
var schema = {
name: 'myindex',
fields: [
{
name: 'id',
type: 'Edm.String',
searchable: false,
filterable: true,
retrievable: true,
sortable: true,
facetable: true,
key: true
},
{
name: 'description',
type: 'Edm.String',
searchable: true,
filterable: false,
retrievable: true,
sortable: false,
facetable: false,
key: false
},
{
name: 'category',
type: 'Edm.String',
searchable: true,
filterable: false,
retrievable: true,
sortable: false,
facetable: true,
key: false,
analyzer: 'phonetic_area_analyzer'
},
{
name: 'foo',
type: 'Edm.String',
searchable: true,
filterable: false,
retrievable: true,
sortable: false,
facetable: false,
key: false
}
],
'analyzers': [
{
'name': 'phonetic_area_analyzer',
'@odata.type': '#Microsoft.Azure.Search.CustomAnalyzer',
'tokenizer': 'area_standard',
'tokenFilters': ['lowercase', 'asciifolding', 'areas_phonetc']
}
],
'charFilters': [],
'tokenizers': [
{
'name': 'area_standard',
'@odata.type': '#Microsoft.Azure.Search.StandardTokenizerV2'
},
{
'name': 'area_keyword',
'@odata.type': '#Microsoft.Azure.Search.KeywordTokenizerV2'
}
],
'tokenFilters': [
{
'name': 'area_edge',
'@odata.type': '#Microsoft.Azure.Search.EdgeNGramTokenFilterV2',
'minGram': 2,
'maxGram': 50
},
{
'name': 'area_token_edge',
'@odata.type': '#Microsoft.Azure.Search.EdgeNGramTokenFilterV2',
'minGram': 2,
'maxGram': 20
},
{
'name': 'areas_phonetc',
'@odata.type': '#Microsoft.Azure.Search.PhoneticTokenFilter',
'encoder': 'doubleMetaphone'
}
],
suggesters: [
{
name: 'sg',
searchMode: 'analyzingInfixMatching',
sourceFields: ['description', 'id']
}
],
scoringProfiles: [],
defaultScoringProfile: null,
corsOptions: { allowedOrigins: ['*'] }
}
client.updateIndex('myindex', schema, function (err) {
if (err) return done('error returned ' + err.message)
return done()
})
})
it('creates a blob data source', function (done) {
var options = {
name: 'blob-datasource',
type: 'azureblob',
credentials: { connectionString: storageConnectionString },
container: { name: 'azuresearchtest', query: '' }
}
client.createDataSource(options, function (err, data) {
if (err) return done('error returned ' + err.message)
return done()
})
})
it('gets data source', function (done) {
client.getDataSource('blob-datasource', function (err, data) {
if (err) return done('error returned')
if (!data) return done('datasource is null')
return done()
})
})
it('updates a blob data source', function (done) {
var options = {
name: 'blob-datasource',
type: 'azureblob',
credentials: { connectionString: storageConnectionString },
container: { name: 'azuresearchtest', query: '' }
}
client.updateDataSource(options, function (err, data) {
if (err) return done('error returned ' + err.message)
return done()
})
})
it('creates a table data source', function (done) {
var options = {
name: 'table-datasource',
type: 'azuretable',
credentials: { connectionString: storageConnectionString },
container: { name: 'azuresearchtest', query: '' }
}
client.createDataSource(options, function (err, data) {
if (err) return done('error returned ' + err.message)
return done()
})
})
it('updates a table data source', function (done) {
var options = {
name: 'table-datasource',
type: 'azuretable',
credentials: { connectionString: storageConnectionString },
container: { name: 'azuresearchtest', query: '' }
}
client.updateDataSource(options, function (err, data) {
if (err) return done('error returned ' + err.message)
return done()
})
})
it('creates an indexer', function (done) {
var schema = {
name: 'myindexer',
description: 'Anything', // Optional. Anything you want, or null
dataSourceName: 'blob-datasource', // Required. The name of an existing data source
targetIndexName: 'myindex' // Required. The name of an existing index
}
client.createIndexer(schema, function (err) {
if (err) return done('error returned ' + err.message)
return done()
})
})
it('updates an indexer', function (done) {
var schema = {
name: 'myindexer',
description: 'Anything Different', // Optional. Anything you want, or null
dataSourceName: 'blob-datasource', // Required. The name of an existing data source
targetIndexName: 'myindex', // Required. The name of an existing index
schedule: { // Optional. All of the parameters below are required.
interval: 'PT15M', // The pattern for this is: 'P[nD][T[nH][nM]]'. Examples: PT15M for every 15 minutes, PT2H for every 2 hours.
startTime: '2016-06-01T00:00:00Z' // A UTC datetime when the indexer should start running.
}
}
client.updateIndexer('myindexer', schema, function (err) {
if (err) return done('error returned ' + err.message)
return done()
})
})
it('runs an indexer', function (done) {
client.runIndexer('myindexer', function (err, indexer) {
if (err) return done('error returned', err)
return done()
})
})
it('resets an indexer', function (done) {
client.resetIndexer('myindexer', function (err, indexer) {
if (err) return done('error returned', err)
return done()
})
})
it('deletes an indexer', function (done) {
client.deleteIndexer('myindexer', function (err, indexer) {
if (err) return done('error returned', err)
return done()
})
})
it('deletes a blob data source', function (done) {
client.deleteDataSource('blob-datasource', function (err, indexer) {
if (err) return done('error returned', err)
return done()
})
})
it('deletes a table data source', function (done) {
client.deleteDataSource('table-datasource', function (err, indexer) {
if (err) return done('error returned', err)
return done()
})
})
it('deletes an index', function (done) {
client.deleteIndex('myindex', function (err, index) {
if (err) return done('error returned', err)
return done()
})
})
it('creates a synonymmap', function (done) {
var schema = {
name: 'mysynonmap',
format: 'solr',
synonyms: 'a=>b\nb=>c'
}
client.createSynonymMap(schema, function (err, data) {
if (err) return done('error returned ' + err.message)
if (!data) return done('data is not defined')
if (data.name !== 'mysynonmap') return done('wrong synonym map name')
if (data.format !== 'solr') return done('wrong synonym map format')
if (data.synonyms.split('\n').length !== 2) return done('wrong synonym rows')
return done()
})
})
it('updates a synonymmap', function (done) {
var schema = {
name: 'mysynonmap',
format: 'solr',
synonyms: 'd=>e\ng=>h\nz=>x'
}
client.updateOrCreateSynonymMap('mysynonmap', schema, function (err, data) {
if (err) return done('error returned ' + err.message)
return done()
})
})
it('gets a synonymmap', function (done) {
client.getSynonymMap('mysynonmap', function (err, data) {
if (err) return done('error returned ' + err.message)
if (!data) return done('data is not defined')
if (data.name !== 'mysynonmap') return done('wrong synonym map name')
if (data.format !== 'solr') return done('wrong synonym map format')
if (data.synonyms.split('\n').length !== 3) return done('wrong synonym rows')
return done()
})
})
it('lists synonymmaps', function (done) {
client.listSynonymMaps(function (err, maps) {
if (err) return done('error returned', err)
if (!Array.isArray(maps)) return done('indexes is not an array')
var found = false
for (var idx = 0; idx < maps.length && !found; idx++) {
if (maps[idx].name === 'mysynonmap') {
found = true
}
}
if (!found) {
return done('Expected synonym map "mysynonmap" was not found')
}
return done()
})
})
it('deletes a synonymmap', function (done) {
client.deleteSynonymMap('mysynonmap', function (err) {
if (err) return done('error returned', err)
return done()
})
})
it('creates while updating a non existant synonymmap', function (done) {
var schema = {
name: 'mysynonmap2',
format: 'solr',
synonyms: 'd=>e\ng=>h\nz=>x'
}
client.updateOrCreateSynonymMap(schema.name, schema, function (err, data) {
if (err) return done('error returned ' + err.message)
if (!data) return done('data is not defined')
if (data.name !== schema.name) return done('wrong synonym map name')
if (data.format !== 'solr') return done('wrong synonym map format')
if (data.synonyms.split('\n').length !== 3) return done('wrong synonym rows')
return done()
})
})
it('deletes a synonymmap - again', function (done) {
client.deleteSynonymMap('mysynonmap2', function (err, index) {
if (err) return done('error returned', err)
return done()
})
})
it('creates a skillset', function (done) {
var schema = {
name: 'myskillset',
description: 'My skillset',
skills: [{
'@odata.type': '#Microsoft.Skills.Text.SentimentSkill',
inputs: [{
name: 'text',
source: '/document/content'
}],
outputs: [{
name: 'score',
targetName: 'myScore'
}]
}]
}
client.createSkillset(schema, function (err, data) {
if (err) return done('error returned ' + err.message)
if (!data) return done('data is not defined')
if (data.name !== 'myskillset') return done('wrong skillset name')
if (data.description !== 'My skillset') return done('wrong skillset description')
if (data.skills.length !== 1) return done('wrong number of skills')
return done()
})
})
it('updates a skillset', function (done) {
var schema = {
name: 'myskillset',
description: 'My updated skillset',
skills: [{
'@odata.type': '#Microsoft.Skills.Text.SentimentSkill',
inputs: [{
name: 'text',
source: '/document/content'
}],
outputs: [{
name: 'score',
targetName: 'myScore'
}]
}]
}
client.updateOrCreateSkillset('myskillset', schema, function (err, data) {
if (err) return done('error returned ' + err.message)
return done()
})
})
it('gets a skillset', function (done) {
client.getSkillset('myskillset', function (err, data) {
if (err) return done('error returned ' + err.message)
if (data.name !== 'myskillset') return done('wrong skillset name')
if (data.description !== 'My updated skillset') return done('wrong skillset description')
if (data.skills.length !== 1) return done('wrong number of skills')
return done()
})
})
it('lists skillsets', function (done) {
client.listSkillsets(function (err, skillsets) {
if (err) return done('error returned', err)
if (!Array.isArray(skillsets)) return done('indexes is not an array')
var found = false
for (var idx = 0; idx < skillsets.length && !found; idx++) {
if (skillsets[idx].name === 'myskillset') {
found = true
}
}
if (!found) {
return done('Expected skillset "myskillset" was not found')
}
return done()
})
})
it('deletes a skillset', function (done) {
client.deleteSkillset('myskillset', function (err) {
if (err) return done('error returned', err)
return done()
})
})
it('handles azure errors', function (done) {
client.getSynonymMap('nonexistant', function (err, data) {
if (!err) return done('expected error is missing')
if (err && !err.code) return done('error code not provided')
if (err && !err.message) return done('error message not provided')
return done()
})
})
it('handles http errors', function (done) {
var badClientConfiguration = {
url: 'https://no.no.no.nothing.here.zz',
key: clientConfiguration.key
}
var badClient = clientFactory(badClientConfiguration)
badClient.getSynonymMap('nonexistant', function (err, data) {
if (!err) return done('expected error is missing')
if (err && !err.code) return done('error code not provided')
if (err && !err.message) return done('error message not provided')
return done()
})
})
it('handles plain text errors', function (done) {
client.count('nosuchindex', function (err, count) {
if (!err) return done('expected error is missing')
return done()
})
})
// This is an edge case
// happens when accessing an API endppoint from an API version which is too old
it('handles error status codes with a response body', function (done) {
var oldClientConfiguration = {
url: clientConfiguration.url,
key: clientConfiguration.key,
version: '2016-09-01'
}
var oldClient = clientFactory(oldClientConfiguration)
oldClient.getSynonymMap('mysynonmap', function (err, data) {
if (!err) return done('expected error is missing')
if (err && !err.code) return done('error code not provided')
return done()
})
})
})