Skip to content

Commit

Permalink
Finished model tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Josep M. Bach committed Apr 11, 2011
1 parent 5c83f5f commit 4dc74bd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ module.exports = function (db, collection_name) {
options = utils.merge(options, {upsert: true, multi: true});
query[document_name + '._id'] = id;

MONGO.mongo('update', query, {'$push': update}, options, callback);
MODEL.mongo('update', query, {'$push': update}, options, callback);

return MODEL;
};
Expand Down
50 changes: 50 additions & 0 deletions test/model_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,56 @@ testosterone
assert.ok(callback_called, 'Model#validateAndUpdate never called the callback');
})

.add('#getEmbeddedDocument filters the document following the skeletons directive', function () {
var comment = {'_id': 1, title: 'foo', body: 'Lorem ipsum'};
User.skeletons = {
comment: ['_id', 'title']
};

assert.deepEqual(User.getEmbeddedDocument('comment', comment), { _id: 1, title: 'foo' });
assert.deepEqual(User.getEmbeddedDocument('comment', comment, 'post.comment'), { 'post.comment._id': 1, 'post.comment.title': 'foo' });
})

.add('#updateEmbeddedDocument updates an embedded object', function () {
var embeddedDocument = {},
opts = {},
cb = function () {};

gently.expect(User, 'getEmbeddedDocument', function() {
return embeddedDocument;
})

gently.expect(User, 'mongo', function (action, query, update, options, callback) {
assert.equal(action, 'update');
assert.deepEqual(query, {'author._id': 1});
assert.deepEqual(update, {'$set': embeddedDocument});
assert.equal(options, opts);
assert.equal(callback, cb);
});

User.updateEmbeddedDocument(1, 'author', {}, opts, cb);
})

.add('#pushEmbeddedDocument pushes an embedded object', function () {
var embeddedDocument = {},
opts = {},
cb = function () {};

gently.expect(User, 'getEmbeddedDocument', function() {
return embeddedDocument;
})

gently.expect(User, 'mongo', function (action, query, update, options, callback) {
assert.equal(action, 'update');
assert.deepEqual(query, {'author._id': 1});
assert.deepEqual(update, {'$push': embeddedDocument});
assert.equal(options, opts);
assert.equal(callback, cb);
});

User.pushEmbeddedDocument(1, 'author', {}, opts, cb);
})

.run(function () {
require('sys').print('done!');
});

0 comments on commit 4dc74bd

Please sign in to comment.