Inital Commit
[oweals/finalsclub.git] / node_modules / mongoose / test / schema.onthefly.test.js
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;
7
8 /**
9  * Setup.
10  */
11
12 var DecoratedSchema = new Schema({
13     title     : String
14 });
15
16 mongoose.model('Decorated', DecoratedSchema);
17
18 var collection = 'decorated_' + random();
19
20 module.exports = {
21   'setting on the fly schemas should cache the type schema and cast values appropriately': function () {
22     var db = start()
23       , Decorated = db.model('Decorated', collection);
24
25     var post = new Decorated();
26     post.set('adhoc', '9', Number);
27     post.get('adhoc').valueOf().should.eql(9);
28     db.close();
29   },
30
31   'on the fly schemas should be local to the particular document': function () {
32     var db = start()
33       , Decorated = db.model('Decorated', collection);
34
35     var postOne = new Decorated();
36     postOne.set('adhoc', '9', Number);
37     postOne._path('adhoc').should.not.equal(undefined);
38
39     var postTwo = new Decorated();
40     postTwo._path('title').should.not.equal(undefined);
41     should.strictEqual(undefined, postTwo._path('adhoc'));
42     db.close();
43   },
44
45   'querying a document that had an on the fly schema should work': function () {
46     var db = start()
47       , Decorated = db.model('Decorated', collection);
48
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) {
56         db.close();
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');
62       });
63     });
64   },
65
66   'on the fly Embedded Array schemas should cast properly': function () {
67     var db = start()
68       , Decorated = db.model('Decorated', collection);
69
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');
73     db.close();
74   },
75
76   'on the fly Embedded Array schemas should get from a fresh queried document properly': function () {
77     var db = start()
78       , Decorated = db.model('Decorated', collection);
79
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) {
87         db.close();
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);
95
96         var NewModeratorSchema = new Schema({ name: String, ranking: String});
97         rankingPostCast = found.get('moderators', [NewModeratorSchema])[0].ranking;
98         rankingPostCast.should.equal('1');
99       });
100     });
101   },
102   'should support on the fly nested documents': function () {
103     // TODO
104   }
105 };