Browse files

migrated from gently to sinon

  • Loading branch information...
1 parent 1e8c887 commit ddecb3e2405e5f1211e043b9ea14ebb3f73ab684 @masylum committed May 9, 2012
Showing with 450 additions and 388 deletions.
  1. +1 −1 Makefile
  2. +1 −1 README.md
  3. +3 −1 lib/helpers/collection_proxy.js
  4. +3 −6 lib/model.js
  5. +1 −1 package.json
  6. +303 −248 test/helpers_collection_proxy_test.js
  7. +2 −2 test/integration_test.js
  8. +136 −126 test/model_test.js
  9. +0 −2 test/validator_test.js
View
2 Makefile
@@ -1,4 +1,4 @@
-MOCHA = ./node_modules/.bin/mocha --reporter spec
+MOCHA = ./node_modules/.bin/mocha --reporter dot
test:
@$(MOCHA)
View
2 README.md
@@ -455,7 +455,7 @@ var User = function (db) {
## Tests
-Mongolia is fully tested using [testosterone](http://github.com/masylum/testosterone)
+Mongolia is fully tested using [mocha](http://github.com/visionmedia/mocha)
To run the tests use:
```bash
View
4 lib/helpers/collection_proxy.js
@@ -27,7 +27,9 @@ PROXY.proxy = function (model, options, args, callback) {
callback = function () {};
}
- if (error) return callback(error, null);
+ if (error) {
+ return callback(error, null);
+ }
if (options.namespacing && model.namespaces && model.namespaces[options.namespace]) {
// has side effects, alters args
View
9 lib/model.js
@@ -1,7 +1,3 @@
-if (global.GENTLY) {
- require = global.GENTLY.hijack(require);
-}
-
var _ = require('underscore');
module.exports = function (db, collection_name) {
@@ -31,6 +27,8 @@ module.exports = function (db, collection_name) {
return MODEL;
};
+ MODEL.validator = require('./validator');
+
/**
* Proxies `collection` calls and then:
*
@@ -92,8 +90,7 @@ module.exports = function (db, collection_name) {
* @param {Function} callback
*/
MODEL.validate = function (document, update, callback) {
- var validator = require('./validator')(document, update);
- callback(null, validator);
+ callback(null, MODEL.validator(document, update));
};
/**
View
2 package.json
@@ -17,7 +17,7 @@
"devDependencies": {
"mocha": "*",
"testosterone": "1.2.0",
- "gently": "git://github.com/masylum/node-gently.git"
+ "sinon": "1.3.4"
},
"repository" : {"type": "git" , "url": "http://github.com/masylum/mongolia.git" },
"engines": { "node": ">= 0.4.0" }
View
551 test/helpers_collection_proxy_test.js
@@ -1,13 +1,18 @@
-/*globals describe, it, beforeEach*/
+/*globals describe, it, beforeEach, afterEach*/
var assert = require('assert')
- , gently = global.GENTLY = new (require('gently'))()
+ , sinon = require('sinon')
, CollectionProxy = require('./../lib/helpers/collection_proxy')
- , Collection = require('mongodb/lib/mongodb/collection').Collection.prototype
+ , noop = function () {
+ return function () {};
+ }
+ , Collection = { update: noop(), insert: noop(), findArray: noop()
+ , find: noop(), findAndModify: noop(), mapReduce: noop(), remove: noop()}
, Model;
-function _stubGetCollection() {
- gently.expect(Model, 'getCollection', function (_callback) {
+function stubGetCollection(sinon) {
+ var stub = sinon.stub(Model, 'getCollection', function (_callback) {
_callback(null, Collection);
+ stub.restore();
});
}
@@ -17,320 +22,370 @@ describe('collection proxy test', function () {
Model = require('../').model({}, 'models');
});
- it('`proxy` delegates every call to collection_proxy or native driver collection functions', function () {
- var cb = function cb() {},
- args = ['zemba', cb];
-
- ['update', 'insert', 'findArray'].forEach(function (method) {
- _stubGetCollection();
- gently.expect(CollectionProxy, method, function (_model, _collection, _args, _callback) {
- assert.equal(Model, _model);
- assert.equal(Collection, _collection);
- assert.deepEqual(args, _args);
- assert.equal(cb, _callback);
+ describe('proxy', function () {
+ it('delegates every call to collection_proxy or native driver collection functions', sinon.test(function () {
+ var cb = function cb() {}
+ , self = this
+ , stub
+ , args = ['zemba', cb];
+
+ ['update', 'insert', 'findArray'].forEach(function (method) {
+ stubGetCollection(self);
+ stub = self.stub(CollectionProxy, method).withArgs(Model, Collection, args, cb);
+ CollectionProxy.proxy(Model, {method: method, hooks: true}, args, cb);
+ sinon.assert.calledOnce(stub);
});
- CollectionProxy.proxy(Model, {method: method, hooks: true}, args, cb);
- });
-
- ['find', 'foo', 'update', 'insert', 'findArray'].forEach(function (method) {
- _stubGetCollection();
- gently.expect(Collection, method, function (_arg, _callback) {
- assert.equal(_arg, args[0]);
- assert.deepEqual(_callback, args[1]);
+ ['find', 'update', 'insert'].forEach(function (method) {
+ stubGetCollection(self);
+ stub = self.stub(Collection, method).withArgs(args[0], args[1]);
+ CollectionProxy.proxy(Model, {method: method, hooks: false}, args, cb);
+ sinon.assert.calledOnce(stub);
});
+ }));
- CollectionProxy.proxy(Model, {method: method, hooks: false}, args, cb);
- });
- });
+ describe('namespace', function () {
+ var cb, args;
- it('`proxy` modifies the arguments according to `namespaces` unless the `namespacing` option is set to `false`', function () {
- var cb = function () {},
+ beforeEach(function () {
+ cb = function () {};
args = [{foo: 'bar'}, cb];
- Model.namespaces = {
- foo: ['zemba', 'fleiba']
- };
-
- // namespacing: true
- _stubGetCollection();
- gently.expect(CollectionProxy.namespacer, 'filter', function (_namespaces, _namespace, _fn, _args) {
- assert.deepEqual(_namespaces, Model.namespaces);
- assert.deepEqual(_namespace, 'foo');
- assert.equal(_fn, 'find');
- assert.deepEqual(_args, args);
- });
+ Model.namespaces = {foo: ['zemba', 'fleiba']};
+ });
- gently.expect(Collection, 'find', function (_arg, _callback) {
- assert.equal(_arg, args[0]);
- assert.deepEqual(_callback, args[1]);
- });
+ it('modifies the arguments if `namespace` is set to `true`', sinon.test(function () {
+ var self = this, stub1, stub2;
+
+ stubGetCollection(self);
+ stub1 = self.stub(CollectionProxy.namespacer, 'filter').withArgs(Model.namespaces, 'foo', 'find', args);
+ stub2 = self.stub(Collection, 'find').withArgs(args[0], args[1]);
- CollectionProxy.proxy(Model, {method: 'find', namespace: 'foo', namespacing: true}, args, cb);
+ CollectionProxy.proxy(Model, {method: 'find', namespace: 'foo', namespacing: true}, args, cb);
+ sinon.assert.calledOnce(stub1);
+ sinon.assert.calledOnce(stub2);
+ }));
- // namespacing: false
- _stubGetCollection();
- gently.expect(Collection, 'find', function (_arg, _callback) {
- assert.equal(_arg, args[0]);
- assert.deepEqual(_callback, args[1]);
+ it('doesnt modify the arguments if `namespace` is set to `false`', sinon.test(function () {
+ var self = this, stub;
+
+ stubGetCollection(self);
+ stub = self.stub(Collection, 'find').withArgs(args[0], args[1]);
+
+ CollectionProxy.proxy(Model, {method: 'find', namespace: 'foo', namespacing: false}, args, cb);
+ sinon.assert.calledOnce(stub);
+ }));
});
- CollectionProxy.proxy(Model, {method: 'find', namespace: 'foo', namespacing: false}, args, cb);
- });
+ describe('maps', function () {
+ var cb, args;
- it('`proxy` maps the arguments according to `maps` unless the `mapping` option is set to `false`', function () {
- var cb = function () {},
+ beforeEach(function () {
+ cb = function () {};
args = [{foo: 'bar', _id: '3'}, cb];
- Model.maps = {foo: Boolean};
+ Model.maps = {foo: Boolean};
+ });
- _stubGetCollection();
- gently.expect(CollectionProxy.mapper, 'map', function (_maps, _fn, _args) {
- assert.deepEqual(_maps, {foo: Boolean});
- assert.equal(_fn, 'find');
- assert.deepEqual(_args, args);
- });
+ it('maps the arguments according if the `mapping` option is set to `true`', sinon.test(function () {
+ var self = this, stub1, stub2;
- gently.expect(Collection, 'find', function (_arg, _callback) {
- assert.equal(_arg, args[0]);
- assert.deepEqual(_callback, args[1]);
- });
+ stubGetCollection(self);
+ stub1 = self.stub(CollectionProxy.mapper, 'map').withArgs({foo: Boolean}, 'find', args);
+ stub2 = self.stub(Collection, 'find').withArgs(args[0], args[1]);
+
+ CollectionProxy.proxy(Model, {method: 'find', mapping: true}, args, cb);
+ sinon.assert.calledOnce(stub1);
+ sinon.assert.calledOnce(stub2);
+ }));
- CollectionProxy.proxy(Model, {method: 'find', mapping: true}, args, cb);
+ it('does not map the arguments if the `mapping` option is set to `false`', sinon.test(function () {
+ var self = this, stub;
- _stubGetCollection();
- gently.expect(Collection, 'find', function (_arg, _callback) {
- assert.equal(_arg, args[0]);
- assert.deepEqual(_callback, args[1]);
+ stubGetCollection(self);
+ stub = self.stub(Collection, 'find').withArgs(args[0], args[1]);
+
+ CollectionProxy.proxy(Model, {method: 'find', mapping: false}, args, cb);
+ sinon.assert.calledOnce(stub);
+ }));
});
- CollectionProxy.proxy(Model, {method: 'find', mapping: false}, args, cb);
- });
+ it('can be called with no callback', sinon.test(function () {
+ var args = ['zemba']
+ , stub
+ , self = this;
- it('`proxy` can be called with no callback', function () {
- var args = ['zemba'];
+ ['update', 'insert', 'findArray'].forEach(function (method) {
+ stubGetCollection(self);
+ stub = self.stub(CollectionProxy, method, function (m, c, a, callback) {
+ assert.equal(typeof callback, 'function');
+ });
- ['update', 'insert', 'findArray'].forEach(function (method) {
- _stubGetCollection();
- gently.expect(CollectionProxy, method, function (_model, _collection, _args, _callback) {
- assert.equal(_model, Model);
- assert.equal(_collection, Collection);
- assert.deepEqual(_args, args);
- assert.equal(typeof _callback, 'function');
+ CollectionProxy.proxy(Model, {method: method, hooks: true}, args);
+ sinon.assert.calledOnce(stub);
});
- CollectionProxy.proxy(Model, {method: method, hooks: true}, args);
- });
+ ['find', 'update', 'insert'].forEach(function (method) {
+ stubGetCollection(self);
+ stub = self.stub(Collection, method, function (args, callback) {
+ assert.equal(typeof callback, 'function');
+ });
- ['find', 'foo', 'update', 'insert', 'findArray'].forEach(function (method) {
- _stubGetCollection();
- gently.expect(Collection, method, function (_arg, _callback) {
- assert.equal(_arg, args[0]);
- assert.deepEqual(_callback, args[1]);
+ CollectionProxy.proxy(Model, {method: method, hooks: false}, args);
+ sinon.assert.calledOnce(stub);
});
-
- CollectionProxy.proxy(Model, {method: method, hooks: false}, args);
- });
+ }));
});
- it('`findArray` calls find on a collection with some arguments', function () {
- var cb = function (error, cursor) {},
- cursor = {fleiba: 'zemba'},
- error_result = null,
- args = ['fleiba', cb];
-
- gently.expect(Collection, 'find', function (_collection, _args) {
- assert.deepEqual(_collection, args[0]);
- gently.expect(cursor, 'toArray');
- args[args.length - 1](null, cursor);
- });
+ describe('findArray', function () {
+ it('calls find on a collection with some arguments', sinon.test(function () {
+ var cb = function (error, cursor) {}
+ , self = this
+ , stub1, stub2
+ , cursor = {toArray: function () {}}
+ , error_result = null
+ , args = ['fleiba', cb];
+
+ stub1 = self.stub(Collection, 'find', function (_collection, _args) {
+ assert.deepEqual(_collection, args[0]);
+ stub2 = self.stub(cursor, 'toArray');
+ args[args.length - 1](null, cursor);
+ });
- CollectionProxy.findArray(Model, Collection, args, cb);
+ CollectionProxy.findArray(Model, Collection, args, cb);
+ sinon.assert.calledOnce(stub1);
+ sinon.assert.calledOnce(stub2);
+ }));
});
- it('`insert` inserts a record', function () {
- var callback_called = false,
- cb = function (error, ret) {
- assert.deepEqual(error, null);
- assert.deepEqual(ret, [1, 2, 3]);
- callback_called = true;
- },
- args = ['fleiba', cb];
-
- gently.expect(Model, 'beforeInsert', function (ar, callback) {
- assert.deepEqual(ar, args[0]);
-
- gently.expect(Collection.insert, 'apply', function (_collection, _args) {
- assert.deepEqual(_collection, Collection);
- assert.deepEqual(_args[0], ['document1', 'document2']);
-
- gently.expect(Model, 'afterInsert', function (_docs, _callback) {
- assert.deepEqual(_docs, ['document1', 'document2']);
- _callback(null);
- assert.ok(callback_called);
+ describe('insert', function () {
+ it('inserts a record', sinon.test(function () {
+ var callback_called = false
+ , self = this
+ , stub1, stub2, stub3
+ , cb = function (error, ret) {
+ assert.deepEqual(error, null);
+ assert.deepEqual(ret, [1, 2, 3]);
+ callback_called = true;
+ }
+ , args = ['fleiba', cb];
+
+ stub1 = self.stub(Model, 'beforeInsert', function (ar, callback) {
+ assert.deepEqual(ar, args[0]);
+
+ stub2 = self.stub(Collection.insert, 'apply', function (_collection, _args) {
+ assert.deepEqual(_collection, Collection);
+ assert.deepEqual(_args[0], ['document1', 'document2']);
+
+ stub3 = self.stub(Model, 'afterInsert', function (_docs, _callback) {
+ assert.deepEqual(_docs, ['document1', 'document2']);
+ _callback(null);
+ assert.ok(callback_called);
+ });
+
+ _args[1](null, [1, 2, 3]);
});
- _args[1](null, [1, 2, 3]);
+ callback(null, ['document1', 'document2']);
});
- callback(null, ['document1', 'document2']);
- });
-
- CollectionProxy.insert(Model, Collection, args, cb);
+ CollectionProxy.insert(Model, Collection, args, cb);
+ sinon.assert.calledOnce(stub1);
+ sinon.assert.calledOnce(stub2);
+ sinon.assert.calledOnce(stub3);
+ }));
});
- it('`update` finds and modifies a record', function () {
- var callback_called = false,
- cb = function (error, ret) {
- assert.deepEqual(error, null);
- assert.deepEqual(ret, [1, 2, 3]);
- callback_called = true;
- },
- args = [{name: 'zemba'}, {'$set': {name: 'foo'}}, {}, cb];
-
- gently.expect(Model, 'beforeUpdate', function (_query, _update, _callback) {
- assert.deepEqual(_query, args[0]);
- assert.deepEqual(_update, args[1]);
- _update.$set.updated_at = 123;
-
- gently.expect(Collection.update, 'apply', function (_collection, _args) {
- assert.deepEqual(_collection, Collection);
- assert.deepEqual(_args[0], args[0]);
- assert.deepEqual(_args[1].$set.updated_at, 123);
-
- gently.expect(Model, 'afterUpdate', function (_doc, _update, _callback) {
- assert.deepEqual(_doc, args[0]);
- assert.deepEqual(_update, args[1]);
- _callback(null);
- assert.ok(callback_called);
+ describe('update', function () {
+ it('finds and modifies a record', sinon.test(function () {
+ var callback_called = false
+ , self = this
+ , stub1, stub2, stub3
+ , cb = function (error, ret) {
+ assert.deepEqual(error, null);
+ assert.deepEqual(ret, [1, 2, 3]);
+ callback_called = true;
+ }
+ , args = [{name: 'zemba'}, {'$set': {name: 'foo'}}, {}, cb];
+
+ stub1 = sinon.stub(Model, 'beforeUpdate', function (_query, _update, _callback) {
+ assert.deepEqual(_query, args[0]);
+ assert.deepEqual(_update, args[1]);
+ _update.$set.updated_at = 123;
+
+ stub2 = sinon.stub(Collection.update, 'apply', function (_collection, _args) {
+ assert.deepEqual(_collection, Collection);
+ assert.deepEqual(_args[0], args[0]);
+ assert.deepEqual(_args[1].$set.updated_at, 123);
+
+ stub3 = sinon.stub(Model, 'afterUpdate', function (_doc, _update, _callback) {
+ assert.deepEqual(_doc, args[0]);
+ assert.deepEqual(_update, args[1]);
+ _callback(null);
+ assert.ok(callback_called);
+ });
+
+ _args[3](null, [1, 2, 3]);
});
- _args[3](null, [1, 2, 3]);
+ _callback(null, _query, _update);
});
- _callback(null, _query, _update);
- });
-
- CollectionProxy.update(Model, Collection, args, cb);
+ CollectionProxy.update(Model, Collection, args, cb);
+ sinon.assert.calledOnce(stub1);
+ sinon.assert.calledOnce(stub2);
+ sinon.assert.calledOnce(stub3);
+ }));
});
- it('`findAndModify` finds and modifies a record', function () {
- var callback_called = false,
- cb = function (error, ret) {
- assert.deepEqual(error, null);
- assert.deepEqual(ret, [1, 2, 3]);
- callback_called = true;
- },
- args = [{name: 'zemba'}, [], {'$set': {name: 'foo'}}, {}, cb];
-
- gently.expect(Model, 'beforeUpdate', function (_query, _update, _callback) {
- assert.deepEqual(_query, args[0]);
- assert.deepEqual(_update, args[2]);
- _update.$set.updated_at = 123;
-
- gently.expect(Collection.findAndModify, 'apply', function (_collection, _args) {
- assert.deepEqual(_collection, Collection);
- assert.deepEqual(_args[0], args[0]);
- assert.deepEqual(_args[2].$set.updated_at, 123);
-
- gently.expect(Model, 'afterUpdate', function (_doc, _update, _callback) {
- assert.deepEqual(_doc, args[0]);
- assert.deepEqual(_update, args[2]);
- _callback(null);
- assert.ok(callback_called);
+ describe('findAndModify', function () {
+ it('finds and modifies a record', sinon.test(function () {
+ var callback_called = false
+ , self = this
+ , stub1, stub2, stub3
+ , cb = function (error, ret) {
+ assert.deepEqual(error, null);
+ assert.deepEqual(ret, [1, 2, 3]);
+ callback_called = true;
+ }
+ , args = [{name: 'zemba'}, [], {'$set': {name: 'foo'}}, {}, cb];
+
+ stub1 = self.stub(Model, 'beforeUpdate', function (_query, _update, _callback) {
+ assert.deepEqual(_query, args[0]);
+ assert.deepEqual(_update, args[2]);
+ _update.$set.updated_at = 123;
+
+ stub2 = self.stub(Collection.findAndModify, 'apply', function (_collection, _args) {
+ assert.deepEqual(_collection, Collection);
+ assert.deepEqual(_args[0], args[0]);
+ assert.deepEqual(_args[2].$set.updated_at, 123);
+
+ stub3 = self.stub(Model, 'afterUpdate', function (_doc, _update, _callback) {
+ assert.deepEqual(_doc, args[0]);
+ assert.deepEqual(_update, args[2]);
+ _callback(null);
+ assert.ok(callback_called);
+ });
+
+ _args[4](null, [1, 2, 3]);
});
- _args[4](null, [1, 2, 3]);
+ _callback(null, _query, _update);
});
- _callback(null, _query, _update);
- });
-
- CollectionProxy.findAndModify(Model, Collection, args, cb);
+ CollectionProxy.findAndModify(Model, Collection, args, cb);
+ sinon.assert.calledOnce(stub1);
+ sinon.assert.calledOnce(stub2);
+ sinon.assert.calledOnce(stub3);
+ }));
});
- it('`mapReduceCursor` calls `mapReduce` returning a cursor', function () {
- var args = ['a', 'b'],
- coll = {foo: 'bar'};
+ describe('mapReduceCursor', function () {
+ var args, stub1, stub2, coll, cb;
+
+ beforeEach(function () {
+ args = ['a', 'b'];
+ coll = {find: function () {}};
+ });
- [null, 'could not access the DB'].forEach(function (error) {
- var cb = null;
+ it('calls `mapReduce` returning a cursor if no intermediate errors occur', sinon.test(function () {
+ var self = this;
- gently.expect(Collection.mapReduce, 'apply', function (_collection, _args) {
+ stub1 = self.stub(Collection.mapReduce, 'apply', function (_collection, _args) {
assert.equal(_collection, Collection);
assert.equal(_args, args);
- if (!error) {
- gently.expect(coll, 'find', function (callback) {
- assert.ok(callback);
- });
- }
+ stub2 = self.stub(coll, 'find', function (callback) {
+ assert.ok(callback);
+ });
- _args[1](error, coll);
+ _args[1](null, coll);
});
- // TODO: Refactor this test
- if (error) {
- cb = gently.expect(function (_err, _coll) {
- assert.equal(_err, error);
- assert.equal(_coll, null);
- });
- } else {
- cb = function () {};
- }
+ cb = function () {};
CollectionProxy.mapReduceCursor(Model, Collection, args, cb);
- });
+ sinon.assert.calledOnce(stub1);
+ sinon.assert.calledOnce(stub2);
+ }));
+
+ it('calls `mapReduce` returning an error if an intermediate error occurs', sinon.test(function () {
+ var self = this
+ , error = 'could not access the DB';
+
+ stub1 = self.stub(Collection.mapReduce, 'apply', function (_collection, _args) {
+ assert.equal(_collection, Collection);
+ assert.equal(_args, args);
+ _args[1](error, coll);
+ });
+
+ stub2 = self.spy().withArgs(error, null);
+
+ CollectionProxy.mapReduceCursor(Model, Collection, args, stub2);
+ sinon.assert.calledOnce(stub1);
+ sinon.assert.calledOnce(stub2);
+ }));
});
- it('`mapReduceArray` returns a `mapReduceCursor` to Array', function () {
- var cb = function () {},
- args = ['fleiba', cb],
- cursor = {};
+ describe('mapReduceArray', function () {
+ it('returns a `mapReduceCursor` to Array', sinon.test(function () {
+ var cb = function () {}
+ , self = this
+ , stub1, stub2
+ , args = ['fleiba', cb]
+ , cursor = {toArray: function () {}};
+
+ stub1 = self.stub(CollectionProxy, 'mapReduceCursor', function (_model, _collection, _args, _callback) {
+ assert.equal(_model, Model);
+ assert.equal(_collection, Collection);
+ assert.equal(_args, args);
- gently.expect(CollectionProxy, 'mapReduceCursor', function (_model, _collection, _args, _callback) {
- assert.equal(_model, Model);
- assert.equal(_collection, Collection);
- assert.equal(_args, args);
+ stub2 = self.stub(cursor, 'toArray').withArgs(cb);
- gently.expect(cursor, 'toArray', function (_callback) {
- assert.equal(_callback, cb);
+ _callback(null, cursor);
});
- _callback(null, cursor);
- });
-
- CollectionProxy.mapReduceArray(Model, Collection, args, cb);
+ CollectionProxy.mapReduceArray(Model, Collection, args, cb);
+ sinon.assert.calledOnce(stub1);
+ sinon.assert.calledOnce(stub2);
+ }));
});
- it('`remove` removes a document', function () {
- var callback_called = false,
- cb = function (error, ret) {
- assert.deepEqual(error, null);
- assert.deepEqual(ret, [1, 2, 3]);
- callback_called = true;
- },
- args = ['fleiba', cb];
-
- gently.expect(Model, 'beforeRemove', function (_query, _callback) {
- assert.deepEqual(_query, args[0]);
-
- gently.expect(Collection.remove, 'apply', function (_collection, _args) {
- assert.deepEqual(Collection, _collection);
- assert.deepEqual(_args[0], args[0]);
-
- gently.expect(Model, 'afterRemove', function (_query, _callback) {
- assert.deepEqual(_query, args[0]);
- _callback(null);
- assert.ok(callback_called);
+ describe('remove', function () {
+ it('removes a document', sinon.test(function () {
+ var callback_called = false
+ , self = this
+ , stub1, stub2, stub3
+ , cb = function (error, ret) {
+ assert.deepEqual(error, null);
+ assert.deepEqual(ret, [1, 2, 3]);
+ callback_called = true;
+ }
+ , args = ['fleiba', cb];
+
+ stub1 = self.stub(Model, 'beforeRemove', function (_query, _callback) {
+ assert.deepEqual(_query, args[0]);
+
+ stub2 = self.stub(Collection.remove, 'apply', function (_collection, _args) {
+ assert.deepEqual(Collection, _collection);
+ assert.deepEqual(_args[0], args[0]);
+
+ stub3 = self.stub(Model, 'afterRemove', function (_query, _callback) {
+ assert.deepEqual(_query, args[0]);
+ _callback(null);
+ assert.ok(callback_called);
+ });
+
+ _args[1](null, [1, 2, 3]);
});
- _args[1](null, [1, 2, 3]);
+ _callback(null, _query);
});
- _callback(null, _query);
- });
-
- CollectionProxy.remove(Model, Collection, args, cb);
+ CollectionProxy.remove(Model, Collection, args, cb);
+ sinon.assert.calledOnce(stub1);
+ sinon.assert.calledOnce(stub2);
+ sinon.assert.calledOnce(stub3);
+ }));
});
});
View
4 test/integration_test.js
@@ -232,7 +232,7 @@ describe('Integration test', function () {
var User = Model(db, 'users');
User.validate = function (document, update, callback) {
- var validator = require('./../../lib/validator')(document, update);
+ var validator = require('./../lib/validator')(document, update);
if (update.name !== 'zemba') {
validator.addError('name', 'We only love Zemba here');
@@ -259,7 +259,7 @@ describe('Integration test', function () {
User.mongo('insert', {name: 'John Smith', age: 30}, function (errors, documents) {});
User.validate = function (document, update, callback) {
- var validator = require('./../../lib/validator')(document, update);
+ var validator = require('./../lib/validator')(document, update);
if (validator.updated_document.name !== 'zemba') {
validator.addError('name', 'We only love Zemba here');
View
262 test/model_test.js
@@ -1,10 +1,10 @@
/*globals describe, beforeEach, it*/
var assert = require('assert')
- , gently = global.GENTLY = new (require('gently'))()
-
+ , sinon = require('sinon')
, Model = require('./../lib/model')
+ , Validator = require('./../lib/validator')
- , _db = {bson_serializer: {}}
+ , _db = {bson_serializer: {}, collection: function () {}}
, _mock_validator = function (ret) {
return {
hasErrors: function () {
@@ -32,35 +32,37 @@ describe('Models', function () {
}, 'You must specify a collection name');
});
- it('`getCollection` returns a document collection', function () {
- var cb = function (error, collection) {};
+ it('`getCollection` returns a document collection', sinon.test(function () {
+ var cb = function () {}
+ , stub, self = this;
- gently.expect(_db, 'collection', function (_collection_name, _callback) {
- assert.equal(_collection_name, 'users');
- assert.equal(_callback, cb);
- });
+ stub = self.stub(_db, 'collection').withArgs('users', cb);
User.getCollection(cb);
- });
+ sinon.assert.calledOnce(stub);
+ }));
- it('`mongo` proxies collection calls', function () {
+ it('`mongo` proxies collection calls', sinon.test(function () {
var callback = function (error, doc) {}
+ , stub, self = this
, query = {name: 'zemba'};
- gently.expect(User.collection_proxy, 'proxy', function (_model, _options, _args, _callback) {
+ stub = self.stub(User.collection_proxy, 'proxy', function (_model, _options, _args, _callback) {
assert.deepEqual(_options, {hooks: true, namespacing: true, mapping: true, method: 'findArray'});
assert.deepEqual(_args[0], query);
assert.equal(_callback, callback);
});
User.mongo('findArray', query, callback);
- });
+ sinon.assert.calledOnce(stub);
+ }));
- it('`mongo` proxies namespaced collection calls', function () {
+ it('`mongo` proxies namespaced collection calls', sinon.test(function () {
var callback = function (error, doc) {}
+ , stub, self = this
, query = {name: 'zemba'};
- gently.expect(User.collection_proxy, 'proxy', function (_model, _options, _args, _callback) {
+ stub = self.stub(User.collection_proxy, 'proxy', function (_model, _options, _args, _callback) {
assert.deepEqual(_options, {
hooks: true
, namespacing: true
@@ -73,13 +75,15 @@ describe('Models', function () {
});
User.mongo('findArray:public', query, callback);
- });
+ sinon.assert.calledOnce(stub);
+ }));
- it('`mongo` proxies with options', function () {
+ it('`mongo` proxies with options', sinon.test(function () {
var callback = function (error, doc) {}
+ , stub, self = this
, query = {name: 'zemba'};
- gently.expect(User.collection_proxy, 'proxy', function (_model, _options, _args, _callback) {
+ stub = self.stub(User.collection_proxy, 'proxy', function (_model, _options, _args, _callback) {
assert.deepEqual(_options, {
hooks: false
, namespacing: true
@@ -92,12 +96,14 @@ describe('Models', function () {
});
User.mongo({method: 'findArray', namespace: 'public', hooks: false}, query, callback);
- });
+ sinon.assert.calledOnce(stub);
+ }));
- it('`mongo` can be called without a callback', function () {
- var query = {name: 'zemba'};
+ it('`mongo` can be called without a callback', sinon.test(function () {
+ var query = {name: 'zemba'}
+ , stub, self = this;
- gently.expect(User.collection_proxy, 'proxy', function (_model, _options, _args, _callback) {
+ stub = self.stub(User.collection_proxy, 'proxy', function (_model, _options, _args, _callback) {
assert.deepEqual(_options, {
hooks: true
, namespacing: true
@@ -110,75 +116,71 @@ describe('Models', function () {
});
User.mongo('findArray', query);
- });
-
- it('`validate` validates a mongo document', function () {
- var document = {},
- update = {name: 'Pau'},
- validator = {data: 'foo'},
- callback;
-
- gently.hijacked['./validator'] = gently.expect(function (_document, _update) {
- assert.deepEqual(document, _document);
- assert.deepEqual(update, _update);
- return validator;
- });
+ sinon.assert.calledOnce(stub);
+ }));
+
+ it('`validate` validates a mongo document', sinon.test(function () {
+ var document = {}
+ , self = this, stub
+ , update = {name: 'Pau'}
+ , validator = {data: 'foo'}
+ , callback;
- callback = gently.expect(function (_error, _validator) {
- assert.equal(_error, null);
- assert.deepEqual(validator, validator);
- });
+ stub = self.stub(User, 'validator').withArgs(document, update).returns(validator);
+ callback = self.spy().withArgs(null, validator);
User.validate(document, update, callback);
- });
+ sinon.assert.calledOnce(stub);
+ sinon.assert.calledOnce(callback);
+ }));
- it('`validateAndInsert` when the model is invalid does not insert it', function () {
- var document = {},
- validator = _mock_validator(true),
- callback;
+ it('`validateAndInsert` when the model is invalid does not insert it', sinon.test(function () {
+ var document = {}
+ , self = this, stub
+ , validator = _mock_validator(true)
+ , callback;
- gently.expect(User, 'validate', function (_document, _data, _callback) {
+ stub = self.stub(User, 'validate', function (_document, _data, _callback) {
_callback(null, validator);
});
-
- callback = gently.expect(function (_error, _validator) {
- assert.equal(_error, null);
- assert.deepEqual(_validator, validator);
- });
+ callback = self.spy().withArgs(null, validator);
User.validateAndInsert(document, callback);
- });
+ sinon.assert.calledOnce(stub);
+ sinon.assert.calledOnce(callback);
+ }));
- it('`validateAndInsert` when the model is valid inserts it afterwards', function () {
- var document = {foo: 'bar'},
- validator = _mock_validator(false),
- callback;
+ it('`validateAndInsert` when the model is valid inserts it afterwards', sinon.test(function () {
+ var document = {foo: 'bar'}
+ , self = this, stub1, stub2
+ , validator = _mock_validator(false)
+ , callback;
User.maps = {
foo: function (el) {
return el.toUpperCase();
}
};
- gently.expect(User, 'validate', function (_document, _data, _callback) {
+ stub1 = self.stub(User, 'validate', function (_document, _data, _callback) {
_callback(null, validator);
});
- gently.expect(User, 'mongo', function (_action, _document, _callback) {
+ stub2 = self.stub(User, 'mongo', function (_action, _document, _callback) {
assert.deepEqual(_action, {method: 'insert', namespacing: false, mapping: false});
assert.deepEqual(_document.foo, 'BAR');
_callback(null, _document);
});
- callback = gently.expect(function (_error, _validator) {
- assert.equal(_error, null);
- assert.deepEqual(_validator, validator);
- });
+ callback = self.spy().withArgs(null, validator);
User.validateAndInsert(document, callback);
- });
+ sinon.assert.calledOnce(stub1);
+ sinon.assert.calledOnce(stub2);
+ sinon.assert.calledOnce(callback);
+ }));
- it('`beforeInsert` default hook sets the created_at date', function () {
+ it('`beforeInsert` default hook sets the created_at date', sinon.test(function () {
var documents = [{name: 'zemba'}, {foo: 'bar'}];
User.beforeInsert(documents, function (_error, _documents) {
@@ -187,47 +189,47 @@ describe('Models', function () {
assert.equal(document.created_at.constructor, (new Date()).constructor);
});
});
- });
+ }));
- it('`beforeUpdate` default hook updated the updated_at date', function () {
- var query = {foo: 'bar'},
- update = {'$set': {fleiba: 'zemba'}};
+ it('`beforeUpdate` default hook updated the updated_at date', sinon.test(function () {
+ var query = {foo: 'bar'}
+ , update = {'$set': {fleiba: 'zemba'}};
User.beforeUpdate(query, update, function (error, _query, _update) {
assert.ok(_update.$set);
assert.ok(_update.$set.updated_at);
assert.equal(_update.$set.updated_at.constructor, (new Date()).constructor);
});
- });
+ }));
- it('`validateAndUpdate` when the model is invalid does not update it', function () {
+ it('`validateAndUpdate` when the model is invalid does not update it', sinon.test(function () {
var query = {foo: 'bar'}
+ , self = this, stub1, stub2
, document = {foo: 'bar', fleiba: 'foo'}
, update = {fleiba: 'zemba'}
, validator = _mock_validator(true)
, options = {}
, callback;
- gently.expect(User, 'mongo', function (_method, _query, _callback) {
+ stub1 = self.stub(User, 'mongo', function (_method, _query, _callback) {
assert.equal(_method, 'findOne');
assert.deepEqual(_query, query);
_callback(null, document);
});
-
- gently.expect(User, 'validate', function (_document, _data, _callback) {
+ stub2 = self.stub(User, 'validate', function (_document, _data, _callback) {
_callback(null, validator);
});
-
- callback = gently.expect(function (_error, _validator) {
- assert.equal(_error, null);
- assert.deepEqual(_validator, validator);
- });
+ callback = self.spy().withArgs(null, validator);
User.validateAndUpdate(query, update, options, callback);
- });
+ sinon.assert.calledOnce(stub1);
+ sinon.assert.calledOnce(stub2);
+ sinon.assert.calledOnce(callback);
+ }));
- it('`validateAndUpdate` when the model is valid updates it afterwards', function () {
+ it('`validateAndUpdate` when the model is valid updates it afterwards', sinon.test(function () {
var query = {foo: 'bar'}
+ , self = this, stub1, stub2, stub3
, document = {_id: '123', foo: 'bar'}
, update = {'$set': {fleiba: 'John'}}
, validator = _mock_validator(false)
@@ -240,37 +242,40 @@ describe('Models', function () {
}
};
- gently.expect(User, 'mongo', function (_method, _query, _callback) {
+ stub1 = self.stub(User, 'mongo', function (_method, _query, _callback) {
assert.equal(_method, 'findOne');
assert.deepEqual(_query, query);
+
+ sinon.assert.calledOnce(stub1);
+ stub1.restore();
+ stub3 = self.stub(User, 'mongo', function (_action, _document, _update, _options, _callback) {
+ assert.deepEqual(_action, {method: 'update', namespacing: false, mapping: false});
+ assert.deepEqual(_document._id, document._id);
+ assert.deepEqual(_update.$set.fleiba, 'john');
+ assert.deepEqual(_options, options);
+ _callback(null, _document);
+ });
+
_callback(null, document);
});
User.beforeUpdate = function (_query, _update, _callback) {
_callback(null, document);
};
- gently.expect(User, 'validate', function (_document, _data, _callback) {
+ stub2 = self.stub(User, 'validate', function (_document, _data, _callback) {
_callback(null, validator);
});
- gently.expect(User, 'mongo', function (_action, _document, _update, _options, _callback) {
- assert.deepEqual(_action, {method: 'update', namespacing: false, mapping: false});
- assert.deepEqual(_document._id, document._id);
- assert.deepEqual(_update.$set.fleiba, 'john');
- assert.deepEqual(_options, options);
- _callback(null, _document);
- });
-
- callback = gently.expect(function (_error, _validator) {
- assert.equal(_error, null);
- assert.deepEqual(_validator, validator);
- });
+ callback = self.spy().withArgs(null, validator);
User.validateAndUpdate(query, update, options, callback);
- });
+ sinon.assert.calledOnce(stub2);
+ sinon.assert.calledOnce(stub3);
+ sinon.assert.calledOnce(callback);
+ }));
- it('`getEmbeddedDocument` filters the document following the skeletons directive', function () {
+ it('`getEmbeddedDocument` filters the document following the skeletons directive', sinon.test(function () {
var comment = {_id: 1, title: 'foo', body: 'Lorem ipsum'};
User.skeletons = {
@@ -279,12 +284,12 @@ describe('Models', function () {
assert.deepEqual(User.getEmbeddedDocument('comment', comment), { _id: 1, title: 'foo' });
assert.deepEqual(
- User.getEmbeddedDocument('comment', comment, 'post.comment'),
- {post: {comment: {_id: 1, title: 'foo'}}}
+ User.getEmbeddedDocument('comment', comment, 'post.comment')
+ , {post: {comment: {_id: 1, title: 'foo'}}}
);
- });
+ }));
- it('`getEmbeddedDocument` filters the document following recursive skeletons directives', function () {
+ it('`getEmbeddedDocument` filters the document following recursive skeletons directives', sinon.test(function () {
var post = {_id: 1, title: 'foo', body: 'Lorem ipsum', comment: {body: 'comment body!', created_at: Date.now()}};
User.skeletons = {
@@ -293,12 +298,12 @@ describe('Models', function () {
assert.deepEqual(User.getEmbeddedDocument('post', post), {_id: 1, title: 'foo', comment: {body: 'comment body!'}});
assert.deepEqual(
- User.getEmbeddedDocument('post', post, 'post'),
- {post: {_id: 1, title: 'foo', comment: {body: 'comment body!'}}}
+ User.getEmbeddedDocument('post', post, 'post')
+ , {post: {_id: 1, title: 'foo', comment: {body: 'comment body!'}}}
);
- });
+ }));
- it('`getEmbeddedDocument` returns appropiate `dot_notation` strings', function () {
+ it('`getEmbeddedDocument` returns appropiate `dot_notation` strings', sinon.test(function () {
var comment = {_id: 1, title: 'foo', body: 'Lorem ipsum'};
User.skeletons = {
@@ -311,46 +316,47 @@ describe('Models', function () {
, {'post._id': 1, 'post.title': 'foo'}
);
assert.deepEqual(
- User.getEmbeddedDocument('comment', comment, 'post.comment', true),
- {'post.comment._id': 1, 'post.comment.title': 'foo'}
+ User.getEmbeddedDocument('comment', comment, 'post.comment', true)
+ , {'post.comment._id': 1, 'post.comment.title': 'foo'}
);
- });
+ }));
- it('`getEmbeddedDocument` returns appropiate `dot_notation` strings using rescursive stuff', function () {
+ it('`getEmbeddedDocument` returns appropiate `dot_notation` strings using rescursive stuff', sinon.test(function () {
var post = {_id: 1, title: 'foo', body: 'Lorem ipsum', comment: {body: 'comment body!', created_at: Date.now()}};
User.skeletons = {
post: ['_id', 'title', 'comment.body']
};
assert.deepEqual(
- User.getEmbeddedDocument('post', post, 'user.post', true),
- {'user.post._id': 1, 'user.post.title': 'foo', 'user.post.comment.body': 'comment body!'}
+ User.getEmbeddedDocument('post', post, 'user.post', true)
+ , {'user.post._id': 1, 'user.post.title': 'foo', 'user.post.comment.body': 'comment body!'}
);
- });
+ }));
- it('`getEmbeddedDocument` works without specifying the skeletons', function () {
+ it('`getEmbeddedDocument` works without specifying the skeletons', sinon.test(function () {
var comment = {_id: 1, title: 'foo', body: 'Lorem ipsum'};
User.skeletons = null;
assert.deepEqual(User.getEmbeddedDocument('comment', comment), { _id: 1, title: 'foo', body: 'Lorem ipsum'});
assert.deepEqual(
- User.getEmbeddedDocument('comment', comment, 'post.comment'),
- {post: {comment: comment}}
+ User.getEmbeddedDocument('comment', comment, 'post.comment')
+ , {post: {comment: comment}}
);
- });
+ }));
- it('`updateEmbeddedDocument` updates embedded objects', function () {
- var embeddedDocument = {name: 'john', surname: 'snow', bo: 'vale'},
- options = {upsert: true},
- callback = function () {};
+ it('`updateEmbeddedDocument` updates embedded objects', sinon.test(function () {
+ var embeddedDocument = {name: 'john', surname: 'snow', bo: 'vale'}
+ , self = this, stub
+ , options = {upsert: true}
+ , callback = function () {};
User.skeletons = {
author: ['_id', 'name', 'surname']
};
- gently.expect(User, 'mongo', function (_opts, _query, _update, _options, _callback) {
+ stub = self.stub(User, 'mongo', function (_opts, _query, _update, _options, _callback) {
assert.deepEqual(_opts, {method: 'update', hooks: false});
assert.deepEqual(_query, {'author._id': 1});
assert.deepEqual(_update, {'$set': {'author.name': 'john', 'author.surname': 'snow'}});
@@ -359,27 +365,31 @@ describe('Models', function () {
});
User.updateEmbeddedDocument({_id: 1}, 'author', embeddedDocument, options, callback);
- });
+ sinon.assert.calledOnce(stub);
+ }));
- it('`pushEmbeddedDocument` pushes embedded objects', function () {
- var embeddedDocument = {name: 'john'},
- collection = {foo: 'bar'};
+ it('`pushEmbeddedDocument` pushes embedded objects', sinon.test(function () {
+ var embeddedDocument = {name: 'john'}
+ , self = this, stub1, stub2
+ , collection = {foo: 'bar'};
- gently.expect(User, 'getEmbeddedDocument', function (_name, _doc, _scope, _dot_notation) {
+ stub1 = self.stub(User, 'getEmbeddedDocument', function (_name, _doc, _scope, _dot_notation) {
assert.equal(_name, 'author');
assert.deepEqual(_doc, embeddedDocument);
assert.ifError(_scope);
assert.ifError(_dot_notation);
return embeddedDocument;
});
- gently.expect(User, 'mongo', function (_opts, _query, _update, _options, _callback) {
+ stub2 = self.stub(User, 'mongo', function (_opts, _query, _update, _options, _callback) {
assert.deepEqual(_opts, {method: 'update', hooks: false});
assert.deepEqual(_query, {_id: 1});
assert.deepEqual(_update, {'$push': {author: embeddedDocument}});
assert.deepEqual(_options, {upsert: false, multi: true});
});
User.pushEmbeddedDocument({_id: 1}, 'author', embeddedDocument);
- });
+ sinon.assert.calledOnce(stub1);
+ sinon.assert.calledOnce(stub2);
+ }));
});
View
2 test/validator_test.js
@@ -1,7 +1,5 @@
/*globals describe, beforeEach, it*/
var assert = require('assert')
- , gently = global.GENTLY = new (require('gently'))()
-
, Validator = require('./../lib/validator');
describe('validators', function () {

0 comments on commit ddecb3e

Please sign in to comment.