Skip to content

Commit

Permalink
adding tests, code refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Jun 4, 2015
1 parent c8b315f commit 9be38bb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
10 changes: 6 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ queryResult = {
// promiseLib: null
// - Overrides the promise library to be used.
// }
module.exports = function (options) {
function main(options) {

if (options !== null && options !== undefined && typeof(options) !== 'object') {
throw new Error("Invalid parameter 'options' specified.");
Expand Down Expand Up @@ -87,8 +87,8 @@ module.exports = function (options) {
// library instance;
var inst = function (cn) {
if (!cn) {
// Cannot instantiate a database with an empty connection;
throw new Error("Invalid parameter 'cn' specified.");
// Cannot instantiate a database without connection details;
throw new Error("Connection details must be specified.");
}
return dbInit(cn, options);
};
Expand All @@ -112,7 +112,7 @@ module.exports = function (options) {
};

return inst;
};
}

// Initializes a database object;
function dbInit(cn, options) {
Expand Down Expand Up @@ -691,3 +691,5 @@ function $sequence(t, factory) {

return loop();
}

module.exports = main;
32 changes: 31 additions & 1 deletion test/dbSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var db = dbHeader.db;

describe("Database Instantiation", function () {
it("must throw an error when empty or no connection passed", function () {
var err = "Invalid parameter 'cn' specified.";
var err = "Connection details must be specified.";
expect(pgp).toThrow(err);
expect(function () {
pgp(null);
Expand Down Expand Up @@ -406,6 +406,36 @@ describe("When a nested transaction fails", function () {
});
});

describe("Detached Transaction", function () {
it("must throw an error on any query request", function () {
var stop, error, tx;
db.tx(function () {
tx = this;
return promise.resolve();
})
.then(function () {
try {
// cannot use transaction context
// outside of transaction callback;
tx.query("select 'test'");
} catch (err) {
error = err;
}
stop = true;
}, function (reason) {
error = reason;
stop = true;
});
waitsFor(function () {
return stop === true;
}, "Query timed out", 5000);
runs(function () {
expect(error instanceof Error).toBe(true);
expect(error.message).toBe("Unexpected call outside of transaction.");
});
});
});

describe("When a nested transaction fails", function () {
it("both transactions must rollback", function () {
var result, error, nestError, THIS1, THIS2, context1, context2;
Expand Down

0 comments on commit 9be38bb

Please sign in to comment.