forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.onthefly.test.js
128 lines (108 loc) · 4.18 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
var start = require('./common')
, assert = require('assert')
, mongoose = start.mongoose
, random = require('../lib/utils').random
, Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;
/**
* Setup.
*/
var DecoratedSchema = new Schema({
title : String
}, { strict: false });
mongoose.model('Decorated', DecoratedSchema);
var collection = 'decorated_' + random();
describe('schema.onthefly', function(){
it('setting should cache the schema type and cast values appropriately', function (done) {
var db = start()
, Decorated = db.model('Decorated', collection);
db.close();
var post = new Decorated();
post.set('adhoc', '9', Number);
assert.equal(9, post.get('adhoc').valueOf());
done();
});
it('should be local to the particular document', function (done) {
var db = start()
, Decorated = db.model('Decorated', collection);
db.close();
var postOne = new Decorated();
postOne.set('adhoc', '9', Number);
assert.notStrictEqual(postOne.$__path('adhoc'),undefined);
var 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) {
var db = start()
, Decorated = db.model('Decorated', collection);
var post = new Decorated({title: 'AD HOC'});
// Interpret adhoc as a Number
post.set('adhoc', '9', Number);
assert.equal(9, post.get('adhoc').valueOf());
post.save(function (err) {
assert.ifError(err);
assert.strictEqual(null, err);
Decorated.findById(post.id, function (err, found) {
db.close();
assert.strictEqual(null, err);
assert.equal(9, found.get('adhoc'));
// Interpret adhoc as a String instead of a Number now
assert.equal('9', found.get('adhoc', String));
assert.equal('9', found.get('adhoc'));
// set adhoc as an Object
found.set('adhoc', '3', Object);
assert.equal('string', typeof found.get('adhoc'));
found.set('adhoc', 3, Object);
assert.equal('number', typeof found.get('adhoc'));
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('string', typeof found.get('adhoc'));
found.set('adhoc', 3, Object);
assert.equal('number', typeof found.get('adhoc'));
done();
});
});
});
it('on the fly Embedded Array schemas should cast properly', function (done) {
var db = start()
, Decorated = db.model('Decorated', collection);
db.close();
var 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) {
var db = start()
, Decorated = db.model('Decorated', collection);
var post = new Decorated()
, 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) {
db.close();
assert.ifError(err);
var rankingPreCast = found.get('moderators')[0].ranking;
assert.equal(1, rankingPreCast);
assert.strictEqual(undefined, rankingPreCast.increment);
var rankingPostCast = found.get('moderators', [ModeratorSchema])[0].ranking;
assert.equal(1, rankingPostCast);
var NewModeratorSchema = new Schema({ name: String, ranking: String});
rankingPostCast = found.get('moderators', [NewModeratorSchema])[0].ranking;
assert.equal(1, rankingPostCast);
done();
});
});
})
it('should support on the fly nested documents', function (done) {
// TODO
done();
});
})