forked from mongodb/node-mongodb-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_tests.js
5125 lines (4538 loc) · 190 KB
/
integration_tests.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
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
GLOBAL.DEBUG = true;
debug = require("sys").debug,
inspect = require("sys").inspect;
test = require("assert");
var Db = require('../lib/mongodb').Db,
GridStore = require('../lib/mongodb').GridStore,
Chunk = require('../lib/mongodb').Chunk,
Server = require('../lib/mongodb').Server,
ServerPair = require('../lib/mongodb').ServerPair,
ServerCluster = require('../lib/mongodb').ServerCluster,
Code = require('../lib/mongodb/bson/bson').Code;
Binary = require('../lib/mongodb/bson/bson').Binary;
ObjectID = require('../lib/mongodb/bson/bson').ObjectID,
DBRef = require('../lib/mongodb/bson/bson').DBRef,
Cursor = require('../lib/mongodb/cursor').Cursor,
Collection = require('../lib/mongodb/collection').Collection,
BinaryParser = require('../lib/mongodb/bson/binary_parser').BinaryParser,
Buffer = require('buffer').Buffer,
fs = require('fs'),
Script = require('vm');
/*******************************************************************************************************
Integration Tests
*******************************************************************************************************/
var all_tests = {
// Test unicode characters
test_unicode_characters : function() {
client.createCollection('unicode_test_collection', function(err, collection) {
var test_strings = ["ouooueauiOUOOUEAUI", "öüóőúéáűíÖÜÓŐÚÉÁŰÍ", "本荘由利地域に洪水警報"];
collection.insert({id: 0, text: test_strings[0]}, function(err, ids) {
collection.insert({id: 1, text: test_strings[1]}, function(err, ids) {
collection.insert({id: 2, text: test_strings[2]}, function(err, ids) {
collection.find(function(err, cursor) {
cursor.each(function(err, item) {
if(item !== null) {
test.equal(test_strings[item.id], item.text);
}
});
finished_test({test_unicode_characters:'ok'});
});
});
});
});
});
},
// Test the creation of a collection on the mongo db
test_collection_methods : function() {
client.createCollection('test_collection_methods', function(err, collection) {
// Verify that all the result are correct coming back (should contain the value ok)
test.equal('test_collection_methods', collection.collectionName);
// Let's check that the collection was created correctly
client.collectionNames(function(err, documents) {
var found = false;
documents.forEach(function(document) {
if(document.name == "integration_tests_.test_collection_methods") found = true;
});
test.ok(true, found);
// Rename the collection and check that it's gone
client.renameCollection("test_collection_methods", "test_collection_methods2", function(err, reply) {
test.equal(1, reply.documents[0].ok);
// Drop the collection and check that it's gone
client.dropCollection("test_collection_methods2", function(err, result) {
test.equal(true, result);
finished_test({test_collection_methods:'ok'});
})
});
});
})
},
// Test the authentication method for the user
test_authentication : function() {
var user_name = 'spongebob';
var password = 'password';
client.authenticate('admin', 'admin', function(err, replies) {
test.ok(err instanceof Error);
test.ok(!replies);
// Add a user
client.addUser(user_name, password, function(err, result) {
client.authenticate(user_name, password, function(err, replies) {
// test.ok(replies);
finished_test({test_authentication:'ok'});
});
});
});
},
// Test the access to collections
test_collections : function() {
// Create two collections
client.createCollection('test.spiderman', function(r) {
client.createCollection('test.mario', function(r) {
// Insert test documents (creates collections)
client.collection('test.spiderman', function(err, spiderman_collection) {
spiderman_collection.insert({foo:5});
});
client.collection('test.mario', function(err, mario_collection) {
mario_collection.insert({bar:0});
});
// Assert collections
client.collections(function(err, collections) {
var found_spiderman = false;
var found_mario = false;
var found_does_not_exist = false;
collections.forEach(function(collection) {
if(collection.collectionName == "test.spiderman") found_spiderman = true;
if(collection.collectionName == "test.mario") found_mario = true;
if(collection.collectionName == "does_not_exist") found_does_not_exist = true;
});
test.ok(found_spiderman);
test.ok(found_mario);
test.ok(!found_does_not_exist);
finished_test({test_collections:'ok'});
});
});
});
},
// Test the generation of the object ids
test_object_id_generation : function() {
var number_of_tests_done = 0;
client.collection('test_object_id_generation.data', function(err, collection) {
// Insert test documents (creates collections and test fetch by query)
collection.insert({name:"Fred", age:42}, function(err, ids) {
test.equal(1, ids.length);
test.ok(ids[0]['_id'].toHexString().length == 24);
// Locate the first document inserted
collection.findOne({name:"Fred"}, function(err, document) {
test.equal(ids[0]['_id'].toHexString(), document._id.toHexString());
number_of_tests_done++;
});
});
// Insert another test document and collect using ObjectId
collection.insert({name:"Pat", age:21}, function(err, ids) {
test.equal(1, ids.length);
test.ok(ids[0]['_id'].toHexString().length == 24);
// Locate the first document inserted
collection.findOne(ids[0]['_id'], function(err, document) {
test.equal(ids[0]['_id'].toHexString(), document._id.toHexString());
number_of_tests_done++;
});
});
// Manually created id
var objectId = new client.bson_serializer.ObjectID(null);
// Insert a manually created document with generated oid
collection.insert({"_id":objectId, name:"Donald", age:95}, function(err, ids) {
test.equal(1, ids.length);
test.ok(ids[0]['_id'].toHexString().length == 24);
test.equal(objectId.toHexString(), ids[0]['_id'].toHexString());
// Locate the first document inserted
collection.findOne(ids[0]['_id'], function(err, document) {
test.equal(ids[0]['_id'].toHexString(), document._id.toHexString());
test.equal(objectId.toHexString(), document._id.toHexString());
number_of_tests_done++;
});
});
});
var intervalId = setInterval(function() {
if(number_of_tests_done == 3) {
clearInterval(intervalId);
finished_test({test_object_id_generation:'ok'});
}
}, 100);
},
test_object_id_to_and_from_hex_string : function() {
var objectId = new client.bson_serializer.ObjectID(null);
var originalHex= objectId.toHexString();
var newObjectId= new client.bson_serializer.ObjectID.createFromHexString(originalHex)
newHex= newObjectId.toHexString();
test.equal(originalHex, newHex);
finished_test({test_object_id_to_and_from_hex_string:'ok'});
},
// Test the auto connect functionality of the db
test_automatic_reconnect : function() {
var automatic_connect_client = new Db('integration_tests_', new Server("127.0.0.1", 27017, {auto_reconnect: true}), {});
automatic_connect_client.bson_deserializer = client.bson_deserializer;
automatic_connect_client.bson_serializer = client.bson_serializer;
automatic_connect_client.pkFactory = client.pkFactory;
automatic_connect_client.open(function(err, automatic_connect_client) {
// Listener for closing event
var closeListener = function(has_error) {
// Remove the listener for the close to avoid loop
automatic_connect_client.removeListener("close", closeListener);
// Let's insert a document
automatic_connect_client.collection('test_object_id_generation.data2', function(err, collection) {
// Insert another test document and collect using ObjectId
collection.insert({"name":"Patty", "age":34}, function(err, ids) {
test.equal(1, ids.length);
test.ok(ids[0]._id.toHexString().length == 24);
collection.findOne({"name":"Patty"}, function(err, document) {
test.equal(ids[0]._id.toHexString(), document._id.toHexString());
// Let's close the db
finished_test({test_automatic_reconnect:'ok'});
automatic_connect_client.close();
});
});
});
};
// Add listener to close event
automatic_connect_client.on("close", closeListener);
automatic_connect_client.close();
});
},
// Test that error conditions are handled correctly
test_connection_errors : function() {
// Test error handling for single server connection
var serverConfig = new Server("127.0.0.1", 21017, {auto_reconnect: true});
var error_client = new Db('integration_tests_', serverConfig, {});
error_client.on("error", function(err) {});
error_client.on("close", function(connection) {
test.ok(typeof connection == typeof serverConfig);
test.equal("127.0.0.1", connection.host);
test.equal(21017, connection.port);
test.equal(true, connection.autoReconnect);
});
error_client.open(function(err, error_client) {});
// Test error handling for server pair (works for cluster aswell)
var serverConfig = new Server("127.0.0.1", 20017, {});
var normalServer = new Server("127.0.0.1", 27017);
var serverPairConfig = new ServerPair(normalServer, serverConfig);
var error_client_pair = new Db('integration_tests_21', serverPairConfig, {});
var closeListener = function(connection) {
test.ok(typeof connection == typeof serverConfig);
test.equal("127.0.0.1", connection.host);
test.equal(20017, connection.port);
test.equal(false, connection.autoReconnect);
// Let's close the db
finished_test({test_connection_errors:'ok'});
error_client_pair.removeListener("close", closeListener);
normalServer.close();
};
error_client_pair.on("error", function(err) {});
error_client_pair.on("close", closeListener);
error_client_pair.open(function(err, error_client_pair) {});
},
// Test the error reporting functionality
test_error_handling : function() {
var error_client = new Db('integration_tests2_', new Server("127.0.0.1", 27017, {auto_reconnect: false}), {});
error_client.bson_deserializer = client.bson_deserializer;
error_client.bson_serializer = client.bson_serializer;
error_client.pkFactory = client.pkFactory;
error_client.open(function(err, error_client) {
error_client.resetErrorHistory(function() {
error_client.error(function(err, documents) {
test.equal(true, documents[0].ok);
test.equal(0, documents[0].n);
// Force error on server
error_client.executeDbCommand({forceerror: 1}, function(err, r) {
test.equal(0, r.documents[0].ok);
test.ok(r.documents[0].errmsg.length > 0);
// // Check for previous errors
error_client.previousErrors(function(err, documents) {
test.equal(true, documents[0].ok);
test.equal(1, documents[0].nPrev);
test.equal("forced error", documents[0].err);
// Check for the last error
error_client.error(function(err, documents) {
test.equal("forced error", documents[0].err);
// Force another error
error_client.collection('test_error_collection', function(err, collection) {
collection.findOne({name:"Fred"}, function(err, document) {
// Check that we have two previous errors
error_client.previousErrors(function(err, documents) {
test.equal(true, documents[0].ok);
test.equal(2, documents[0].nPrev);
test.equal("forced error", documents[0].err);
error_client.resetErrorHistory(function() {
error_client.previousErrors(function(err, documents) {
test.equal(true, documents[0].ok);
test.equal(-1, documents[0].nPrev);
error_client.error(function(err, documents) {
test.equal(true, documents[0].ok);
test.equal(0, documents[0].n);
// Let's close the db
finished_test({test_error_handling:'ok'});
error_client.close();
});
})
});
});
});
});
})
});
});
});
});
});
},
// Test the last status functionality of the driver
test_last_status : function() {
client.createCollection('test_last_status', function(err, collection) {
test.ok(collection instanceof Collection);
test.equal('test_last_status', collection.collectionName);
// Get the collection
client.collection('test_last_status', function(err, collection) {
// Remove all the elements of the collection
collection.remove(function(err, result) {
// Check update of a document
collection.insert({i:1}, function(err, ids) {
test.equal(1, ids.length);
test.ok(ids[0]._id.toHexString().length == 24);
// Update the record
collection.update({i:1}, {"$set":{i:2}}, function(err, result) {
// Check for the last message from the server
client.lastStatus(function(err, status) {
test.equal(true, status.documents[0].ok);
test.equal(true, status.documents[0].updatedExisting);
// Check for failed update of document
collection.update({i:1}, {"$set":{i:500}}, function(err, result) {
client.lastStatus(function(err, status) {
test.equal(true, status.documents[0].ok);
test.equal(false, status.documents[0].updatedExisting);
// Check safe update of a document
collection.insert({x:1}, function(err, ids) {
collection.update({x:1}, {"$set":{x:2}}, {'safe':true}, function(err, document) {
});
collection.update({x:1}, {"$set":{x:2}}, {'safe':true});
collection.update({y:1}, {"$set":{y:2}}, {'safe':true}, function(err, result) {
test.equal(0, result);
// Let's close the db
finished_test({test_last_status:'ok'});
});
});
});
});
});
});
});
});
});
});
},
// Test clearing out of the collection
test_clear : function() {
client.createCollection('test_clear', function(err, r) {
client.collection('test_clear', function(err, collection) {
collection.insert({i:1}, function(err, ids) {
collection.insert({i:2}, function(err, ids) {
collection.count(function(err, count) {
test.equal(2, count);
// Clear the collection
collection.remove({}, {safe:true}, function(err, result) {
test.equal(2, result);
collection.count(function(err, count) {
test.equal(0, count);
// Let's close the db
finished_test({test_clear:'ok'});
});
});
});
});
});
});
});
},
// Test insert of documents
test_insert : function() {
client.createCollection('test_insert', function(err, r) {
client.collection('test_insert', function(err, collection) {
for(var i = 1; i < 1000; i++) {
collection.insert({c:i}, function(err, r) {});
}
collection.insert({a:2}, function(err, r) {
collection.insert({a:3}, function(err, r) {
collection.count(function(err, count) {
test.equal(1001, count);
// Locate all the entries using find
collection.find(function(err, cursor) {
cursor.toArray(function(err, results) {
test.equal(1001, results.length);
test.ok(results[0] != null);
// Let's close the db
finished_test({test_insert:'ok'});
});
});
});
});
});
});
});
},
test_failing_insert_due_to_unique_index : function () {
client.createCollection('test_failing_insert_due_to_unique_index', function(err, r) {
client.collection('test_failing_insert_due_to_unique_index', function(err, collection) {
collection.ensureIndex([['a', 1 ]], true, function(err, indexName) {
collection.insert({a:2}, {safe: true}, function(err, r) {
test.ok(err == null);
collection.insert({a:2}, {safe: true}, function(err, r) {
test.ok(err != null);
finished_test({test_failing_insert_due_to_unique_index:'ok'});
})
})
})
})
})
},
// Test the error reporting functionality
test_failing_insert_due_to_unique_index_strict : function() {
var error_client = new Db('integration_tests2_', new Server("127.0.0.1", 27017, {auto_reconnect: false}), {strict:true});
error_client.bson_deserializer = client.bson_deserializer;
error_client.bson_serializer = client.bson_serializer;
error_client.pkFactory = client.pkFactory;
error_client.open(function(err, error_client) {
error_client.dropCollection('test_failing_insert_due_to_unique_index_strict', function(err, r) {
error_client.createCollection('test_failing_insert_due_to_unique_index_strict', function(err, r) {
error_client.collection('test_failing_insert_due_to_unique_index_strict', function(err, collection) {
collection.ensureIndex([['a', 1 ]], true, function(err, indexName) {
collection.insert({a:2}, function(err, r) {
test.ok(err == null);
collection.insert({a:2}, function(err, r) {
test.ok(err != null);
error_client.close();
finished_test({test_failing_insert_due_to_unique_index_strict:'ok'});
})
})
})
})
})
});
});
},
// Test multiple document insert
test_multiple_insert : function() {
client.createCollection('test_multiple_insert', function(err, r) {
var collection = client.collection('test_multiple_insert', function(err, collection) {
var docs = [{a:1}, {a:2}];
collection.insert(docs, function(err, ids) {
ids.forEach(function(doc) {
test.ok(((doc['_id']) instanceof ObjectID || Object.prototype.toString.call(doc['_id']) === '[object ObjectID]'));
});
// Let's ensure we have both documents
collection.find(function(err, cursor) {
cursor.toArray(function(err, docs) {
test.equal(2, docs.length);
var results = [];
// Check that we have all the results we want
docs.forEach(function(doc) {
if(doc.a == 1 || doc.a == 2) results.push(1);
});
test.equal(2, results.length);
// Let's close the db
finished_test({test_multiple_insert:'ok'});
});
});
});
});
});
},
// Test the count result on a collection that does not exist
test_count_on_nonexisting : function() {
client.collection('test_multiple_insert_2', function(err, collection) {
collection.count(function(err, count) {
test.equal(0, count);
// Let's close the db
finished_test({test_count_on_nonexisting:'ok'});
});
});
},
// Test a simple find
test_find_simple : function() {
client.createCollection('test_find_simple', function(err, r) {
var collection = client.collection('test_find_simple', function(err, collection) {
var doc1 = null;
var doc2 = null;
// Insert some test documents
collection.insert([{a:2}, {b:3}], function(err, docs) {doc1 = docs[0]; doc2 = docs[1]});
// Ensure correct insertion testing via the cursor and the count function
collection.find(function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(2, documents.length);
})
});
collection.count(function(err, count) {
test.equal(2, count);
});
// Fetch values by selection
collection.find({'a': doc1.a}, function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(1, documents.length);
test.equal(doc1.a, documents[0].a);
// Let's close the db
finished_test({test_find_simple:'ok'});
});
});
});
});
},
// Test a simple find chained
test_find_simple_chained : function() {
client.createCollection('test_find_simple_chained', function(err, r) {
var collection = client.collection('test_find_simple_chained', function(err, collection) {
var doc1 = null;
var doc2 = null;
// Insert some test documents
collection.insert([{a:2}, {b:3}], function(err, docs) {doc1 = docs[0]; doc2 = docs[1]});
// Ensure correct insertion testing via the cursor and the count function
collection.find().toArray(function(err, documents) {
test.equal(2, documents.length);
});
collection.count(function(err, count) {
test.equal(2, count);
});
// Fetch values by selection
collection.find({'a': doc1.a}).toArray(function(err, documents) {
test.equal(1, documents.length);
test.equal(doc1.a, documents[0].a);
// Let's close the db
finished_test({test_find_simple_chained:'ok'});
});
});
});
},
// Test advanced find
test_find_advanced : function() {
client.createCollection('test_find_advanced', function(err, r) {
var collection = client.collection('test_find_advanced', function(err, collection) {
var doc1 = null, doc2 = null, doc3 = null;
// Insert some test documents
collection.insert([{a:1}, {a:2}, {b:3}], function(err, docs) {
var doc1 = docs[0], doc2 = docs[1], doc3 = docs[2];
// Locate by less than
collection.find({'a':{'$lt':10}}, function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(2, documents.length);
// Check that the correct documents are returned
var results = [];
// Check that we have all the results we want
documents.forEach(function(doc) {
if(doc.a == 1 || doc.a == 2) results.push(1);
});
test.equal(2, results.length);
});
});
// Locate by greater than
collection.find({'a':{'$gt':1}}, function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(1, documents.length);
test.equal(2, documents[0].a);
});
});
// Locate by less than or equal to
collection.find({'a':{'$lte':1}}, function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(1, documents.length);
test.equal(1, documents[0].a);
});
});
// Locate by greater than or equal to
collection.find({'a':{'$gte':1}}, function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(2, documents.length);
// Check that the correct documents are returned
var results = [];
// Check that we have all the results we want
documents.forEach(function(doc) {
if(doc.a == 1 || doc.a == 2) results.push(1);
});
test.equal(2, results.length);
});
});
// Locate by between
collection.find({'a':{'$gt':1, '$lt':3}}, function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(1, documents.length);
test.equal(2, documents[0].a);
});
});
// Locate in clause
collection.find({'a':{'$in':[1,2]}}, function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(2, documents.length);
// Check that the correct documents are returned
var results = [];
// Check that we have all the results we want
documents.forEach(function(doc) {
if(doc.a == 1 || doc.a == 2) results.push(1);
});
test.equal(2, results.length);
});
});
// Locate in _id clause
collection.find({'_id':{'$in':[doc1['_id'], doc2['_id']]}}, function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(2, documents.length);
// Check that the correct documents are returned
var results = [];
// Check that we have all the results we want
documents.forEach(function(doc) {
if(doc.a == 1 || doc.a == 2) results.push(1);
});
test.equal(2, results.length);
// Let's close the db
finished_test({test_find_advanced:'ok'});
});
});
});
});
});
},
// Test sorting of results
test_find_sorting : function() {
client.createCollection('test_find_sorting', function(err, r) {
client.collection('test_find_sorting', function(err, collection) {
var doc1 = null, doc2 = null, doc3 = null, doc4 = null;
// Insert some test documents
collection.insert([{a:1, b:2},
{a:2, b:1},
{a:3, b:2},
{a:4, b:1}
], function(err, docs) {doc1 = docs[0]; doc2 = docs[1]; doc3 = docs[2]; doc4 = docs[3]});
// Test sorting (ascending)
collection.find({'a': {'$lt':10}}, {'sort': [['a', 1]]}, function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(4, documents.length);
test.equal(1, documents[0].a);
test.equal(2, documents[1].a);
test.equal(3, documents[2].a);
test.equal(4, documents[3].a);
});
});
// Test sorting (descending)
collection.find({'a': {'$lt':10}}, {'sort': [['a', -1]]}, function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(4, documents.length);
test.equal(4, documents[0].a);
test.equal(3, documents[1].a);
test.equal(2, documents[2].a);
test.equal(1, documents[3].a);
});
});
// Test sorting (descending), sort is hash
collection.find({'a': {'$lt':10}}, {sort: {a: -1}}, function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(4, documents.length);
test.equal(4, documents[0].a);
test.equal(3, documents[1].a);
test.equal(2, documents[2].a);
test.equal(1, documents[3].a);
});
});
// Sorting using array of names, assumes ascending order
collection.find({'a': {'$lt':10}}, {'sort': ['a']}, function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(4, documents.length);
test.equal(1, documents[0].a);
test.equal(2, documents[1].a);
test.equal(3, documents[2].a);
test.equal(4, documents[3].a);
});
});
// Sorting using single name, assumes ascending order
collection.find({'a': {'$lt':10}}, {'sort': 'a'}, function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(4, documents.length);
test.equal(1, documents[0].a);
test.equal(2, documents[1].a);
test.equal(3, documents[2].a);
test.equal(4, documents[3].a);
});
});
// Sorting using single name, assumes ascending order, sort is hash
collection.find({'a': {'$lt':10}}, {sort: {'a':1}}, function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(4, documents.length);
test.equal(1, documents[0].a);
test.equal(2, documents[1].a);
test.equal(3, documents[2].a);
test.equal(4, documents[3].a);
});
});
collection.find({'a': {'$lt':10}}, {'sort': ['b', 'a']}, function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(4, documents.length);
test.equal(2, documents[0].a);
test.equal(4, documents[1].a);
test.equal(1, documents[2].a);
test.equal(3, documents[3].a);
});
});
// Sorting using empty array, no order guarantee should not blow up
collection.find({'a': {'$lt':10}}, {'sort': []}, function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(4, documents.length);
});
});
/* NONACTUAL */
// Sorting using ordered hash
collection.find({'a': {'$lt':10}}, {'sort': {a:-1}}, function(err, cursor) {
cursor.toArray(function(err, documents) {
// Fail test if not an error
//test.ok(err instanceof Error);
//test.equal("Error: Invalid sort argument was supplied", err.message);
test.equal(4, documents.length);
// Let's close the db
finished_test({test_find_sorting:'ok'});
});
});
});
});
},
// Test the limit function of the db
test_find_limits : function() {
client.createCollection('test_find_limits', function(err, r) {
client.collection('test_find_limits', function(err, collection) {
var doc1 = null, doc2 = null, doc3 = null, doc4 = null;
// Insert some test documents
collection.insert([{a:1},
{b:2},
{c:3},
{d:4}
], function(err, docs) {doc1 = docs[0]; doc2 = docs[1]; doc3 = docs[2]; doc4 = docs[3]});
// Test limits
collection.find({}, {'limit': 1}, function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(1, documents.length);
});
});
collection.find({}, {'limit': 2}, function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(2, documents.length);
});
});
collection.find({}, {'limit': 3}, function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(3, documents.length);
});
});
collection.find({}, {'limit': 4}, function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(4, documents.length);
});
});
collection.find({}, {}, function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(4, documents.length);
});
});
collection.find({}, {'limit':99}, function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(4, documents.length);
// Let's close the db
finished_test({test_find_limits:'ok'});
});
});
});
});
},
// Test find by non-quoted values (issue #128)
test_find_non_quoted_values : function() {
client.createCollection('test_find_non_quoted_values', function(err, r) {
client.collection('test_find_non_quoted_values', function(err, collection) {
// insert test document
collection.insert([{ a: 19, b: 'teststring', c: 59920303 },
{ a: "19", b: 'teststring', c: 3984929 }]);
collection.find({ a: 19 }, function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(1, documents.length);
finished_test({test_find_non_quoted_values:'ok'});
});
});
});
});
},
// Test for querying embedded document using dot-notation (issue #126)
test_find_embedded_document : function() {
client.createCollection('test_find_embedded_document', function(err, r) {
client.collection('test_find_embedded_document', function(err, collection) {
// insert test document
collection.insert([{ a: { id: 10, value: 'foo' }, b: 'bar', c: { id: 20, value: 'foobar' }},
{ a: { id: 11, value: 'foo' }, b: 'bar2', c: { id: 20, value: 'foobar' }}]);
// test using integer value
collection.find({ 'a.id': 10 }, function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(1, documents.length);
test.equal('bar', documents[0].b);
});
});
// test using string value
collection.find({ 'a.value': 'foo' }, function(err, cursor) {
cursor.toArray(function(err, documents) {
// should yield 2 documents
test.equal(2, documents.length);
test.equal('bar', documents[0].b);
test.equal('bar2', documents[1].b);
finished_test({test_find_embedded_document:'ok'});
});
});
});
});
},
// Find no records
test_find_one_no_records : function() {
client.createCollection('test_find_one_no_records', function(err, r) {
client.collection('test_find_one_no_records', function(err, collection) {
collection.find({'a':1}, {}, function(err, cursor) {
cursor.toArray(function(err, documents) {
test.equal(0, documents.length);
// Let's close the db
finished_test({test_find_one_no_records:'ok'});
});
});
});
});
},
// Test dropping of collections
test_drop_collection : function() {
client.createCollection('test_drop_collection2', function(err, r) {
client.dropCollection('test_drop_collection', function(err, r) {
test.ok(err instanceof Error);
test.equal("ns not found", err.message);
var found = false;
// Ensure we don't have the collection in the set of names
client.collectionNames(function(err, replies) {
replies.forEach(function(err, document) {
if(document.name == "test_drop_collection") {
found = true;
return;
}
});
// Let's close the db
finished_test({test_drop_collection:'ok'});
// If we have an instance of the index throw and error
if(found) throw new Error("should not fail");
});
});
});
},
// Test dropping using the collection drop command
test_other_drop : function() {
client.createCollection('test_other_drop', function(err, r) {
client.collection('test_other_drop', function(err, collection) {
collection.drop(function(err, reply) {
// Ensure we don't have the collection in the set of names
client.collectionNames(function(err, replies) {
var found = false;
replies.forEach(function(document) {
if(document.name == "test_other_drop") {
found = true;
return;
}
});
// Let's close the db
finished_test({test_other_drop:'ok'});
// If we have an instance of the index throw and error
if(found) throw new Error("should not fail");
});
});
});
});
},
test_collection_names : function() {
client.createCollection('test_collection_names', function(err, r) {
client.collectionNames(function(err, documents) {
var found = false;
var found2 = false;
documents.forEach(function(document) {
if(document.name == 'integration_tests_.test_collection_names') found = true;
});
test.ok(found);
// Insert a document in an non-existing collection should create the collection
client.collection('test_collection_names2', function(err, collection) {
collection.insert({a:1})
client.collectionNames(function(err, documents) {
documents.forEach(function(document) {
if(document.name == 'integration_tests_.test_collection_names2') found = true;
if(document.name == 'integration_tests_.test_collection_names') found2 = true;
});
test.ok(found);
test.ok(found2);
});
// Let's close the db
finished_test({test_collection_names:'ok'});
});
});
});
},
test_collections_info : function() {
client.createCollection('test_collections_info', function(err, r) {
client.collectionsInfo(function(err, cursor) {
test.ok((cursor instanceof Cursor));
// Fetch all the collection info
cursor.toArray(function(err, documents) {
test.ok(documents.length > 1);
var found = false;
documents.forEach(function(document) {
if(document.name == 'integration_tests_.test_collections_info') found = true;
});
test.ok(found);
});
// Let's close the db
finished_test({test_collections_info:'ok'});
});
});
},
test_collection_options : function() {
client.createCollection('test_collection_options', {'capped':true, 'size':1024}, function(err, collection) {
test.ok(collection instanceof Collection);
test.equal('test_collection_options', collection.collectionName);
// Let's fetch the collection options
collection.options(function(err, options) {
test.equal(true, options.capped);
test.equal(1024, options.size);
test.equal("test_collection_options", options.create);
// Let's close the db
finished_test({test_collection_options:'ok'});