Permalink
Browse files

Test atomicity

  • Loading branch information...
1 parent db2b77f commit 7a10eabc7caccab5a5018064d58d5eb3b46c5dc5 @mde mde committed Sep 3, 2013
View
1 Jakefile
@@ -8,6 +8,7 @@ var t = new jake.TestTask('Model', function () {
this.testFiles.include('test/**/*.js');
this.testFiles.exclude('test/fixtures/*.js');
this.testFiles.exclude('test/integration/adapters/shared.js');
+ this.testFiles.exclude('test/integration/adapters/helpers.js');
this.testFiles.exclude('test/config.js');
this.testFiles.exclude('test/db.json');
this.testFiles.exclude('test/db.sample.json');
View
69 test/integration/adapters/helpers.js
@@ -0,0 +1,69 @@
+var model = require('../../../lib')
+ , fixtures = ['Event', 'Person', 'Participation', 'Message', 'Photo']
+
+module.exports = {
+ fixtures: fixtures
+
+, createFixtures: function (cb) {
+ var relations = fixtures.slice()
+ , doIt = function () {
+ var relation = relations.shift()
+ , items = []
+ , letter;
+ if (relation) {
+ letters = 'abcdefghijklmnopqrst'.split('');
+ letters.forEach(function (letter) {
+ items.push(model[relation].create({title: letter}));
+ });
+ model[relation].save(items);
+ doIt();
+ }
+ else {
+ cb();
+ }
+ };
+ doIt();
+ }
+
+, deleteFixtures: function (cb) {
+ var relations = fixtures.slice()
+ , doIt = function () {
+ var relation = relations.shift();
+ if (relation) {
+ model[relation].all({}, function (err, data) {
+ var ids = [];
+ if (err) { throw err; }
+ data.forEach(function (item) {
+ ids.push(item.id);
+ });
+ model[relation].remove({id: ids}, function (err, data) {
+ if (err) { throw err; }
+ doIt();
+ });
+ });
+ }
+ else {
+ cb();
+ }
+ };
+ doIt();
+ }
+
+, updateItems: function (coll, cb) {
+ var collection = coll.slice()
+ , doIt = function () {
+ var item = collection.pop();
+ if (item) {
+ item.save(function (err, data) {
+ if (err) { throw err; }
+ doIt();
+ });
+ }
+ else {
+ cb();
+ }
+ };
+ doIt();
+ }
+};
+
View
9 test/integration/adapters/memory/index.js
@@ -1,6 +1,7 @@
var utils = require('utilities')
, assert = require('assert')
, model = require('../../../../lib')
+ , helpers = require('.././helpers')
, Adapter = require('../../../../lib/adapters/memory').Adapter
, adapter
, tests
@@ -20,6 +21,7 @@ tests = {
, 'Membership'
, 'Team'
]
+ relations = relations.concat(helpers.fixtures)
, models = [];
adapter = new Adapter();
@@ -47,7 +49,12 @@ tests = {
};
for (var p in shared) {
- tests[p + ' (Memory)'] = shared[p];
+ if (p == 'beforeEach' || p == 'afterEach') {
+ tests[p] = shared[p];
+ }
+ else {
+ tests[p + ' (Memory)'] = shared[p];
+ }
}
module.exports = tests;
View
9 test/integration/adapters/mongo/index.js
@@ -1,6 +1,7 @@
var utils = require('utilities')
, assert = require('assert')
, model = require('../../../../lib')
+ , helpers = require('.././helpers')
, Adapter = require('../../../../lib/adapters/mongo').Adapter
, generator = require('../../../../lib/generators/sql')
, adapter
@@ -22,6 +23,7 @@ tests = {
, 'Membership'
, 'Team'
]
+ relations = relations.concat(helpers.fixtures)
, models = [];
adapter = new Adapter(config.mongo);
@@ -48,7 +50,12 @@ tests = {
};
for (var p in shared) {
- tests[p + ' (Mongo)'] = shared[p];
+ if (p == 'beforeEach' || p == 'afterEach') {
+ tests[p] = shared[p];
+ }
+ else {
+ tests[p + ' (Mongo)'] = shared[p];
+ }
}
module.exports = tests;
View
9 test/integration/adapters/riak/index.js
@@ -1,6 +1,7 @@
var utils = require('utilities')
, assert = require('assert')
, model = require('../../../../lib')
+ , helpers = require('.././helpers')
, Adapter = require('../../../../lib/adapters/riak').Adapter
, adapter
, tests
@@ -23,6 +24,7 @@ tests = {
, 'Membership'
, 'Team'
]
+ relations = relations.concat(helpers.fixtures)
, models = [];
adapter = new Adapter(config.mongo);
@@ -50,7 +52,12 @@ tests = {
};
for (var p in shared) {
- tests[p + ' (Riak)'] = shared[p];
+ if (p == 'beforeEach' || p == 'afterEach') {
+ tests[p] = shared[p];
+ }
+ else {
+ tests[p + ' (Riak)'] = shared[p];
+ }
}
module.exports = tests;
View
20 test/integration/adapters/shared.js
@@ -1,19 +1,37 @@
var utils = require('utilities')
, assert = require('assert')
+ , model = require('../../../lib')
+ , helpers = require('./helpers')
, currentId
, currentDateProp
, tests
, testItems
+
+ // Old fixtures
, Zooby = require('../../fixtures/zooby').Zooby
, User = require('../../fixtures/user').User
, Profile = require('../../fixtures/profile').Profile
, Account = require('../../fixtures/account').Account
, Team = require('../../fixtures/team').Team
, Membership = require('../../fixtures/membership').Membership;
+// Import the model description for each fixture
+helpers.fixtures.forEach(function (f) {
+ model[f] = require('../../fixtures/' + f.toLowerCase())[f];
+});
+
+
tests = {
- 'test save new, string UUID id, required nunmber is 0': function (next) {
+ 'beforeEach': function (next) {
+ helpers.createFixtures(next);
+ }
+
+, 'afterEach': function (next) {
+ helpers.deleteFixtures(next);
+ }
+
+, 'test save new, string UUID id, required nunmber is 0': function (next) {
var z = Zooby.create({
foo: 'GROO'
, zong: new Date()
View
96 test/integration/adapters/sql/postgres.js
@@ -1,18 +1,14 @@
var utils = require('utilities')
, assert = require('assert')
, model = require('../../../../lib')
+ , helpers = require('.././helpers')
, Adapter = require('../../../../lib/adapters/sql/postgres').Adapter
, generator = require('../../../../lib/generators/sql')
, adapter
, currentId
, tests
, config = require('../../../config')
, shared = require('../shared')
- , createFixtures
- , deleteFixtures
- , updateItems
- , fixtures = ['Event', 'Person', 'Participation', 'Message', 'Photo']
-
// Fixtures
, Zooby = require('../../../fixtures/zooby').Zooby
, User = require('../../../fixtures/user').User
@@ -22,71 +18,6 @@ var utils = require('utilities')
, Membership = require('../../../fixtures/membership').Membership;
-fixtures.forEach(function (f) {
- model[f] = require('../../../fixtures/' + f.toLowerCase())[f];
-});
-
-createFixtures = function (cb) {
- var relations = fixtures.slice()
- , doIt = function () {
- var relation = relations.shift()
- , items = []
- , letter;
- if (relation) {
- letters = 'abcdefghijklmnopqrst'.split('');
- letters.forEach(function (letter) {
- items.push(model[relation].create({title: letter}));
- });
- model[relation].save(items);
- doIt();
- }
- else {
- cb();
- }
- };
- doIt();
-};
-
-deleteFixtures = function (cb) {
- var relations = fixtures.slice()
- , doIt = function () {
- var relation = relations.shift();
- if (relation) {
- model[relation].all({}, function (err, data) {
- var ids = [];
- if (err) { throw err; }
- data.forEach(function (item) {
- ids.push(item.id);
- });
- model[relation].remove({id: ids}, function (err, data) {
- if (err) { throw err; }
- doIt();
- });
- });
- }
- else {
- cb();
- }
- };
- doIt();
-};
-
-updateItems = function (coll, cb) {
- var collection = coll.slice()
- , doIt = function () {
- var item = collection.pop();
- if (item) {
- item.save(function (err, data) {
- if (err) { throw err; }
- doIt();
- });
- }
- else {
- cb();
- }
- };
- doIt();
-};
tests = {
'before': function (next) {
@@ -98,7 +29,7 @@ tests = {
, 'Membership'
, 'Team'
]
- relations = relations.concat(fixtures)
+ relations = relations.concat(helpers.fixtures)
, models = [];
adapter = new Adapter(config.postgres);
@@ -112,9 +43,7 @@ tests = {
if (err) {
throw err;
}
- createFixtures(function () {
- deleteFixtures(next);
- });
+ next();
});
});
adapter.connect();
@@ -137,14 +66,6 @@ tests = {
adapter.disconnect();
}
-, 'beforeEach': function (next) {
- createFixtures(next);
- }
-
-, 'afterEach': function (next) {
- deleteFixtures(next);
- }
-
, 'test create adapter': function () {
assert.ok(adapter instanceof Adapter);
}
@@ -162,7 +83,12 @@ tests = {
};
for (var p in shared) {
- tests[p + ' (Postgres)'] = shared[p];
+ if (p == 'beforeEach' || p == 'afterEach') {
+ tests[p] = shared[p];
+ }
+ else {
+ tests[p + ' (Postgres)'] = shared[p];
+ }
}
var eagerAssnTests = {
@@ -231,7 +157,7 @@ var eagerAssnTests = {
data.forEach(function (p) {
p.setEvent(ev);
});
- updateItems(data, function () {
+ helpers.updateItems(data, function () {
model.Photo.all({}, {includes: ['event']}, function (err, data) {
if (err) { throw err; }
var every = data.every(function (p) {
@@ -263,7 +189,7 @@ var eagerAssnTests = {
}
incr++;
});
- updateItems(data, function () {
+ helpers.updateItems(data, function () {
model.Event.all({id: [evA.id, evB.id]},
{includes: ['photos'], sort: {'title': 'desc',
'photo.title': 'asc'}}, function (err, data) {

0 comments on commit 7a10eab

Please sign in to comment.