/
schema.alias.test.js
215 lines (186 loc) · 6.01 KB
/
schema.alias.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
'use strict';
/**
* Module dependencies.
*/
const start = require('./common');
const assert = require('assert');
const mongoose = start.mongoose;
const Schema = mongoose.Schema;
describe('schema alias option', function() {
let db;
before(function() {
db = start();
});
after(async function() {
await db.close();
});
beforeEach(() => db.deleteModel(/.*/));
afterEach(() => require('./util').clearTestData(db));
afterEach(() => require('./util').stopRemainingOps(db));
it('works with all basic schema types', async function() {
const schema = new Schema({
string: { type: String, alias: 'StringAlias' },
number: { type: Number, alias: 'NumberAlias' },
date: { type: Date, alias: 'DateAlias' },
buffer: { type: Buffer, alias: 'BufferAlias' },
boolean: { type: Boolean, alias: 'BooleanAlias' },
mixed: { type: Schema.Types.Mixed, alias: 'MixedAlias' },
objectId: { type: Schema.Types.ObjectId, alias: 'ObjectIdAlias' },
array: { type: [], alias: 'ArrayAlias' }
});
const S = db.model('Test', schema);
const s = await S.create({
string: 'hello',
number: 1,
date: new Date(),
buffer: Buffer.from('World'),
boolean: false,
mixed: [1, [], 'three', { four: 5 }],
objectId: new mongoose.Types.ObjectId(),
array: ['a', 'b', 'c', 'd']
});
// Comparing with aliases
assert.equal(s.string, s.StringAlias);
assert.equal(s.number, s.NumberAlias);
assert.equal(s.date, s.DateAlias);
assert.equal(s.buffer, s.BufferAlias);
assert.equal(s.boolean, s.BooleanAlias);
assert.equal(s.mixed, s.MixedAlias);
assert.equal(s.objectId, s.ObjectIdAlias);
assert.equal(s.array, s.ArrayAlias);
});
it('works with nested schema types', async function() {
const schema = new Schema({
nested: {
string: { type: String, alias: 'StringAlias' },
number: { type: Number, alias: 'NumberAlias' },
date: { type: Date, alias: 'DateAlias' },
buffer: { type: Buffer, alias: 'BufferAlias' },
boolean: { type: Boolean, alias: 'BooleanAlias' },
mixed: { type: Schema.Types.Mixed, alias: 'MixedAlias' },
objectId: { type: Schema.Types.ObjectId, alias: 'ObjectIdAlias' },
array: { type: [], alias: 'ArrayAlias' }
}
});
const S = db.model('Test', schema);
const s = await S.create({
nested: {
string: 'hello',
number: 1,
date: new Date(),
buffer: Buffer.from('World'),
boolean: false,
mixed: [1, [], 'three', { four: 5 }],
objectId: new mongoose.Types.ObjectId(),
array: ['a', 'b', 'c', 'd']
}
});
// Comparing with aliases
assert.equal(s.nested.string, s.StringAlias);
assert.equal(s.nested.number, s.NumberAlias);
assert.equal(s.nested.date, s.DateAlias);
assert.equal(s.nested.buffer, s.BufferAlias);
assert.equal(s.nested.boolean, s.BooleanAlias);
assert.equal(s.nested.mixed, s.MixedAlias);
assert.equal(s.nested.objectId, s.ObjectIdAlias);
assert.equal(s.nested.array, s.ArrayAlias);
});
it('throws when alias option is invalid', function() {
assert.throws(function() {
new Schema({
foo: { type: String, alias: 456 }
});
});
});
it('with add() (gh-6593)', function() {
const s = new Schema({ name: { type: String, alias: 'test1' } });
assert.deepEqual(Object.keys(s.aliases), ['test1']);
s.add({ name2: { type: String, alias: 'test2' } });
assert.deepEqual(Object.keys(s.aliases), ['test1', 'test2']);
});
it('nested aliases (gh-6671)', function(done) {
const childSchema = new Schema({
n: {
type: String,
alias: 'name'
}
}, { _id: false });
const parentSchema = new Schema({
// If in a child schema, alias doesn't need to include the full nested path
c: childSchema,
name: {
f: {
type: String,
// Alias needs to include the full nested path if declared inline
alias: 'name.first'
}
}
});
// acquit:ignore:start
mongoose.deleteModel(/Test/);
const Parent = mongoose.model('Test', parentSchema);
const doc = new Parent({
c: {
name: 'foo'
},
name: {
first: 'bar'
}
});
assert.deepEqual(doc.toObject().c, { n: 'foo' });
assert.deepEqual(doc.toObject().name, { f: 'bar' });
done();
// acquit:ignore:end
});
it('array of aliases (gh-12368)', function() {
const productSchema = new Schema({
n: {
type: String,
alias: ['name', 'product_name']
}
});
const Product = db.model('Test', productSchema);
const doc = new Product({});
doc['product_name'] = 'Turbo Man';
assert.equal(doc.n, 'Turbo Man');
assert.equal(doc.name, 'Turbo Man');
});
it('alias() method (gh-12368)', function() {
const schema = new Schema({ name: String });
schema.alias('name', 'otherName');
assert.equal(schema.aliases['otherName'], 'name');
assert.ok(schema.virtuals['otherName']);
schema.alias('name', ['name1', 'name2']);
assert.equal(schema.aliases['name1'], 'name');
assert.equal(schema.aliases['name2'], 'name');
assert.ok(schema.virtuals['name1']);
assert.ok(schema.virtuals['name2']);
});
it('supports passing the alias name for an index (gh-13276)', function() {
const testSchema = new Schema({
fullName: {
type: String,
alias: 'short'
}
});
testSchema.index({ short: 1 });
const indexes = testSchema.indexes();
assert.equal(indexes[0][0].fullName, 1);
});
it('should disable the id virtual entirely if there\'s a field with alias `id` gh-13650', async function() {
const testSchema = new Schema({
_id: {
type: String,
required: true,
set: (val) => val.replace(/\s+/g, ' '),
alias: 'id'
}
});
const Test = db.model('gh13650', testSchema);
const doc = new Test({
id: 'H-1'
});
await doc.save();
assert.ok(doc);
});
});