forked from Automattic/mongoose
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests and implementation for on-the-fly schemas.
- Loading branch information
Showing
6 changed files
with
192 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3281,5 +3281,4 @@ module.exports = { | |
}); | ||
}) | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
var start = require('./common') | ||
, should = require('should') | ||
, mongoose = start.mongoose | ||
, random = require('mongoose/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'); | ||
}); | ||
}); | ||
} | ||
}; |