Skip to content

Commit

Permalink
Test atomicity
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Sep 3, 2013
1 parent db2b77f commit 7a10eab
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 89 deletions.
1 change: 1 addition & 0 deletions Jakefile
Expand Up @@ -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');
Expand Down
69 changes: 69 additions & 0 deletions 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();
}
};

9 changes: 8 additions & 1 deletion 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
Expand All @@ -20,6 +21,7 @@ tests = {
, 'Membership'
, 'Team'
]
relations = relations.concat(helpers.fixtures)
, models = [];
adapter = new Adapter();

Expand Down Expand Up @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion 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
Expand All @@ -22,6 +23,7 @@ tests = {
, 'Membership'
, 'Team'
]
relations = relations.concat(helpers.fixtures)
, models = [];
adapter = new Adapter(config.mongo);

Expand All @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion 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
Expand All @@ -23,6 +24,7 @@ tests = {
, 'Membership'
, 'Team'
]
relations = relations.concat(helpers.fixtures)
, models = [];
adapter = new Adapter(config.mongo);

Expand Down Expand Up @@ -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;
20 changes: 19 additions & 1 deletion 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()
Expand Down
96 changes: 11 additions & 85 deletions 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
Expand All @@ -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) {
Expand All @@ -98,7 +29,7 @@ tests = {
, 'Membership'
, 'Team'
]
relations = relations.concat(fixtures)
relations = relations.concat(helpers.fixtures)
, models = [];

adapter = new Adapter(config.postgres);
Expand All @@ -112,9 +43,7 @@ tests = {
if (err) {
throw err;
}
createFixtures(function () {
deleteFixtures(next);
});
next();
});
});
adapter.connect();
Expand All @@ -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);
}
Expand All @@ -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 = {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 7a10eab

Please sign in to comment.