Permalink
Browse files

Refactor tests using setUp and tearDown so that they don't rely on or…

…der of execution anymore.
  • Loading branch information...
1 parent 5bc4c25 commit ba0e6b10b07fc19d6491dc94e7e0f708d975fe36 @maritz committed Jan 9, 2012
Showing with 1,537 additions and 1,515 deletions.
  1. +7 −5 lib/nohm.js
  2. +108 −88 test/connectTests.js
  3. +163 −186 test/featureTests.js
  4. +488 −480 test/findTests.js
  5. +20 −0 test/helper.js
  6. +341 −327 test/relationTests.js
  7. +8 −42 test/tests.js
  8. +402 −387 test/validationTests.js
View
@@ -157,11 +157,13 @@ Nohm.setExtraValidations = function (files) {
files = [files];
}
files.forEach(function (path) {
- __extraValidators.push(path);
- var validators = require(path);
- Object.keys(validators).forEach(function (name) {
- Nohm.__validators[name] = validators[name];
- });
+ if (__extraValidators.indexOf(path) === -1) {
+ __extraValidators.push(path);
+ var validators = require(path);
+ Object.keys(validators).forEach(function (name) {
+ Nohm.__validators[name] = validators[name];
+ });
+ }
});
};
View
@@ -1,5 +1,12 @@
-var Nohm = require(__dirname+'/../lib/nohm').Nohm;
-Nohm.model('UserConnectMockup', {
+var nohm = require(__dirname+'/../lib/nohm').Nohm;
+var args = require(__dirname+'/testArgs.js');
+var redis = args.redis;
+var h = require(__dirname+'/helper.js');
+var vm = require('vm');
+
+nohm.setExtraValidations(__dirname + '/custom_validations.js');
+
+nohm.model('UserConnectMockup', {
properties: {
name: {
type: 'string',
@@ -46,7 +53,7 @@ Nohm.model('UserConnectMockup', {
}
}
});
-Nohm.model('ExcludedConnectMockup', {
+nohm.model('ExcludedConnectMockup', {
properties: {
name: {
type: 'string',
@@ -57,7 +64,6 @@ Nohm.model('ExcludedConnectMockup', {
}
}
});
-var vm = require('vm');
var setup = function (t, expected, options, callback) {
t.expect(3+expected);
@@ -96,107 +102,121 @@ var setup = function (t, expected, options, callback) {
var url = (options && options.url) ? options.url : '/nohmValidations.js';
- Nohm.connect(options)({url: url }, dummyRes, function () {
+ nohm.connect(options)({url: url }, dummyRes, function () {
t.ok(false, 'Connect middleware called next with valid url.');
t.done();
});
};
-exports.connectNoOptions = function (t) {
-
- setup(t, 2, undefined, function (sandbox, str) {
- var val = sandbox.nohmValidations.models.UserConnectMockup;
- t.ok(val.name.indexOf('notEmpty') === 0, 'UserConnectMockup did not have the proper validations');
- t.same(val.name[1], [
- 'length', {
- min: 2
- }
- ], 'UserConnectMockup did not have the proper validations');
- t.done();
- });
-
-};
-
-exports.connectValidate = function (t) {
-
- setup(t, 2, undefined, function (sandbox) {
- var val = sandbox.nohmValidations.validate;
- val('UserConnectMockup', {name: 'asd', excludedProperty: 'asd', excludedValidation: 'asd'}, function (valid) {
- t.same(valid, true, 'Validate did not work as expected.');
-
- val('UserConnectMockup', {name: 'a', excludedProperty: '', excludedValidation: 'a'}, function (valid, errors) {
- t.same(errors, {name: ['length'], excludedProperty: ['notEmpty'], excludedValidation: ['length']}, 'Validate did not work as expected.');
- t.done();
- });
- });
- });
-};
-exports.connectOptions = function (t) {
+exports.connect = {
- setup(t, 1, {url: './nohm.js', namespace: 'hurgel'}, function (sandbox) {
- t.ok(sandbox.hurgel, 'Namespace option not successful');
- t.done();
- });
+ setUp: function (next) {
+ if ( ! nohm.client) {
+ nohm.setClient(redis);
+ }
+ next();
+ },
+ tearDown: function (next) {
+ h.cleanUp(redis, args.prefix, next);
+ },
-};
-
-exports.connectExtraFiles = function (t) {
- setup(t, 1, {extraFiles: __dirname+'/custom_validations2.js'}, function (sandbox) {
- sandbox.nohmValidations.validate('UserConnectMockup', {
- customValidationFile: 'NOPE',
- customValidationFileTimesTwo: 'NOPE'
- }, function (valid, errors) {
- t.same(errors, {
- customValidationFile: ['customValidationFile'],
- customValidationFileTimesTwo: ['customValidationFileTimesTwo']
- }, 'Validate did not work as expected.');
- t.done();
- });
- });
+ connectNoOptions: function (t) {
-};
+ setup(t, 2, undefined, function (sandbox, str) {
+ var val = sandbox.nohmValidations.models.UserConnectMockup;
+ t.ok(val.name.indexOf('notEmpty') === 0, 'UserConnectMockup did not have the proper validations');
+ t.same(val.name[1], [
+ 'length', {
+ min: 2
+ }
+ ], 'UserConnectMockup did not have the proper validations');
+ t.done();
+ });
+ },
-exports.connectExceptions = function (t) {
+ connectValidate: function (t) {
+
+ setup(t, 2, undefined, function (sandbox) {
+ var val = sandbox.nohmValidations.validate;
+ val('UserConnectMockup', {name: 'asd', excludedProperty: 'asd', excludedValidation: 'asd'}, function (valid) {
+ t.same(valid, true, 'Validate did not work as expected.');
+
+ val('UserConnectMockup', {name: 'a', excludedProperty: '', excludedValidation: 'a'}, function (valid, errors) {
+ t.same(errors, {name: ['length'], excludedProperty: ['notEmpty'], excludedValidation: ['length']}, 'Validate did not work as expected.');
+ t.done();
+ });
+ });
+ });
+ },
- setup(t, 2, {exclusions: {
- UserConnectMockup: {
- excludedValidation: [1],
- excludedProperty: true
- },
- ExcludedConnectMockup: true
- }}, function (sandbox) {
- var validate = sandbox.nohmValidations.validate;
- validate('UserConnectMockup', {
- excludedValidation: 'a',
- excludedProperty: ''
- }, function (valid) {
- t.same(valid, true, 'Validate did not work as expected with exclusions.');
+ connectOptions: function (t) {
+
+ setup(t, 1, {url: './nohm.js', namespace: 'hurgel'}, function (sandbox) {
+ t.ok(sandbox.hurgel, 'Namespace option not successful');
+ t.done();
+ });
+
+ },
- try {
- validate('ExcludedConnectMockup', {
- name: ''
- }, function () {
- t.ok(false, 'Validate should have thrown an error about an invalid modelname');
+ connectExtraFiles: function (t) {
+
+ setup(t, 1, {extraFiles: __dirname+'/custom_validations2.js'}, function (sandbox) {
+ sandbox.nohmValidations.validate('UserConnectMockup', {
+ customValidationFile: 'NOPE',
+ customValidationFileTimesTwo: 'NOPE'
+ }, function (valid, errors) {
+ t.same(errors, {
+ customValidationFile: ['customValidationFile'],
+ customValidationFileTimesTwo: ['customValidationFileTimesTwo']
+ }, 'Validate did not work as expected.');
t.done();
});
- } catch (e) {
- t.same(e.message, 'Invalid modelName passed to nohm or model was not properly exported.', 'Validate did not work as expected with exclusions.');
- }
- t.done();
});
- });
+
+ },
-};
-
-exports.connectValidateEmpty = function (t) {
+ connectExceptions: function (t) {
+
+ setup(t, 2, {exclusions: {
+ UserConnectMockup: {
+ excludedValidation: [1],
+ excludedProperty: true
+ },
+ ExcludedConnectMockup: true
+ }}, function (sandbox) {
+ var validate = sandbox.nohmValidations.validate;
+ validate('UserConnectMockup', {
+ excludedValidation: 'a',
+ excludedProperty: ''
+ }, function (valid) {
+ t.same(valid, true, 'Validate did not work as expected with exclusions.');
+
+ try {
+ validate('ExcludedConnectMockup', {
+ name: ''
+ }, function () {
+ t.ok(false, 'Validate should have thrown an error about an invalid modelname');
+ t.done();
+ });
+ } catch (e) {
+ t.same(e.message, 'Invalid modelName passed to nohm or model was not properly exported.', 'Validate did not work as expected with exclusions.');
+ }
+ t.done();
+ });
+ });
+
+ },
- setup(t, 1, undefined, function (sandbox) {
- var val = sandbox.nohmValidations.validate;
- val('UserConnectMockup', {excludedProperty: 'asd', excludedValidation: 'asd'}, function (valid) {
- t.same(valid, true, 'Validate did not work as expected.');
- t.done();
+ connectValidateEmpty: function (t) {
+
+ setup(t, 1, undefined, function (sandbox) {
+ var val = sandbox.nohmValidations.validate;
+ val('UserConnectMockup', {excludedProperty: 'asd', excludedValidation: 'asd'}, function (valid) {
+ t.same(valid, true, 'Validate did not work as expected.');
+ t.done();
+ });
});
- });
+ }
};
Oops, something went wrong.

0 comments on commit ba0e6b1

Please sign in to comment.