forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.hooks.test.js
407 lines (337 loc) · 8.62 KB
/
document.hooks.test.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/**
* Module dependencies.
*/
var start = require('./common')
, mongoose = start.mongoose
, assert = require('assert')
, Schema = mongoose.Schema
, ObjectId = Schema.ObjectId
, Document = require('../lib/document')
, DocumentObjectId = mongoose.Types.ObjectId;
/**
* Test Document constructor.
*/
function TestDocument () {
Document.apply(this, arguments);
};
/**
* Inherits from Document.
*/
TestDocument.prototype.__proto__ = Document.prototype;
/**
* Set a dummy schema to simulate compilation.
*/
var em = new Schema({ title: String, body: String });
em.virtual('works').get(function () {
return 'em virtual works'
});
var schema = new Schema({
test : String
, oids : [ObjectId]
, numbers : [Number]
, nested : {
age : Number
, cool : ObjectId
, deep : { x: String }
, path : String
, setr : String
}
, nested2 : {
nested: String
, yup : {
nested : Boolean
, yup : String
, age : Number
}
}
, em: [em]
});
TestDocument.prototype.$__setSchema(schema);
schema.virtual('nested.agePlus2').get(function (v) {
return this.nested.age + 2;
});
schema.virtual('nested.setAge').set(function (v) {
this.nested.age = v;
});
schema.path('nested.path').get(function (v) {
return this.nested.age + (v ? v : '');
});
schema.path('nested.setr').set(function (v) {
return v + ' setter';
});
/**
* Method subject to hooks. Simply fires the callback once the hooks are
* executed.
*/
TestDocument.prototype.hooksTest = function(fn){
fn(null, arguments);
};
describe('document: hooks:', function () {
it('step order', function(done){
var doc = new TestDocument()
, steps = 0
, awaiting = 0
, called = false;
// serial
doc.pre('hooksTest', function(next){
steps++;
setTimeout(function(){
// make sure next step hasn't executed yet
assert.equal(1, steps);
next();
}, 50);
});
doc.pre('hooksTest', function(next){
steps++;
next();
});
// parallel
doc.pre('hooksTest', true, function(next, done){
steps++;
assert.equal(3, steps);
setTimeout(function(){
assert.equal(4, steps);
}, 10);
setTimeout(function(){
steps++;
done();
}, 110);
next();
});
doc.pre('hooksTest', true, function(next, done){
steps++;
setTimeout(function(){
assert.equal(4, steps);
}, 10);
setTimeout(function(){
steps++;
done();
}, 110);
next();
});
doc.hooksTest(function(err){
assert.ifError(err);
assert.equal(6, steps);
done();
});
});
it('calling next twice does not break', function(done){
var doc = new TestDocument()
, steps = 0
, called = false;
doc.pre('hooksTest', function(next){
steps++;
next();
next();
});
doc.pre('hooksTest', function(next){
steps++;
next();
});
doc.hooksTest(function(err){
assert.ifError(err);
assert.equal(2, steps);
done();
});
});
it('calling done twice does not break', function(done){
var doc = new TestDocument()
, steps = 0
doc.pre('hooksTest', true, function(next, done){
steps++;
next();
done();
done();
});
doc.pre('hooksTest', true, function(next, done){
steps++;
next();
done();
done();
});
doc.hooksTest(function(err){
assert.ifError(err);
assert.equal(2, steps);
done();
});
});
it('errors from a serial hook', function(done){
var doc = new TestDocument()
, steps = 0
, called = false;
doc.pre('hooksTest', function(next){
steps++;
next();
});
doc.pre('hooksTest', function(next){
steps++;
next(new Error);
});
doc.pre('hooksTest', function(next){
steps++;
});
doc.hooksTest(function(err){
assert.ok(err instanceof Error);
assert.equal(2, steps);
done();
});
});
it('errors from last serial hook', function(done){
var doc = new TestDocument()
, called = false;
doc.pre('hooksTest', function(next){
next(new Error);
});
doc.hooksTest(function(err){
assert.ok(err instanceof Error);
done();
});
});
it('mutating incoming args via middleware', function(done){
var doc = new TestDocument();
doc.pre('set', function(next, path, val){
next(path, 'altered-' + val);
});
doc.set('test', 'me');
assert.equal('altered-me', doc.test);
done();
});
it('test hooks system errors from a parallel hook', function(done){
var doc = new TestDocument()
, steps = 0
, called = false;
doc.pre('hooksTest', true, function(next, done){
steps++;
next();
done();
});
doc.pre('hooksTest', true, function(next, done){
steps++;
next();
done();
});
doc.pre('hooksTest', true, function(next, done){
steps++;
next();
done(new Error);
});
doc.hooksTest(function(err){
assert.ok(err instanceof Error);
assert.equal(3, steps);
done();
});
});
it('passing two arguments to a method subject to hooks and return value', function(done){
var doc = new TestDocument()
, called = false;
doc.pre('hooksTest', function (next) {
next();
});
doc.hooksTest(function (err, args) {
assert.equal(2, args.length);
assert.equal(args[1], 'test');
done();
}, 'test')
});
it('hooking set works with document arrays (gh-746)', function(done){
var db = start();
var child = new Schema({ text: String });
child.pre('set', function (next, path, value, type) {
next(path, value, type);
});
var schema = new Schema({
name: String
, e: [child]
});
var S = db.model('docArrayWithHookedSet', schema);
var s = new S({ name: "test" });
s.e = [{ text: 'hi' }];
s.save(function (err) {
assert.ifError(err);
S.findById(s.id, function (err ,s) {
assert.ifError(err);
s.e = [{ text: 'bye' }];
s.save(function (err) {
assert.ifError(err);
S.findById(s.id, function (err, s) {
db.close();
assert.ifError(err);
assert.equal('bye', s.e[0].text);
done();
})
})
})
});
});
it('pre save hooks on sub-docs should not exec after validation errors', function(done){
var db = start();
var presave = false;
var child = new Schema({ text: { type: String, required: true }});
child.pre('save', function (next) {
presave = true;
next();
});
var schema = new Schema({
name: String
, e: [child]
});
var S = db.model('docArrayWithHookedSave', schema);
var s = new S({ name: 'hi', e: [{}] });
s.save(function (err) {
assert.ok(err);
assert.ok(err.errors['e.0.text']);
assert.equal(false, presave);
done();
});
});
it('post remove hooks on subdocuments work', function(done) {
var db = start();
var sub = Schema({ _id: Number });
var called = { pre: 0, post: 0 };
sub.pre('remove', function (next) {
called.pre++;
next();
});
sub.post('remove', function (doc) {
called.post++;
assert.ok(doc instanceof Document);
});
var par = Schema({ sub: [sub], name: String });
var M = db.model('post-remove-hooks-sub', par);
var m = new M({ sub: [{ _id: 1 }, { _id: 2 }] });
m.save(function (err) {
assert.ifError(err);
assert.equal(0, called.pre);
assert.equal(0, called.post);
M.findById(m, function(err, doc) {
assert.ifError(err);
doc.sub.id(1).remove();
doc.save(function (err) {
assert.ifError(err);
assert.equal(1, called.pre);
assert.equal(1, called.post);
// does not get called when not removed
doc.name = 'changed1';
doc.save(function (err) {
assert.ifError(err);
assert.equal(1, called.pre);
assert.equal(1, called.post);
doc.sub.id(2).remove();
doc.remove(function (err) {
assert.ifError(err);
assert.equal(2, called.pre);
assert.equal(2, called.post);
// does not get called twice
doc.remove(function (err) {
assert.ifError(err);
assert.equal(2, called.pre);
assert.equal(2, called.post);
done();
});
});
});
});
});
});
})
});