forked from azure-contrib/node-azure-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
286 lines (237 loc) · 8.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
// to test, first create a search service in azure, and set the url and key here.
var client = require('./index')({
url: "https://xxx.search.windows.net",
key: "yyy"
});
describe("search service", function(){
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,
suggestions: false,
key: true },
{ name: 'description',
type: 'Edm.String',
searchable: true,
filterable: false,
retrievable: true,
sortable: false,
facetable: false,
suggestions: true,
key: false } ],
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 !== 2) 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("indexes a document", function(done){
var doc1 = {
"id": "document1",
"description": "this is the description of my unique document"
}
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("uploads a document", function(done){
var doc4 = {
"id": "document4",
"description": "this is the description of my document"
};
client.uploadDocuments("myindex", [doc4], function(err, data){
if (err) return done("error returned");
if (!data) return done("data is null");
//update description field
doc4.description = "updated description";
client.uploadDocuments("myindex", [doc4], function(err, data){
if (err) return done("error returned");
if (!data) return done("data is null");
//ensure changes were saved
client.lookup("myindex", "document4", function(err, results){
if (err) return done("error returned");
if (!results) return done("results is null");
if (results.description !== doc4.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");
if (indexes[0].name !== "myindex") return done("entry 0 has the wrong name");
return done();
});
});
it("get an indexes", 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"}, 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("searches with full results", function(done){
client.search("myindex", {search:"unique", $count:true, fullResults: true}, function(err, results){
if (err) return done("error returned");
if (!results) return done("results is null");
if (!results["@odata.count"]) return done("results[\"@odata.count\"] doesn't exists");
if (!Array.isArray(results.value)) return done("results.value is not an array");
if (results.value.length !== 1) return done("results.value is not the right length");
if (results.value[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,
suggestions: false,
key: true },
{ name: 'description',
type: 'Edm.String',
searchable: true,
filterable: false,
retrievable: true,
sortable: false,
facetable: false,
suggestions: true,
key: false },
{ name: 'foo',
type: 'Edm.String',
searchable: true,
filterable: false,
retrievable: true,
sortable: false,
facetable: false,
suggestions: true,
key: false } ],
scoringProfiles: [],
defaultScoringProfile: null,
corsOptions: {allowedOrigins: ["*"]}
};
client.updateIndex("myindex", schema, function(err){
if (err) return done("error returned " + err.message);
return done();
});
});
it("deletes an index", function(done){
client.deleteIndex("myindex", function(err, index){
if (err) return done("error returned", err);
return done();
});
});
});