UNPKG

15.4 kBJavaScriptView Raw
1'use strict';
2
3module.exports = function (dbType, context) {
4
5 describe(dbType + ': ltgt', function () {
6 it('does gt queries', function () {
7 var db = context.db;
8 var index = {
9 "index": {
10 "fields": ["foo"]
11 },
12 "name": "foo-index",
13 "type": "json"
14 };
15
16 return db.createIndex(index).then(function () {
17 return db.bulkDocs([
18 { _id: '1', foo: 'eyo'},
19 { _id: '2', foo: 'ebb'},
20 { _id: '3', foo: 'eba'},
21 { _id: '4', foo: 'abo'}
22 ]);
23 }).then(function () {
24 return db.find({
25 selector: {foo: {"$gt": "eb"}},
26 fields: ["_id", "foo"],
27 sort: [{foo: "asc"}]
28 });
29 }).then(function (resp) {
30 resp.should.deep.equal({
31 docs: [
32 {_id: '3', foo: 'eba'},
33 {_id: '2', foo: 'ebb'},
34 {_id: '1', foo: 'eyo'}
35 ]
36 });
37 });
38 });
39
40 it('does lt queries', function () {
41 var db = context.db;
42 var index = {
43 "index": {
44 "fields": ["foo"]
45 },
46 "name": "foo-index",
47 "type": "json"
48 };
49
50 return db.createIndex(index).then(function () {
51 return db.bulkDocs([
52 { _id: '1', foo: 'eyo'},
53 { _id: '2', foo: 'ebb'},
54 { _id: '3', foo: 'eba'},
55 { _id: '4', foo: 'abo'}
56 ]);
57 }).then(function () {
58 return db.find({
59 selector: {foo: {"$lt": "eb"}},
60 fields: ["_id", "foo"]
61 });
62 }).then(function (resp) {
63 resp.should.deep.equal({
64 docs: [
65 {_id: '4', foo: 'abo'}
66 ]
67 });
68 });
69 });
70
71 it('#20 - lt queries with sort descending return correct number of docs', function () {
72 var db = context.db;
73 var index = {
74 "index": {
75 "fields": ["debut"]
76 },
77 "name": "foo-index",
78 "type": "json"
79 };
80
81 return db.createIndex(index).then(function () {
82 return db.bulkDocs([
83 { _id: '1', debut: 1983},
84 { _id: '2', debut: 1981},
85 { _id: '3', debut: 1989},
86 { _id: '4', debut: 1990}
87 ]);
88 }).then(function () {
89 return db.find({
90 selector: {debut: {$lt: 1990}},
91 sort: [{debut: 'desc'}]
92 });
93 }).then(function (resp) {
94 var docs = resp.docs.map(function (x) { delete x._rev; return x; });
95 docs.should.deep.equal([
96 { _id: '3', debut: 1989},
97 { _id: '1', debut: 1983},
98 { _id: '2', debut: 1981}
99 ]);
100 });
101 });
102 // ltge - {include_docs: true, reduce: false, descending: true, startkey: 1990}
103 // lt no sort {include_docs: true, reduce: false, endkey: 1990, inclusive_end: false}
104 // lt sort {include_docs: true, reduce: false, descending: true,
105 // startkey: 1990, inclusive_start: false}
106
107 it('does lte queries', function () {
108 var db = context.db;
109 var index = {
110 "index": {
111 "fields": ["foo"]
112 },
113 "name": "foo-index",
114 "type": "json"
115 };
116
117 return db.createIndex(index).then(function () {
118 return db.bulkDocs([
119 { _id: '1', foo: 'eyo'},
120 { _id: '2', foo: 'ebb'},
121 { _id: '3', foo: 'eba'},
122 { _id: '4', foo: 'abo'}
123 ]);
124 }).then(function () {
125 return db.find({
126 selector: {foo: {"$lte": "eba"}},
127 fields: ["_id", "foo"],
128 sort: [{foo: "asc"}]
129 });
130 }).then(function (resp) {
131 resp.should.deep.equal({
132 docs: [
133 {_id: '4', foo: 'abo'},
134 {_id: '3', foo: 'eba'},
135 ]
136 });
137 });
138 });
139
140 it('#41 another complex multifield query', function () {
141 var db = context.db;
142 var index = {
143 "index": {
144 "fields": ["datetime"]
145 }
146 };
147
148 return db.createIndex(index).then(function () {
149 return db.bulkDocs([
150 {_id: '1',
151 datetime: 1434054640000,
152 glucoseType: 'fasting',
153 patientId: 1
154 },
155 {_id: '2',
156 datetime: 1434054650000,
157 glucoseType: 'fasting',
158 patientId: 1
159 },
160 {_id: '3',
161 datetime: 1434054660000,
162 glucoseType: 'fasting',
163 patientId: 1
164 },
165 {_id: '4',
166 datetime: 1434054670000,
167 glucoseType: 'fasting',
168 patientId: 1
169 }
170 ]);
171 }).then(function () {
172 return db.find({
173 selector: {
174 datetime: { "$lt": 1434054660000 },
175 glucoseType: { "$eq": 'fasting' },
176 patientId: { "$eq": 1 }
177 }
178 });
179 }).then(function (res) {
180 var docs = res.docs.map(function (x) { delete x._rev; return x; });
181 docs.should.deep.equal([
182 {
183 "_id": "1",
184 "datetime": 1434054640000,
185 "glucoseType": "fasting",
186 "patientId": 1
187 },
188 {
189 "_id": "2",
190 "datetime": 1434054650000,
191 "glucoseType": "fasting",
192 "patientId": 1
193 }
194 ]);
195 });
196 });
197
198 it('does gt queries, desc sort', function () {
199 var db = context.db;
200 var index = {
201 "index": {
202 "fields": ["foo"]
203 },
204 "name": "foo-index",
205 "type": "json"
206 };
207
208 return db.createIndex(index).then(function () {
209 return db.bulkDocs([
210 { _id: '1', foo: 'eyo'},
211 { _id: '2', foo: 'ebb'},
212 { _id: '3', foo: 'eba'},
213 { _id: '4', foo: 'abo'}
214 ]);
215 }).then(function () {
216 return db.find({
217 selector: {foo: {"$gt": "eb"}},
218 fields: ["_id", "foo"],
219 sort: [{foo: "desc"}]
220 });
221 }).then(function (resp) {
222 resp.should.deep.equal({
223 docs: [
224 {_id: '1', foo: 'eyo'},
225 {_id: '2', foo: 'ebb'},
226 {_id: '3', foo: 'eba'}
227 ]
228 });
229 });
230 });
231
232 it('#38 $gt with dates', function () {
233 var db = context.db;
234
235 var startDate = "2015-05-25T00:00:00.000Z";
236 var endDate = "2015-05-26T00:00:00.000Z";
237
238 return db.createIndex({
239 index: {
240 fields: ['docType', 'logDate']
241 }
242 }).then(function () {
243 return db.bulkDocs([
244 {_id: '1', docType: 'log', logDate: "2015-05-24T00:00:00.000Z"},
245 {_id: '2', docType: 'log', logDate: "2015-05-25T00:00:00.000Z"},
246 {_id: '3', docType: 'log', logDate: "2015-05-26T00:00:00.000Z"},
247 {_id: '4', docType: 'log', logDate: "2015-05-27T00:00:00.000Z"}
248 ]);
249 }).then(function() {
250 return db.find({
251 selector: {docType: 'log'}
252 }).then(function (result) {
253 result.docs.map(function (x) { delete x._rev; return x; }).should.deep.equal([
254 {
255 "_id": "1",
256 "docType": "log",
257 "logDate": "2015-05-24T00:00:00.000Z"
258 },
259 {
260 "_id": "2",
261 "docType": "log",
262 "logDate": "2015-05-25T00:00:00.000Z"
263 },
264 {
265 "_id": "3",
266 "docType": "log",
267 "logDate": "2015-05-26T00:00:00.000Z"
268 },
269 {
270 "_id": "4",
271 "docType": "log",
272 "logDate": "2015-05-27T00:00:00.000Z"
273 }
274 ], 'test 1');
275 });
276 }).then(function () {
277 return db.find({
278 selector: {docType: 'log', logDate: {$gt: startDate}}
279 }).then(function (result) {
280 result.docs.map(function (x) { delete x._rev; return x; }).should.deep.equal([
281 {
282 "_id": "3",
283 "docType": "log",
284 "logDate": "2015-05-26T00:00:00.000Z"
285 },
286 {
287 "_id": "4",
288 "docType": "log",
289 "logDate": "2015-05-27T00:00:00.000Z"
290 }
291 ], 'test 2');
292 });
293 }).then(function () {
294 return db.find({
295 selector: {docType: 'log', logDate: {$gte: startDate}}
296 }).then(function (result) {
297 result.docs.map(function (x) { delete x._rev; return x; }).should.deep.equal([
298 {
299 "_id": "2",
300 "docType": "log",
301 "logDate": "2015-05-25T00:00:00.000Z"
302 },
303 {
304 "_id": "3",
305 "docType": "log",
306 "logDate": "2015-05-26T00:00:00.000Z"
307 },
308 {
309 "_id": "4",
310 "docType": "log",
311 "logDate": "2015-05-27T00:00:00.000Z"
312 }
313 ], 'test 3');
314 });
315 }).then(function () {
316 return db.find({
317 selector: {
318 docType: 'log',
319 logDate: {$gte: startDate, $lte: endDate}
320 }
321 }).then(function (result) {
322 result.docs.map(function (x) { delete x._rev; return x; }).should.deep.equal([
323 {
324 "_id": "2",
325 "docType": "log",
326 "logDate": "2015-05-25T00:00:00.000Z"
327 },
328 {
329 "_id": "3",
330 "docType": "log",
331 "logDate": "2015-05-26T00:00:00.000Z"
332 }
333 ], 'test 4');
334 });
335 });
336 });
337
338 it('bunch of equivalent queries', function () {
339 var db = context.db;
340
341 function normalize(res) {
342 return res.docs.map(function getId(x) {
343 return x._id;
344 }).sort();
345 }
346
347 return db.createIndex({
348 index: {
349 fields: ['foo']
350 }
351 }).then(function () {
352 return db.bulkDocs([
353 {_id: '1', foo: 1},
354 {_id: '2', foo: 2},
355 {_id: '3', foo: 3},
356 {_id: '4', foo: 4}
357 ]);
358 }).then(function() {
359 return db.find({
360 selector: { $and: [{foo: {$gt: 2}}, {foo: {$gte: 2}}]}
361 });
362 }).then(function (res) {
363 normalize(res).should.deep.equal(['3', '4']);
364 return db.find({
365 selector: { $and: [{foo: {$eq: 2}}, {foo: {$gte: 2}}]}
366 });
367 }).then(function (res) {
368 normalize(res).should.deep.equal(['2']);
369 return db.find({
370 selector: { $and: [{foo: {$eq: 2}}, {foo: {$lte: 2}}]}
371 });
372 }).then(function (res) {
373 normalize(res).should.deep.equal(['2']);
374 return db.find({
375 selector: { $and: [{foo: {$lte: 3}}, {foo: {$lt: 3}}]}
376 });
377 }).then(function (res) {
378 normalize(res).should.deep.equal(['1', '2']);
379 return db.find({
380 selector: { $and: [{foo: {$eq: 4}}, {foo: {$gte: 2}}]}
381 });
382 }).then(function (res) {
383 normalize(res).should.deep.equal(['4']);
384 return db.find({
385 selector: { $and: [{foo: {$lte: 3}}, {foo: {$eq: 1}}]}
386 });
387 }).then(function (res) {
388 normalize(res).should.deep.equal(['1']);
389 return db.find({
390 selector: { $and: [{foo: {$eq: 4}}, {foo: {$gt: 2}}]}
391 });
392 }).then(function (res) {
393 normalize(res).should.deep.equal(['4']);
394 return db.find({
395 selector: { $and: [{foo: {$lt: 3}}, {foo: {$eq: 1}}]}
396 });
397 }).then(function (res) {
398 normalize(res).should.deep.equal(['1']);
399 });
400 });
401
402 it('bunch of equivalent queries 2', function () {
403 var db = context.db;
404
405 function normalize(res) {
406 return res.docs.map(function getId(x) {
407 return x._id;
408 }).sort();
409 }
410
411 return db.createIndex({
412 index: {
413 fields: ['foo']
414 }
415 }).then(function () {
416 return db.bulkDocs([
417 {_id: '1', foo: 1},
418 {_id: '2', foo: 2},
419 {_id: '3', foo: 3},
420 {_id: '4', foo: 4}
421 ]);
422 }).then(function() {
423 return db.find({
424 selector: { $and: [{foo: {$gt: 2}}, {foo: {$gte: 1}}]}
425 });
426 }).then(function (res) {
427 normalize(res).should.deep.equal(['3', '4']);
428 return db.find({
429 selector: { $and: [{foo: {$lt: 3}}, {foo: {$lte: 4}}]}
430 });
431 }).then(function (res) {
432 normalize(res).should.deep.equal(['1', '2']);
433 return db.find({
434 selector: { $and: [{foo: {$gt: 2}}, {foo: {$gte: 3}}]}
435 });
436 }).then(function (res) {
437 normalize(res).should.deep.equal(['3', '4']);
438 return db.find({
439 selector: { $and: [{foo: {$lt: 3}}, {foo: {$lte: 1}}]}
440 });
441 }).then(function (res) {
442 normalize(res).should.deep.equal(['1']);
443 return db.find({
444 selector: { $and: [{foo: {$gte: 2}}, {foo: {$gte: 1}}]}
445 });
446 }).then(function (res) {
447 normalize(res).should.deep.equal(['2', '3', '4']);
448 return db.find({
449 selector: { $and: [{foo: {$lte: 3}}, {foo: {$lte: 4}}]}
450 });
451 }).then(function (res) {
452 normalize(res).should.deep.equal(['1', '2', '3']);
453 return db.find({
454 selector: { $and: [{foo: {$gt: 2}}, {foo: {$gt: 3}}]}
455 });
456 }).then(function (res) {
457 normalize(res).should.deep.equal(['4']);
458 return db.find({
459 selector: { $and: [{foo: {$lt: 3}}, {foo: {$lt: 2}}]}
460 });
461 }).then(function (res) {
462 normalize(res).should.deep.equal(['1']);
463 });
464 });
465
466 it('bunch of equivalent queries 3', function () {
467 var db = context.db;
468
469 function normalize(res) {
470 return res.docs.map(function getId(x) {
471 return x._id;
472 }).sort();
473 }
474
475 return db.createIndex({
476 index: {
477 fields: ['foo']
478 }
479 }).then(function () {
480 return db.bulkDocs([
481 {_id: '1', foo: 1},
482 {_id: '2', foo: 2},
483 {_id: '3', foo: 3},
484 {_id: '4', foo: 4}
485 ]);
486 }).then(function() {
487 return db.find({
488 selector: { $and: [{foo: {$gte: 1}}, {foo: {$gt: 2}}]}
489 });
490 }).then(function (res) {
491 normalize(res).should.deep.equal(['3', '4']);
492 return db.find({
493 selector: { $and: [{foo: {$lte: 4}}, {foo: {$lt: 3}}]}
494 });
495 }).then(function (res) {
496 normalize(res).should.deep.equal(['1', '2']);
497 return db.find({
498 selector: { $and: [{foo: {$gte: 3}}, {foo: {$gt: 2}}]}
499 });
500 }).then(function (res) {
501 normalize(res).should.deep.equal(['3', '4']);
502 return db.find({
503 selector: { $and: [{foo: {$lte: 1}}, {foo: {$lt: 3}}]}
504 });
505 }).then(function (res) {
506 normalize(res).should.deep.equal(['1']);
507 return db.find({
508 selector: { $and: [{foo: {$gte: 1}}, {foo: {$gte: 2}}]}
509 });
510 }).then(function (res) {
511 normalize(res).should.deep.equal(['2', '3', '4']);
512 return db.find({
513 selector: { $and: [{foo: {$lte: 4}}, {foo: {$lte: 3}}]}
514 });
515 }).then(function (res) {
516 normalize(res).should.deep.equal(['1', '2', '3']);
517 return db.find({
518 selector: { $and: [{foo: {$gt: 3}}, {foo: {$gt: 2}}]}
519 });
520 }).then(function (res) {
521 normalize(res).should.deep.equal(['4']);
522 return db.find({
523 selector: { $and: [{foo: {$lt: 2}}, {foo: {$lt: 3}}]}
524 });
525 }).then(function (res) {
526 normalize(res).should.deep.equal(['1']);
527 });
528 });
529
530 });
531};