1 var start = require('./common')
2 , should = require('should')
3 , mongoose = start.mongoose
4 , random = require('mongoose/utils').random
5 , Schema = mongoose.Schema
6 , ObjectId = Schema.ObjectId;
12 var DecoratedSchema = new Schema({
16 mongoose.model('Decorated', DecoratedSchema);
18 var collection = 'decorated_' + random();
21 'setting on the fly schemas should cache the type schema and cast values appropriately': function () {
23 , Decorated = db.model('Decorated', collection);
25 var post = new Decorated();
26 post.set('adhoc', '9', Number);
27 post.get('adhoc').valueOf().should.eql(9);
31 'on the fly schemas should be local to the particular document': function () {
33 , Decorated = db.model('Decorated', collection);
35 var postOne = new Decorated();
36 postOne.set('adhoc', '9', Number);
37 postOne._path('adhoc').should.not.equal(undefined);
39 var postTwo = new Decorated();
40 postTwo._path('title').should.not.equal(undefined);
41 should.strictEqual(undefined, postTwo._path('adhoc'));
45 'querying a document that had an on the fly schema should work': function () {
47 , Decorated = db.model('Decorated', collection);
49 var post = new Decorated({title: 'AD HOC'});
50 // Interpret adhoc as a Number
51 post.set('adhoc', '9', Number);
52 post.get('adhoc').valueOf().should.eql(9);
53 post.save( function (err) {
54 should.strictEqual(null, err);
55 Decorated.findById(post.id, function (err, found) {
57 should.strictEqual(null, err);
58 found.get('adhoc').should.eql(9);
59 // Interpret adhoc as a String instead of a Number now
60 found.get('adhoc', String).should.eql('9');
61 found.get('adhoc').should.eql('9');
66 'on the fly Embedded Array schemas should cast properly': function () {
68 , Decorated = db.model('Decorated', collection);
70 var post = new Decorated();
71 post.set('moderators', [{name: 'alex trebek'}], [new Schema({name: String})]);
72 post.get('moderators')[0].name.should.eql('alex trebek');
76 'on the fly Embedded Array schemas should get from a fresh queried document properly': function () {
78 , Decorated = db.model('Decorated', collection);
80 var post = new Decorated()
81 , ModeratorSchema = new Schema({name: String, ranking: Number});
82 post.set('moderators', [{name: 'alex trebek', ranking: '1'}], [ModeratorSchema]);
83 post.get('moderators')[0].name.should.eql('alex trebek');
84 post.save( function (err) {
85 should.strictEqual(null, err);
86 Decorated.findById(post.id, function (err, found) {
88 should.strictEqual(null, err);
89 var rankingPreCast = found.get('moderators')[0].ranking;
90 rankingPreCast.should.eql(1);
91 should.strictEqual(undefined, rankingPreCast.increment);
92 var rankingPostCast = found.get('moderators', [ModeratorSchema])[0].ranking;
93 rankingPostCast.valueOf().should.equal(1);
94 rankingPostCast.increment.should.not.equal(undefined);
96 var NewModeratorSchema = new Schema({ name: String, ranking: String});
97 rankingPostCast = found.get('moderators', [NewModeratorSchema])[0].ranking;
98 rankingPostCast.should.equal('1');
102 'should support on the fly nested documents': function () {