Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

migrated tests from testosterone to mocha

  • Loading branch information...
commit ab8b3e8cf046ebd5efa39b6bbfe5b1238ef527dd 1 parent ab1ac41
Pau Ramon Revilla authored
11 Makefile
View
@@ -1,24 +1,25 @@
NODE = node
+MOCHA = ./node_modules/.bin/mocha --reporter spec
test: test_model test_validator test_proxy test_update_document test_namespacer test_mapper test_integration
test_model:
- @$(NODE) test/model_test.js
+ @$(MOCHA) test/model_test
test_validator:
- @$(NODE) test/validator_test.js
+ @$(MOCHA) test/validator_test.js
test_update_document:
@$(NODE) test/helpers/update_document_test.js
test_namespacer:
- @$(NODE) test/helpers/namespacer_test.js
+ @$(MOCHA) test/helpers/namespacer_test.js
test_mapper:
- @$(NODE) test/helpers/mapper_test.js
+ @$(MOCHA) test/helpers/mapper_test.js
test_proxy:
@$(NODE) test/helpers/collection_proxy_test.js
test_integration:
- @$(NODE) test/integration/integration_test.js
+ @$(MOCHA) test/integration/integration_test.js
3  package.json
View
@@ -15,8 +15,9 @@
"underscore": "1.1.6"
},
"devDependencies": {
+ "mocha": "*",
"testosterone": "1.2.0",
- "gently": "0.9.1"
+ "gently": "git://github.com/masylum/node-gently.git"
},
"repository" : {"type": "git" , "url": "http://github.com/masylum/mongolia.git" },
"engines": { "node": ">= 0.4.0" }
15 test/helpers/mapper_test.js
View
@@ -1,9 +1,9 @@
-var testosterone = require('testosterone')({post: 3000, sync: true, title: 'mongolia/helpers/mapper_test.js'}),
- assert = testosterone.assert,
- Mapper = require('./../../lib/helpers/mapper');
+/*global describe, it*/
+var assert = require('assert')
+ , Mapper = require('./../../lib/helpers/mapper');
-testosterone
- .add('`mapDocument` should filter documents before being inserted or updated', function () {
+describe('mapper', function () {
+ it('`mapDocument` should filter documents before being inserted or updated', function () {
var test = Mapper.mapDocument
, arg
, toUpper = function (val) {
@@ -52,7 +52,6 @@ testosterone
assert.equal(typeof arg.list[0], typeof update.list[0]);
assert.equal(typeof arg.list[1], typeof update.list[1]);
assert.equal(typeof arg.list[2], typeof update.list[2]);
- })
-
- .run();
+ });
+});
34 test/helpers/namespacer_test.js
View
@@ -1,12 +1,9 @@
-var testosterone = require('testosterone')({sync: true, title: 'mongolia/helpers/namespacer_test.js'}),
- assert = testosterone.assert,
- Namespacer = require('./../../lib/helpers/namespacer');
+/*globals describe, it*/
+var assert = require('assert')
+ , Namespacer = require('./../../lib/helpers/namespacer');
-testosterone
- .before(function () {
- })
-
- .add('`resolveNamespace` implements the whole namespace API', function () {
+describe('namespacer', function () {
+ it('`resolveNamespace` implements the whole namespace API', function () {
var test = Namespacer.resolveNamespace
, namespaces = { jazz: ['mile.davis', 'coltrane', 'ellington', 'fitzgerald']
, modern: { extend: 'jazz'
@@ -15,9 +12,9 @@ testosterone
assert.deepEqual(test(namespaces, 'jazz'), namespaces.jazz);
assert.deepEqual(test(namespaces, 'modern'), ['mile.davis', 'coltrane', 'cohen', 'corea']);
- })
+ });
- .add('`addFieldFindOne` adds `fields` to findOne method if using a namespace', function () {
+ it('`addFieldFindOne` adds `fields` to findOne method if using a namespace', function () {
var test = Namespacer.addFieldFindOne
, args
, cb = function () {}
@@ -36,9 +33,9 @@ testosterone
args = [{_id: 1}, {fields: {zemba: -1}}, cb];
test(visibility, args);
assert.deepEqual(args[1], fields);
- })
+ });
- .add('`addFieldFind` adds `fields` to find methods if using a namespace', function () {
+ it('`addFieldFind` adds `fields` to find methods if using a namespace', function () {
var test = Namespacer.addFieldFind
, args
, cb = function () {}
@@ -60,9 +57,9 @@ testosterone
args = [{_id: 1}, {zemba: -1}, {skip: 1}, cb];
test(visibility, args);
assert.deepEqual(args[1], fields);
- })
+ });
- .add('`addFieldFindAndModify` adds `fields` to findAndModify method if using a namespace', function () {
+ it('`addFieldFindAndModify` adds `fields` to findAndModify method if using a namespace', function () {
var test = Namespacer.addFieldFindAndModify
, args
, cb = function () {}
@@ -84,9 +81,9 @@ testosterone
args = [{_id: 1}, [], {foo: 'bar'}, {fields: {zemba: -1}}, cb];
test(visibility, args);
assert.deepEqual(args[3], fields);
- })
+ });
- .add('`filterUpdate` should filter documents before being inserted or updated', function () {
+ it('`filterUpdate` should filter documents before being inserted or updated', function () {
var test = Namespacer.filterUpdate
, arg
, insert = {django: {reinhardt: 'guitar', framework: null}, charlie: {parker: 'saxophone'}}
@@ -126,6 +123,5 @@ testosterone
arg = {'$set': {django: {reinhardt: 'guitar', framework: null}, will_not: 'be_update', charlie: { christian: 'guitar', parker: 'saxophone'}}};
test(visibility, arg);
assert.deepEqual(arg, {'$set': {django: {reinhardt: 'guitar', framework: null}, charlie: {parker: 'saxophone'}}});
- })
-
- .run();
+ });
+});
455 test/integration/integration_test.js
View
@@ -1,13 +1,13 @@
-var testosterone = require('testosterone')({post: 3000, title: 'integration/integration_test.js'}),
- assert = testosterone.assert,
+/*globals describe, it, before*/
+var assert = require('assert')
- Model = require('./../../lib/model'),
- mongodb = require('mongodb'),
- Db = mongodb.Db,
- Server = mongodb.Server,
- db = new Db('mongolia_test', new Server('localhost', 27017, {auto_reconnect: true, native_parser: true}), {}),
+ , Model = require('./../../lib/model')
+ , mongodb = require('mongodb')
+ , Db = mongodb.Db
+ , Server = mongodb.Server
+ , db = new Db('mongolia_test', new Server('localhost', 27017, {auto_reconnect: true, native_parser: true}), {})
- remove_users = function (cb) {
+ , remove_users = function (cb) {
db.collection('users', function (error, collection) {
collection.remove({}, function () {
db.collection('countries', function (error, collection) {
@@ -17,261 +17,266 @@ var testosterone = require('testosterone')({post: 3000, title: 'integration/inte
});
};
-db.open(function (error) {
+describe('Integration test', function () {
- if (error) {
- throw error;
- }
+ before(function (done) {
+ db.open(function (error) {
+ if (error) {
+ throw error;
+ }
- remove_users(function () {
+ remove_users(done);
+ });
+ });
- testosterone
+ it('`Insert` documents with `before/afterInsert` hooks', function (done) {
+ var User = Model(db, 'users'),
+ Country = Model(db, 'countries');
- .add('`Insert` documents with `before/afterInsert` hooks', function (done) {
- var User = Model(db, 'users'),
- Country = Model(db, 'countries');
+ User.beforeInsert = function (documents, callback) {
+ documents.forEach(function (document) {
+ document.has_country = true;
+ });
+ callback(null, documents);
+ };
- User.beforeInsert = function (documents, callback) {
- documents.forEach(function (document) {
- document.has_country = true;
- });
- callback(null, documents);
- };
+ User.afterInsert = function (documents, callback) {
+ documents.forEach(function (document) {
+ document.comments = [];
+ });
+ callback(null, documents);
+ };
- User.afterInsert = function (documents, callback) {
- documents.forEach(function (document) {
- document.comments = [];
- });
- callback(null, documents);
- };
-
- User.namespaces = {
- 'public': ['name', 'country']
- };
-
- Country.maps = {
- name: function capitalize(val) {
- return val.charAt(0).toUpperCase() + val.slice(1);
- }
- , iso: {id: Number}
- };
-
- Country.mongo('insert', {name: 'andorra', iso: {id: '123'}}, function (error, docs) {
- var doc = docs[0];
- assert.equal(doc.name, 'Andorra');
- assert.ok(doc.created_at);
- assert.ok(doc.iso.id, 123);
-
- User.mongo('insert:public', {name: 'zemba', country: doc, password: 'malicious'}, function (error, docs) {
- var doc = docs[0];
- assert.equal(doc.name, 'zemba');
- assert.equal(doc.has_country, true);
- assert.deepEqual(doc.country.name, 'Andorra');
- assert.equal(doc.password, null); // namespaced!
- assert.deepEqual(doc.comments, []);
- done();
- });
- });
- })
-
- .add('`Update embedded` documents', function (done) {
- var User = Model(db, 'users'),
- Country = Model(db, 'countries');
-
- Country.mongo('findOne', {name: 'Andorra'}, function (error, doc) {
- User.updateEmbeddedDocument({_id: doc._id}, 'country', {name: 'France'}, {}, function (error) {
- User.mongo('findOne', {name: 'zemba'}, done(function (error, doc) {
- assert.equal(doc.country.name, 'France');
- assert.equal(doc.country.iso.id, '123');
- }));
- });
- });
- })
-
- .add('`Push embedded` documents', function (done) {
- var User = Model(db, 'users'),
- funk = require('funk')(),
- query = {name: 'zemba'};
-
- User.pushEmbeddedDocument(query, 'comments', {body: 'bla bla bla'}, {}, funk.nothing());
- User.pushEmbeddedDocument(query, 'comments', {body: 'trolling bla'}, {}, funk.nothing());
-
- funk.parallel(function () {
- User.mongo('findOne', {name: 'zemba'}, done(function (error, doc) {
- assert.deepEqual(doc.comments, [
- {body: 'bla bla bla'}
- , {body: 'trolling bla'}
- ]);
- done();
- }));
+ User.namespaces = {
+ 'public': ['name', 'country']
+ };
+
+ Country.maps = {
+ name: function capitalize(val) {
+ return val.charAt(0).toUpperCase() + val.slice(1);
+ }
+ , iso: {id: Number}
+ };
+
+ Country.mongo('insert', {name: 'andorra', iso: {id: '123'}}, function (error, docs) {
+ var doc = docs[0];
+ assert.equal(doc.name, 'Andorra');
+ assert.ok(doc.created_at);
+ assert.ok(doc.iso.id, 123);
+
+ User.mongo('insert:public', {name: 'zemba', country: doc, password: 'malicious'}, function (error, docs) {
+ var doc = docs[0];
+ assert.equal(doc.name, 'zemba');
+ assert.equal(doc.has_country, true);
+ assert.deepEqual(doc.country.name, 'Andorra');
+ assert.equal(doc.password, null); // namespaced!
+ assert.deepEqual(doc.comments, []);
+ done();
+ });
+ });
+ });
+
+ it('`Update embedded` documents', function (done) {
+ var User = Model(db, 'users'),
+ Country = Model(db, 'countries');
+
+ Country.mongo('findOne', {name: 'Andorra'}, function (error, doc) {
+ User.updateEmbeddedDocument({_id: doc._id}, 'country', {name: 'France'}, {}, function (error) {
+ User.mongo('findOne', {name: 'zemba'}, function (error, doc) {
+ assert.equal(doc.country.name, 'France');
+ assert.equal(doc.country.iso.id, '123');
+ done();
});
- })
+ });
+ });
+ });
- .add('`findAndModify` documents with `before/afterUpdate` hooks', function (done) {
- var User = Model(db, 'users'),
- Country = Model(db, 'countries'),
- query = {name: 'zemba'},
- update = {'$set': {name: 'fleiba'}},
- calledBefore = false,
- calledAfter = false;
+ it('`Push embedded` documents', function (done) {
+ var User = Model(db, 'users'),
+ funk = require('funk')(),
+ query = {name: 'zemba'};
+
+ User.pushEmbeddedDocument(query, 'comments', {body: 'bla bla bla'}, {}, funk.nothing());
+ User.pushEmbeddedDocument(query, 'comments', {body: 'trolling bla'}, {}, funk.nothing());
+
+ funk.parallel(function () {
+ User.mongo('findOne', {name: 'zemba'}, function (error, doc) {
+ assert.deepEqual(doc.comments, [
+ {body: 'bla bla bla'}
+ , {body: 'trolling bla'}
+ ]);
+ done();
+ });
+ });
+ });
+ it('`findAndModify` documents with `before/afterUpdate` hooks', function (done) {
+ var User = Model(db, 'users'),
+ Country = Model(db, 'countries'),
+ query = {name: 'zemba'},
+ update = {'$set': {name: 'fleiba'}},
+ calledBefore = false,
+ calledAfter = false;
- User.beforeUpdate = function (_query, _update, _callback) {
- calledBefore = true;
- _update.$set.updated_at = new Date();
+ User.beforeUpdate = function (_query, _update, _callback) {
+ calledBefore = true;
- Country.mongo('findOne', {name: 'Andorra'}, function (error, doc) {
- _update.$set['country.name'] = doc.name;
+ _update.$set.updated_at = new Date();
- assert.deepEqual(_query, query);
- assert.deepEqual(_update.$set.name, update.$set.name);
- assert.deepEqual(_update.$set['country.name'], 'Andorra');
+ Country.mongo('findOne', {name: 'Andorra'}, function (error, doc) {
+ _update.$set['country.name'] = doc.name;
- _callback(error, _query, _update);
- });
- };
+ assert.deepEqual(_query, query);
+ assert.deepEqual(_update.$set.name, update.$set.name);
+ assert.deepEqual(_update.$set['country.name'], 'Andorra');
+
+ _callback(error, _query, _update);
+ });
+ };
+
+ User.afterUpdate = function (_query, _update, _callback) {
+ calledAfter = true;
- User.afterUpdate = function (_query, _update, _callback) {
- calledAfter = true;
+ assert.deepEqual(_query, query);
+ assert.deepEqual(_update.$set.name, update.$set.name);
+ assert.deepEqual(_update.$set['country.name'], 'Andorra');
- assert.deepEqual(_query, query);
- assert.deepEqual(_update.$set.name, update.$set.name);
- assert.deepEqual(_update.$set['country.name'], 'Andorra');
+ _callback(null, _query, _update);
+ };
- _callback(null, _query, _update);
- };
+ User.mongo('findAndModify', query, [], update, {'new': true}, function (error, doc) {
+ assert.ok(calledBefore);
+ assert.ok(calledAfter);
+ assert.deepEqual(doc.country.name, 'Andorra');
+ assert.deepEqual(doc.name, 'fleiba');
+ done();
+ });
+ });
- User.mongo('findAndModify', query, [], update, {'new': true}, done(function (error, doc) {
- assert.ok(calledBefore);
- assert.ok(calledAfter);
- assert.deepEqual(doc.country.name, 'Andorra');
- assert.deepEqual(doc.name, 'fleiba');
- }));
- })
+ it('`update` documents with `before/afterUpdate` hooks', function (done) {
+ var User = Model(db, 'users'),
+ Country = Model(db, 'countries'),
+ query = {name: 'fleiba'},
+ update = {'$set': {name: 'zemba'}},
+ calledBefore = false,
+ calledAfter = false;
- .add('`update` documents with `before/afterUpdate` hooks', function (done) {
- var User = Model(db, 'users'),
- Country = Model(db, 'countries'),
- query = {name: 'fleiba'},
- update = {'$set': {name: 'zemba'}},
- calledBefore = false,
- calledAfter = false;
+ User.beforeUpdate = function (_query, _update, _callback) {
+ calledBefore = true;
- User.beforeUpdate = function (_query, _update, _callback) {
- calledBefore = true;
+ _update.$set.updated_at = new Date();
- _update.$set.updated_at = new Date();
+ Country.mongo('findOne', {name: 'Andorra'}, function (error, doc) {
+ _update.$set['country.name'] = 'France';
- Country.mongo('findOne', {name: 'Andorra'}, function (error, doc) {
- _update.$set['country.name'] = 'France';
+ assert.deepEqual(_query, query);
+ assert.deepEqual(_update.$set.name, update.$set.name);
- assert.deepEqual(_query, query);
- assert.deepEqual(_update.$set.name, update.$set.name);
+ _callback(error, _query, _update);
+ });
+ };
- _callback(error, _query, _update);
- });
- };
-
- User.afterUpdate = function (_query, _update, _callback) {
- calledAfter = true;
-
- assert.deepEqual(_query, query);
- assert.deepEqual(_update.$set.name, update.$set.name);
- assert.deepEqual(_update.$set['country.name'], 'France');
-
- _callback(null, _query, _update);
- };
-
- User.mongo('update', query, update, function (error, doc) {
- assert.ok(calledBefore);
- assert.ok(calledAfter);
- User.mongo('findArray', update.$set, done(function (error, docs) {
- var doc = docs[0];
- assert.deepEqual(doc.country.name, 'France');
- assert.deepEqual(doc.name, 'zemba');
- }));
- });
- })
-
- .add('`Remove` documents with `before/afterRemove` hooks', function (done) {
- var User = Model(db, 'users'),
- query = {name: 'zemba'},
- calledBefore = false,
- calledAfter = false;
-
- User.beforeRemove = function (_query, callback) {
- calledBefore = true;
- assert.deepEqual(query, _query);
- callback(null, _query);
- };
-
- User.afterRemove = function (_query, callback) {
- calledAfter = true;
- assert.deepEqual(query, _query);
- callback(null);
- };
-
- User.mongo('remove', query, function (error, ret) {
- assert.ok(calledBefore);
- assert.ok(calledAfter);
- User.mongo('findArray', {}, done(function (error, docs) {
- assert.deepEqual(docs, []);
- }));
- });
- })
+ User.afterUpdate = function (_query, _update, _callback) {
+ calledAfter = true;
- .add('`validateAndInsert` validates and inserts', function (done) {
- var User = Model(db, 'users');
+ assert.deepEqual(_query, query);
+ assert.deepEqual(_update.$set.name, update.$set.name);
+ assert.deepEqual(_update.$set['country.name'], 'France');
- User.validate = function (document, update, callback) {
- var validator = require('./../../lib/validator')(document, update);
+ _callback(null, _query, _update);
+ };
- if (update.name !== 'zemba') {
- validator.addError('name', 'We only love Zemba here');
- }
+ User.mongo('update', query, update, function (error, doc) {
+ assert.ok(calledBefore);
+ assert.ok(calledAfter);
+ User.mongo('findArray', update.$set, function (error, docs) {
+ var doc = docs[0];
+ assert.deepEqual(doc.country.name, 'France');
+ assert.deepEqual(doc.name, 'zemba');
+ done();
+ });
+ });
+ });
- callback(null, validator);
- };
+ it('`Remove` documents with `before/afterRemove` hooks', function (done) {
+ var User = Model(db, 'users'),
+ query = {name: 'zemba'},
+ calledBefore = false,
+ calledAfter = false;
- User.validateAndInsert({name: 'zemba'}, done(function (error, validation) {
- assert.equal(validation.updated_document.name, 'zemba');
- assert.deepEqual(validation.errors, {});
+ User.beforeRemove = function (_query, callback) {
+ calledBefore = true;
+ assert.deepEqual(query, _query);
+ callback(null, _query);
+ };
- // Try to insert an invalid record
- User.validateAndInsert({name: 'barbaz'}, function (error, validation) {
- assert.deepEqual(validation.errors.name, ['We only love Zemba here']);
- });
- }));
- })
+ User.afterRemove = function (_query, callback) {
+ calledAfter = true;
+ assert.deepEqual(query, _query);
+ callback(null);
+ };
+
+ User.mongo('remove', query, function (error, ret) {
+ assert.ok(calledBefore);
+ assert.ok(calledAfter);
+ User.mongo('findArray', {}, function (error, docs) {
+ assert.deepEqual(docs, []);
+ done();
+ });
+ });
+ });
- .add('`validateAndUpdate` validates and updates', function (done) {
- var User = Model(db, 'users');
+ it('`validateAndInsert` validates and inserts', function (done) {
+ var User = Model(db, 'users');
- User.mongo('insert', {name: 'John Smith', age: 30}, function (errors, documents) {});
+ User.validate = function (document, update, callback) {
+ var validator = require('./../../lib/validator')(document, update);
- User.validate = function (document, update, callback) {
- var validator = require('./../../lib/validator')(document);
+ if (update.name !== 'zemba') {
+ validator.addError('name', 'We only love Zemba here');
+ }
- if (update.name !== 'zemba') {
- validator.addError('name', 'We only love Zemba here');
- }
+ callback(null, validator);
+ };
- callback(null, validator);
- };
+ User.validateAndInsert({name: 'zemba'}, function (error, validation) {
+ assert.equal(validation.updated_document.name, 'zemba');
+ assert.deepEqual(validation.errors, {});
- User.validateAndUpdate({name: 'John Smith'}, {'$set': {name: 'foobar'}}, function (error, validation) {
- assert.deepEqual(validation.errors.name, ['We only love Zemba here']);
- assert.deepEqual(validation.updated_document.name, 'John Smith');
+ // Try to insert an invalid record
+ User.validateAndInsert({name: 'barbaz'}, function (error, validation) {
+ assert.deepEqual(validation.errors.name, ['We only love Zemba here']);
+ done();
+ });
+ });
+ });
- User.validateAndUpdate({name: 'John Smith'}, {'$set': {name: 'zemba'}}, function (error, validation) {
- assert.deepEqual(validation.errors, {});
- done();
- });
- });
- })
+ it('`validateAndUpdate` validates and updates', function (done) {
+ var User = Model(db, 'users');
- .run();
+ User.mongo('insert', {name: 'John Smith', age: 30}, function (errors, documents) {});
+
+ User.validate = function (document, update, callback) {
+ var validator = require('./../../lib/validator')(document, update);
+
+ if (validator.updated_document.name !== 'zemba') {
+ validator.addError('name', 'We only love Zemba here');
+ }
+
+ callback(null, validator);
+ };
+
+ User.validateAndUpdate({name: 'John Smith'}, {'$set': {name: 'foobar'}}, function (error, validation) {
+ assert.deepEqual(validation.errors.name, ['We only love Zemba here']);
+ assert.deepEqual(validation.updated_document.name, 'foobar');
+
+ User.validateAndUpdate({name: 'John Smith'}, {'$set': {name: 'zemba'}}, function (error, validation) {
+ assert.deepEqual(validation.errors, {});
+ assert.deepEqual(validation.updated_document.name, 'zemba');
+ done();
+ });
+ });
});
});
109 test/model_test.js
View
@@ -1,38 +1,38 @@
-var testosterone = require('testosterone')({sync: true, title: 'mongolia/model.js'}),
- assert = testosterone.assert,
- gently = global.GENTLY = new (require('gently')),
+/*globals describe, beforeEach, it*/
+var assert = require('assert')
+ , gently = global.GENTLY = new (require('gently'))()
- Model = require('./../lib/model'),
+ , Model = require('./../lib/model')
- _db = {bson_serializer: {}},
- _mock_validator = function (ret) {
+ , _db = {bson_serializer: {}}
+ , _mock_validator = function (ret) {
return {
hasErrors: function () {
return ret;
}
};
- },
- User;
+ }
+ , User;
-testosterone
+describe('Models', function () {
- .before(function () {
+ beforeEach(function () {
User = Model(_db, 'users');
- })
+ });
- .add('`core` throws an error when there is no db', function () {
+ it('`core` throws an error when there is no db', function () {
assert.throws(function () {
Model(null);
}, 'You must specify a db');
- })
+ });
- .add('`core` throws an error when collection is missing', function () {
+ it('`core` throws an error when collection is missing', function () {
assert.throws(function () {
Model(_db);
}, 'You must specify a collection name');
- })
+ });
- .add('`getCollection` returns a document collection', function () {
+ it('`getCollection` returns a document collection', function () {
var cb = function (error, collection) {};
gently.expect(_db, 'collection', function (_collection_name, _callback) {
@@ -41,9 +41,9 @@ testosterone
});
User.getCollection(cb);
- })
+ });
- .add('`mongo` proxies collection calls', function () {
+ it('`mongo` proxies collection calls', function () {
var callback = function (error, doc) {}
, query = {name: 'zemba'};
@@ -54,9 +54,9 @@ testosterone
});
User.mongo('findArray', query, callback);
- })
+ });
- .add('`mongo` proxies namespaced collection calls', function () {
+ it('`mongo` proxies namespaced collection calls', function () {
var callback = function (error, doc) {}
, query = {name: 'zemba'};
@@ -73,9 +73,9 @@ testosterone
});
User.mongo('findArray:public', query, callback);
- })
+ });
- .add('`mongo` proxies with options', function () {
+ it('`mongo` proxies with options', function () {
var callback = function (error, doc) {}
, query = {name: 'zemba'};
@@ -92,9 +92,9 @@ testosterone
});
User.mongo({method: 'findArray', namespace: 'public', hooks: false}, query, callback);
- })
+ });
- .add('`mongo` can be called without a callback', function () {
+ it('`mongo` can be called without a callback', function () {
var query = {name: 'zemba'};
gently.expect(User.collection_proxy, 'proxy', function (_model, _options, _args, _callback) {
@@ -110,9 +110,9 @@ testosterone
});
User.mongo('findArray', query);
- })
+ });
- .add('`validate` validates a mongo document', function () {
+ it('`validate` validates a mongo document', function () {
var document = {},
update = {name: 'Pau'},
validator = {data: 'foo'},
@@ -130,9 +130,9 @@ testosterone
});
User.validate(document, update, callback);
- })
+ });
- .add('`validateAndInsert` when the model is invalid does not insert it', function () {
+ it('`validateAndInsert` when the model is invalid does not insert it', function () {
var document = {},
validator = _mock_validator(true),
callback;
@@ -147,9 +147,9 @@ testosterone
});
User.validateAndInsert(document, callback);
- })
+ });
- .add('`validateAndInsert` when the model is valid inserts it afterwards', function () {
+ it('`validateAndInsert` when the model is valid inserts it afterwards', function () {
var document = {foo: 'bar'},
validator = _mock_validator(false),
callback;
@@ -176,9 +176,9 @@ testosterone
});
User.validateAndInsert(document, callback);
- })
+ });
- .add('`beforeInsert` default hook sets the created_at date', function () {
+ it('`beforeInsert` default hook sets the created_at date', function () {
var documents = [{name: 'zemba'}, {foo: 'bar'}];
User.beforeInsert(documents, function (_error, _documents) {
@@ -187,9 +187,9 @@ testosterone
assert.equal(document.created_at.constructor, (new Date()).constructor);
});
});
- })
+ });
- .add('`beforeUpdate` default hook updated the updated_at date', function () {
+ it('`beforeUpdate` default hook updated the updated_at date', function () {
var query = {foo: 'bar'},
update = {'$set': {fleiba: 'zemba'}};
@@ -198,9 +198,9 @@ testosterone
assert.ok(_update.$set.updated_at);
assert.equal(_update.$set.updated_at.constructor, (new Date()).constructor);
});
- })
+ });
- .add('`validateAndUpdate` when the model is invalid does not update it', function () {
+ it('`validateAndUpdate` when the model is invalid does not update it', function () {
var query = {foo: 'bar'}
, document = {foo: 'bar', fleiba: 'foo'}
, update = {fleiba: 'zemba'}
@@ -224,9 +224,9 @@ testosterone
});
User.validateAndUpdate(query, update, options, callback);
- })
+ });
- .add('`validateAndUpdate` when the model is valid updates it afterwards', function () {
+ it('`validateAndUpdate` when the model is valid updates it afterwards', function () {
var query = {foo: 'bar'}
, document = {_id: '123', foo: 'bar'}
, update = {'$set': {fleiba: 'John'}}
@@ -268,9 +268,9 @@ testosterone
});
User.validateAndUpdate(query, update, options, callback);
- })
+ });
- .add('`getEmbeddedDocument` filters the document following the skeletons directive', function () {
+ it('`getEmbeddedDocument` filters the document following the skeletons directive', function () {
var comment = {_id: 1, title: 'foo', body: 'Lorem ipsum'};
User.skeletons = {
@@ -282,9 +282,9 @@ testosterone
User.getEmbeddedDocument('comment', comment, 'post.comment'),
{post: {comment: {_id: 1, title: 'foo'}}}
);
- })
+ });
- .add('`getEmbeddedDocument` filters the document following recursive skeletons directives', function () {
+ it('`getEmbeddedDocument` filters the document following recursive skeletons directives', function () {
var post = {_id: 1, title: 'foo', body: 'Lorem ipsum', comment: {body: 'comment body!', created_at: Date.now()}};
User.skeletons = {
@@ -296,9 +296,9 @@ testosterone
User.getEmbeddedDocument('post', post, 'post'),
{post: {_id: 1, title: 'foo', comment: {body: 'comment body!'}}}
);
- })
+ });
- .add('`getEmbeddedDocument` returns appropiate `dot_notation` strings', function () {
+ it('`getEmbeddedDocument` returns appropiate `dot_notation` strings', function () {
var comment = {_id: 1, title: 'foo', body: 'Lorem ipsum'};
User.skeletons = {
@@ -314,9 +314,9 @@ testosterone
User.getEmbeddedDocument('comment', comment, 'post.comment', true),
{'post.comment._id': 1, 'post.comment.title': 'foo'}
);
- })
+ });
- .add('`getEmbeddedDocument` returns appropiate `dot_notation` strings using rescursive stuff', function () {
+ it('`getEmbeddedDocument` returns appropiate `dot_notation` strings using rescursive stuff', function () {
var post = {_id: 1, title: 'foo', body: 'Lorem ipsum', comment: {body: 'comment body!', created_at: Date.now()}};
User.skeletons = {
@@ -327,9 +327,9 @@ testosterone
User.getEmbeddedDocument('post', post, 'user.post', true),
{'user.post._id': 1, 'user.post.title': 'foo', 'user.post.comment.body': 'comment body!'}
);
- })
+ });
- .add('`getEmbeddedDocument` works without specifying the skeletons', function () {
+ it('`getEmbeddedDocument` works without specifying the skeletons', function () {
var comment = {_id: 1, title: 'foo', body: 'Lorem ipsum'};
User.skeletons = null;
@@ -339,9 +339,9 @@ testosterone
User.getEmbeddedDocument('comment', comment, 'post.comment'),
{post: {comment: comment}}
);
- })
+ });
- .add('`updateEmbeddedDocument` updates embedded objects', function () {
+ it('`updateEmbeddedDocument` updates embedded objects', function () {
var embeddedDocument = {name: 'john', surname: 'snow', bo: 'vale'},
options = {upsert: true},
callback = function () {};
@@ -359,9 +359,9 @@ testosterone
});
User.updateEmbeddedDocument({_id: 1}, 'author', embeddedDocument, options, callback);
- })
+ });
- .add('`pushEmbeddedDocument` pushes embedded objects', function () {
+ it('`pushEmbeddedDocument` pushes embedded objects', function () {
var embeddedDocument = {name: 'john'},
collection = {foo: 'bar'};
@@ -381,6 +381,5 @@ testosterone
});
User.pushEmbeddedDocument({_id: 1}, 'author', embeddedDocument);
- })
-
- .run();
+ });
+});
81 test/validator_test.js
View
@@ -1,12 +1,12 @@
-var testosterone = require('testosterone')({sync: true, title: 'mongolia/validator.js'}),
- assert = testosterone.assert,
- gently = global.GENTLY = new (require('gently')),
+/*globals describe, beforeEach, it*/
+var assert = require('assert')
+ , gently = global.GENTLY = new (require('gently'))()
- Validator = require('./../lib/validator');
+ , Validator = require('./../lib/validator');
-testosterone
+describe('validators', function () {
- .add('initial status', function () {
+ it('initial status', function () {
var update = {'$set': {foo: 'bar'}}
, doc = {foo: 'zemba', hey: 'joe'}
, val = Validator({}, update)
@@ -21,59 +21,59 @@ testosterone
assert.deepEqual(val2.document, doc);
assert.deepEqual(val2.update, update);
assert.deepEqual(val2.updated_document, {foo: 'bar', hey: 'joe'});
- })
+ });
- .add('`addError` adds a validation error', function () {
+ it('`addError` adds a validation error', function () {
var val = Validator({}, {foo: 'bar'})
, error = 'foo error';
val.addError('foo', error);
assert.equal(val.errors.foo[0], error);
- })
+ });
- .add('`hasErrors` returns true if the validator has errors', function () {
+ it('`hasErrors` returns true if the validator has errors', function () {
var val = Validator({}, {foo: 'bar'});
val.addError('foo', 'foo error');
assert.ok(val.hasErrors());
- })
+ });
- .add('`hasError` returns true if the validator has a particular error', function () {
+ it('`hasError` returns true if the validator has a particular error', function () {
var val = Validator({}, {foo: 'bar'});
val.addError('foo', 'foo error');
assert.ok(val.hasError('foo'));
- })
+ });
- .add('nested `addError` adds a nested error', function () {
+ it('nested `addError` adds a nested error', function () {
var val = Validator({}, {foo: 'bar'});
val.addError('foo.zemba', 'foo error');
assert.equal(val.errors.foo.zemba[0], 'foo error');
- })
+ });
- .add('`hasErrors` returns whether the validator has nested errors', function () {
+ it('`hasErrors` returns whether the validator has nested errors', function () {
var val = Validator({}, {foo: 'bar'});
val.addError('foo.zemba', 'foo error');
assert.ok(val.hasErrors());
- })
+ });
- .add('`hasError` returns whether the validator has a particular nested error', function () {
+ it('`hasError` returns whether the validator has a particular nested error', function () {
var val = Validator({}, {foo: 'bar'});
val.addError('foo.zemba', 'foo error');
assert.ok(val.hasError('foo.zemba'));
- })
+ });
- .add('`hasError` and `hasErrors` return false when there are no errors', function () {
+ it('`hasError` and `hasErrors` return false when there are no errors', function () {
var val = Validator({}, {foo: 'bar'});
assert.equal(val.hasError('foo.zemba'), false);
assert.equal(val.hasErrors(), false);
- })
+ });
- .add('multiple errors per field', function () {
+ it('multiple errors per field', function () {
var val = Validator({}, {foo: 'bar'});
val.addError('foo.zemba', 'error1');
@@ -86,27 +86,27 @@ testosterone
val.addError('foo.bla', 'error3');
assert.deepEqual(val.errors.foo.zemba, ['error1', 'error2']);
assert.deepEqual(val.errors.foo.bla, ['error3']);
- })
+ });
- .add('`isUpdating` returns whether the model is being updated', function () {
+ it('`isUpdating` returns whether the model is being updated', function () {
var update = {foo: 'bar'}
, val = Validator({}, update)
, val2 = Validator({zemba: 'fleiba'}, update);
assert.equal(val.isUpdating(), false);
assert.equal(val2.isUpdating(), true);
- })
+ });
- .add('`isInserting` returns whether the model is being inserted', function () {
+ it('`isInserting` returns whether the model is being inserted', function () {
var update = {foo: 'bar'}
, val = Validator({}, update)
, val2 = Validator({zemba: 'fleiba'}, update);
assert.equal(val.isInserting(), true);
assert.equal(val2.isInserting(), false);
- })
+ });
- .add('`attrChanged` returns whether the attribute changed', function () {
+ it('`attrChanged` returns whether the attribute changed', function () {
var update = {foo: 'bar'}
, val = Validator({}, update)
, val2 = Validator({foo: 'zemba'}, update)
@@ -115,9 +115,9 @@ testosterone
assert.equal(val.attrChanged('foo'), true);
assert.equal(val2.attrChanged('foo'), true);
assert.equal(val3.attrChanged('foo'), false);
- })
+ });
- .add('`getErrorBucket` gets or sets the error bucket for the attribute', function () {
+ it('`getErrorBucket` gets or sets the error bucket for the attribute', function () {
var update = {foo: 'bar'}
, val = Validator({}, update);
@@ -127,9 +127,9 @@ testosterone
assert.deepEqual(val.getErrorBucket('foo'), []);
val.addError('foo', 'ERROR');
assert.deepEqual(val.getErrorBucket('foo'), ['ERROR']);
- })
+ });
- .add('`getErrorBucket` also works with nested attributes', function () {
+ it('`getErrorBucket` also works with nested attributes', function () {
var update = {'$set': {'foo.bar': 'zemba'}}
, val = Validator({}, update);
@@ -139,9 +139,9 @@ testosterone
assert.deepEqual(val.getErrorBucket('foo.bar'), []);
val.addError('foo.bar', 'ERROR');
assert.deepEqual(val.getErrorBucket('foo.bar'), ['ERROR']);
- })
+ });
- .add('`validateExistence` validates existence of attributes', function () {
+ it('`validateExistence` validates existence of attributes', function () {
var update = {'$set': {hey: 'joe', 'foo.bar': 'zemba'}}
, val = Validator({}, update);
@@ -158,9 +158,9 @@ testosterone
assert.ok(val.hasError('inexistant'));
assert.deepEqual(val.getErrorBucket('foo.boo'), ['foo.boo error']);
assert.deepEqual(val.getErrorBucket('inexistant'), ['inexistant error']);
- })
+ });
- .add('`validateRegex` validates attributes matching regexes', function () {
+ it('`validateRegex` validates attributes matching regexes', function () {
var update = {'$set': {hey: 'joe', 'foo.bar': 'zemba'}}
, val = Validator({}, update);
@@ -180,9 +180,9 @@ testosterone
assert.deepEqual(val.getErrorBucket('foo.bar'), ['foo.bar error']);
assert.deepEqual(val.getErrorBucket('foo.boo'), ['foo.boo error']);
assert.deepEqual(val.getErrorBucket('inexistant'), ['inexistant error']);
- })
+ });
- .add('`validateConfirmation` validates attributes being equal', function () {
+ it('`validateConfirmation` validates attributes being equal', function () {
var update = {'$set': {hey: 'joe', 'foo.bar': 'zemba', 'foo.boo': 'zemba'}}
, val = Validator({}, update);
@@ -199,6 +199,5 @@ testosterone
assert.ok(val.hasError('inexistant'));
assert.deepEqual(val.getErrorBucket('hey'), ['hey error']);
assert.deepEqual(val.getErrorBucket('inexistant'), ['inexistant error']);
- })
-
- .run();
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.