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;
11 'test connecting to the demo database': function(beforeExit){
15 db.on('open', function(){
20 beforeExit(function(){
21 connected.should.be.true;
25 'test default connection': function(beforeExit){
26 var db = mongoose.connection
27 , uri = 'mongodb://localhost/mongoose_test'
30 mongoose.connect(process.env.MONGOOSE_TEST_URI || uri);
31 db.on('open', function(){
36 beforeExit(function(){
37 connected.should.be.true;
41 'test setting options': function(){
42 var mongoose = new Mongoose();
44 mongoose.set('a', 'b');
45 mongoose.set('long option', 'c');
47 mongoose.get('a').should.eql('b');
48 mongoose.set('a').should.eql('b');
49 mongoose.get('long option').should.eql('c');
52 'test declaring global plugins': function (beforeExit) {
53 var mong = new Mongoose()
54 , schema = new Schema()
57 mong.plugin(function (s) {
58 s.should.equal(schema);
62 schema.plugin(function (s) {
63 s.should.equal(schema);
67 mong.model('GlobalPlugins', schema);
69 beforeExit(function () {
74 'test disconnection of all connections': function (beforeExit) {
75 var mong = new Mongoose()
76 , uri = 'mongodb://localhost/mongoose_test'
80 mong.connect(process.env.MONGOOSE_TEST_URI || uri);
81 var db = mong.connection;
83 db.on('open', function(){
87 db.on('close', function () {
91 var db2 = mong.createConnection(process.env.MONGOOSE_TEST_URI || uri);
93 db2.on('open', function () {
97 db2.on('close', function () {
103 beforeExit(function () {
104 connections.should.eql(2);
105 disconnections.should.eql(2);
109 'test disconnection of all connections callback': function (beforeExit) {
110 var mong = new Mongoose()
111 , uri = 'mongodb://localhost/mongoose_test'
114 mong.connect(process.env.MONGOOSE_TEST_URI || uri);
116 mong.connection.on('open', function () {
117 mong.disconnect(function () {
122 beforeExit(function () {
123 called.should.be.true;
127 'try accessing a model that hasn\'t been defined': function () {
128 var mong = new Mongoose()
134 /hasn't been registered/.test(e.message).should.be.true;
138 thrown.should.be.true;
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';
145 uri = url.parse(uri);
147 mong.connect(uri.hostname, uri.pathname.substr(1), function (err) {
148 should.strictEqual(err, null);
149 mong.connection.close();
153 'test connecting to a replica set': function () {
154 var uri = process.env.MONGOOSE_SET_TEST_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'
164 var mong = new Mongoose();
166 mong.connectSet(uri, function (err) {
167 should.strictEqual(err, null);
169 mong.model('Test', new mongoose.Schema({
173 var Test = mong.model('Test')
177 test.save(function (err) {
178 should.strictEqual(err, null);
180 Test.findById(test._id, function (err, doc) {
181 should.strictEqual(err, null);
183 doc.test.should.eql('aa');
185 mong.connection.close();
191 'test initializing a new Connection to a replica set': function () {
192 var uri = process.env.MONGOOSE_SET_TEST_URI;
196 var mong = new Mongoose(true);
198 var conn = mong.createSetConnection(uri, function (err) {
199 should.strictEqual(err, null);
201 mong.model('ReplSetTwo', new mongoose.Schema({
205 var Test = conn.model('ReplSetTwo')
209 test.save(function (err) {
210 should.strictEqual(err, null);
212 Test.findById(test._id, function (err, doc) {
213 should.strictEqual(err, null);
215 doc.test.should.eql('aa');
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');