forked from pouchdb/pouchdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.views.js
684 lines (658 loc) · 18.3 KB
/
test.views.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
/*jshint expr:true */
'use strict';
var adapters = [
['local', 'http'],
];
adapters.forEach(function (adapters) {
describe('test.views.js-' + adapters[0] + '-' + adapters[1], function () {
var dbs = {};
beforeEach(function (done) {
dbs.name = testUtils.adapterUrl(adapters[0], 'testdb');
dbs.remote = testUtils.adapterUrl(adapters[1], 'test_repl_remote');
testUtils.cleanup([dbs.name, dbs.remote], done);
});
after(function (done) {
testUtils.cleanup([dbs.name, dbs.remote], done);
});
it('Test basic view', function (done) {
var db = new PouchDB(dbs.name);
db.bulkDocs({
docs: [
{ foo: 'bar' },
{
_id: 'volatile',
foo: 'baz'
}
]
}, {}, function () {
var queryFun = {
map: function (doc) {
emit(doc.foo, doc);
}
};
db.get('volatile', function (_, doc) {
db.remove(doc, function (_, resp) {
db.query(queryFun, {
include_docs: true,
reduce: false
}, function (_, res) {
res.rows.should.have.length(1, 'Dont include deleted documents');
res.total_rows.should.equal(1, 'Include total_rows property.');
res.rows.forEach(function (x, i) {
x.id.should.exist;
x.key.should.exist;
x.value._rev.should.exist;
x.doc._rev.should.exist;
});
done();
});
});
});
});
});
it('Test passing just a function', function (done) {
var db = new PouchDB(dbs.name);
db.bulkDocs({
docs: [
{ foo: 'bar' },
{
_id: 'volatile',
foo: 'baz'
}
]
}, {}, function () {
var queryFun = function (doc) {
emit(doc.foo, doc);
};
db.get('volatile', function (_, doc) {
db.remove(doc, function (_, resp) {
db.query(queryFun, {
include_docs: true,
reduce: false
}, function (_, res) {
res.rows.should.have.length(1, 'Dont include deleted documents');
res.rows.forEach(function (x, i) {
x.id.should.exist;
x.key.should.exist;
x.value._rev.should.exist;
x.doc._rev.should.exist;
});
done();
});
});
});
});
});
it('Test opts.startkey/opts.endkey', function (done) {
var db = new PouchDB(dbs.name);
db.bulkDocs({
docs: [
{ key: 'key1' },
{ key: 'key2' },
{ key: 'key3' },
{ key: 'key4' },
{ key: 'key5' }
]
}, {}, function () {
var queryFun = {
map: function (doc) {
emit(doc.key, doc);
}
};
db.query(queryFun, {
reduce: false,
startkey: 'key2'
}, function (_, res) {
res.rows.should.have.length(4, 'Startkey is inclusive');
db.query(queryFun, {
reduce: false,
endkey: 'key3'
}, function (_, res) {
res.rows.should.have.length(3, 'Endkey is inclusive');
db.query(queryFun, {
reduce: false,
startkey: 'key2',
endkey: 'key3'
}, function (_, res) {
res.rows.should.have.length(2, 'Startkey and endkey together');
db.query(queryFun, {
reduce: false,
startkey: 'key4',
endkey: 'key4'
}, function (_, res) {
res.rows.should.have.length(1, 'Startkey=endkey');
done();
});
});
});
});
});
});
it('Test opts.key', function (done) {
var db = new PouchDB(dbs.name);
db.bulkDocs({
docs: [
{ key: 'key1' },
{ key: 'key2' },
{ key: 'key3' },
{ key: 'key3' }
]
}, {}, function () {
var queryFun = {
map: function (doc) {
emit(doc.key, doc);
}
};
db.query(queryFun, {
reduce: false,
key: 'key2'
}, function (_, res) {
res.rows.should.have.length(1, 'Doc with key');
db.query(queryFun, {
reduce: false,
key: 'key3'
}, function (_, res) {
res.rows.should.have.length(2, 'Multiple docs with key');
done();
});
});
});
});
it.skip('Test basic view collation', function (done) {
var values = [];
// special values sort before all other types
values.push(null);
values.push(false);
values.push(true);
// then numbers
values.push(1);
values.push(2);
values.push(3);
values.push(4);
// then text, case sensitive
// currently chrome uses ascii ordering and so wont handle
// capitals properly
values.push('a');
//values.push("A");
values.push('aa');
values.push('b');
//values.push("B");
values.push('ba');
values.push('bb');
// then arrays. compared element by element until different.
// Longer arrays sort after their prefixes
values.push(['a']);
values.push(['b']);
values.push([
'b',
'c'
]);
values.push([
'b',
'c',
'a'
]);
values.push([
'b',
'd'
]);
values.push([
'b',
'd',
'e'
]);
// then object, compares each key value in the list until different.
// larger objects sort after their subset objects.
values.push({ a: 1 });
values.push({ a: 2 });
values.push({ b: 1 });
values.push({ b: 2 });
values.push({
b: 2,
a: 1
});
// Member order does matter for collation.
// CouchDB preserves member order
// but doesn't require that clients will.
// (this test might fail if used with a js engine
// that doesn't preserve order)
values.push({
b: 2,
c: 2
});
var db = new PouchDB(dbs.name);
var docs = values.map(function (x, i) {
return {
_id: i.toString(),
foo: x
};
});
db.bulkDocs({ docs: docs }, {}, function (err) {
var queryFun = {
map: function (doc) {
emit(doc.foo, null);
}
};
if (err) {
done(err);
}
db.query(queryFun, { reduce: false }, function (err, res) {
if (err) {
done(err);
}
res.rows.forEach(function (x, i) {
x.key.should.deep.equal(values[i], 'keys collate');
});
db.query(queryFun, {
descending: true,
reduce: false
}, function (err, res) {
if (err) {
done(err);
}
res.rows.forEach(function (x, i) {
x.key.should.deep
.equal(values[values.length - 1 - i],
'keys collate descending');
});
done();
});
});
});
});
it('Test joins', function (done) {
var db = new PouchDB(dbs.name);
db.bulkDocs({
docs: [
{
_id: 'mydoc',
foo: 'bar'
},
{ doc_id: 'mydoc' }
]
}, {}, function () {
var queryFun = {
map: function (doc) {
if (doc.doc_id) {
emit(doc._id, { _id: doc.doc_id });
}
}
};
db.query(queryFun, {
include_docs: true,
reduce: false
}, function (_, res) {
res.rows[0].doc.should.exist;
res.rows[0].doc._id.should.equal('mydoc', 'mydoc included');
done();
});
});
});
it('No reduce function', function (done) {
var db = new PouchDB(dbs.name);
db.post({ foo: 'bar' }, function (err, res) {
var queryFun = {
map: function (doc) {
emit('key', 'val');
}
};
db.query(queryFun, function (err, res) {
done();
});
});
});
it('Built in _sum reduce function', function (done) {
var db = new PouchDB(dbs.name);
db.bulkDocs({
docs: [
{ val: 'bar' },
{ val: 'bar' },
{ val: 'baz' }
]
}, null, function () {
var queryFun = {
map: function (doc) {
emit(doc.val, 1);
},
reduce: '_sum'
};
db.query(queryFun, {
reduce: true,
group_level: 999
}, function (err, res) {
res.rows.should.have.length(2);
res.rows[0].value.should.equal(2);
res.rows[1].value.should.equal(1);
done();
});
});
});
it('Built in _count reduce function', function (done) {
var db = new PouchDB(dbs.name);
db.bulkDocs({
docs: [
{ val: 'bar' },
{ val: 'bar' },
{ val: 'baz' }
]
}, null, function () {
var queryFun = {
map: function (doc) {
emit(doc.val, doc.val);
},
reduce: '_count'
};
db.query(queryFun, {
reduce: true,
group_level: 999
}, function (err, res) {
res.rows.should.have.length(2);
res.rows[0].value.should.equal(2);
res.rows[1].value.should.equal(1);
done();
});
});
});
it('Built in _stats reduce function', function (done) {
var db = new PouchDB(dbs.name);
db.bulkDocs({
docs: [
{ val: 'bar' },
{ val: 'bar' },
{ val: 'baz' }
]
}, null, function () {
var queryFun = {
map: function (doc) {
emit(doc.val, 1);
},
reduce: '_stats'
};
db.query(queryFun, {
reduce: true,
group_level: 999
}, function (err, res) {
var stats = res.rows[0].value;
stats.sum.should.equal(2);
stats.count.should.equal(2);
stats.min.should.equal(1);
stats.max.should.equal(1);
stats.sumsqr.should.equal(2);
done();
});
});
});
it('No reduce function, passing just a function', function (done) {
var db = new PouchDB(dbs.name);
db.post({ foo: 'bar' }, function (err, res) {
var queryFun = function (doc) {
emit('key', 'val');
};
db.query(queryFun, function (err, res) {
done();
});
});
});
it('Views should include _conflicts', function (done) {
var doc1 = {
_id: '1',
foo: 'bar'
};
var doc2 = {
_id: '1',
foo: 'baz'
};
var queryFun = function (doc) {
emit(doc._id, !!doc._conflicts);
};
var db = new PouchDB(dbs.name);
var remote = new PouchDB(dbs.remote);
db.post(doc1, function (err, res) {
remote.post(doc2, function (err, res) {
db.replicate.from(remote, function (err, res) {
db.get(doc1._id, { conflicts: true }, function (err, res) {
res._conflicts.should.exist;
db.query(queryFun, function (err, res) {
res.rows[0].value.should.exist;
done();
});
});
});
});
});
});
it('Map only documents with _conflicts (#1000)', function (done) {
var docs1 = [
{
_id: '1',
foo: 'bar'
},
{
_id: '2',
name: 'two'
}
];
var doc2 = {
_id: '1',
foo: 'baz'
};
var queryFun = function (doc) {
if (doc._conflicts) {
emit(doc._id, doc._conflicts);
}
};
var db = new PouchDB(dbs.name);
var remote = new PouchDB(dbs.remote);
db.bulkDocs({ docs: docs1 }, function (err, res) {
var revId1 = res[0].rev;
remote.post(doc2, function (err, res) {
var revId2 = res.rev;
db.replicate.from(remote, function (err, res) {
db.get(docs1[0]._id, { conflicts: true }, function (err, res) {
var winner = res._rev;
var looser = winner === revId1 ? revId2 : revId1;
res._conflicts.should.exist;
db.query(queryFun, function (err, res) {
res.rows.should.have.length(1, 'One doc with conflicts');
res.rows[0].key.should
.equal('1', 'Correct document with conflicts.');
res.rows[0].value.should.deep
.equal([looser], 'Correct conflicts included.');
done();
});
});
});
});
});
});
it('Test view querying with limit option', function (done) {
var db = new PouchDB(dbs.name);
db.bulkDocs({
docs: [
{ foo: 'bar' },
{ foo: 'bar' },
{ foo: 'baz' }
]
}, null, function () {
db.query(function (doc) {
if (doc.foo === 'bar') {
emit(doc.foo);
}
}, { limit: 1 }, function (err, res) {
res.total_rows.should.equal(2, 'Correctly returns total rows');
res.rows.should.have.length(1, 'Correctly limits returned rows');
done();
});
});
});
it('Query non existing view returns error', function (done) {
var db = new PouchDB(dbs.name);
var doc = {
_id: '_design/barbar',
views:
{ scores:
{ map: 'function (doc) { if (doc.score) ' +
'{ emit(null, doc.score); } }' } }
};
db.post(doc, function (err, info) {
db.query('barbar/dontExist', { key: 'bar' }, function (err, res) {
if (!err.name) {
err.name = err.error;
err.message = err.reason;
}
err.name.should.be.a('string');
err.message.should.be.a('string');
done();
});
});
});
it('Special document member _doc_id_rev', function (done) {
var db = new PouchDB(dbs.name);
db.bulkDocs({ docs: [{ foo: 'bar' }] }, null, function () {
db.query(function (doc) {
if (doc.foo === 'bar') {
emit(doc.foo);
}
}, { include_docs: true }, function (err, res) {
should.not.exist(res.rows[0].doc._doc_id_rev);
done();
});
});
});
it('If reduce function returns 0', function (done) {
var db = new PouchDB(dbs.name);
db.bulkDocs({ docs: [{ foo: 'bar' }] }, null, function () {
db.query({
map: function (doc) {
emit(doc.foo);
},
reduce: function (key, values, rereduce) {
return 0;
}
}, function (err, data) {
should.not.equal(data.rows[0].value, null, 'value is null');
done();
});
});
});
it('Testing skip with a view', function (done) {
var db = new PouchDB(dbs.name);
db.bulkDocs({
docs: [
{ foo: 'bar' },
{ foo: 'baz' },
{ foo: 'baf' }
]
}, null, function () {
db.query(function (doc) {
emit(doc.foo, null);
}, { skip: 1 }, function (err, data) {
should.not.exist(err);
data.rows.should.have.length(2);
done();
});
});
});
it('Testing skip with allDocs', function (done) {
var db = new PouchDB(dbs.name);
db.bulkDocs({
docs: [
{ foo: 'bar' },
{ foo: 'baz' },
{ foo: 'baf' }
]
}, null, function () {
db.allDocs({ skip: 1 }, function (err, data) {
should.not.exist(err);
data.rows.should.have.length(2);
done();
});
});
});
it('Map documents on 0/null/undefined/empty string', function (done) {
var db = new PouchDB(dbs.name);
var docs = [
{
_id: 'doc0',
num: 0
},
{
_id: 'doc1',
num: 1
},
{ _id: 'doc2' },
{
_id: 'doc3',
num: null
},
{
_id: 'doc4',
num: ''
}
];
db.bulkDocs({ docs: docs }, function (err) {
var mapFunction = function (doc) {
emit(doc.num, null);
};
db.query(mapFunction, {
key: 0,
include_docs: true
}, function (err, data) {
data.rows.should.have.length(1);
data.rows[0].doc._id.should.equal('doc0');
});
db.query(mapFunction, {
key: null,
include_docs: true
}, function (err, data) {
data.rows.should.have.length(2);
data.rows[0].doc._id.should.equal('doc2');
data.rows[1].doc._id.should.equal('doc3');
});
db.query(mapFunction, {
key: '',
include_docs: true
}, function (err, data) {
data.rows.should.have.length(1);
data.rows[0].doc._id.should.equal('doc4');
});
db.query(mapFunction, {
key: undefined,
include_docs: true
}, function (err, data) {
data.rows.should.have.length(5);
// everything
done();
});
});
});
if (typeof process !== 'undefined' && !process.browser) {
var fs = require('fs');
it("destroy using prototype", function () {
return new PouchDB(dbs.name + 1).then(function (db) {
var doc = {
_id: '_design/barbar',
views: {
scores: {
map: function (doc) {
if (doc.score) {
emit(null, doc.score);
}
}.toString(),
reduce: '_sum'
},
}
};
return db.bulkDocs([doc, {score: 3}, {score: 5}]).then(function () {
return db.query('barbar/scores');
}).then(function (a) {
a.rows[0].value.should.equal(8);
return db.destroy();
}).then(function (a) {
fs.readdirSync('./tmp').should.have.length(0);
});
});
});
}
});
});