From a8cfa18f0953e85b699e180ec706f25b62bbfb7f Mon Sep 17 00:00:00 2001 From: bmc Date: Mon, 22 Apr 2013 05:18:13 -0500 Subject: [PATCH 1/8] Add tests for pg@9.2 on travis --- Makefile | 8 +++++++- package.json | 2 +- script/travis-pg-9.2-install.sh | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100755 script/travis-pg-9.2-install.sh diff --git a/Makefile b/Makefile index 383b151e..a7121a73 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ params := $(connectionString) node-command := xargs -n 1 -I file node file $(params) .PHONY : test test-connection test-integration bench test-native \ - build/default/binding.node jshint + build/default/binding.node jshint upgrade-pg all: npm install @@ -20,6 +20,12 @@ test: test-unit test-all: jshint test-unit test-integration test-native test-binary +test-travis: test-all upgrade-pg test-integration test-native test-binary + +upgrade-pg: + @chmod 755 script/travis-pg-9.2-install.sh + @./script/travis-pg-9.2-install.sh + bench: @find benchmark -name "*-bench.js" | $(node-command) diff --git a/package.json b/package.json index 77179859..e2647c84 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "semver": "~1.1.4" }, "scripts": { - "test": "make test-all connectionString=pg://postgres@localhost:5432/postgres", + "test": "make test-travis connectionString=pg://postgres@localhost:5432/postgres", "prepublish": "rm -r build || (exit 0)", "install": "node-gyp rebuild || (exit 0)" }, diff --git a/script/travis-pg-9.2-install.sh b/script/travis-pg-9.2-install.sh new file mode 100755 index 00000000..cd74ee09 --- /dev/null +++ b/script/travis-pg-9.2-install.sh @@ -0,0 +1,18 @@ +#! /usr/bin/env bash +#sudo cat /etc/postgresql/9.1/main/pg_hba.conf +#sudo cat /etc/postgresql/9.1/main/pg_ident.conf +#sudo cat /etc/postgresql/9.1/main/postgresql.conf +sudo /etc/init.d/postgresql stop +sudo apt-get -y --purge remove postgresql +echo "yes" | sudo add-apt-repository ppa:pitti/postgresql +sudo apt-get update -qq +sudo apt-get -q -y -o Dpkg::Options::=--force-confdef install postgresql-9.2 postgresql-contrib-9.2 +sudo chmod 777 /etc/postgresql/9.2/main/pg_hba.conf +sudo echo "local all postgres trust" > /etc/postgresql/9.2/main/pg_hba.conf +sudo echo "local all all trust" >> /etc/postgresql/9.2/main/pg_hba.conf +sudo echo "host all all 127.0.0.1/32 trust" >> /etc/postgresql/9.2/main/pg_hba.conf +sudo echo "host all all ::1/128 trust" >> /etc/postgresql/9.2/main/pg_hba.conf +sudo echo "host all all 0.0.0.0/0 trust" >> /etc/postgresql/9.2/main/pg_hba.conf +sudo echo "host all all 0.0.0.0 255.255.255.255 trust" >> /etc/postgresql/9.2/main/pg_hba.conf +sudo /etc/init.d/postgresql restart +node script/create-test-tables.js pg://postgres@127.0.0.1:5432/postgres From 94cc165b5efa9504240a01ba61cd3a5fdd0b5f14 Mon Sep 17 00:00:00 2001 From: bmc Date: Mon, 22 Apr 2013 05:48:33 -0500 Subject: [PATCH 2/8] Remove create DB portion of travis script --- script/travis-pg-9.2-install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/travis-pg-9.2-install.sh b/script/travis-pg-9.2-install.sh index cd74ee09..0f53ff8d 100755 --- a/script/travis-pg-9.2-install.sh +++ b/script/travis-pg-9.2-install.sh @@ -15,4 +15,4 @@ sudo echo "host all all ::1/128 trust" >> /etc/ sudo echo "host all all 0.0.0.0/0 trust" >> /etc/postgresql/9.2/main/pg_hba.conf sudo echo "host all all 0.0.0.0 255.255.255.255 trust" >> /etc/postgresql/9.2/main/pg_hba.conf sudo /etc/init.d/postgresql restart -node script/create-test-tables.js pg://postgres@127.0.0.1:5432/postgres +#node script/create-test-tables.js pg://postgres@127.0.0.1:5432/postgres From 3997b38b44d530f5e01e29d3b3c8647c085b2589 Mon Sep 17 00:00:00 2001 From: bmc Date: Mon, 22 Apr 2013 05:52:41 -0500 Subject: [PATCH 3/8] Shell out for recursive make --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index a7121a73..033c73cd 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,8 @@ test: test-unit test-all: jshint test-unit test-integration test-native test-binary -test-travis: test-all upgrade-pg test-integration test-native test-binary +test-travis: test-all upgrade-pg + @make test-all upgrade-pg: @chmod 755 script/travis-pg-9.2-install.sh From 10e6d85266310956b39020e25f35b6dbca258dc9 Mon Sep 17 00:00:00 2001 From: bmc Date: Mon, 22 Apr 2013 10:18:17 -0500 Subject: [PATCH 4/8] Add support for JSON data type requires >= 9.2 of postgres --- lib/types/textParsers.js | 1 + lib/utils.js | 5 +++- .../client/json-type-parsing-tests.js | 30 +++++++++++++++++++ .../client/query-callback-error-tests.js | 1 - 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 test/integration/client/json-type-parsing-tests.js diff --git a/lib/types/textParsers.js b/lib/types/textParsers.js index 0a49ec6b..558ce73b 100644 --- a/lib/types/textParsers.js +++ b/lib/types/textParsers.js @@ -183,6 +183,7 @@ var init = function(register) { register(1009, parseStringArray); register(1186, parseInterval); register(17, parseByteA); + register(114, JSON.parse.bind(JSON)); }; module.exports = { diff --git a/lib/utils.js b/lib/utils.js index 267f3972..3d12d827 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -55,7 +55,10 @@ var prepareValue = function(val) { if(Array.isArray(val)) { return arrayString(val); } - return val === null ? null : val.toString(); + if(!val || typeof val !== 'object') { + return val === null ? null : val.toString(); + } + return JSON.stringify(val); }; function dateToString(date) { diff --git a/test/integration/client/json-type-parsing-tests.js b/test/integration/client/json-type-parsing-tests.js new file mode 100644 index 00000000..215e2262 --- /dev/null +++ b/test/integration/client/json-type-parsing-tests.js @@ -0,0 +1,30 @@ +var helper = require(__dirname + '/test-helper'); +var assert = require('assert'); +//if you want binary support, pull request me! +if (helper.config.binary) { + return; +} + +test('can read and write json', function() { + helper.pg.connect(helper.config, function(err, client, done) { + assert.ifError(err); + client.query('CREATE TEMP TABLE stuff(id SERIAL PRIMARY KEY, data JSON)'); + var value ={name: 'Brian', age: 250, alive: true, now: new Date()}; + client.query('INSERT INTO stuff (data) VALUES ($1)', [value]); + client.query('SELECT * FROM stuff', assert.success(function(result) { + assert.equal(result.rows.length, 1); + assert.equal(typeof result.rows[0].data, 'object'); + var row = result.rows[0].data; + assert.strictEqual(row.name, value.name); + assert.strictEqual(row.age, value.age); + assert.strictEqual(row.alive, value.alive); + test('row should have "now" as a date', function() { + return false; + assert(row.now instanceof Date, 'row.now should be a date instance but is ' + typeof row.now); + }); + assert.equal(JSON.stringify(row.now), JSON.stringify(value.now)); + done(); + helper.pg.end(); + })); + }); +}); diff --git a/test/integration/client/query-callback-error-tests.js b/test/integration/client/query-callback-error-tests.js index 4fba387a..bd80153c 100644 --- a/test/integration/client/query-callback-error-tests.js +++ b/test/integration/client/query-callback-error-tests.js @@ -6,7 +6,6 @@ var withQuery = function(text, resultLength, cb) { var client = new Client(helper.args); process.removeAllListeners('uncaughtException'); assert.emits(process, 'uncaughtException', function() { - console.log('got uncaught exception') assert.equal(client.activeQuery, null, 'should remove active query even if error happens in callback'); client.query('SELECT * FROM blah', assert.success(function(result) { assert.equal(result.rows.length, resultLength); From 874c924f7a04c79bfab8a597912abbce6a342237 Mon Sep 17 00:00:00 2001 From: bmc Date: Mon, 22 Apr 2013 10:19:08 -0500 Subject: [PATCH 5/8] Add test file Forgot to add this to the last commit --- test/integration/client/json-type-parsing-tests.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/integration/client/json-type-parsing-tests.js b/test/integration/client/json-type-parsing-tests.js index 215e2262..300f093f 100644 --- a/test/integration/client/json-type-parsing-tests.js +++ b/test/integration/client/json-type-parsing-tests.js @@ -2,6 +2,7 @@ var helper = require(__dirname + '/test-helper'); var assert = require('assert'); //if you want binary support, pull request me! if (helper.config.binary) { + console.log('binary mode does not support JSON right now'); return; } From 537e8e763e4e5f500be469c4ac567e4584a7a940 Mon Sep 17 00:00:00 2001 From: bmc Date: Mon, 22 Apr 2013 10:26:43 -0500 Subject: [PATCH 6/8] Skip JSON tests on older versions of postgres --- .../client/json-type-parsing-tests.js | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/test/integration/client/json-type-parsing-tests.js b/test/integration/client/json-type-parsing-tests.js index 300f093f..1c0759bf 100644 --- a/test/integration/client/json-type-parsing-tests.js +++ b/test/integration/client/json-type-parsing-tests.js @@ -9,23 +9,30 @@ if (helper.config.binary) { test('can read and write json', function() { helper.pg.connect(helper.config, function(err, client, done) { assert.ifError(err); - client.query('CREATE TEMP TABLE stuff(id SERIAL PRIMARY KEY, data JSON)'); - var value ={name: 'Brian', age: 250, alive: true, now: new Date()}; - client.query('INSERT INTO stuff (data) VALUES ($1)', [value]); - client.query('SELECT * FROM stuff', assert.success(function(result) { - assert.equal(result.rows.length, 1); - assert.equal(typeof result.rows[0].data, 'object'); - var row = result.rows[0].data; - assert.strictEqual(row.name, value.name); - assert.strictEqual(row.age, value.age); - assert.strictEqual(row.alive, value.alive); - test('row should have "now" as a date', function() { - return false; - assert(row.now instanceof Date, 'row.now should be a date instance but is ' + typeof row.now); - }); - assert.equal(JSON.stringify(row.now), JSON.stringify(value.now)); - done(); - helper.pg.end(); + helper.versionGTE(client, '9.2.0', assert.success(function(jsonSupported) { + if(!jsonSupported) { + console.log('skip json test on older versions of postgres'); + done(); + return helper.pg.end(); + } + client.query('CREATE TEMP TABLE stuff(id SERIAL PRIMARY KEY, data JSON)'); + var value ={name: 'Brian', age: 250, alive: true, now: new Date()}; + client.query('INSERT INTO stuff (data) VALUES ($1)', [value]); + client.query('SELECT * FROM stuff', assert.success(function(result) { + assert.equal(result.rows.length, 1); + assert.equal(typeof result.rows[0].data, 'object'); + var row = result.rows[0].data; + assert.strictEqual(row.name, value.name); + assert.strictEqual(row.age, value.age); + assert.strictEqual(row.alive, value.alive); + test('row should have "now" as a date', function() { + return false; + assert(row.now instanceof Date, 'row.now should be a date instance but is ' + typeof row.now); + }); + assert.equal(JSON.stringify(row.now), JSON.stringify(value.now)); + done(); + helper.pg.end(); + })); })); }); }); From 12e4a2b98de460e640935cb9cef113245d148093 Mon Sep 17 00:00:00 2001 From: bmc Date: Mon, 22 Apr 2013 10:35:26 -0500 Subject: [PATCH 7/8] Run the second pass of the tests on postgres 9.2 --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 033c73cd..145be5ba 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ test: test-unit test-all: jshint test-unit test-integration test-native test-binary test-travis: test-all upgrade-pg - @make test-all + @make test-all connectionString=pg://postgres@localhost:5433/postgres upgrade-pg: @chmod 755 script/travis-pg-9.2-install.sh From ddc2ae9bec17d6e84cfa2f6255a4c4d09949c5fe Mon Sep 17 00:00:00 2001 From: bmc Date: Mon, 22 Apr 2013 10:38:47 -0500 Subject: [PATCH 8/8] Run script to create test table on postgres 9.2 --- script/travis-pg-9.2-install.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/script/travis-pg-9.2-install.sh b/script/travis-pg-9.2-install.sh index 0f53ff8d..06b7e55d 100755 --- a/script/travis-pg-9.2-install.sh +++ b/script/travis-pg-9.2-install.sh @@ -15,4 +15,6 @@ sudo echo "host all all ::1/128 trust" >> /etc/ sudo echo "host all all 0.0.0.0/0 trust" >> /etc/postgresql/9.2/main/pg_hba.conf sudo echo "host all all 0.0.0.0 255.255.255.255 trust" >> /etc/postgresql/9.2/main/pg_hba.conf sudo /etc/init.d/postgresql restart -#node script/create-test-tables.js pg://postgres@127.0.0.1:5432/postgres +# for some reason both postgres 9.1 and 9.2 are started +# 9.2 is running on port 5433 +node script/create-test-tables.js pg://postgres@localhost:5433/postgres