forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.onthefly.test.js
139 lines (111 loc) · 4.51 KB
/
schema.onthefly.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
'use strict';
const assert = require('assert');
const random = require('../lib/utils').random;
const start = require('./common');
const mongoose = start.mongoose;
const Schema = mongoose.Schema;
const ObjectId = Schema.ObjectId;
describe('schema.onthefly', function() {
let DecoratedSchema;
let collection;
let db;
before(function() {
DecoratedSchema = new Schema({
title: String
}, {strict: false});
mongoose.model('Decorated', DecoratedSchema);
db = start();
collection = 'decorated_' + random();
});
after(function(done) {
db.close(done);
});
it('setting should cache the schema type and cast values appropriately', function(done) {
const Decorated = db.model('Decorated', collection);
const post = new Decorated();
post.set('adhoc', '9', Number);
assert.equal(post.get('adhoc').valueOf(), 9);
done();
});
it('should be local to the particular document', function(done) {
const Decorated = db.model('Decorated', collection);
const postOne = new Decorated();
postOne.set('adhoc', '9', Number);
assert.notStrictEqual(postOne.$__path('adhoc'), undefined);
const postTwo = new Decorated();
assert.notStrictEqual(postTwo.$__path('title'), undefined);
assert.strictEqual(undefined, postTwo.$__path('adhoc'));
done();
});
it('querying a document that had an on the fly schema should work', function(done) {
const Decorated = db.model('Decorated', collection);
const post = new Decorated({title: 'AD HOC'});
// Interpret adhoc as a Number
post.set('adhoc', '9', Number);
assert.equal(post.get('adhoc').valueOf(), 9);
post.save(function(err) {
assert.ifError(err);
assert.strictEqual(null, err);
Decorated.findById(post.id, function(err, found) {
assert.strictEqual(null, err);
assert.equal(found.get('adhoc'), 9);
// Interpret adhoc as a String instead of a Number now
assert.equal(found.get('adhoc', String), '9');
assert.equal(found.get('adhoc'), '9');
// set adhoc as an Object
found.set('adhoc', '3', Object);
assert.equal(typeof found.get('adhoc'), 'string');
found.set('adhoc', 3, Object);
assert.equal(typeof found.get('adhoc'), 'number');
found.set('adhoc', ['hello'], Object);
assert.ok(Array.isArray(found.get('adhoc')));
found.set('adhoc', ['hello'], {});
assert.ok(Array.isArray(found.get('adhoc')));
found.set('adhoc', 3, String);
assert.equal(typeof found.get('adhoc'), 'string');
found.set('adhoc', 3, Object);
assert.equal(typeof found.get('adhoc'), 'number');
done();
});
});
});
it('on the fly Embedded Array schemas should cast properly', function(done) {
const Decorated = db.model('Decorated', collection);
const post = new Decorated();
post.set('moderators', [{name: 'alex trebek'}], [new Schema({name: String})]);
assert.equal(post.get('moderators')[0].name, 'alex trebek');
done();
});
it('on the fly Embedded Array schemas should get from a fresh queried document properly', function(done) {
const Decorated = db.model('Decorated', collection);
const post = new Decorated();
const ModeratorSchema = new Schema({name: String, ranking: Number});
post.set('moderators', [{name: 'alex trebek', ranking: '1'}], [ModeratorSchema]);
assert.equal(post.get('moderators')[0].name, 'alex trebek');
post.save(function(err) {
assert.ifError(err);
Decorated.findById(post.id, function(err, found) {
assert.ifError(err);
const rankingPreCast = found.get('moderators')[0].ranking;
assert.equal(rankingPreCast, 1);
assert.strictEqual(undefined, rankingPreCast.increment);
let rankingPostCast = found.get('moderators', [ModeratorSchema])[0].ranking;
assert.equal(rankingPostCast, 1);
const NewModeratorSchema = new Schema({name: String, ranking: String});
rankingPostCast = found.get('moderators', [NewModeratorSchema])[0].ranking;
assert.equal(rankingPostCast, 1);
done();
});
});
});
it('casts on get() (gh-2360)', function(done) {
const Decorated = db.model('gh2360', DecoratedSchema, 'gh2360');
const d = new Decorated({title: '1'});
assert.equal(typeof d.get('title', Number), 'number');
d.title = '000000000000000000000001';
assert.equal(d.get('title', ObjectId).constructor.name, 'ObjectID');
d.set('title', 1, Number);
assert.equal(typeof d.get('title'), 'number');
done();
});
});