From dc19e9a2be73dc2c840eb358524e8350494749b0 Mon Sep 17 00:00:00 2001 From: Christopher De Cairos Date: Fri, 3 Jul 2015 15:22:27 -0400 Subject: [PATCH] Fix up remix promises, add tests for coverage --- services/api/lib/postgre.js | 26 ++++---- test/services/api/handlers/projects.js | 84 +++++++++++++++++++++++++- 2 files changed, 98 insertions(+), 12 deletions(-) diff --git a/services/api/lib/postgre.js b/services/api/lib/postgre.js index 36c6775..cb2dd3c 100644 --- a/services/api/lib/postgre.js +++ b/services/api/lib/postgre.js @@ -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); @@ -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); @@ -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); }); }); diff --git a/test/services/api/handlers/projects.js b/test/services/api/handlers/projects.js index 9860490..a48efbb 100644 --- a/test/services/api/handlers/projects.js +++ b/test/services/api/handlers/projects.js @@ -173,7 +173,6 @@ experiment('Project Handlers', function() { }); }); - test('remix transaction error', function(done) { var opts = configs.pgAdapter.remixFail; @@ -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() {