1 var mongodb = process.env['TEST_NATIVE'] != null ? require('../lib/mongodb').native() : require('../lib/mongodb').pure();
3 var testCase = require('../deps/nodeunit').testCase,
4 debug = require('util').debug
5 inspect = require('util').inspect,
6 nodeunit = require('../deps/nodeunit'),
8 Cursor = mongodb.Cursor,
9 Collection = mongodb.Collection,
10 Server = mongodb.Server;
12 var MONGODB = 'integration_tests';
13 var client = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize: 4}), {native_parser: (process.env['TEST_NATIVE'] != null)});
15 // Define the tests, we want them to run as a nested test so we only clean up the
17 var tests = testCase({
18 setUp: function(callback) {
19 client.open(function(err, db_p) {
20 if(numberOfTestsRun == Object.keys(tests).length) {
21 // If first test drop the db
22 client.dropDatabase(function(err, done) {
31 tearDown: function(callback) {
32 numberOfTestsRun = numberOfTestsRun - 1;
33 // Drop the database and close it
34 if(numberOfTestsRun <= 0) {
35 // client.dropDatabase(function(err, done) {
45 shouldCorrectlyExtractIndexInformation : function(test) {
46 client.createCollection('test_index_information', function(err, collection) {
47 collection.insert({a:1}, {safe:true}, function(err, ids) {
48 // Create an index on the collection
49 client.createIndex(collection.collectionName, 'a', function(err, indexName) {
50 test.equal("a_1", indexName);
51 // Let's fetch the index information
52 client.indexInformation(collection.collectionName, function(err, collectionInfo) {
53 test.ok(collectionInfo['_id_'] != null);
54 test.equal('_id', collectionInfo['_id_'][0][0]);
55 test.ok(collectionInfo['a_1'] != null);
56 test.deepEqual([["a", 1]], collectionInfo['a_1']);
58 client.indexInformation(function(err, collectionInfo2) {
59 var count1 = 0, count2 = 0;
60 // Get count of indexes
61 for(var i in collectionInfo) { count1 += 1;}
62 for(var i in collectionInfo2) { count2 += 1;}
65 test.ok(count2 >= count1);
66 test.ok(collectionInfo2['_id_'] != null);
67 test.equal('_id', collectionInfo2['_id_'][0][0]);
68 test.ok(collectionInfo2['a_1'] != null);
69 test.deepEqual([["a", 1]], collectionInfo2['a_1']);
70 test.ok((collectionInfo[indexName] != null));
71 test.deepEqual([["a", 1]], collectionInfo[indexName]);
82 shouldCorrectlyHandleMultipleColumnIndexes : function(test) {
83 client.createCollection('test_multiple_index_cols', function(err, collection) {
84 collection.insert({a:1}, function(err, ids) {
85 // Create an index on the collection
86 client.createIndex(collection.collectionName, [['a', -1], ['b', 1], ['c', -1]], function(err, indexName) {
87 test.equal("a_-1_b_1_c_-1", indexName);
88 // Let's fetch the index information
89 client.indexInformation(collection.collectionName, function(err, collectionInfo) {
91 // Get count of indexes
92 for(var i in collectionInfo) { count1 += 1;}
95 test.equal(2, count1);
96 test.ok(collectionInfo[indexName] != null);
97 test.deepEqual([['a', -1], ['b', 1], ['c', -1]], collectionInfo[indexName]);
107 shouldCorrectlyHandleUniqueIndex : function(test) {
108 // Create a non-unique index and test inserts
109 client.createCollection('test_unique_index', function(err, collection) {
110 client.createIndex(collection.collectionName, 'hello', function(err, indexName) {
112 collection.insert([{'hello':'world'}, {'hello':'mike'}, {'hello':'world'}], {safe:true}, function(err, errors) {
113 // Assert that we have no erros
114 client.error(function(err, errors) {
115 test.equal(1, errors.length);
116 test.equal(null, errors[0].err);
118 // Create a unique index and test that insert fails
119 client.createCollection('test_unique_index2', function(err, collection) {
120 client.createIndex(collection.collectionName, 'hello', {unique:true}, function(err, indexName) {
122 collection.insert([{'hello':'world'}, {'hello':'mike'}, {'hello':'world'}], {safe:true}, function(err, ids) {
123 test.ok(err != null);
134 shouldCorrectlyCreateSubfieldIndex : function(test) {
135 // Create a non-unique index and test inserts
136 client.createCollection('test_index_on_subfield', function(err, collection) {
137 collection.insert([{'hello': {'a':4, 'b':5}}, {'hello': {'a':7, 'b':2}}, {'hello': {'a':4, 'b':10}}], {safe:true}, function(err, ids) {
138 // Assert that we have no erros
139 client.error(function(err, errors) {
140 test.equal(1, errors.length);
141 test.ok(errors[0].err == null);
143 // Create a unique subfield index and test that insert fails
144 client.createCollection('test_index_on_subfield2', function(err, collection) {
145 client.createIndex(collection.collectionName, 'hello.a', true, function(err, indexName) {
146 collection.insert([{'hello': {'a':4, 'b':5}}, {'hello': {'a':7, 'b':2}}, {'hello': {'a':4, 'b':10}}], {safe:true}, function(err, ids) {
147 // Assert that we have erros
148 test.ok(err != null);
158 shouldCorrectlyDropIndexes : function(test) {
159 client.createCollection('test_drop_indexes', function(err, collection) {
160 collection.insert({a:1}, {safe:true}, function(err, ids) {
161 // Create an index on the collection
162 client.createIndex(collection.collectionName, 'a', function(err, indexName) {
163 test.equal("a_1", indexName);
164 // Drop all the indexes
165 collection.dropIndexes(function(err, result) {
166 test.equal(true, result);
168 collection.indexInformation(function(err, result) {
169 test.ok(result['a_1'] == null);
178 shouldCorrectlyHandleDistinctIndexes : function(test) {
179 client.createCollection('test_distinct_queries', function(err, collection) {
180 collection.insert([{'a':0, 'b':{'c':'a'}},
181 {'a':1, 'b':{'c':'b'}},
182 {'a':1, 'b':{'c':'c'}},
183 {'a':2, 'b':{'c':'a'}}, {'a':3}, {'a':3}], {safe:true}, function(err, ids) {
184 collection.distinct('a', function(err, docs) {
185 test.deepEqual([0, 1, 2, 3], docs.sort());
188 collection.distinct('b.c', function(err, docs) {
189 test.deepEqual(['a', 'b', 'c'], docs.sort());
196 shouldCorrectlyExecuteEnsureIndex : function(test) {
197 client.createCollection('test_ensure_index', function(err, collection) {
198 // Create an index on the collection
199 client.ensureIndex(collection.collectionName, 'a', function(err, indexName) {
200 test.equal("a_1", indexName);
201 // Let's fetch the index information
202 client.indexInformation(collection.collectionName, function(err, collectionInfo) {
203 test.ok(collectionInfo['_id_'] != null);
204 test.equal('_id', collectionInfo['_id_'][0][0]);
205 test.ok(collectionInfo['a_1'] != null);
206 test.deepEqual([["a", 1]], collectionInfo['a_1']);
208 client.ensureIndex(collection.collectionName, 'a', function(err, indexName) {
209 test.equal("a_1", indexName);
210 // Let's fetch the index information
211 client.indexInformation(collection.collectionName, function(err, collectionInfo) {
212 test.ok(collectionInfo['_id_'] != null);
213 test.equal('_id', collectionInfo['_id_'][0][0]);
214 test.ok(collectionInfo['a_1'] != null);
215 test.deepEqual([["a", 1]], collectionInfo['a_1']);
216 // Let's close the db
225 shouldCorrectlyCreateAndUseSparseIndex : function(test) {
226 client.createCollection('create_and_use_sparse_index_test', function(err, r) {
227 client.collection('create_and_use_sparse_index_test', function(err, collection) {
229 collection.ensureIndex({title:1}, {sparse:true}, function(err, indexName) {
230 collection.insert([{name:"Jim"}, {name:"Sarah", title:"Princess"}], {safe:true}, function(err, result) {
231 collection.find({title:{$ne:null}}).sort({title:1}).toArray(function(err, items) {
232 test.equal(1, items.length);
233 test.equal("Sarah", items[0].name);
235 // Fetch the info for the indexes
236 collection.indexInformation({full:true}, function(err, indexInfo) {
237 test.equal(null, err);
238 test.equal(2, indexInfo.length);
249 // Stupid freaking workaround due to there being no way to run setup once for each suite
250 var numberOfTestsRun = Object.keys(tests).length;
252 module.exports = tests;