forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.documentarray.test.js
More file actions
73 lines (61 loc) · 2.05 KB
/
schema.documentarray.test.js
File metadata and controls
73 lines (61 loc) · 2.05 KB
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
/**
* Module dependencies.
*/
var start = require('./common'),
mongoose = start.mongoose,
assert = require('power-assert'),
Schema = mongoose.Schema;
/**
* Test.
*/
describe('schema.documentarray', function() {
it('defaults should be preserved', function(done) {
var child = new Schema({title: String});
var schema1 = new Schema({x: {type: [child], default: [{title: 'Prometheus'}]}});
var schema2 = new Schema({x: {type: [child], default: {title: 'Prometheus'}}});
var schema3 = new Schema({
x: {
type: [child], default: function() {
return [{title: 'Prometheus'}];
}
}
});
var M = mongoose.model('DefaultDocArrays1', schema1);
var N = mongoose.model('DefaultDocArrays2', schema2);
var O = mongoose.model('DefaultDocArrays3', schema3);
[M, N, O].forEach(function(M) {
var m = new M;
assert.ok(Array.isArray(m.x));
assert.equal(m.x.length, 1);
assert.equal(m.x[0].title, 'Prometheus');
});
done();
});
it('only sets if document has same schema (gh-3701)', function(done) {
var schema1 = new Schema({
arr: [new Schema({a: Number, b: Number}, {_id: false})]
});
var schema2 = new Schema({
arr: [new Schema({a: Number}, {_id: false})]
});
var Model1 = mongoose.model('gh3701_0', schema1);
var Model2 = mongoose.model('gh3701_1', schema2);
var source = new Model1({arr: [{a: 1, b: 1}, {a: 2, b: 2}]});
var dest = new Model2({arr: source.arr});
assert.deepEqual(dest.toObject().arr, [{a: 1}, {a: 2}]);
done();
});
it('sets $implicitlyCreated if created by interpretAsType (gh-4271)', function(done) {
var schema1 = new Schema({
arr: [{ name: String }]
});
var schema2 = new Schema({
arr: [new Schema({ name: String })]
});
assert.equal(schema1.childSchemas.length, 1);
assert.equal(schema2.childSchemas.length, 1);
assert.ok(schema1.childSchemas[0].schema.$implicitlyCreated);
assert.ok(!schema2.childSchemas[0].schema.$implicitlyCreated);
done();
});
});