forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.strict.test.js
368 lines (300 loc) · 9.64 KB
/
document.strict.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
/**
* Test dependencies.
*/
var start = require('./common')
, mongoose = start.mongoose
, assert = require('assert')
, 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
, MongooseNumber = mongoose.Types.Number
, MongooseArray = mongoose.Types.Array
, MongooseError = mongoose.Error;
describe('document: strict mode:', function(){
it('should work', function(done){
var db = start();
var raw = {
ts : { type: Date, default: Date.now }
, content: String
, mixed: {}
, deepMixed: { '4a': {}}
, arrayMixed: []
};
var lax = new Schema(raw, { strict: false });
var strict = new Schema(raw);
var Lax = db.model('Lax', lax);
var Strict = db.model('Strict', strict);
var l = new Lax({content: 'sample', rouge: 'data'});
assert.equal(false, l.$__.strictMode);
l = l.toObject();
assert.ok('ts' in l);
assert.equal('sample', l.content);
assert.equal('data', l.rouge);
var s = new Strict({content: 'sample', rouge: 'data'});
assert.equal(true, s.$__.strictMode);
s = s.toObject();
assert.ok('ts' in s);
assert.equal('sample', s.content);
assert.ok(!('rouge' in s));
assert.ok(!s.rouge);
// instance override
var instance = new Lax({content: 'sample', rouge: 'data'}, true);
assert.ok(instance.$__.strictMode);
instance = instance.toObject();
assert.equal('sample', instance.content);
assert.ok(!instance.rouge);
assert.ok('ts' in instance);
// hydrate works as normal, but supports the schema level flag.
var s2 = new Strict({content: 'sample', rouge: 'data'}, false);
assert.equal(false, s2.$__.strictMode);
s2 = s2.toObject();
assert.ok('ts' in s2);
assert.equal('sample', s2.content);
assert.ok('rouge' in s2);
// testing init
var s3 = new Strict();
s3.init({content: 'sample', rouge: 'data'});
var s3obj = s3.toObject();
assert.equal('sample', s3.content);
assert.ok(!('rouge' in s3));
assert.ok(!s3.rouge);
// strict on create
Strict.create({content: 'sample2', rouge: 'data'}, function(err, doc){
db.close();
assert.equal('sample2', doc.content);
assert.ok(!('rouge' in doc));
assert.ok(!doc.rouge);
done();
});
})
it('nested doc', function(done){
var db = start();
var lax = new Schema({
name: { last: String }
}, { strict: false });
var strict = new Schema({
name: { last: String }
});
var Lax = db.model('NestedLax', lax, 'nestdoc'+random());
var Strict = db.model('NestedStrict', strict, 'nestdoc'+random());
db.close();
var l = new Lax;
l.set('name', { last: 'goose', hack: 'xx' });
l = l.toObject();
assert.equal('goose', l.name.last);
assert.equal('xx', l.name.hack);
var s = new Strict;
s.set({ name: { last: 'goose', hack: 'xx' }});
s = s.toObject();
assert.equal('goose', s.name.last);
assert.ok(!('hack' in s.name));
assert.ok(!s.name.hack);
s = new Strict;
s.set('name', { last: 'goose', hack: 'xx' });
s.set('shouldnt.exist', ':(');
s = s.toObject();
assert.equal('goose', s.name.last);
assert.ok(!('hack' in s.name));
assert.ok(!s.name.hack);
assert.ok(!s.shouldnt);
done();
})
it('sub doc', function(done){
var db = start();
var lax = new Schema({
ts : { type: Date, default: Date.now }
, content: String
}, { strict: false });
var strict = new Schema({
ts : { type: Date, default: Date.now }
, content: String
});
var Lax = db.model('EmbeddedLax', new Schema({ dox: [lax] }, { strict: false }), 'embdoc'+random());
var Strict = db.model('EmbeddedStrict', new Schema({ dox: [strict] }, { strict: false }), 'embdoc'+random());
var l = new Lax({ dox: [{content: 'sample', rouge: 'data'}] });
assert.equal(false, l.dox[0].$__.strictMode);
l = l.dox[0].toObject();
assert.equal('sample', l.content);
assert.equal('data', l.rouge);
assert.ok(l.rouge);
var s = new Strict({ dox: [{content: 'sample', rouge: 'data'}] });
assert.equal(true, s.dox[0].$__.strictMode);
s = s.dox[0].toObject();
assert.ok('ts' in s);
assert.equal('sample', s.content);
assert.ok(!('rouge' in s));
assert.ok(!s.rouge);
// testing init
var s3 = new Strict();
s3.init({dox: [{content: 'sample', rouge: 'data'}]});
var s3obj = s3.toObject();
assert.equal('sample', s3.dox[0].content);
assert.ok(!('rouge' in s3.dox[0]));
assert.ok(!s3.dox[0].rouge);
// strict on create
Strict.create({dox:[{content: 'sample2', rouge: 'data'}]}, function(err, doc){
db.close();
assert.equal('sample2', doc.dox[0].content);
assert.ok(!('rouge' in doc.dox[0]));
assert.ok(!doc.dox[0].rouge);
done();
});
})
it('virtuals', function(done){
var db = start();
var getCount = 0
, setCount = 0;
var strictSchema = new Schema({
email: String
, prop: String
});
strictSchema
.virtual('myvirtual')
.get(function() {
getCount++;
return 'ok';
})
.set(function(v) {
setCount++;
this.prop = v;
});
var StrictModel = db.model('StrictVirtual', strictSchema);
var strictInstance = new StrictModel({
email: 'hunter@skookum.com'
, myvirtual: 'test'
});
db.close();
assert.equal(0, getCount);
assert.equal(1, setCount);
strictInstance.myvirtual = 'anotherone';
var myvirtual = strictInstance.myvirtual;
assert.equal(1, getCount);
assert.equal(2, setCount);
done();
})
it('can be overridden during set()', function(done){
var db = start();
var strict = new Schema({
bool: Boolean
});
var Strict = db.model('Strict', strict);
var s = new Strict({ bool: true });
// insert non-schema property
var doc = s.toObject();
doc.notInSchema = true;
Strict.collection.insert(doc, { w: 1 }, function (err) {
assert.ifError(err);
Strict.findById(doc._id, function (err, doc) {
assert.ifError(err);
assert.equal(true, doc._doc.bool);
assert.equal(true, doc._doc.notInSchema);
doc.bool = undefined;
doc.set('notInSchema', undefined, { strict: false });
doc.save(function (err) {
Strict.findById(doc._id, function (err, doc) {
db.close();
assert.ifError(err);
assert.equal(undefined, doc._doc.bool);
assert.equal(undefined, doc._doc.notInSchema);
done();
});
})
})
})
})
it('can be overridden during update()', function(done){
var db = start();
var strict = new Schema({
bool: Boolean
});
var Strict = db.model('Strict', strict);
var s = new Strict({ bool: true });
// insert non-schema property
var doc = s.toObject();
doc.notInSchema = true;
Strict.collection.insert(doc, { w: 1 }, function (err) {
assert.ifError(err);
Strict.findById(doc._id, function (err, doc) {
assert.ifError(err);
assert.equal(true, doc._doc.bool);
assert.equal(true, doc._doc.notInSchema);
Strict.update(
{ _id: doc._id }
, { $unset: { bool: 1, notInSchema: 1 }}
, { strict: false, w: 1 }
, function (err) {
assert.ifError(err);
Strict.findById(doc._id, function (err, doc) {
db.close();
assert.ifError(err);
assert.equal(undefined, doc._doc.bool);
assert.equal(undefined, doc._doc.notInSchema);
done();
});
})
})
})
})
describe('"throws" mode', function(){
it('throws on set() of unknown property', function(done){
var schema = Schema({ n: String, docs:[{x:[{y:String}]}] });
schema.set('strict', 'throw')
var M = mongoose.model('throwStrictSet', schema, 'tss_'+random());
var m = new M;
var badField = /Field `[\w\.]+` is not in schema/;
assert.throws(function(){
m.set('unknown.stuff.is.here', 3);
}, badField);
assert.throws(function(){
m.set('n.something', 3);
}, badField);
assert.throws(function(){
m.set('n.3', 3);
}, badField);
assert.throws(function(){
m.set('z', 3);
}, badField);
assert.throws(function(){
m.set('docs.z', 3);
}, badField);
assert.throws(function(){
m.set('docs.0.z', 3);
}, badField);
assert.throws(function(){
m.set('docs.0.x.z', 3);
}, badField);
assert.throws(function(){
m.set('docs.0.x.4.z', 3);
}, badField);
assert.throws(function(){
m.set('docs.0.x.4.y.z', 3);
}, badField);
done();
})
it('fails with extra fields', function (done) {
var m = new mongoose.Mongoose;
// Simple schema with throws option
var FooSchema = new mongoose.Schema({
name: { type: String }
}, {strict: "throw"});
// Create the model
var Foo = m.model('Foo', FooSchema);
assert.doesNotThrow(function(){
new Foo({name: 'bar'});
})
assert.throws(function(){
// The extra baz field should throw
new Foo({name: 'bar', baz: 'bam'});
}, /Field `baz` is not in schema/);
done();
});
})
})