deno.land / x / mongoose@6.7.5 / test / types.embeddeddocumentdeclarative.test.js

types.embeddeddocumentdeclarative.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
'use strict';
/** * Module dependencies. */
const assert = require('assert');const start = require('./common');
const mongoose = start.mongoose;const Schema = mongoose.Schema;
/** * Test. */
describe('types.embeddeddocumentdeclarative', function() { describe('with a parent with a field with type set to a POJO', function() { const ParentSchemaDef = { name: String, child: { type: { name: String } } };
describe('creates subdocument schema if `type` is an object with keys', function() { const ParentSchema = new mongoose.Schema(ParentSchemaDef); it('interprets the POJO as a subschema (gh-7494)', function(done) { assert.equal(ParentSchema.paths.child.instance, 'Embedded'); assert.strictEqual(ParentSchema.paths.child['$isSingleNested'], true); done(); }); it('enforces provided schema on the child path, unlike Mixed (gh-7494)', function(done) { const ParentModel = mongoose.model('ParentModel-7494-EmbeddedDeclarativeSubschema', ParentSchema); const kingDaphnes = new ParentModel({ name: 'King Daphnes Nohansen Hyrule', child: { name: 'Princess Zelda', mixedUp: 'not' } }); const princessZelda = kingDaphnes.child.toObject();
assert.equal(princessZelda.name, 'Princess Zelda'); assert.strictEqual(princessZelda.mixedUp, undefined); done(); });
it('underneath array (gh-8627)', function() { const schema = new Schema({ arr: [{ nested: { type: { test: String } } }] });
assert.ok(schema.path('arr').schema.path('nested').instance !== 'Mixed'); assert.ok(schema.path('arr').schema.path('nested.test') instanceof mongoose.Schema.Types.String); });
it('nested array (gh-8627)', function() { const schema = new Schema({ l1: { type: { l2: { type: { test: String } } } } });
assert.ok(schema.path('l1').instance !== 'Mixed'); assert.ok(schema.path('l1.l2').instance !== 'Mixed'); }); }); }); describe('with a parent with a POJO field with a field "type" with a type set to "String"', function() { const ParentSchemaDef = { name: String, child: { name: String, type: { type: String } } }; const ParentSchemaNotMixed = new Schema(ParentSchemaDef); const ParentSchemaNotSubdoc = new Schema(ParentSchemaDef); it('does not create a path for child in either option', function(done) { assert.equal(ParentSchemaNotMixed.paths['child.name'].instance, 'String'); assert.equal(ParentSchemaNotSubdoc.paths['child.name'].instance, 'String'); done(); }); it('treats type as a property name not a type in both options', function(done) { assert.equal(ParentSchemaNotMixed.paths['child.type'].instance, 'String'); assert.equal(ParentSchemaNotSubdoc.paths['child.type'].instance, 'String'); done(); }); it('enforces provided schema on the child tree in both options, unlike Mixed (gh-7494)', function(done) { const ParentModelNotMixed = mongoose.model('ParentModel-7494-EmbeddedDeclarativeNestedNotMixed', ParentSchemaNotMixed); const ParentModelNotSubdoc = mongoose.model('ParentModel-7494-EmbeddedDeclarativeNestedNotSubdoc', ParentSchemaNotSubdoc);
const grandmother = new ParentModelNotMixed({ name: 'Grandmother', child: { name: 'Rito Chieftan', type: 'Mother', confidence: 10 } }); const ritoChieftan = new ParentModelNotSubdoc({ name: 'Rito Chieftan', child: { name: 'Prince Komali', type: 'Medli', confidence: 1 } });
assert.equal(grandmother.child.name, 'Rito Chieftan'); assert.equal(grandmother.child.type, 'Mother'); assert.strictEqual(grandmother.child.confidence, undefined); assert.equal(ritoChieftan.child.name, 'Prince Komali'); assert.equal(ritoChieftan.child.type, 'Medli'); assert.strictEqual(ritoChieftan.child.confidence, undefined); done(); }); });});
mongoose

Version Info

Tagged at
a year ago