Skip to content

Commit

Permalink
Fix up remix promises, add tests for coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
cadecairos committed Jul 3, 2015
1 parent e2b6307 commit dc19e9a
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 12 deletions.
26 changes: 15 additions & 11 deletions services/api/lib/postgre.js
Expand Up @@ -157,11 +157,12 @@ module.exports = function (pg) {
});
}).catch(function(err) {
if ( transaction ) {
return rollback(transaction).then(function() {
done(err);
}).catch(function(rollbackErr) {
done(rollbackErr);
});
return rollback(transaction)
.then(function() {
done(err);
}).catch(function(rollbackErr) {
done(rollbackErr);
});
}

done(err);
Expand All @@ -173,7 +174,7 @@ module.exports = function (pg) {
var remixedElements;
var remixedPages;
var transaction;
var transactionErr;

getTransactionClient().then(function(t) {
transaction = t;
return begin(transaction);
Expand Down Expand Up @@ -273,11 +274,14 @@ module.exports = function (pg) {
}).then(function() {
done(null, remixedProject);
}).catch(function(err) {
transactionErr = err;
return rollback(transaction);
}).then(function() {
done(transactionErr);
}).catch(function(err) {
if ( transaction ) {
return rollback(transaction)
.then(function() {
done(err);
}).catch(function(rollbackErr) {
done(rollbackErr);
});
}
done(err);
});
});
Expand Down
84 changes: 83 additions & 1 deletion test/services/api/handlers/projects.js
Expand Up @@ -173,7 +173,6 @@ experiment('Project Handlers', function() {
});
});


test('remix transaction error', function(done) {
var opts = configs.pgAdapter.remixFail;

Expand All @@ -200,6 +199,89 @@ experiment('Project Handlers', function() {
done();
});
});

test('Handles error from pg when creating a transaction (remixing)', function(done) {
var opts = configs.pgAdapter.remixFail;

sinon.stub(server.methods.users, 'find')
.callsArgWith(1, null, { rows: [ userFixtures.chris_testing ] });

sinon.stub(server.methods.projects, 'findDataForRemix')
.callsArgWith(1, null, { rows: [ {} ] });

sinon.stub(server.methods.utils, 'formatRemixData')
.returns({});

sinon.stub(server.plugins['webmaker-postgre-adapter'].pg, 'connect')
.callsArgWith(1, mockErr());

server.inject(opts, function(resp) {
server.plugins['webmaker-postgre-adapter'].pg.connect.restore();
server.methods.users.find.restore();
server.methods.projects.findDataForRemix.restore();
server.methods.utils.formatRemixData.restore();
expect(resp.statusCode).to.equal(500);
expect(resp.result.error).to.equal('Internal Server Error');
expect(resp.result.message).to.equal('An internal server error occurred');
done();
});
});

test('Handles error from pg after rolling back a transaction (remixing)', function(done) {
var opts = configs.pgAdapter.remixFail;
var clientStub = {
query: sinon.stub()
};

clientStub.query.onFirstCall()
.onFirstCall().callsArgWith(1, userFixtures.chris_testing)
.onSecondCall().callsArgWith(1, null, { rows: [ {} ] })
.onThirdCall().callsArgWith(1, null, { rows: [{ id: '1' }] })
.onCall(3).callsArgWith(1, null, { rows: [{ id: '1' }] })
.onCall(4).callsArgWith(1, mockErr())
.onCall(5).callsArgWith(1, null, {});

sinon.stub(server.plugins['webmaker-postgre-adapter'].pg, 'connect')
.callsArgWith(1, null, clientStub, function() {});

server.inject(opts, function(resp) {
server.plugins['webmaker-postgre-adapter'].pg.connect.restore();
expect(resp.statusCode).to.equal(500);
expect(resp.result.error).to.equal('Internal Server Error');
expect(resp.result.message).to.equal('An internal server error occurred');
done();
});
});

test('Handles error if rollback fails (remixing)', function(done) {
var opts = configs.pgAdapter.remixFail;

sinon.stub(server.methods.users, 'find')
.callsArgWith(1, null, { rows: [ userFixtures.chris_testing ] });

sinon.stub(server.methods.projects, 'findDataForRemix')
.callsArgWith(1, null, { rows: [ {} ] });

sinon.stub(server.methods.utils, 'formatRemixData')
.returns({});

sinon.stub(server.plugins['webmaker-postgre-adapter'].pg, 'connect')
.onFirstCall()
.callsArgWith(1, null, {})
.onSecondCall()
.callsArgWith(1, mockErr());

server.inject(opts, function(resp) {
server.plugins['webmaker-postgre-adapter'].pg.connect.restore();
server.methods.users.find.restore();
server.methods.projects.findDataForRemix.restore();
server.methods.utils.formatRemixData.restore();
expect(resp.statusCode).to.equal(500);
expect(resp.result.error).to.equal('Internal Server Error');
expect(resp.result.message).to.equal('An internal server error occurred');
done();
});
});
});

experiment('prequisites errors', function() {
Expand Down

0 comments on commit dc19e9a

Please sign in to comment.