/
common.js
138 lines (113 loc) · 2.71 KB
/
common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Module dependencies.
*/
var mongoose = require('../')
, should = require('should')
, Table = require('cli-table')
, Mongoose = mongoose.Mongoose
, Collection = mongoose.Collection
, Assertion = should.Assertion
, startTime = Date.now()
, queryCount = 0
, opened = 0
, closed = 0;
/**
* Override all Collection related queries to keep count
*/
[ 'ensureIndex'
, 'findAndModify'
, 'findOne'
, 'find'
, 'insert'
, 'save'
, 'update'
, 'remove'
, 'count'
, 'distinct'
].forEach(function (method) {
var oldMethod = Collection.prototype[method];
Collection.prototype[method] = function () {
queryCount++;
return oldMethod.apply(this, arguments);
};
});
/**
* Override Collection#onOpen to keep track of connections
*/
var oldOnOpen = Collection.prototype.onOpen;
Collection.prototype.onOpen = function(){
opened++;
return oldOnOpen.apply(this, arguments);
};
/**
* Override Collection#onClose to keep track of disconnections
*/
var oldOnClose = Collection.prototype.onClose;
Collection.prototype.onClose = function(){
closed++;
return oldOnClose.apply(this, arguments);
};
/**
* Assert that a connection is open or that mongoose connections are open.
* Examples:
* mongoose.should.be.connected;
* db.should.be.connected;
*
* @api public
*/
Assertion.prototype.__defineGetter__('connected', function(){
if (this.obj instanceof Mongoose)
this.obj.connections.forEach(function(connected){
c.should.be.connected;
});
else
this.obj.readyState.should.eql(1);
});
/**
* Assert that a connection is closed or that a mongoose connections are closed.
* Examples:
* mongoose.should.be.disconnected;
* db.should.be.disconnected;
*
* @api public
*/
Assertion.prototype.__defineGetter__('disconnected', function(){
if (this.obj instanceof Mongoose)
this.obj.connections.forEach(function(){
c.should.be.disconnected;
});
else
this.obj.readyState.should.eql(0);
});
/**
* Create a connection to the test database.
* You can set the environmental variable MONGOOSE_TEST_URI to override this.
*
* @api private
*/
module.exports = function (options) {
return mongoose.createConnection(
process.env.MONGOOSE_TEST_URI || 'mongodb://localhost/mongoose_test'
, options
);
};
/**
* Provide stats for tests
*/
process.on('beforeExit', function(){
var table = new Table({
head: ['Stat', 'Time (ms)']
, colWidths: [23, 15]
});
table.push(
['Queries run', queryCount]
, ['Time ellapsed', Date.now() - startTime]
, ['Connections opened', opened]
, ['Connections closed', closed]
);
console.error(table.toString());
});
/**
* Module exports.
*/
module.exports.mongoose = mongoose;