-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
schematype.test.js
232 lines (191 loc) · 7.44 KB
/
schematype.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
'use strict';
/**
* Module dependencies.
*/
const mongoose = require('./common').mongoose;
const assert = require('assert');
const Schema = mongoose.Schema;
describe('schematype', function() {
it('honors the selected option', function() {
const s = new Schema({ thought: { type: String, select: false } });
assert.ok(!s.path('thought').selected);
const a = new Schema({ thought: { type: String, select: true } });
assert.ok(a.path('thought').selected);
});
it('properly handles specifying index in combination with unique or sparse', function() {
let s = new Schema({ name: { type: String, index: true, unique: true } });
assert.deepEqual(s.path('name')._index, { unique: true });
s = new Schema({ name: { type: String, unique: true, index: true } });
assert.deepEqual(s.path('name')._index, { unique: true });
s = new Schema({ name: { type: String, index: true, sparse: true } });
assert.deepEqual(s.path('name')._index, { sparse: true });
s = new Schema({ name: { type: String, sparse: true, index: true } });
assert.deepEqual(s.path('name')._index, { sparse: true });
});
it('handles index: false with unique, sparse, text set to false (gh-7620)', function() {
let s = new Schema({ name: { type: String, index: false, unique: false } });
assert.equal(s.path('name')._index, false);
s = new Schema({ name: { type: String, unique: false, index: false } });
assert.equal(s.path('name')._index, false);
s = new Schema({ name: { type: String, index: false, sparse: false } });
assert.equal(s.path('name')._index, false);
s = new Schema({ name: { type: String, sparse: false, index: false } });
assert.equal(s.path('name')._index, false);
s = new Schema({ name: { type: String, index: false, text: false } });
assert.equal(s.path('name')._index, false);
s = new Schema({ name: { type: String, text: false, index: false } });
assert.equal(s.path('name')._index, false);
});
it('handles unique, sparse, text set to false with no index property (gh-10738)', function() {
let s = new Schema({ name: { type: String, unique: false } });
assert.equal(s.path('name')._index, null);
s = new Schema({ name: { type: String, sparse: false } });
assert.equal(s.path('name')._index, null);
s = new Schema({ name: { type: String, text: false } });
assert.equal(s.path('name')._index, null);
});
describe('checkRequired()', function() {
it('with inherits (gh-7486)', function() {
const m = new mongoose.Mongoose();
function CustomNumber(path, options) {
m.Schema.Types.Number.call(this, path, options);
}
CustomNumber.prototype.cast = v => v;
require('util').inherits(CustomNumber, m.Schema.Types.Number);
mongoose.Schema.Types.CustomNumber = CustomNumber;
function CustomString(path, options) {
m.Schema.Types.String.call(this, path, options);
}
CustomString.prototype.cast = v => v;
require('util').inherits(CustomString, m.Schema.Types.String);
mongoose.Schema.Types.CustomString = CustomString;
function CustomObjectId(path, options) {
m.Schema.Types.ObjectId.call(this, path, options);
}
CustomObjectId.prototype.cast = v => v;
require('util').inherits(CustomObjectId, m.Schema.Types.ObjectId);
mongoose.Schema.Types.CustomObjectId = CustomObjectId;
const s = new Schema({
foo: { type: CustomNumber, required: true },
bar: { type: CustomString, required: true },
baz: { type: CustomObjectId, required: true }
});
const M = m.model('Test', s);
const doc = new M({ foo: 1, bar: '2', baz: new mongoose.Types.ObjectId() });
const err = doc.validateSync();
assert.ifError(err);
});
});
it('handles function as positional message arg (gh-8360)', function() {
const schema = Schema({
name: {
type: String,
validate: [() => false, err => `${err.path} is invalid!`]
}
});
const err = schema.path('name').doValidateSync('test');
assert.equal(err.name, 'ValidatorError');
assert.equal(err.message, 'name is invalid!');
});
describe('clone()', function() {
let schemaType;
beforeEach(function() {
schemaType = Schema({ value: String }).path('value');
});
function cloneAndTestDeepEquals() {
const clone = schemaType.clone();
assert.deepStrictEqual(clone, schemaType);
}
it('clones added default', function() {
schemaType.default(() => 'abc');
cloneAndTestDeepEquals();
});
it('clones added getters', function() {
schemaType.get(v => v.trim());
cloneAndTestDeepEquals();
});
it('clones added immutable', function() {
// Note: cannot compare with deep equals due to the immutable function
schemaType.immutable(true);
let clonePath = schemaType.clone();
assert.equal(schemaType.$immutable, clonePath.$immutable);
assert.equal(schemaType.setters.length, clonePath.setters.length);
schemaType.immutable(false);
clonePath = schemaType.clone();
assert.equal(schemaType.$immutable, clonePath.$immutable);
assert.equal(schemaType.setters.length, clonePath.setters.length);
});
it('clones added index', function() {
schemaType.index(true);
cloneAndTestDeepEquals();
});
it('clones added ref', function() {
schemaType.ref('User');
cloneAndTestDeepEquals();
});
it('clones added required', function() {
schemaType.required(true);
cloneAndTestDeepEquals();
});
it('clones added select: false', function() {
schemaType.select(false);
cloneAndTestDeepEquals();
});
it('clones added setter', function() {
schemaType.set(v => v.trim());
cloneAndTestDeepEquals();
});
it('clones added sparse', function() {
schemaType.sparse(true);
cloneAndTestDeepEquals();
});
it('clones added sparse (index option)', function() {
schemaType.sparse(true);
cloneAndTestDeepEquals();
});
it('clones added text (index option)', function() {
schemaType.text(true);
cloneAndTestDeepEquals();
});
it('clones added unique (index option)', function() {
schemaType.unique(true);
cloneAndTestDeepEquals();
});
it('clones added validator', function() {
schemaType.validate(v => v.length > 3);
cloneAndTestDeepEquals();
});
it('clones updated caster', function() {
schemaType.cast(v => v.length > 3 ? v : v.trim());
cloneAndTestDeepEquals();
});
});
describe('set()', function() {
describe('SchemaType.set()', function() {
it('SchemaType.set, is a function', () => {
assert.equal(typeof mongoose.SchemaType.set, 'function');
});
});
[
mongoose.SchemaTypes.String,
mongoose.SchemaTypes.Number,
mongoose.SchemaTypes.Boolean,
mongoose.SchemaTypes.Array,
mongoose.SchemaTypes.Buffer,
mongoose.SchemaTypes.Date,
mongoose.SchemaTypes.ObjectId,
mongoose.SchemaTypes.Mixed,
mongoose.SchemaTypes.Decimal128,
mongoose.SchemaTypes.Map
].forEach((type) => {
it(type.name + ', when given a default option, set its', () => {
// Act
type.set('someRandomOption', true);
const schema = new mongoose.Schema({ test: type });
// Assert
assert.equal(schema.path('test').options.someRandomOption, true);
delete type.defaultOptions.someRandomOption;
});
});
});
});