forked from Automattic/mongoose
/
schema.onthefly.test.js
105 lines (89 loc) · 3.47 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
var start = require('./common')
, should = require('should')
, mongoose = start.mongoose
, random = require('../lib/utils').random
, Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;
/**
* Setup.
*/
var DecoratedSchema = new Schema({
title : String
});
mongoose.model('Decorated', DecoratedSchema);
var collection = 'decorated_' + random();
module.exports = {
'setting on the fly schemas should cache the type schema and cast values appropriately': function () {
var db = start()
, Decorated = db.model('Decorated', collection);
var post = new Decorated();
post.set('adhoc', '9', Number);
post.get('adhoc').valueOf().should.eql(9);
db.close();
},
'on the fly schemas should be local to the particular document': function () {
var db = start()
, Decorated = db.model('Decorated', collection);
var postOne = new Decorated();
postOne.set('adhoc', '9', Number);
postOne._path('adhoc').should.not.equal(undefined);
var postTwo = new Decorated();
postTwo._path('title').should.not.equal(undefined);
should.strictEqual(undefined, postTwo._path('adhoc'));
db.close();
},
'querying a document that had an on the fly schema should work': function () {
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);
post.get('adhoc').valueOf().should.eql(9);
post.save( function (err) {
should.strictEqual(null, err);
Decorated.findById(post.id, function (err, found) {
db.close();
should.strictEqual(null, err);
found.get('adhoc').should.eql(9);
// Interpret adhoc as a String instead of a Number now
found.get('adhoc', String).should.eql('9');
found.get('adhoc').should.eql('9');
});
});
},
'on the fly Embedded Array schemas should cast properly': function () {
var db = start()
, Decorated = db.model('Decorated', collection);
var post = new Decorated();
post.set('moderators', [{name: 'alex trebek'}], [new Schema({name: String})]);
post.get('moderators')[0].name.should.eql('alex trebek');
db.close();
},
'on the fly Embedded Array schemas should get from a fresh queried document properly': function () {
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]);
post.get('moderators')[0].name.should.eql('alex trebek');
post.save( function (err) {
should.strictEqual(null, err);
Decorated.findById(post.id, function (err, found) {
db.close();
should.strictEqual(null, err);
var rankingPreCast = found.get('moderators')[0].ranking;
rankingPreCast.should.eql(1);
should.strictEqual(undefined, rankingPreCast.increment);
var rankingPostCast = found.get('moderators', [ModeratorSchema])[0].ranking;
rankingPostCast.valueOf().should.equal(1);
rankingPostCast.increment.should.not.equal(undefined);
var NewModeratorSchema = new Schema({ name: String, ranking: String});
rankingPostCast = found.get('moderators', [NewModeratorSchema])[0].ranking;
rankingPostCast.should.equal('1');
});
});
},
'should support on the fly nested documents': function () {
// TODO
}
};