deno.land / x / mongoose@6.7.5 / test / schema.subdocumentpath.test.js

schema.subdocumentpath.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
'use strict';
/** * Module dependencies. */
const mongoose = require('./common').mongoose;
const assert = require('assert');
const Schema = mongoose.Schema;
describe('SubdocumentPath', function() { describe('discriminator()', function() { describe('recursive nested discriminators', function() { it('allow multiple levels of data in the schema', function() { const singleEventSchema = new Schema({ message: String }, { _id: false, discriminatorKey: 'kind' });
const subEventSchema = new Schema({ sub_events: [singleEventSchema] }, { _id: false });
subEventSchema.path('sub_events').discriminator('SubEvent', subEventSchema, { clone: false });
let currentEventLevel = subEventSchema; for (let i = 0; i < 5; i++) { const subEventSchemaDiscriminators = currentEventLevel.path('sub_events').schema.discriminators; assert.ok(subEventSchemaDiscriminators); assert.ok(subEventSchemaDiscriminators.SubEvent); currentEventLevel = subEventSchemaDiscriminators.SubEvent; } });
it('allow multiple levels of data in a document', function() { const singleEventSchema = new Schema({ message: String }, { _id: false, discriminatorKey: 'kind' });
const subEventSchema = new Schema({ sub_events: [singleEventSchema] }, { _id: false });
subEventSchema.path('sub_events').discriminator('SubEvent', subEventSchema, { clone: false });
const SubEvent = mongoose.model('MultiLevelDataDoc', subEventSchema); const multiLevel = { // To create a recursive document, the schema was modified, so kind & message are added kind: 'SubEvent', message: 'level 1', sub_events: [{ kind: 'SubEvent', message: 'level 2', sub_events: [{ kind: 'SubEvent', message: 'level 3', sub_events: [{ kind: 'SubEvent', message: 'level 4', sub_events: [{ kind: 'SubEvent', message: 'level 5', sub_events: [] }] }] }] }] }; const subEvent = SubEvent(multiLevel);
assert.deepStrictEqual(multiLevel, subEvent.toJSON()); });
it('allow multiple levels of data in the schema when the base schema has _id without auto', function() { const singleEventSchema = new Schema({ _id: { type: Number, required: true }, message: String }, { discriminatorKey: 'kind' });
const subEventSchema = new Schema({ sub_events: [singleEventSchema] });
subEventSchema.path('sub_events').discriminator('SubEvent', subEventSchema, { clone: false });
// To create a recursive document, the schema was modified, so the _id property is now a number assert.equal(subEventSchema.path('_id').instance, 'Number');
let currentEventLevel = subEventSchema; for (let i = 0; i < 5; i++) { const subEventSchemaDiscriminators = currentEventLevel.path('sub_events').schema.discriminators; assert.ok(subEventSchemaDiscriminators); assert.ok(subEventSchemaDiscriminators.SubEvent); currentEventLevel = subEventSchemaDiscriminators.SubEvent; assert.equal(currentEventLevel.path('_id').instance, 'Number'); } });
it('allow multiple levels of data in a document when the base schema has _id without auto', function() { const singleEventSchema = new Schema({ _id: { type: Number, required: true }, message: String }, { discriminatorKey: 'kind' });
const subEventSchema = new Schema({ sub_events: [singleEventSchema] });
subEventSchema.path('sub_events').discriminator('SubEvent', subEventSchema, { clone: false });
const SubEvent = mongoose.model('MultiLevelDataWithIdDoc', subEventSchema); const multiLevel = { // To create a recursive document, the schema was modified, so kind & message are added & _id is now Number _id: 1, kind: 'SubEvent', message: 'level 1', sub_events: [{ _id: 1, kind: 'SubEvent', message: 'level 2', sub_events: [{ _id: 1, kind: 'SubEvent', message: 'level 3', sub_events: [{ _id: 1, kind: 'SubEvent', message: 'level 4', sub_events: [{ _id: 1, kind: 'SubEvent', message: 'level 5', sub_events: [] }] }] }] }] }; const subEvent = SubEvent(multiLevel);
assert.deepStrictEqual(multiLevel, subEvent.toJSON()); }); }); });
it('copies over `requiredValidator` (gh-8819)', function() { const authorSchema = new mongoose.Schema({ name: String, pseudonym: String });
const bookSchema = new mongoose.Schema({ author: { type: authorSchema, required: true } });
const clone = bookSchema.clone(); assert.ok(clone.path('author').requiredValidator); assert.strictEqual(clone.path('author').requiredValidator, clone.path('author').validators[0].validator); });
it('supports `set()` (gh-8883)', function() { mongoose.deleteModel(/Test/); mongoose.Schema.Types.Subdocument.set('required', true);
const Model = mongoose.model('Test', mongoose.Schema({ nested: mongoose.Schema({ test: String }) }));
const doc = new Model({});
const err = doc.validateSync(); assert.ok(err); assert.ok(err.errors['nested']);
mongoose.Schema.Types.Subdocument.set('required', false); });
it('supports setting _id globally (gh-11541) (gh-8883)', function() { mongoose.deleteModel(/Test/); mongoose.Schema.Types.Subdocument.set('_id', false);
const Model = mongoose.model('Test', mongoose.Schema({ nested: mongoose.Schema({ test: String }) }));
const doc = new Model({ nested: {} });
assert.ok(!doc.nested._id);
delete mongoose.Schema.Types.Subdocument.defaultOptions._id; mongoose.Schema.Types.Subdocument.set('required', false); });});
mongoose

Version Info

Tagged at
a year ago