/
types.subdocument.test.js
107 lines (90 loc) · 2.16 KB
/
types.subdocument.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
/**
* Module dependencies.
*/
'use strict';
const assert = require('assert');
const start = require('./common');
const mongoose = start.mongoose;
const Schema = mongoose.Schema;
/**
* Test.
*/
describe('types.subdocument', function() {
let GrandChildSchema;
let ChildSchema;
let ParentSchema;
let db;
before(function() {
GrandChildSchema = new Schema({
name: String
});
ChildSchema = new Schema({
name: String,
child: GrandChildSchema
});
ParentSchema = new Schema({
name: String,
children: [ChildSchema]
});
mongoose.model('Parent-3589-Sub', ParentSchema);
db = start();
});
after(function(done) {
db.close(done);
});
it('returns a proper ownerDocument (gh-3589)', function(done) {
const Parent = mongoose.model('Parent-3589-Sub');
const p = new Parent({
name: 'Parent Parentson',
children: [
{
name: 'Child Parentson',
child: {
name: 'GrandChild Parentson'
}
}
]
});
assert.equal(p._id, p.children[0].child.ownerDocument()._id);
done();
});
it('not setting timestamps in subdocuments', function() {
const Thing = db.model('Thing', new Schema({
subArray: [{
testString: String
}]
}, {
timestamps: true
}));
const thingy = new Thing({
subArray: [{
testString: 'Test 1'
}]
});
let id;
return thingy.save().
then(function() {
id = thingy._id;
}).
then(function() {
const thingy2 = {
subArray: [{
testString: 'Test 2'
}]
};
return Thing.updateOne({
_id: id
}, {$set: thingy2});
});
});
describe('#isModified', function() {
it('defers to parent isModified (gh-8223)', function() {
const childSchema = Schema({ id: Number, text: String });
const parentSchema = Schema({ child: childSchema });
const Model = db.model('gh8223', parentSchema);
const doc = new Model({ child: { text: 'foo' } });
assert.ok(doc.isModified('child.id'));
assert.ok(doc.child.isModified('id'));
});
});
});