forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgh-1408.test.js
91 lines (76 loc) · 2.67 KB
/
gh-1408.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
/**
* Test dependencies.
*/
var start = require('./common')
, assert = require('assert')
, mongoose = start.mongoose
, random = require('../lib/utils').random
, Query = require('../lib/query')
, Schema = mongoose.Schema
, SchemaType = mongoose.SchemaType
, CastError = mongoose.Error.CastError
, ValidatorError = mongoose.Error.ValidatorError
, ValidationError = mongoose.Error.ValidationError
, ObjectId = Schema.Types.ObjectId
, DocumentObjectId = mongoose.Types.ObjectId
, DocumentArray = mongoose.Types.DocumentArray
, EmbeddedDocument = mongoose.Types.Embedded
, MongooseArray = mongoose.Types.Array
, MongooseError = mongoose.Error;
describe('documents should not be converted to _id (gh-1408)', function(){
it('if an embedded doc', function(done){
var db = start();
var PreferenceSchema = new Schema ({
_id: {type: Schema.ObjectId, auto: true},
preference: { type: String, required: true },
value: { type: Schema.Types.Mixed }
}, { versionKey: false });
var BrandSchema = new Schema({
settings: {
preferences: [PreferenceSchema],
}
});
var A = db.model('gh-1408', BrandSchema);
var a = new A({
settings: {
preferences:
[ { preference: 'group_colors', value: false },
{ preference: 'can_force_orders', value: true },
{ preference: 'hide_from_buyers', value: true },
{ preference: 'no_orders', value: '' }
]
}
})
a.save(function (err, a) {
if (err) return done(err);
A.findById(a, function (err, doc) {
if (err) return done(err);
var newData = {
settings: {
preferences:
[ { preference: 'group_colors', value: true },
{ preference: 'can_force_orders', value: true },
{ preference: 'custom_csv', value: '' },
{ preference: 'hide_from_buyers', value: false },
{ preference: 'nozoom', value: false },
{ preference: 'no_orders', value: false }
]
}
}
doc.set('settings', newData.settings, { merge: true });
doc.markModified('settings'); // <== this caused the bug
doc.save(function (err) {
if (err) return done(err);
A.findById(doc, function (err, doc) {
if (err) return done(err);
doc.settings.preferences.forEach(function (pref, i) {
assert.equal(pref.preference, newData.settings.preferences[i].preference);
assert.equal(pref.value, newData.settings.preferences[i].value);
})
done();
})
})
})
})
})
})