From f55a0cd1b4a219a256c95f2ed74d16e48a30a02c Mon Sep 17 00:00:00 2001 From: bmc Date: Wed, 17 Apr 2013 09:26:31 -0500 Subject: [PATCH 1/4] fix tests for postgres >= v9.2.0 --- package.json | 3 +- .../client/query-error-handling-tests.js | 32 ++++++++++------ .../connection-pool/error-tests.js | 38 +++++++++++-------- test/integration/test-helper.js | 9 +++++ 4 files changed, 53 insertions(+), 29 deletions(-) diff --git a/package.json b/package.json index 151e33f4..c652b871 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,8 @@ "buffer-writer": "1.0.0" }, "devDependencies": { - "jshint": "1.1.0" + "jshint": "1.1.0", + "semver": "~1.1.4" }, "scripts": { "test": "make test-all connectionString=pg://postgres@localhost:5432/postgres", diff --git a/test/integration/client/query-error-handling-tests.js b/test/integration/client/query-error-handling-tests.js index 068173ca..b110c985 100644 --- a/test/integration/client/query-error-handling-tests.js +++ b/test/integration/client/query-error-handling-tests.js @@ -5,18 +5,26 @@ test('error during query execution', function() { var client = new Client(helper.args); client.connect(assert.success(function() { var sleepQuery = 'select pg_sleep(5)'; - client.query(sleepQuery, assert.calls(function(err, result) { - assert(err); - client.end(); - })); - var client2 = new Client(helper.args); - client2.connect(assert.success(function() { -var killIdleQuery = "SELECT procpid, (SELECT pg_terminate_backend(procpid)) AS killed FROM pg_stat_activity WHERE current_query = $1"; - client2.query(killIdleQuery, [sleepQuery], assert.calls(function(err, res) { - assert.ifError(err); - assert.equal(res.rowCount, 1); - client2.end(); - assert.emits(client2, 'end'); + var pidColName = 'procpid' + var queryColName = 'current_query'; + helper.versionGTE(client, '9.2.0', assert.success(function(isGreater) { + if(isGreater) { + pidColName = 'pid'; + queryColName = 'query'; + } + client.query(sleepQuery, assert.calls(function(err, result) { + assert(err); + client.end(); + })); + var client2 = new Client(helper.args); + client2.connect(assert.success(function() { + var killIdleQuery = "SELECT " + pidColName + ", (SELECT pg_terminate_backend(" + pidColName + ")) AS killed FROM pg_stat_activity WHERE " + queryColName + " = $1"; + client2.query(killIdleQuery, [sleepQuery], assert.calls(function(err, res) { + assert.ifError(err); + assert.equal(res.rowCount, 1); + client2.end(); + assert.emits(client2, 'end'); + })); })); })); })); diff --git a/test/integration/connection-pool/error-tests.js b/test/integration/connection-pool/error-tests.js index e1dd6614..1add336b 100644 --- a/test/integration/connection-pool/error-tests.js +++ b/test/integration/connection-pool/error-tests.js @@ -5,25 +5,31 @@ pg = pg; //first make pool hold 2 clients pg.defaults.poolSize = 2; -var killIdleQuery = 'SELECT procpid, (SELECT pg_terminate_backend(procpid)) AS killed FROM pg_stat_activity WHERE current_query LIKE \'\''; //get first client pg.connect(helper.config, assert.success(function(client, done) { client.id = 1; - pg.connect(helper.config, assert.success(function(client2, done2) { - client2.id = 2; - done2(); - //subscribe to the pg error event - assert.emits(pg, 'error', function(error, brokenClient) { - assert.ok(error); - assert.ok(brokenClient); - assert.equal(client.id, brokenClient.id); - }); - //kill the connection from client - client2.query(killIdleQuery, assert.success(function(res) { - //check to make sure client connection actually was killed - assert.lengthIs(res.rows, 1); - pg.end(); + pg.connect(helper.config, assert.success(function(client2, done2) { + client2.id = 2; + var pidColName = 'procpid' + helper.versionGTE(client2, '9.2.0', assert.success(function(isGreater) { + if(isGreater) { + pidColName = 'pid'; + } + var killIdleQuery = 'SELECT ' + pidColName + ', (SELECT pg_terminate_backend(' + pidColName + ')) AS killed FROM pg_stat_activity WHERE state = $1'; + done2(); + //subscribe to the pg error event + assert.emits(pg, 'error', function(error, brokenClient) { + assert.ok(error); + assert.ok(brokenClient); + assert.equal(client.id, brokenClient.id); + }); + //kill the connection from client + client2.query(killIdleQuery, ['idle'], assert.success(function(res) { + //check to make sure client connection actually was killed + assert.lengthIs(res.rows, 1); + pg.end(); + })); + })); })); - })); })); diff --git a/test/integration/test-helper.js b/test/integration/test-helper.js index 55d11420..7905d157 100644 --- a/test/integration/test-helper.js +++ b/test/integration/test-helper.js @@ -13,6 +13,15 @@ helper.client = function() { return client; }; +var semver = require('semver'); +helper.versionGTE = function(client, versionString, callback) { + client.query('SELECT version()', assert.calls(function(err, result) { + if(err) return callback(err); + var version = result.rows[0].version.split(' ')[1]; + return callback(null, semver.gte(version, versionString)); + })); +}; + //export parent helper stuffs module.exports = helper; From f5f5320b154cb98f235ca2d6825b9ca5d0b434fd Mon Sep 17 00:00:00 2001 From: brianc Date: Wed, 17 Apr 2013 09:54:16 -0500 Subject: [PATCH 2/4] fix tests on older versions of postgres --- test/integration/connection-pool/error-tests.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/integration/connection-pool/error-tests.js b/test/integration/connection-pool/error-tests.js index 1add336b..4115db95 100644 --- a/test/integration/connection-pool/error-tests.js +++ b/test/integration/connection-pool/error-tests.js @@ -13,10 +13,12 @@ pg.connect(helper.config, assert.success(function(client, done) { client2.id = 2; var pidColName = 'procpid' helper.versionGTE(client2, '9.2.0', assert.success(function(isGreater) { - if(isGreater) { - pidColName = 'pid'; + var killIdleQuery = 'SELECT pid, (SELECT pg_terminate_backend(pid)) AS killed FROM pg_stat_activity WHERE state = $1'; + var params = ['idle']; + if(!isGreater) { + killIdleQuery = 'SELECT procpid, (SELECT pg_terminate_backend(procpid)) AS killed FROM pg_stat_activity WHERE current_query LIKE $1'; + params = ['%IDLE%'] } - var killIdleQuery = 'SELECT ' + pidColName + ', (SELECT pg_terminate_backend(' + pidColName + ')) AS killed FROM pg_stat_activity WHERE state = $1'; done2(); //subscribe to the pg error event assert.emits(pg, 'error', function(error, brokenClient) { @@ -25,7 +27,7 @@ pg.connect(helper.config, assert.success(function(client, done) { assert.equal(client.id, brokenClient.id); }); //kill the connection from client - client2.query(killIdleQuery, ['idle'], assert.success(function(res) { + client2.query(killIdleQuery, params, assert.success(function(res) { //check to make sure client connection actually was killed assert.lengthIs(res.rows, 1); pg.end(); From 01f3f3da53a9b580cc3b2de2a4b63a664f1497e6 Mon Sep 17 00:00:00 2001 From: Brian C Date: Wed, 17 Apr 2013 09:59:10 -0500 Subject: [PATCH 3/4] use master branch url for travis image --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 245d96f8..aa58e576 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ #node-postgres -[![Build Status](https://secure.travis-ci.org/brianc/node-postgres.png)](http://travis-ci.org/brianc/node-postgres) +[![Build Status](https://secure.travis-ci.org/brianc/node-postgres.png?branch=master)](http://travis-ci.org/brianc/node-postgres) PostgreSQL client for node.js. Pure JavaScript and native libpq bindings. From 3f5df0afa26944e026be4fbe75b59fc97e25bd99 Mon Sep 17 00:00:00 2001 From: brianc Date: Wed, 17 Apr 2013 10:29:21 -0500 Subject: [PATCH 4/4] make tests pass on pg@8.4.9 --- lib/result.js | 2 +- .../client/query-error-handling-tests.js | 2 +- .../client/result-metadata-tests.js | 40 ++++++++++--------- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/lib/result.js b/lib/result.js index fd920ed4..4eabbe7f 100644 --- a/lib/result.js +++ b/lib/result.js @@ -8,7 +8,7 @@ var Result = function() { this.rows = []; }; -var matchRegexp = /([A-Za-z]+) (\d+ )?(\d+)?/; +var matchRegexp = /([A-Za-z]+) ?(\d+ )?(\d+)?/; //adds a command complete message Result.prototype.addCommandComplete = function(msg) { diff --git a/test/integration/client/query-error-handling-tests.js b/test/integration/client/query-error-handling-tests.js index b110c985..8ac060ea 100644 --- a/test/integration/client/query-error-handling-tests.js +++ b/test/integration/client/query-error-handling-tests.js @@ -21,7 +21,7 @@ test('error during query execution', function() { var killIdleQuery = "SELECT " + pidColName + ", (SELECT pg_terminate_backend(" + pidColName + ")) AS killed FROM pg_stat_activity WHERE " + queryColName + " = $1"; client2.query(killIdleQuery, [sleepQuery], assert.calls(function(err, res) { assert.ifError(err); - assert.equal(res.rowCount, 1); + assert.equal(res.rows.length, 1); client2.end(); assert.emits(client2, 'end'); })); diff --git a/test/integration/client/result-metadata-tests.js b/test/integration/client/result-metadata-tests.js index 98d065ea..8f66fe16 100644 --- a/test/integration/client/result-metadata-tests.js +++ b/test/integration/client/result-metadata-tests.js @@ -5,29 +5,31 @@ test('should return insert metadata', function() { pg.connect(helper.config, assert.calls(function(err, client, done) { assert.isNull(err); - client.query("CREATE TEMP TABLE zugzug(name varchar(10))", assert.calls(function(err, result) { - assert.isNull(err); - assert.equal(result.oid, null); - assert.equal(result.command, 'CREATE'); + helper.versionGTE(client, '9.0.0', assert.success(function(hasRowCount) { + client.query("CREATE TEMP TABLE zugzug(name varchar(10))", assert.calls(function(err, result) { + assert.isNull(err); + assert.equal(result.oid, null); + assert.equal(result.command, 'CREATE'); - var q = client.query("INSERT INTO zugzug(name) VALUES('more work?')", assert.calls(function(err, result) { - assert.equal(result.command, "INSERT"); - assert.equal(result.rowCount, 1); - - client.query('SELECT * FROM zugzug', assert.calls(function(err, result) { - assert.isNull(err); + var q = client.query("INSERT INTO zugzug(name) VALUES('more work?')", assert.calls(function(err, result) { + assert.equal(result.command, "INSERT"); assert.equal(result.rowCount, 1); - assert.equal(result.command, 'SELECT'); - process.nextTick(pg.end.bind(pg)); + + client.query('SELECT * FROM zugzug', assert.calls(function(err, result) { + assert.isNull(err); + if(hasRowCount) assert.equal(result.rowCount, 1); + assert.equal(result.command, 'SELECT'); + process.nextTick(pg.end.bind(pg)); + })); })); + + assert.emits(q, 'end', function(result) { + assert.equal(result.command, "INSERT"); + if(hasRowCount) assert.equal(result.rowCount, 1); + done(); + }); + })); - - assert.emits(q, 'end', function(result) { - assert.equal(result.command, "INSERT"); - assert.equal(result.rowCount, 1); - done(); - }); - })); })); });