Skip to content

Commit

Permalink
- Comprehensive tests added;
Browse files Browse the repository at this point in the history
- Fixed issue with replacing text;
- Added Windows file;
- Incremented version.
- Documented testing.
  • Loading branch information
Vitaly Tomilov committed Mar 9, 2015
1 parent c292a70 commit a795294
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 10 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@ This library joins [Promise] and [PG] to help writing easy-to-read database code
* Database connections are managed automatically in every usage case;
* Functions, Procedures and Transactions are all fully supported.

# Install
# Installing
```
$ npm install pg-promise
```

# Testing
* make sure you have [jasmine] installed:
$npm install jasmine-node -g
* install all the project dependencies:
$ npm install
* run tests:
$ make test

On Windows you can also run tests with `test.bat`

# Getting started

### 1. Load the library
Expand Down Expand Up @@ -311,6 +321,7 @@ If you do not call it, your process may be waiting for 30 seconds (default) or s
[PG]:https://github.com/brianc/node-postgres
[Promise]:https://github.com/then/promise
[ConnectionParameters]:https://github.com/brianc/node-postgres/blob/master/lib/connection-parameters.js
[jasmine]:https://github.com/jasmine/jasmine-npm

# License

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ var $wrap = {
}
// replacing single-quote symbols with two of them, and then
// wrapping in quotes, for compatibility with PostgreSQL.
return $wrapText(txt.replace("'", "''"));
return $wrapText(txt.replace(/'/g, "''"));
},
bool: function (val) {
if ($isNull(val)) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg-promise",
"version": "0.4.6",
"version": "0.4.7",
"description": "PG + Promise made easy, with transactions support.",
"main": "index.js",
"scripts": {
Expand Down
5 changes: 5 additions & 0 deletions test.bat
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
@ECHO OFF

REM Use this batch to run tests on Windows.
REM NOTE: Make sure to install the packages first, using 'npm install'

node_modules/.bin/jasmine-node.cmd test
85 changes: 78 additions & 7 deletions test/indexSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ describe("Library Instance Check", function () {
it("must be a function", function () {
expect(typeof(pgp)).toBe('function');
});
it("must have pg property", function () {
it("must have property 'pg'", function () {
expect(typeof(pgp.pg)).toBe('object');
});
it("must have as property", function () {
it("must have property 'as'", function () {
expect(typeof(pgp.as)).toBe('object');
});
it("must have end function", function () {
});
it("must have valid property 'as'", function () {
expect(typeof(pgp.as.text)).toBe('function');
expect(typeof(pgp.as.bool)).toBe('function');
expect(typeof(pgp.as.date)).toBe('function');
expect(typeof(pgp.as.csv)).toBe('function');
});
it("must have function 'end'", function () {
expect(typeof(pgp.end)).toBe('function');
});
});
Expand All @@ -28,18 +34,83 @@ describe("Query Result must be available", function () {
expect(typeof(queryResult)).toBe('object');
});
it("must have all properties set correctly", function () {
expect(queryResult.one === 1 && queryResult.many === 2 && queryResult.none === 4 && queryResult.any === 6).toBe(true);
expect(queryResult.one).toBe(1);
expect(queryResult.many).toBe(2);
expect(queryResult.none).toBe(4);
expect(queryResult.any).toBe(6);
});
});

var db;

describe("Database Instantiation", function () {
it("must throw an error when no connection passed", function () {
expect(pgp).toThrow("Invalid 'cn' parameter passed.");
it("must throw an error when no or empty connection passed", function () {
var err = "Invalid 'cn' parameter passed.";
expect(pgp).toThrow(err);
expect(function () {
pgp(null)
}).toThrow(err);
expect(function () {
pgp("")
}).toThrow(err);
});
db = pgp("connection string");
it("must return a valid object", function () {
expect(typeof(db)).toBe('object');
});
});

describe("Database Instance Check", function () {
it("must have all the protocol functions", function () {
expect(typeof(db.connect)).toBe('function');
expect(typeof(db.query)).toBe('function');
expect(typeof(db.tx)).toBe('function');
expect(typeof(db.one)).toBe('function');
expect(typeof(db.many)).toBe('function');
expect(typeof(db.none)).toBe('function');
expect(typeof(db.oneOrNone)).toBe('function');
expect(typeof(db.manyOrNone)).toBe('function');
expect(typeof(db.func)).toBe('function');
expect(typeof(db.proc)).toBe('function');
});
});

describe("Type conversion tests, namespace pgp.as", function () {
it("must correctly convert any boolean", function () {
expect(pgp.as.bool()).toBe("null");
expect(pgp.as.bool(null)).toBe("null");
expect(pgp.as.bool(0)).toBe("FALSE");
expect(pgp.as.bool(false)).toBe("FALSE");
expect(pgp.as.bool(1)).toBe("TRUE");
expect(pgp.as.bool(true)).toBe("TRUE");
expect(pgp.as.bool(10)).toBe("TRUE");
expect(pgp.as.bool(-10)).toBe("TRUE");
});
it("must correctly convert any text", function () {
expect(pgp.as.text()).toBe("null");
expect(pgp.as.text(null)).toBe("null");
expect(pgp.as.text("")).toBe("''");
expect(pgp.as.text("some text")).toBe("'some text'");
expect(pgp.as.text("'starts with quote")).toBe("'''starts with quote'");
expect(pgp.as.text("ends with quote'")).toBe("'ends with quote'''");
expect(pgp.as.text("has '' two quotes")).toBe("'has '''' two quotes'");
expect(pgp.as.text("'")).toBe("''''");
expect(pgp.as.text("''")).toBe("''''''");
});
it("must correctly convert any Date", function () {
expect(pgp.as.date()).toBe("null");
expect(pgp.as.date(null)).toBe("null");
expect(function () {
pgp.as.date("")
}).toThrow("'' doesn't represent a valid Date object or value.");
expect(function () {
pgp.as.date("bla-bla")
}).toThrow("'bla-bla' doesn't represent a valid Date object or value.");
expect(function () {
pgp.as.date(123)
}).toThrow("'123' doesn't represent a valid Date object or value.");
expect(function () {
pgp.as.date(function(){})
}).toThrow();
});
});

0 comments on commit a795294

Please sign in to comment.