From f55a0cd1b4a219a256c95f2ed74d16e48a30a02c Mon Sep 17 00:00:00 2001 From: bmc Date: Wed, 17 Apr 2013 09:26:31 -0500 Subject: [PATCH] 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 151e33f..c652b87 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 068173c..b110c98 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 e1dd661..1add336 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 55d1142..7905d15 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;