Inital Commit
[oweals/finalsclub.git] / node_modules / mongodb / test / index_test.js
1 var mongodb = process.env['TEST_NATIVE'] != null ? require('../lib/mongodb').native() : require('../lib/mongodb').pure();
2
3 var testCase = require('../deps/nodeunit').testCase,
4   debug = require('util').debug
5   inspect = require('util').inspect,
6   nodeunit = require('../deps/nodeunit'),
7   Db = mongodb.Db,
8   Cursor = mongodb.Cursor,
9   Collection = mongodb.Collection,
10   Server = mongodb.Server;
11
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)});
14
15 // Define the tests, we want them to run as a nested test so we only clean up the 
16 // db connection once
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) {
23           callback();
24         });                
25       } else {
26         return callback();        
27       }      
28     });
29   },
30   
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) {
36         client.close();
37         callback();
38       // });        
39     } else {
40       client.close();
41       callback();        
42     }      
43   },
44
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']);
57   
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;}
63   
64               // Tests
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]);
72   
73               // Let's close the db
74               test.done();
75             });
76           });
77         });
78       })
79     });    
80   },
81   
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) {
90             var count1 = 0;
91             // Get count of indexes
92             for(var i in collectionInfo) { count1 += 1;}
93   
94             // Test
95             test.equal(2, count1);
96             test.ok(collectionInfo[indexName] != null);
97             test.deepEqual([['a', -1], ['b', 1], ['c', -1]], collectionInfo[indexName]);
98   
99             // Let's close the db
100             test.done();
101           });
102         });
103       });
104     });    
105   },
106   
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) {
111         // Insert some docs
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);
117     
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) {
121                 // Insert some docs
122                 collection.insert([{'hello':'world'}, {'hello':'mike'}, {'hello':'world'}], {safe:true}, function(err, ids) {                            
123                   test.ok(err != null);
124                   test.done();
125                 });
126               });
127             });    
128           });
129         });
130       });
131     });  
132   },
133   
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);
142   
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);
149                 test.done();
150               });
151             });
152           });    
153         });
154       });
155     });  
156   },
157   
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);
167   
168             collection.indexInformation(function(err, result) {
169               test.ok(result['a_1'] == null);
170               test.done();
171             })
172           })
173         });
174       })
175     });
176   },  
177   
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());
186           });
187   
188           collection.distinct('b.c', function(err, docs) {
189             test.deepEqual(['a', 'b', 'c'], docs.sort());
190             test.done();
191           });
192       })
193     });
194   },  
195   
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']);
207   
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
217               test.done();
218             });
219           });
220         });
221       });
222     })
223   },  
224   
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) {
228         
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);
234
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);
239                 test.done();
240               })
241             })
242           });          
243         })
244       })
245     })    
246   },  
247 })
248
249 // Stupid freaking workaround due to there being no way to run setup once for each suite
250 var numberOfTestsRun = Object.keys(tests).length;
251 // Assign out tests
252 module.exports = tests;