forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.alias.test.js
163 lines (142 loc) · 4.59 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
'use strict';
/**
* Module dependencies.
*/
const start = require('./common');
const assert = require('assert');
const Buffer = require('safe-buffer').Buffer;
const mongoose = start.mongoose;
const Schema = mongoose.Schema;
describe('schema alias option', function() {
let db;
before(function() {
db = start();
});
after(function(done) {
db.close(done);
});
beforeEach(() => db.deleteModel(/.*/));
afterEach(() => require('./util').clearTestData(db));
afterEach(() => require('./util').stopRemainingOps(db));
it('works with all basic schema types', function(done) {
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);
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']
}, function(err, s) {
assert.ifError(err);
// 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);
done();
});
});
it('works with nested schema types', function(done) {
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);
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']
}
}, function(err, s) {
assert.ifError(err);
// 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);
done();
});
});
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
});
});