Inital Commit
[oweals/finalsclub.git] / node_modules / mongoose / test / drivers / node-mongodb-native / collection.test.js
1
2 /**
3  * Module dependencies.
4  */
5
6 var start = require('../../common')
7   , mongoose = start.mongoose
8   , should = require('should')
9   , Schema = mongoose.Schema;
10
11 /**
12  * Setup.
13  */
14
15 mongoose.model('NativeDriverTest', new Schema({
16     title: String
17 }));
18
19 /**
20  * Test.
21  */
22
23 module.exports = {
24
25   'test that trying to implement a sparse index works': function () {
26     var db = start()
27       , NativeTestCollection = db.model('NativeDriverTest');
28
29     NativeTestCollection.collection.ensureIndex({ title: 1 }, { sparse: true }, function (err) {
30       should.strictEqual(!!err, false);
31       NativeTestCollection.collection.getIndexes(function (err, indexes) {
32         db.close();
33         should.strictEqual(!!err, false);
34         indexes.should.be.instanceof(Object);
35         indexes['title_1'].should.eql([['title', 1]]);
36       });
37     });
38   },
39
40   'test that the -native traditional ensureIndex spec syntax for fields works': function () {
41     var db = start()
42       , NativeTestCollection = db.model('NativeDriverTest');
43
44     NativeTestCollection.collection.ensureIndex([['a', 1]], function () {
45       db.close();
46     });
47   },
48
49   'unique index fails passes error': function () {
50     var db = start()
51       , schema = new Schema({ title: String })
52       , NativeTestCollection = db.model('NativeDriverTestUnique', schema)
53
54     NativeTestCollection.create({ title: 'x' }, {title:'x'}, function (err) {
55       should.strictEqual(!!err, false);
56
57       NativeTestCollection.collection.ensureIndex({ title: 1 }, { unique: true }, function (err) {
58         ;/E11000 duplicate key error index/.test(err.message).should.equal(true);
59
60         db.close();
61       });
62     });
63   }
64 };