Inital Commit
[oweals/finalsclub.git] / node_modules / mongoose / test / index.test.js
1
2 var url = require('url')
3   , start = require('./common')
4   , should = require('should')
5   , mongoose = start.mongoose
6   , Mongoose = mongoose.Mongoose
7   , Schema = mongoose.Schema;
8
9 module.exports = {
10
11   'test connecting to the demo database': function(beforeExit){
12     var db = start()
13       , connected = false;
14
15     db.on('open', function(){
16       connected = true;
17       db.close();
18     });
19
20     beforeExit(function(){
21       connected.should.be.true;
22     });
23   },
24
25   'test default connection': function(beforeExit){
26     var db = mongoose.connection
27       , uri = 'mongodb://localhost/mongoose_test'
28       , connected = false;
29
30     mongoose.connect(process.env.MONGOOSE_TEST_URI || uri);
31     db.on('open', function(){
32       connected = true;
33       db.close();
34     });
35
36     beforeExit(function(){
37       connected.should.be.true;
38     });
39   },
40
41   'test setting options': function(){
42     var mongoose = new Mongoose();
43
44     mongoose.set('a', 'b');
45     mongoose.set('long option', 'c');
46
47     mongoose.get('a').should.eql('b');
48     mongoose.set('a').should.eql('b');
49     mongoose.get('long option').should.eql('c');
50   },
51
52   'test declaring global plugins': function (beforeExit) {
53     var mong = new Mongoose()
54       , schema = new Schema()
55       , called = 0;
56
57     mong.plugin(function (s) {
58       s.should.equal(schema);
59       called++;
60     });
61
62     schema.plugin(function (s) {
63       s.should.equal(schema);
64       called++;
65     });
66
67     mong.model('GlobalPlugins', schema);
68
69     beforeExit(function () {
70       called.should.eql(2);
71     });
72   },
73
74   'test disconnection of all connections': function (beforeExit) {
75     var mong = new Mongoose()
76       , uri = 'mongodb://localhost/mongoose_test'
77       , connections = 0
78       , disconnections = 0;
79     
80     mong.connect(process.env.MONGOOSE_TEST_URI || uri);
81     var db = mong.connection;
82
83     db.on('open', function(){
84       connections++;
85     });
86
87     db.on('close', function () {
88       disconnections++;
89     });
90
91     var db2 = mong.createConnection(process.env.MONGOOSE_TEST_URI || uri);
92
93     db2.on('open', function () {
94       connections++;
95     });
96
97     db2.on('close', function () {
98       disconnections++;
99     });
100
101     mong.disconnect();
102
103     beforeExit(function () {
104       connections.should.eql(2);
105       disconnections.should.eql(2);
106     });
107   },
108
109   'test disconnection of all connections callback': function (beforeExit) {
110     var mong = new Mongoose()
111       , uri = 'mongodb://localhost/mongoose_test'
112       , called = false;
113
114     mong.connect(process.env.MONGOOSE_TEST_URI || uri);
115
116     mong.connection.on('open', function () {
117       mong.disconnect(function () {
118         called = true;
119       });
120     });
121
122     beforeExit(function () {
123       called.should.be.true;
124     });
125   },
126
127   'try accessing a model that hasn\'t been defined': function () {
128     var mong = new Mongoose()
129       , thrown = false;
130
131     try {
132       mong.model('Test');
133     } catch (e) {
134       /hasn't been registered/.test(e.message).should.be.true;
135       thrown = true;
136     }
137
138     thrown.should.be.true;
139   },
140
141   'test connecting with a signature of host, database, function': function (){
142     var mong = new Mongoose()
143       , uri = process.env.MONGOOSE_TEST_URI || 'mongodb://localhost/mongoose_test';
144
145     uri = url.parse(uri);
146
147     mong.connect(uri.hostname, uri.pathname.substr(1), function (err) {
148       should.strictEqual(err, null);
149       mong.connection.close();
150     });
151   },
152
153   'test connecting to a replica set': function () {
154     var uri = process.env.MONGOOSE_SET_TEST_URI;
155
156     if (!uri) {
157       console.log('\033[31m', '\n', 'You\'re not testing for replica sets!'
158                 , '\n', 'Please set the MONGOOSE_SET_TEST_URI env variable.', '\n'
159                 , 'e.g: `mongodb://localhost:27017/db,mongodb://localhost…`', '\n'
160                 , '\033[39m');
161       return;
162     }
163
164     var mong = new Mongoose();
165
166     mong.connectSet(uri, function (err) {
167       should.strictEqual(err, null);
168
169       mong.model('Test', new mongoose.Schema({
170           test: String
171       }));
172
173       var Test = mong.model('Test')
174         , test = new Test();
175
176       test.test = 'aa';
177       test.save(function (err) {
178         should.strictEqual(err, null);
179
180         Test.findById(test._id, function (err, doc) {
181           should.strictEqual(err, null);
182           
183           doc.test.should.eql('aa');
184
185           mong.connection.close();
186         });
187       });
188     });
189   },
190
191   'test initializing a new Connection to a replica set': function () {
192     var uri = process.env.MONGOOSE_SET_TEST_URI;
193
194     if (!uri) return;
195
196     var mong = new Mongoose(true);
197
198     var conn = mong.createSetConnection(uri, function (err) {
199       should.strictEqual(err, null);
200
201       mong.model('ReplSetTwo', new mongoose.Schema({
202           test: String
203       }));
204
205       var Test = conn.model('ReplSetTwo')
206         , test = new Test();
207
208       test.test = 'aa';
209       test.save(function (err) {
210         should.strictEqual(err, null);
211
212         Test.findById(test._id, function (err, doc) {
213           should.strictEqual(err, null);
214           
215           doc.test.should.eql('aa');
216
217           conn.close();
218         });
219       });
220     });
221   },
222
223   'test public exports': function () {
224     mongoose.version.should.be.a('string');
225     mongoose.Collection.should.be.a('function');
226     mongoose.Connection.should.be.a('function');
227     mongoose.Schema.should.be.a('function');
228     mongoose.SchemaType.should.be.a('function');
229     mongoose.Query.should.be.a('function');
230     mongoose.Promise.should.be.a('function');
231     mongoose.Model.should.be.a('function');
232     mongoose.Document.should.be.a('function');
233   }
234
235 };