forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.create.null.test.js
141 lines (113 loc) · 2.79 KB
/
object.create.null.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
/**
* Test dependencies.
*/
var start = require('./common')
, assert = require('assert')
, mongoose = start.mongoose
, DivergentArrayError = mongoose.Error.DivergentArrayError
, utils = require('../lib/utils')
, random = utils.random
, Schema = mongoose.Schema
, ObjectId = Schema.ObjectId
var schema = new Schema({
a: String
, b: {
c: Number
, d: [{ e: String }]
}
, f: { g: Date }
, h: {}
});
describe('is compatible with object created using Object.create(null) (gh-1484)', function(){
var db;
var M;
before(function(){
db = start();
M = db.model('1484', schema);
})
after(function(done){
db.close(done);
})
it('during construction', function(done){
assert.doesNotThrow(function () {
new M(Object.create(null));
});
assert.doesNotThrow(function () {
var o = Object.create(null);
o.b = Object.create(null);
new M(o);
});
assert.doesNotThrow(function () {
var o = Object.create(null);
o.b = Object.create(null);
o.b.c = 9;
var e = Object.create(null);
e.e = 'hi i am a string';
o.b.d = [e];
var date = new Date;
var f = Object.create(null);
f.g = date;
o.f = f;
var h = Object.create(null);
h.ad = 1;
h.hoc = 2;
h.obj = Object.create(null);
o.h = h
var m = new M(o);
assert.equal(9, m.b.c);
assert.equal('hi i am a string', m.b.d[0].e);
assert.equal(date, m.f.g);
assert.equal(1, m.h.ad);
assert.equal(2, m.h.hoc);
assert.deepEqual({},m.h.obj);
});
done();
})
it('with .set(path, obj)', function(done){
var m = new M;
var b = Object.create(null);
b.c = 9;
m.set('b', b);
var ee = Object.create(null);
ee.e = 'hi i am a string';
var e = [ee];
m.set('b.d', e);
var date = new Date;
var f = Object.create(null);
f.g = date;
m.set('f', f);
var thing = Object.create(null);
thing.h = 'yes';
m.set('h.obj.thing', thing);
assert.equal(9, m.b.c);
assert.equal('hi i am a string', m.b.d[0].e);
assert.equal(date, m.f.g);
assert.deepEqual('yes', m.h.obj.thing.h);
done();
})
it('with schema', function(done){
var o = Object.create(null);
o.name = String;
o.created = Date;
o.nested = Object.create(null);
o.nested.n = Number;
assert.doesNotThrow(function () {
new Schema(o);
});
assert.doesNotThrow(function () {
var s = new Schema;
var o = Object.create(null);
o.yay = Number;
s.path('works', o);
});
assert.doesNotThrow(function () {
var s = new Schema;
var o = Object.create(null);
var o = {};
o.name = String;
var x = { type: [o] };
s.path('works', x);
});
done();
})
})