Skip to content

Commit

Permalink
updating single initialization check + the test for it.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Mar 11, 2015
1 parent 367a659 commit 7784da0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
16 changes: 9 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,28 @@ queryResult = {
// - within 'pg-promise'.
//
// promiseLib: null
// - Overrides the promise library instance to be used
// by the library.
// - Overrides the promise library instance used by
// the library.
// }
module.exports = function (options) {

if (npm) {
throw new Error('Cannot initialize the library more than once.');
} else {
npm = {
pg: require('pg')
};
var promiseLib;
if (options && options.promiseLib) {
if (typeof(options.promiseLib) === 'function') {
npm.promise = options.promiseLib;
promiseLib = options.promiseLib;
} else {
throw new Error('Invalid or unsupported promise library override.');
}
} else {
npm.promise = require('promise');
promiseLib = require('promise');
}
npm = {
pg: require('pg'),
promise: promiseLib
};
}

var lib = function (cn) {
Expand Down
17 changes: 14 additions & 3 deletions test/indexSpec.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
var pgpLib = require('../index')
var pgpLib = require('../index');
var pgp = pgpLib(); // initializing the library;

describe("Library entry object", function () {

it("must be a function", function () {
expect(typeof(pgpLib)).toBe('function');
});
});

var pgp = pgpLib(); // initializing the library;
});

describe("Library initialization object", function () {

it("can be created only once", function () {
expect(function(){
pgpLib();
}).toThrow('Cannot initialize the library more than once.');
});

it("must be a function", function () {
expect(typeof(pgp)).toBe('function');
});

it("must have property 'pg'", function () {
expect(typeof(pgp.pg)).toBe('object');
});

it("must have function 'end'", function () {
expect(typeof(pgp.end)).toBe('function');
});

it("must have valid property 'as'", function () {
expect(typeof(pgp.as)).toBe('object');
expect(typeof(pgp.as.text)).toBe('function');
Expand Down

0 comments on commit 7784da0

Please sign in to comment.