use config dict in all test

instead of the connection string use the config dict in all tests to
be able to specify things like binary mode
This commit is contained in:
Alexander Sulfrian 2011-11-22 04:42:43 +01:00
parent 239d8bd0c2
commit f698ed4459
20 changed files with 70 additions and 118 deletions

View File

@ -1,9 +1,9 @@
var helper = require(__dirname + '/../test/test-helper'); var helper = require(__dirname + '/../test/test-helper');
var connectionString = helper.connectionString();
console.log(); console.log();
console.log("testing ability to connect to '%s'", connectionString); console.log("testing ability to connect to '%j'", helper.config);
var pg = require(__dirname + '/../lib'); var pg = require(__dirname + '/../lib');
pg.connect(connectionString, function(err, client) { pg.connect(helper.config, function(err, client) {
if(err !== null) { if(err !== null) {
console.error("Recieved connection error when attempting to contact PostgreSQL:"); console.error("Recieved connection error when attempting to contact PostgreSQL:");
console.error(err); console.error(err);

View File

@ -5,20 +5,18 @@ if(helper.args.native) {
pg = require(__dirname + '/../../../lib').native; pg = require(__dirname + '/../../../lib').native;
} }
var connectionString = helper.connectionString(__filename);
var log = function() { var log = function() {
//console.log.apply(console, arguments); //console.log.apply(console, arguments);
} }
var sink = new helper.Sink(5, 10000, function() { var sink = new helper.Sink(5, 10000, function() {
log("ending connection pool: %s", connectionString); log("ending connection pool: %j", helper.config);
pg.end(connectionString); pg.end(helper.config);
}); });
test('api', function() { test('api', function() {
log("connecting to %s", connectionString) log("connecting to %j", helper.config)
pg.connect(connectionString, assert.calls(function(err, client) { pg.connect(helper.config, assert.calls(function(err, client) {
assert.equal(err, null, "Failed to connect: " + helper.sys.inspect(err)); assert.equal(err, null, "Failed to connect: " + helper.sys.inspect(err));
client.query('CREATE TEMP TABLE band(name varchar(100))'); client.query('CREATE TEMP TABLE band(name varchar(100))');
@ -60,7 +58,7 @@ test('api', function() {
}) })
test('executing nested queries', function() { test('executing nested queries', function() {
pg.connect(connectionString, assert.calls(function(err, client) { pg.connect(helper.config, assert.calls(function(err, client) {
assert.isNull(err); assert.isNull(err);
log("connected for nested queriese") log("connected for nested queriese")
client.query('select now as now from NOW()', assert.calls(function(err, result) { client.query('select now as now from NOW()', assert.calls(function(err, result) {
@ -87,7 +85,7 @@ test('raises error if cannot connect', function() {
}) })
test("query errors are handled and do not bubble if callback is provded", function() { test("query errors are handled and do not bubble if callback is provded", function() {
pg.connect(connectionString, assert.calls(function(err, client) { pg.connect(helper.config, assert.calls(function(err, client) {
assert.isNull(err) assert.isNull(err)
log("checking for query error") log("checking for query error")
client.query("SELECT OISDJF FROM LEIWLISEJLSE", assert.calls(function(err, result) { client.query("SELECT OISDJF FROM LEIWLISEJLSE", assert.calls(function(err, result) {
@ -99,7 +97,7 @@ test("query errors are handled and do not bubble if callback is provded", functi
}) })
test('callback is fired once and only once', function() { test('callback is fired once and only once', function() {
pg.connect(connectionString, assert.calls(function(err, client) { pg.connect(helper.config, assert.calls(function(err, client) {
assert.isNull(err); assert.isNull(err);
client.query("CREATE TEMP TABLE boom(name varchar(10))"); client.query("CREATE TEMP TABLE boom(name varchar(10))");
var callCount = 0; var callCount = 0;
@ -115,7 +113,7 @@ test('callback is fired once and only once', function() {
}) })
test('can provide callback and config object', function() { test('can provide callback and config object', function() {
pg.connect(connectionString, assert.calls(function(err, client) { pg.connect(helper.config, assert.calls(function(err, client) {
assert.isNull(err); assert.isNull(err);
client.query({ client.query({
name: 'boom', name: 'boom',
@ -128,7 +126,7 @@ test('can provide callback and config object', function() {
}) })
test('can provide callback and config and parameters', function() { test('can provide callback and config and parameters', function() {
pg.connect(connectionString, assert.calls(function(err, client) { pg.connect(helper.config, assert.calls(function(err, client) {
assert.isNull(err); assert.isNull(err);
var config = { var config = {
text: 'select $1::text as val' text: 'select $1::text as val'
@ -142,7 +140,7 @@ test('can provide callback and config and parameters', function() {
}) })
test('null and undefined are both inserted as NULL', function() { test('null and undefined are both inserted as NULL', function() {
pg.connect(connectionString, assert.calls(function(err, client) { pg.connect(helper.config, assert.calls(function(err, client) {
assert.isNull(err); assert.isNull(err);
client.query("CREATE TEMP TABLE my_nulls(a varchar(1), b varchar(1), c integer, d integer, e date, f date)"); client.query("CREATE TEMP TABLE my_nulls(a varchar(1), b varchar(1), c integer, d integer, e date, f date)");
client.query("INSERT INTO my_nulls(a,b,c,d,e,f) VALUES ($1,$2,$3,$4,$5,$6)", [ null, undefined, null, undefined, null, undefined ]); client.query("INSERT INTO my_nulls(a,b,c,d,e,f) VALUES ($1,$2,$3,$4,$5,$6)", [ null, undefined, null, undefined, null, undefined ]);

View File

@ -1,9 +1,8 @@
var helper = require(__dirname + "/test-helper"); var helper = require(__dirname + "/test-helper");
var pg = helper.pg; var pg = helper.pg;
var conString = helper.connectionString();
test('parsing array results', function() { test('parsing array results', function() {
pg.connect(conString, assert.calls(function(err, client) { pg.connect(helper.config, assert.calls(function(err, client) {
assert.isNull(err); assert.isNull(err);
client.query("CREATE TEMP TABLE why(names text[], numbors integer[])"); client.query("CREATE TEMP TABLE why(names text[], numbors integer[])");
client.query('INSERT INTO why(names, numbors) VALUES(\'{"aaron", "brian","a b c" }\', \'{1, 2, 3}\')').on('error', console.log); client.query('INSERT INTO why(names, numbors) VALUES(\'{"aaron", "brian","a b c" }\', \'{1, 2, 3}\')').on('error', console.log);

View File

@ -28,9 +28,9 @@ test("cancellation of a query", function() {
rows4++; rows4++;
}); });
helper.pg.cancel(helper.connectionString, client, query1); helper.pg.cancel(helper.config, client, query1);
helper.pg.cancel(helper.connectionString, client, query2); helper.pg.cancel(helper.config, client, query2);
helper.pg.cancel(helper.connectionString, client, query4); helper.pg.cancel(helper.config, client, query4);
setTimeout(function() { setTimeout(function() {
assert.equal(rows1, 0); assert.equal(rows1, 0);

View File

@ -6,7 +6,7 @@ if(helper.args.native) {
} }
var testDrainOfClientWithPendingQueries = function() { var testDrainOfClientWithPendingQueries = function() {
pg.connect(helper.connectionString(), assert.success(function(client) { pg.connect(helper.config, assert.success(function(client) {
test('when there are pending queries and client is resumed', function() { test('when there are pending queries and client is resumed', function() {
var drainCount = 0; var drainCount = 0;
client.on('drain', function() { client.on('drain', function() {
@ -28,7 +28,7 @@ var testDrainOfClientWithPendingQueries = function() {
})); }));
}; };
pg.connect(helper.connectionString(), assert.success(function(client) { pg.connect(helper.config, assert.success(function(client) {
var drainCount = 0; var drainCount = 0;
client.on('drain', function() { client.on('drain', function() {
drainCount++; drainCount++;

View File

@ -1,10 +1,9 @@
var helper = require(__dirname + "/test-helper"); var helper = require(__dirname + "/test-helper");
var pg = helper.pg; var pg = helper.pg;
var conString = helper.connectionString();
test('should return insert metadata', function() { test('should return insert metadata', function() {
return false; return false;
pg.connect(conString, assert.calls(function(err, client) { pg.connect(helper.config, assert.calls(function(err, client) {
assert.isNull(err); assert.isNull(err);
client.query("CREATE TEMP TABLE zugzug(name varchar(10))", assert.calls(function(err, result) { client.query("CREATE TEMP TABLE zugzug(name varchar(10))", assert.calls(function(err, result) {
assert.isNull(err); assert.isNull(err);

View File

@ -1,21 +1,10 @@
var helper = require(__dirname+'/../test-helper'); var helper = require(__dirname+'/../test-helper');
module.exports = { //creates a client from cli parameters
//creates a client from cli parameters helper.client = function() {
client: function() { var client = new Client(helper.config);
var client = new Client({ client.connect();
database: helper.args.database, return client;
user: helper.args.user,
password: helper.args.password,
host: helper.args.host,
port: helper.args.port
});
client.connect();
return client;
},
connectionString: helper.connectionString,
Sink: helper.Sink,
pg: helper.pg,
args: helper.args
}; };
module.exports = helper;

View File

@ -5,9 +5,7 @@ var sink = new helper.Sink(2, function() {
}); });
test('a single connection transaction', function() { test('a single connection transaction', function() {
var connectionString = helper.connectionString(); helper.pg.connect(helper.config, assert.calls(function(err, client) {
helper.pg.connect(connectionString, assert.calls(function(err, client) {
assert.isNull(err); assert.isNull(err);
client.query('begin'); client.query('begin');
@ -48,8 +46,7 @@ test('a single connection transaction', function() {
}) })
test('gh#36', function() { test('gh#36', function() {
var connectionString = helper.connectionString(); helper.pg.connect(helper.config, function(err, client) {
helper.pg.connect(connectionString, function(err, client) {
if(err) throw err; if(err) throw err;
client.query("BEGIN"); client.query("BEGIN");
client.query({ client.query({

View File

@ -1,9 +1,9 @@
var helper = require(__dirname + '/test-helper'); var helper = require(__dirname + '/test-helper');
var sink; var sink;
var connectionString = helper.connectionString();
var testForTypeCoercion = function(type){ var testForTypeCoercion = function(type){
helper.pg.connect(connectionString, function(err, client) { helper.pg.connect(helper.config, function(err, client) {
assert.isNull(err) assert.isNull(err);
client.query("create temp table test_type(col " + type.name + ")", assert.calls(function(err, result) { client.query("create temp table test_type(col " + type.name + ")", assert.calls(function(err, result) {
assert.isNull(err); assert.isNull(err);
test("Coerces " + type.name, function() { test("Coerces " + type.name, function() {
@ -126,7 +126,7 @@ test("timestampz round trip", function() {
client.on('drain', client.end.bind(client)); client.on('drain', client.end.bind(client));
}); });
helper.pg.connect(helper.connectionString(), assert.calls(function(err, client) { helper.pg.connect(helper.config, assert.calls(function(err, client) {
assert.isNull(err); assert.isNull(err);
client.query('select null as res;', assert.calls(function(err, res) { client.query('select null as res;', assert.calls(function(err, res) {
assert.isNull(err); assert.isNull(err);

View File

@ -1,8 +1,4 @@
var helper = require(__dirname + '/test-helper') var helper = require(__dirname + '/test-helper')
var conString1 = helper.connectionString();
var conString2 = helper.connectionString();
var conString3 = helper.connectionString();
var conString4 = helper.connectionString();
var called = false; var called = false;
test('disconnects', function() { test('disconnects', function() {
@ -11,8 +7,8 @@ test('disconnects', function() {
//this should exit the process, killing each connection pool //this should exit the process, killing each connection pool
helper.pg.end(); helper.pg.end();
}); });
[conString1, conString2, conString3, conString4].forEach(function() { [helper.config, helper.config, helper.config, helper.config].forEach(function(config) {
helper.pg.connect(conString1, function(err, client) { helper.pg.connect(config, function(err, client) {
assert.isNull(err); assert.isNull(err);
client.query("SELECT * FROM NOW()", function(err, result) { client.query("SELECT * FROM NOW()", function(err, result) {
process.nextTick(function() { process.nextTick(function() {

View File

@ -2,24 +2,22 @@ var helper = require(__dirname + "/../test-helper");
var pg = require(__dirname + "/../../../lib"); var pg = require(__dirname + "/../../../lib");
helper.pg = pg; helper.pg = pg;
var conString = helper.connectionString();
//first make pool hold 2 clients //first make pool hold 2 clients
pg.defaults.poolSize = 2; helper.pg.defaults.poolSize = 2;
var killIdleQuery = 'SELECT procpid, (SELECT pg_terminate_backend(procpid)) AS killed FROM pg_stat_activity WHERE current_query LIKE \'<IDLE>\''; var killIdleQuery = 'SELECT procpid, (SELECT pg_terminate_backend(procpid)) AS killed FROM pg_stat_activity WHERE current_query LIKE \'<IDLE>\'';
//get first client //get first client
pg.connect(conString, assert.success(function(client) { helper.pg.connect(helper.config, assert.success(function(client) {
client.id = 1; client.id = 1;
pg.connect(conString, assert.success(function(client2) { helper.pg.connect(helper.config, assert.success(function(client2) {
client2.id = 2; client2.id = 2;
//subscribe to the pg error event //subscribe to the pg error event
assert.emits(pg, 'error', function(error, brokenClient) { assert.emits(helper.pg, 'error', function(error, brokenClient) {
assert.ok(error); assert.ok(error);
assert.ok(brokenClient); assert.ok(brokenClient);
assert.equal(client.id, brokenClient.id); assert.equal(client.id, brokenClient.id);
pg.end(); helper.pg.end();
}); });
//kill the connection from client //kill the connection from client
client2.query(killIdleQuery, assert.success(function(res) { client2.query(killIdleQuery, assert.success(function(res) {

View File

@ -3,7 +3,7 @@ var helper = require(__dirname + '/test-helper');
helper.pg.defaults.poolIdleTimeout = 200; helper.pg.defaults.poolIdleTimeout = 200;
test('idle timeout', function() { test('idle timeout', function() {
helper.pg.connect(helper.connectionString(), assert.calls(function(err, client) { helper.pg.connect(helper.config, assert.calls(function(err, client) {
assert.isNull(err); assert.isNull(err);
client.query('SELECT NOW()'); client.query('SELECT NOW()');
//just let this one time out //just let this one time out

View File

@ -1,18 +1,15 @@
var helper = require(__dirname + "/../test-helper"); var helper = require(__dirname + "/../test-helper");
var pg = require(__dirname + "/../../../lib");
helper.pg = pg;
var testPoolSize = function(max) { helper.testPoolSize = function(max) {
var conString = helper.connectionString();
var sink = new helper.Sink(max, function() { var sink = new helper.Sink(max, function() {
helper.pg.end(conString); helper.pg.end();
}); });
test("can pool " + max + " times", function() { test("can pool " + max + " times", function() {
for(var i = 0; i < max; i++) { for(var i = 0; i < max; i++) {
helper.pg.poolSize = 10; helper.pg.poolSize = 10;
test("connection #" + i + " executes", function() { test("connection #" + i + " executes", function() {
helper.pg.connect(conString, function(err, client) { helper.pg.connect(helper.config, function(err, client) {
assert.isNull(err); assert.isNull(err);
client.query("select * from person", function(err, result) { client.query("select * from person", function(err, result) {
assert.lengthIs(result.rows, 26) assert.lengthIs(result.rows, 26)
@ -30,11 +27,5 @@ var testPoolSize = function(max) {
}) })
} }
module.exports = { module.exports = helper;
args: helper.args,
pg: helper.pg,
connectionString: helper.connectionString,
Sink: helper.Sink,
testPoolSize: testPoolSize
}

View File

@ -7,35 +7,25 @@ helper.pg.defaults.database = helper.args.database;
helper.pg.defaults.port = helper.args.port; helper.pg.defaults.port = helper.args.port;
helper.pg.defaults.host = helper.args.host; helper.pg.defaults.host = helper.args.host;
helper.pg.defaults.poolIdleTimeout = 100; helper.pg.defaults.poolIdleTimeout = 100;
var args = {
user: helper.args.user, var moreArgs = {};
password: helper.args.password, for (c in helper.config) {
database: helper.args.database, moreArgs[c] = helper.config[c];
port: helper.args.port, }
host: helper.args.host moreArgs.zomg = true;
var badArgs = {};
for (c in helper.config) {
badArgs[c] = helper.config[c];
} }
var moreArgs = { badArgs.user = badArgs.user + 'laksdjfl';
database: helper.args.database, badArgs.password = badArgs.password + 'asldkfjlas';
password: helper.args.password, badArgs.zomg = true;
port: helper.args.port,
user: helper.args.user,
host: helper.args.host,
zomg: true
}
var badArgs = {
user: helper.args.user + 'laksdjfl',
host: helper.args.host,
password: helper.args.password + 'asldkfjlas',
database: helper.args.database,
port: helper.args.port,
zomg: true
}
test('connecting with complete config', function() { test('connecting with complete config', function() {
helper.pg.connect(args, assert.calls(function(err, client) { helper.pg.connect(helper.config, assert.calls(function(err, client) {
assert.isNull(err); assert.isNull(err);
client.iGotAccessed = true; client.iGotAccessed = true;
client.query("SELECT NOW()") client.query("SELECT NOW()")

View File

@ -1,9 +1,8 @@
var helper = require(__dirname + "/../test-helper"); var helper = require(__dirname + "/../test-helper");
var Client = require(__dirname + "/../../lib/native"); var Client = require(__dirname + "/../../lib/native");
var conString = helper.connectionString();
test('fires callback with results', function() { test('fires callback with results', function() {
var client = new Client(conString); var client = new Client(helper.config);
client.connect(); client.connect();
client.query('SELECT 1 as num', assert.calls(function(err, result) { client.query('SELECT 1 as num', assert.calls(function(err, result) {
assert.isNull(err); assert.isNull(err);

View File

@ -12,7 +12,7 @@ test('connecting with wrong parameters', function() {
}); });
test('connects', function() { test('connects', function() {
var con = new Client(helper.connectionString()); var con = new Client(helper.config);
con.connect(); con.connect();
assert.emits(con, 'connect', function() { assert.emits(con, 'connect', function() {
test('disconnects', function() { test('disconnects', function() {

View File

@ -1,9 +1,8 @@
var helper = require(__dirname + "/../test-helper"); var helper = require(__dirname + "/../test-helper");
var Client = require(__dirname + "/../../lib/native"); var Client = require(__dirname + "/../../lib/native");
var conString = helper.connectionString();
test('query with non-text as first parameter throws error', function() { test('query with non-text as first parameter throws error', function() {
var client = new Client(conString); var client = new Client(helper.config);
client.connect(); client.connect();
assert.emits(client, 'connect', function() { assert.emits(client, 'connect', function() {
assert.throws(function() { assert.throws(function() {
@ -14,7 +13,7 @@ test('query with non-text as first parameter throws error', function() {
}) })
test('parameterized query with non-text as first parameter throws error', function() { test('parameterized query with non-text as first parameter throws error', function() {
var client = new Client(conString); var client = new Client(helper.config);
client.connect(); client.connect();
assert.emits(client, 'connect', function() { assert.emits(client, 'connect', function() {
assert.throws(function() { assert.throws(function() {
@ -28,7 +27,7 @@ test('parameterized query with non-text as first parameter throws error', functi
}) })
var connect = function(callback) { var connect = function(callback) {
var client = new Client(conString); var client = new Client(helper.config);
client.connect(); client.connect();
assert.emits(client, 'connect', function() { assert.emits(client, 'connect', function() {
callback(client); callback(client);

View File

@ -1,9 +1,8 @@
var helper = require(__dirname + "/../test-helper"); var helper = require(__dirname + "/../test-helper");
var Client = require(__dirname + "/../../lib/native"); var Client = require(__dirname + "/../../lib/native");
var conString = helper.connectionString();
var setupClient = function() { var setupClient = function() {
var client = new Client(conString); var client = new Client(helper.config);
client.connect(); client.connect();
client.query("CREATE TEMP TABLE boom(name varchar(10), age integer)"); client.query("CREATE TEMP TABLE boom(name varchar(10), age integer)");
client.query("INSERT INTO boom(name, age) VALUES('Aaron', 26)"); client.query("INSERT INTO boom(name, age) VALUES('Aaron', 26)");
@ -12,7 +11,7 @@ var setupClient = function() {
} }
test('connects', function() { test('connects', function() {
var client = new Client(conString); var client = new Client(helper.config);
client.connect(); client.connect();
test('good query', function() { test('good query', function() {
var query = client.query("SELECT 1 as num, 'HELLO' as str"); var query = client.query("SELECT 1 as num, 'HELLO' as str");

View File

@ -2,7 +2,7 @@ var helper = require(__dirname + "/../test-helper");
var Client = require(__dirname + "/../../lib/native"); var Client = require(__dirname + "/../../lib/native");
test('many rows', function() { test('many rows', function() {
var client = new Client(helper.connectionString()); var client = new Client(helper.config);
client.connect(); client.connect();
var q = client.query("SELECT * FROM person"); var q = client.query("SELECT * FROM person");
var rows = []; var rows = [];
@ -16,7 +16,7 @@ test('many rows', function() {
}); });
test('many queries', function() { test('many queries', function() {
var client = new Client(helper.connectionString()); var client = new Client(helper.config);
client.connect(); client.connect();
var count = 0; var count = 0;
var expected = 100; var expected = 100;
@ -35,7 +35,7 @@ test('many queries', function() {
test('many clients', function() { test('many clients', function() {
var clients = []; var clients = [];
for(var i = 0; i < 10; i++) { for(var i = 0; i < 10; i++) {
clients.push(new Client(helper.connectionString())); clients.push(new Client(helper.config));
} }
clients.forEach(function(client) { clients.forEach(function(client) {
client.connect(); client.connect();

View File

@ -225,9 +225,7 @@ module.exports = {
args: args, args: args,
Sink: Sink, Sink: Sink,
pg: require(__dirname + '/../lib/'), pg: require(__dirname + '/../lib/'),
connectionString: function() { config: args,
return "pg"+(count++)+"://"+args.user+":"+args.password+"@"+args.host+":"+args.port+"/"+args.database;
},
sys: sys, sys: sys,
Client: Client Client: Client
}; };