Merge pull request #331 from brianc/postgres-gte-9.2

fix tests on new versions of postgres
This commit is contained in:
Brian C 2013-04-17 08:32:51 -07:00
commit 07fd762137
7 changed files with 78 additions and 50 deletions

View File

@ -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.

View File

@ -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) {

View File

@ -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",

View File

@ -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.rows.length, 1);
client2.end();
assert.emits(client2, 'end');
}));
}));
}));
}));

View File

@ -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();
});
}));
}));
});

View File

@ -5,25 +5,33 @@ 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 \'<IDLE>\'';
//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) {
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%']
}
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, params, assert.success(function(res) {
//check to make sure client connection actually was killed
assert.lengthIs(res.rows, 1);
pg.end();
}));
}));
}));
}));
}));

View File

@ -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;