From a795294f8324d8a9ad71c1659ab98f5cc7cda6e8 Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Mon, 9 Mar 2015 17:54:28 +0000 Subject: [PATCH] - Comprehensive tests added; - Fixed issue with replacing text; - Added Windows file; - Incremented version. - Documented testing. --- README.md | 13 +++++++- index.js | 2 +- package.json | 2 +- test.bat | 5 +++ test/indexSpec.js | 85 +++++++++++++++++++++++++++++++++++++++++++---- 5 files changed, 97 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 77fe8d4c..9c4f510b 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/index.js b/index.js index 7baa7d52..72270388 100644 --- a/index.js +++ b/index.js @@ -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)) { diff --git a/package.json b/package.json index 9f94aa42..695ceb78 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test.bat b/test.bat index 9ece9f02..15a70e2d 100644 --- a/test.bat +++ b/test.bat @@ -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 diff --git a/test/indexSpec.js b/test/indexSpec.js index 84473b3c..e526589b 100644 --- a/test/indexSpec.js +++ b/test/indexSpec.js @@ -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'); }); }); @@ -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(); + }); +});