-
Notifications
You must be signed in to change notification settings - Fork 453
/
db.js
857 lines (788 loc) · 30.9 KB
/
db.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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
var async = require('async');
var expect = require('chai').expect;
var Backend = require('../lib/backend');
var ot = require('../lib/ot');
/**
* Run the DB test suite. Useful for testing external DB adapters:
* require('sharedb/test/db')(options);
*
* options.create() should instantiate a database adapter and call the
* provided callback with it.
* options.create = function(callback) {
* // Create instance of Db
* callback(error, db);
* }
*
* options.getQuery() is optional, and only needed if the adapter supports
* queries. It needs to accept the "generic" query provided by the
* test suite, and return a query specific to the adapter.
* options.getQuery = function(genericQuery) {
* // map genericQuery into adapter-specific query
* return mappedQuery;
* }
*/
module.exports = function(options) {
var create = options.create;
describe('db', function() {
beforeEach(function(done) {
var self = this;
create(function(err, db) {
if (err) return done(err);
self.db = db;
self.backend = new Backend({db: db});
done();
});
});
afterEach(function(done) {
this.backend.close(done);
});
it('can call db.close() without callback', function(done) {
create(function(err, db) {
if (err) return done(err);
db.close();
done();
});
});
it('calls db.close() multiple times', function(done) {
var db = this.db;
db.close(function(error) {
if (error) return done(error);
db.close(function(error) {
done(error);
});
});
});
if (options.getQuery) {
require('./client/query-subscribe')({getQuery: options.getQuery});
require('./client/query')({getQuery: options.getQuery});
}
require('./client/projections')({getQuery: options.getQuery});
require('./client/submit')();
require('./client/submit-json1')();
require('./client/subscribe')();
// Simplified mock of how submit request applies operations. The
// noteworthy dependency is that it always calls getSnapshot with
// the {$submit: true} projection and applies the op to the returned
// snapshot before committing. Thus, commit may rely on the behavior
// of getSnapshot with this special projection
function submit(db, collection, id, op, callback) {
db.getSnapshot(collection, id, {$submit: true}, null, function(err, snapshot) {
if (err) return callback(err);
if (snapshot.v !== op.v) {
var succeeded = false;
return callback(null, succeeded);
}
var err = ot.apply(snapshot, op);
if (err) return callback(err);
db.commit(collection, id, op, snapshot, null, callback);
});
}
describe('commit', function() {
function commitConcurrent(db, ops, test, done) {
var numSucceeded = 0;
async.each(ops, function(op, eachCb) {
submit(db, 'testcollection', 'foo', op, function(err, succeeded) {
if (err) return eachCb(err);
if (succeeded) numSucceeded++;
eachCb();
});
}, function(err) {
if (err) return done(err);
expect(numSucceeded).equal(1);
if (!test) return done();
db.getOps('testcollection', 'foo', 0, null, null, function(err, opsOut) {
if (err) return done(err);
db.getSnapshot('testcollection', 'foo', null, null, function(err, snapshotOut) {
if (err) return done(err);
test(opsOut, snapshotOut);
done();
});
});
});
}
function testCreateCommit(ops, snapshot) {
expect(snapshot.v).eql(1);
expect(ops.length).equal(1);
expect(ops[0].create).ok;
}
it('one commit succeeds from 2 simultaneous creates', function(done) {
var ops = [
{v: 0, create: {type: 'json0', data: {x: 3}}},
{v: 0, create: {type: 'json0', data: {x: 5}}}
];
commitConcurrent(this.db, ops, testCreateCommit, done);
});
it('one commit succeeds from 5 simultaneous creates', function(done) {
var ops = [
{v: 0, create: {type: 'json0', data: {x: 3}}},
{v: 0, create: {type: 'json0', data: {x: 5}}},
{v: 0, create: {type: 'json0', data: {x: 7}}},
{v: 0, create: {type: 'json0', data: {x: 9}}},
{v: 0, create: {type: 'json0', data: {x: 11}}}
];
commitConcurrent(this.db, ops, testCreateCommit, done);
});
function createDoc(db, callback) {
var ops = [{v: 0, create: {type: 'json0', data: {x: 3}}}];
commitConcurrent(db, ops, null, callback);
}
function testOpCommit(ops, snapshot) {
expect(snapshot.v).equal(2);
expect(ops.length).equal(2);
expect(ops[0].create).ok;
expect(ops[1].op).ok;
}
function testDelCommit(ops, snapshot) {
expect(snapshot.v).equal(2);
expect(ops.length).equal(2);
expect(snapshot.data).equal(undefined);
expect(snapshot.type).equal(null);
expect(ops[0].create).ok;
expect(ops[1].del).equal(true);
}
function testOpOrDelCommit(ops, snapshot) {
if (ops[1].op) {
testOpCommit(ops, snapshot);
} else {
testDelCommit(ops, snapshot);
}
}
it('one commit succeeds from 2 simultaneous ops', function(done) {
var ops = [
{v: 1, op: []},
{v: 1, op: []}
];
var db = this.db;
createDoc(db, function(err) {
if (err) return done(err);
commitConcurrent(db, ops, testOpCommit, done);
});
});
it('one commit succeeds from 5 simultaneous ops', function(done) {
var ops = [
{v: 1, op: []},
{v: 1, op: []},
{v: 1, op: []},
{v: 1, op: []},
{v: 1, op: []}
];
var db = this.db;
createDoc(db, function(err) {
if (err) return done(err);
commitConcurrent(db, ops, testOpCommit, done);
});
});
it('one commit succeeds from 2 simultaneous deletes', function(done) {
var ops = [
{v: 1, del: true},
{v: 1, del: true}
];
var db = this.db;
createDoc(db, function(err) {
if (err) return done(err);
commitConcurrent(db, ops, testDelCommit, done);
});
});
it('one commit succeeds from 5 simultaneous deletes', function(done) {
var ops = [
{v: 1, del: true},
{v: 1, del: true},
{v: 1, del: true},
{v: 1, del: true},
{v: 1, del: true}
];
var db = this.db;
createDoc(db, function(err) {
if (err) return done(err);
commitConcurrent(db, ops, testDelCommit, done);
});
});
it('one commit succeeds from delete simultaneous with op', function(done) {
var ops = [
{v: 1, del: true},
{v: 1, op: []}
];
var db = this.db;
createDoc(db, function(err) {
if (err) return done(err);
commitConcurrent(db, ops, testOpOrDelCommit, done);
});
});
it('one commit succeeds from op simultaneous with delete', function(done) {
var ops = [
{v: 1, op: []},
{v: 1, del: true}
];
var db = this.db;
createDoc(db, function(err) {
if (err) return done(err);
commitConcurrent(db, ops, testOpOrDelCommit, done);
});
});
});
function commitSnapshotWithMetadata(db, cb) {
var data = {x: 5, y: 6};
var metadata = {test: 3};
var op = {v: 0, create: {type: 'http://sharejs.org/types/JSONv0', data: data}};
var snapshot = {v: 1, type: 'http://sharejs.org/types/JSONv0', data: data, m: metadata};
db.commit('testcollection', 'test', op, snapshot, null, cb);
}
describe('getSnapshot', function() {
it('getSnapshot returns v0 snapshot', function(done) {
this.db.getSnapshot('testcollection', 'test', null, null, function(err, result) {
if (err) return done(err);
expect(result).eql({id: 'test', type: null, v: 0, data: undefined, m: null});
done();
});
});
it('getSnapshot returns committed data', function(done) {
var data = {x: 5, y: 6};
var op = {v: 0, create: {type: 'json0', data: data}};
var db = this.db;
submit(db, 'testcollection', 'test', op, function(err) {
if (err) return done(err);
db.getSnapshot('testcollection', 'test', null, null, function(err, result) {
if (err) return done(err);
expect(result).eql({id: 'test', type: 'http://sharejs.org/types/JSONv0', v: 1, data: data, m: null});
done();
});
});
});
it('getSnapshot does not return committed metadata by default', function(done) {
var db = this.db;
commitSnapshotWithMetadata(db, function(err) {
if (err) return done(err);
db.getSnapshot('testcollection', 'test', null, null, function(err, result) {
if (err) return done(err);
expect(result.m).equal(null);
done();
});
});
});
it('getSnapshot returns metadata when option is true', function(done) {
var db = this.db;
commitSnapshotWithMetadata(db, function(err) {
if (err) return done(err);
db.getSnapshot('testcollection', 'test', null, {metadata: true}, function(err, result) {
if (err) return done(err);
expect(result.m).eql({test: 3});
done();
});
});
});
it('getSnapshot returns metadata when fields is {$submit: true}', function(done) {
var db = this.db;
commitSnapshotWithMetadata(db, function(err) {
if (err) return done(err);
db.getSnapshot('testcollection', 'test', {$submit: true}, null, function(err, result) {
if (err) return done(err);
expect(result.m).eql({test: 3});
done();
});
});
});
});
describe('getSnapshotBulk', function() {
it('getSnapshotBulk returns committed and v0 snapshots', function(done) {
var data = {x: 5, y: 6};
var op = {v: 0, create: {type: 'json0', data: data}};
var db = this.db;
submit(db, 'testcollection', 'test', op, function(err) {
if (err) return done(err);
db.getSnapshotBulk('testcollection', ['test2', 'test'], null, null, function(err, resultMap) {
if (err) return done(err);
expect(resultMap).eql({
test: {id: 'test', type: 'http://sharejs.org/types/JSONv0', v: 1, data: data, m: null},
test2: {id: 'test2', type: null, v: 0, data: undefined, m: null}
});
done();
});
});
});
it('getSnapshotBulk does not return committed metadata by default', function(done) {
var db = this.db;
commitSnapshotWithMetadata(db, function(err) {
if (err) return done(err);
db.getSnapshotBulk('testcollection', ['test2', 'test'], null, null, function(err, resultMap) {
if (err) return done(err);
expect(resultMap.test.m).equal(null);
done();
});
});
});
it('getSnapshotBulk returns metadata when option is true', function(done) {
var db = this.db;
commitSnapshotWithMetadata(db, function(err) {
if (err) return done(err);
db.getSnapshotBulk('testcollection', ['test2', 'test'], null, {metadata: true}, function(err, resultMap) {
if (err) return done(err);
expect(resultMap.test.m).eql({test: 3});
done();
});
});
});
});
describe('getOps', function() {
it('getOps returns empty list when there are no ops', function(done) {
this.db.getOps('testcollection', 'test', 0, null, null, function(err, ops) {
if (err) return done(err);
expect(ops).eql([]);
done();
});
});
it('getOps returns 1 committed op', function(done) {
var op = {v: 0, create: {type: 'json0', data: {x: 5, y: 6}}};
var db = this.db;
submit(db, 'testcollection', 'test', op, function(err) {
if (err) return done(err);
db.getOps('testcollection', 'test', 0, null, null, function(err, ops) {
if (err) return done(err);
expect(ops).eql([op]);
done();
});
});
});
it('getOps returns 2 committed ops', function(done) {
var op0 = {v: 0, create: {type: 'json0', data: {x: 5, y: 6}}};
var op1 = {v: 1, op: [{p: ['x'], na: 1}]};
var db = this.db;
submit(db, 'testcollection', 'test', op0, function(err) {
if (err) return done(err);
submit(db, 'testcollection', 'test', op1, function(err) {
if (err) return done(err);
db.getOps('testcollection', 'test', 0, null, null, function(err, ops) {
if (err) return done(err);
expect(ops).eql([op0, op1]);
done();
});
});
});
});
it('getOps returns all ops when from and to are null', function(done) {
var op0 = {v: 0, create: {type: 'json0', data: {x: 5, y: 6}}};
var op1 = {v: 1, op: [{p: ['x'], na: 1}]};
var db = this.db;
submit(db, 'testcollection', 'test', op0, function(err) {
if (err) return done(err);
submit(db, 'testcollection', 'test', op1, function(err) {
if (err) return done(err);
db.getOps('testcollection', 'test', null, null, null, function(err, ops) {
if (err) return done(err);
expect(ops).eql([op0, op1]);
done();
});
});
});
});
it('getOps returns from specific op number', function(done) {
var op0 = {v: 0, create: {type: 'json0', data: {x: 5, y: 6}}};
var op1 = {v: 1, op: [{p: ['x'], na: 1}]};
var db = this.db;
submit(db, 'testcollection', 'test', op0, function(err) {
if (err) return done(err);
submit(db, 'testcollection', 'test', op1, function(err) {
if (err) return done(err);
db.getOps('testcollection', 'test', 1, null, null, function(err, ops) {
if (err) return done(err);
expect(ops).eql([op1]);
done();
});
});
});
});
it('getOps returns to specific op number', function(done) {
var op0 = {v: 0, create: {type: 'json0', data: {x: 5, y: 6}}};
var op1 = {v: 1, op: [{p: ['x'], na: 1}]};
var db = this.db;
submit(db, 'testcollection', 'test', op0, function(err) {
if (err) return done(err);
submit(db, 'testcollection', 'test', op1, function(err) {
if (err) return done(err);
db.getOps('testcollection', 'test', 0, 1, null, function(err, ops) {
if (err) return done(err);
expect(ops).eql([op0]);
done();
});
});
});
});
it('getOps does not return committed metadata by default', function(done) {
var op = {v: 0, create: {type: 'json0', data: {x: 5, y: 6}}, m: {test: 3}};
var db = this.db;
submit(db, 'testcollection', 'test', op, function(err) {
if (err) return done(err);
db.getOps('testcollection', 'test', null, null, null, function(err, ops) {
if (err) return done(err);
expect(ops[0].m).eql(undefined);
done();
});
});
});
it('getOps returns metadata when option is true', function(done) {
var op = {v: 0, create: {type: 'json0', data: {x: 5, y: 6}}, m: {test: 3}};
var db = this.db;
submit(db, 'testcollection', 'test', op, function(err) {
if (err) return done(err);
db.getOps('testcollection', 'test', null, null, {metadata: true}, function(err, ops) {
if (err) return done(err);
expect(ops[0].m).eql({test: 3});
done();
});
});
});
});
describe('getOpsBulk', function() {
it('getOpsBulk returns empty map when there are no ops', function(done) {
this.db.getOpsBulk('testcollection', {test: 0, test2: 0}, null, null, function(err, opsMap) {
if (err) return done(err);
expect(opsMap).eql({
test: [],
test2: []
});
done();
});
});
it('getOpsBulk returns committed ops', function(done) {
var op = {v: 0, create: {type: 'json0', data: {x: 5, y: 6}}};
var db = this.db;
submit(db, 'testcollection', 'test', op, function(err) {
if (err) return done(err);
submit(db, 'testcollection', 'test2', op, function(err) {
if (err) return done(err);
db.getOpsBulk('testcollection', {test: 0, test2: 0}, null, null, function(err, opsMap) {
if (err) return done(err);
expect(opsMap).eql({
test: [op],
test2: [op]
});
done();
});
});
});
});
it('getOpsBulk returns all ops committed from null', function(done) {
var op = {v: 0, create: {type: 'json0', data: {x: 5, y: 6}}};
var db = this.db;
submit(db, 'testcollection', 'test', op, function(err) {
if (err) return done(err);
submit(db, 'testcollection', 'test2', op, function(err) {
if (err) return done(err);
db.getOpsBulk('testcollection', {test: null, test2: null}, null, null, function(err, opsMap) {
if (err) return done(err);
expect(opsMap).eql({
test: [op],
test2: [op]
});
done();
});
});
});
});
it('getOpsBulk returns committed ops with specific froms', function(done) {
var op0 = {v: 0, create: {type: 'json0', data: {x: 5, y: 6}}};
var op1 = {v: 1, op: [{p: ['x'], na: 1}]};
var db = this.db;
submit(db, 'testcollection', 'test', op0, function(err) {
if (err) return done(err);
submit(db, 'testcollection', 'test2', op0, function(err) {
if (err) return done(err);
submit(db, 'testcollection', 'test', op1, function(err) {
if (err) return done(err);
submit(db, 'testcollection', 'test2', op1, function(err) {
if (err) return done(err);
db.getOpsBulk('testcollection', {test: 0, test2: 1}, null, null, function(err, opsMap) {
if (err) return done(err);
expect(opsMap).eql({
test: [op0, op1],
test2: [op1]
});
done();
});
});
});
});
});
});
it('getOpsBulk returns committed ops with specific to', function(done) {
var op0 = {v: 0, create: {type: 'json0', data: {x: 5, y: 6}}};
var op1 = {v: 1, op: [{p: ['x'], na: 1}]};
var db = this.db;
submit(db, 'testcollection', 'test', op0, function(err) {
if (err) return done(err);
submit(db, 'testcollection', 'test2', op0, function(err) {
if (err) return done(err);
submit(db, 'testcollection', 'test', op1, function(err) {
if (err) return done(err);
submit(db, 'testcollection', 'test2', op1, function(err) {
if (err) return done(err);
db.getOpsBulk('testcollection', {test: 1, test2: 0}, {test2: 1}, null, function(err, opsMap) {
if (err) return done(err);
expect(opsMap).eql({
test: [op1],
test2: [op0]
});
done();
});
});
});
});
});
});
it('getOpsBulk does not return committed metadata by default', function(done) {
var op = {v: 0, create: {type: 'json0', data: {x: 5, y: 6}}, m: {test: 3}};
var db = this.db;
submit(db, 'testcollection', 'test', op, function(err) {
if (err) return done(err);
db.getOpsBulk('testcollection', {test: null}, null, null, function(err, opsMap) {
if (err) return done(err);
expect(opsMap.test[0].m).equal(undefined);
done();
});
});
});
it('getOpsBulk returns metadata when option is true', function(done) {
var op = {v: 0, create: {type: 'json0', data: {x: 5, y: 6}}, m: {test: 3}};
var db = this.db;
submit(db, 'testcollection', 'test', op, function(err) {
if (err) return done(err);
db.getOpsBulk('testcollection', {test: null}, null, {metadata: true}, function(err, opsMap) {
if (err) return done(err);
expect(opsMap.test[0].m).eql({test: 3});
done();
});
});
});
});
describe('getOpsToSnapshot', function() {
it('getOpsToSnapshot returns committed op', function(done) {
var op = {v: 0, create: {type: 'json0', data: {x: 5, y: 6}}};
var db = this.db;
submit(db, 'testcollection', 'test', op, function(err) {
if (err) return done(err);
db.getSnapshot('testcollection', 'test', {$submit: true}, null, function(err, snapshot) {
if (err) return done(err);
db.getOpsToSnapshot('testcollection', 'test', 0, snapshot, null, function(err, ops) {
if (err) return done(err);
expect(ops).eql([op]);
done();
});
});
});
});
it('getOpsToSnapshot does not return committed metadata by default', function(done) {
var op = {v: 0, create: {type: 'json0', data: {x: 5, y: 6}}, m: {test: 3}};
var db = this.db;
submit(db, 'testcollection', 'test', op, function(err) {
if (err) return done(err);
db.getSnapshot('testcollection', 'test', {$submit: true}, null, function(err, snapshot) {
if (err) return done(err);
db.getOpsToSnapshot('testcollection', 'test', 0, snapshot, null, function(err, ops) {
if (err) return done(err);
expect(ops[0].m).equal(undefined);
done();
});
});
});
});
it('getOpsToSnapshot returns metadata when option is true', function(done) {
var op = {v: 0, create: {type: 'json0', data: {x: 5, y: 6}}, m: {test: 3}};
var db = this.db;
submit(db, 'testcollection', 'test', op, function(err) {
if (err) return done(err);
db.getSnapshot('testcollection', 'test', {$submit: true}, null, function(err, snapshot) {
if (err) return done(err);
db.getOpsToSnapshot('testcollection', 'test', 0, snapshot, {metadata: true}, function(err, ops) {
if (err) return done(err);
expect(ops[0].m).eql({test: 3});
done();
});
});
});
});
});
describe('query', function() {
it('query returns data in the collection', function(done) {
var snapshot = {v: 1, type: 'json0', data: {x: 5, y: 6}, m: null};
var db = this.db;
db.commit('testcollection', 'test', {v: 0, create: {}}, snapshot, null, function(err) {
if (err) return done(err);
db.query('testcollection', {x: 5}, null, null, function(err, results) {
if (err) return done(err);
delete results[0].id;
expect(results).eql([snapshot]);
done();
});
});
});
it('query returns nothing when there is no data', function(done) {
this.db.query('testcollection', {x: 5}, null, null, function(err, results) {
if (err) return done(err);
expect(results).eql([]);
done();
});
});
it('query does not return committed metadata by default', function(done) {
var db = this.db;
commitSnapshotWithMetadata(db, function(err) {
if (err) return done(err);
db.query('testcollection', {x: 5}, null, null, function(err, results) {
if (err) return done(err);
expect(results[0].m).equal(null);
done();
});
});
});
it('query returns metadata when option is true', function(done) {
var db = this.db;
commitSnapshotWithMetadata(db, function(err) {
if (err) return done(err);
db.query('testcollection', {x: 5}, null, {metadata: true}, function(err, results) {
if (err) return done(err);
expect(results[0].m).eql({test: 3});
done();
});
});
});
});
describe('projections', function() {
it('query returns only projected fields', function(done) {
if (!this.db.projectsSnapshot) return done();
var snapshot = {type: 'json0', v: 1, data: {x: 5, y: 6}};
var db = this.db;
db.commit('testcollection', 'test', {v: 0, create: {}}, snapshot, null, function(err) {
if (err) return done(err);
db.query('testcollection', {x: 5}, {y: true}, null, function(err, results) {
if (err) return done(err);
expect(results).eql([{type: 'json0', v: 1, data: {y: 6}, id: 'test'}]);
done();
});
});
});
it('query returns no data for matching documents if fields is empty', function(done) {
if (!this.db.projectsSnapshot) return done();
var snapshot = {type: 'json0', v: 1, data: {x: 5, y: 6}};
var db = this.db;
db.commit('testcollection', 'test', {v: 0, create: {}}, snapshot, null, function(err) {
if (err) return done(err);
db.query('testcollection', {x: 5}, {}, null, function(err, results) {
if (err) return done(err);
expect(results).eql([{type: 'json0', v: 1, data: {}, id: 'test'}]);
done();
});
});
});
it('query does not return committed metadata by default with projection', function(done) {
var db = this.db;
commitSnapshotWithMetadata(db, function(err) {
if (err) return done(err);
db.query('testcollection', {x: 5}, {x: true}, null, function(err, results) {
if (err) return done(err);
expect(results[0].m).equal(null);
done();
});
});
});
it('query returns metadata when option is true with projection', function(done) {
var db = this.db;
commitSnapshotWithMetadata(db, function(err) {
if (err) return done(err);
db.query('testcollection', {x: 5}, {x: true}, {metadata: true}, function(err, results) {
if (err) return done(err);
expect(results[0].m).eql({test: 3});
done();
});
});
});
});
if (options.getQuery) {
describe('queryPoll', function() {
it('returns data in the collection', function(done) {
var snapshot = {v: 1, type: 'json0', data: {x: 5, y: 6}};
var db = this.db;
db.commit('testcollection', 'test', {v: 0, create: {}}, snapshot, null, function(err) {
if (err) return done(err);
var dbQuery = options.getQuery({query: {x: 5}});
db.queryPoll('testcollection', dbQuery, null, function(err, ids) {
if (err) return done(err);
expect(ids).eql(['test']);
done();
});
});
});
it('returns nothing when there is no data', function(done) {
var dbQuery = options.getQuery({query: {x: 5}});
this.db.queryPoll('testcollection', dbQuery, null, function(err, ids) {
if (err) return done(err);
expect(ids).eql([]);
done();
});
});
});
describe('queryPollDoc', function() {
it('returns false when the document does not exist', function(done) {
var dbQuery = options.getQuery({query: {}});
if (!this.db.canPollDoc('testcollection', dbQuery)) return done();
var db = this.db;
db.queryPollDoc('testcollection', 'doesnotexist', dbQuery, null, function(err, result) {
if (err) return done(err);
expect(result).equal(false);
done();
});
});
it('returns true when the document matches', function(done) {
var dbQuery = options.getQuery({query: {x: 5}});
if (!this.db.canPollDoc('testcollection', dbQuery)) return done();
var snapshot = {type: 'json0', v: 1, data: {x: 5, y: 6}};
var db = this.db;
db.commit('testcollection', 'test', {v: 0, create: {}}, snapshot, null, function(err) {
if (err) return done(err);
db.queryPollDoc('testcollection', 'test', dbQuery, null, function(err, result) {
if (err) return done(err);
expect(result).equal(true);
done();
});
});
});
it('returns false when the document does not match', function(done) {
var dbQuery = options.getQuery({query: {x: 6}});
if (!this.db.canPollDoc('testcollection', dbQuery)) return done();
var snapshot = {type: 'json0', v: 1, data: {x: 5, y: 6}};
var db = this.db;
db.commit('testcollection', 'test', {v: 0, create: {}}, snapshot, null, function(err) {
if (err) return done(err);
db.queryPollDoc('testcollection', 'test', dbQuery, null, function(err, result) {
if (err) return done(err);
expect(result).equal(false);
done();
});
});
});
});
describe('getQuery', function() {
it('getQuery argument order', function(done) {
// test that options.getQuery({query: {}, sort: [['foo', 1], ['bar', -1]]})
// sorts by foo first, then bar
var snapshots = [
{type: 'json0', id: '0', v: 1, data: {foo: 1, bar: 1}, m: null},
{type: 'json0', id: '1', v: 1, data: {foo: 2, bar: 1}, m: null},
{type: 'json0', id: '2', v: 1, data: {foo: 1, bar: 2}, m: null},
{type: 'json0', id: '3', v: 1, data: {foo: 2, bar: 2}, m: null}
];
var db = this.db;
var dbQuery = options.getQuery({query: {}, sort: [['foo', 1], ['bar', -1]]});
async.each(snapshots, function(snapshot, cb) {
db.commit('testcollection', snapshot.id, {v: 0, create: {}}, snapshot, null, cb);
}, function(err) {
if (err) throw err;
db.query('testcollection', dbQuery, null, null, function(err, results) {
if (err) throw err;
expect(results).eql(
[snapshots[2], snapshots[0], snapshots[3], snapshots[1]]);
done();
});
});
});
});
}
});
};