From f698ed44595ff4f36ab629c979fa9c223f4b2890 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Tue, 22 Nov 2011 04:42:43 +0100 Subject: [PATCH] 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 --- script/test-connection.js | 6 +-- test/integration/client/api-tests.js | 22 +++++------ test/integration/client/array-tests.js | 3 +- test/integration/client/cancel-query-tests.js | 6 +-- test/integration/client/drain-tests.js | 4 +- .../client/result-metadata-tests.js | 3 +- test/integration/client/test-helper.js | 25 ++++-------- test/integration/client/transaction-tests.js | 7 +--- .../integration/client/type-coercion-tests.js | 8 ++-- .../connection-pool/ending-pool-tests.js | 8 +--- .../connection-pool/error-tests.js | 12 +++--- .../connection-pool/idle-timeout-tests.js | 2 +- .../connection-pool/test-helper.js | 17 ++------- .../connection-pool/unique-name-tests.js | 38 +++++++------------ test/native/callback-api-tests.js | 3 +- test/native/connection-tests.js | 2 +- test/native/error-tests.js | 7 ++-- test/native/evented-api-tests.js | 5 +-- test/native/stress-tests.js | 6 +-- test/test-helper.js | 4 +- 20 files changed, 70 insertions(+), 118 deletions(-) diff --git a/script/test-connection.js b/script/test-connection.js index 3099a4d..8112861 100644 --- a/script/test-connection.js +++ b/script/test-connection.js @@ -1,9 +1,9 @@ var helper = require(__dirname + '/../test/test-helper'); -var connectionString = helper.connectionString(); + 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'); -pg.connect(connectionString, function(err, client) { +pg.connect(helper.config, function(err, client) { if(err !== null) { console.error("Recieved connection error when attempting to contact PostgreSQL:"); console.error(err); diff --git a/test/integration/client/api-tests.js b/test/integration/client/api-tests.js index 023cc29..0fd9410 100644 --- a/test/integration/client/api-tests.js +++ b/test/integration/client/api-tests.js @@ -5,20 +5,18 @@ if(helper.args.native) { pg = require(__dirname + '/../../../lib').native; } -var connectionString = helper.connectionString(__filename); - var log = function() { //console.log.apply(console, arguments); } var sink = new helper.Sink(5, 10000, function() { - log("ending connection pool: %s", connectionString); - pg.end(connectionString); + log("ending connection pool: %j", helper.config); + pg.end(helper.config); }); test('api', function() { - log("connecting to %s", connectionString) - pg.connect(connectionString, assert.calls(function(err, client) { + log("connecting to %j", helper.config) + pg.connect(helper.config, assert.calls(function(err, client) { assert.equal(err, null, "Failed to connect: " + helper.sys.inspect(err)); client.query('CREATE TEMP TABLE band(name varchar(100))'); @@ -60,7 +58,7 @@ test('api', 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); log("connected for nested queriese") 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() { - pg.connect(connectionString, assert.calls(function(err, client) { + pg.connect(helper.config, assert.calls(function(err, client) { assert.isNull(err) log("checking for query error") 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() { - pg.connect(connectionString, assert.calls(function(err, client) { + pg.connect(helper.config, assert.calls(function(err, client) { assert.isNull(err); client.query("CREATE TEMP TABLE boom(name varchar(10))"); var callCount = 0; @@ -115,7 +113,7 @@ test('callback is fired once and only once', 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); client.query({ name: 'boom', @@ -128,7 +126,7 @@ test('can provide callback and config object', 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); var config = { 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() { - pg.connect(connectionString, assert.calls(function(err, client) { + pg.connect(helper.config, assert.calls(function(err, client) { 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("INSERT INTO my_nulls(a,b,c,d,e,f) VALUES ($1,$2,$3,$4,$5,$6)", [ null, undefined, null, undefined, null, undefined ]); diff --git a/test/integration/client/array-tests.js b/test/integration/client/array-tests.js index 669100c..548b376 100644 --- a/test/integration/client/array-tests.js +++ b/test/integration/client/array-tests.js @@ -1,9 +1,8 @@ var helper = require(__dirname + "/test-helper"); var pg = helper.pg; -var conString = helper.connectionString(); 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); 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); diff --git a/test/integration/client/cancel-query-tests.js b/test/integration/client/cancel-query-tests.js index 36d5710..842b471 100644 --- a/test/integration/client/cancel-query-tests.js +++ b/test/integration/client/cancel-query-tests.js @@ -28,9 +28,9 @@ test("cancellation of a query", function() { rows4++; }); - helper.pg.cancel(helper.connectionString, client, query1); - helper.pg.cancel(helper.connectionString, client, query2); - helper.pg.cancel(helper.connectionString, client, query4); + helper.pg.cancel(helper.config, client, query1); + helper.pg.cancel(helper.config, client, query2); + helper.pg.cancel(helper.config, client, query4); setTimeout(function() { assert.equal(rows1, 0); diff --git a/test/integration/client/drain-tests.js b/test/integration/client/drain-tests.js index 0aff28e..b6a2434 100644 --- a/test/integration/client/drain-tests.js +++ b/test/integration/client/drain-tests.js @@ -6,7 +6,7 @@ if(helper.args.native) { } 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() { var drainCount = 0; 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; client.on('drain', function() { drainCount++; diff --git a/test/integration/client/result-metadata-tests.js b/test/integration/client/result-metadata-tests.js index b69028f..1c4f94d 100644 --- a/test/integration/client/result-metadata-tests.js +++ b/test/integration/client/result-metadata-tests.js @@ -1,10 +1,9 @@ var helper = require(__dirname + "/test-helper"); var pg = helper.pg; -var conString = helper.connectionString(); test('should return insert metadata', function() { return false; - pg.connect(conString, assert.calls(function(err, client) { + pg.connect(helper.config, assert.calls(function(err, client) { assert.isNull(err); client.query("CREATE TEMP TABLE zugzug(name varchar(10))", assert.calls(function(err, result) { assert.isNull(err); diff --git a/test/integration/client/test-helper.js b/test/integration/client/test-helper.js index b01235c..d8ae3d8 100644 --- a/test/integration/client/test-helper.js +++ b/test/integration/client/test-helper.js @@ -1,21 +1,10 @@ var helper = require(__dirname+'/../test-helper'); -module.exports = { - //creates a client from cli parameters - client: function() { - var client = new Client({ - database: helper.args.database, - 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 +//creates a client from cli parameters +helper.client = function() { + var client = new Client(helper.config); + client.connect(); + return client; }; + +module.exports = helper; diff --git a/test/integration/client/transaction-tests.js b/test/integration/client/transaction-tests.js index 8e3f8ac..4fbfd18 100644 --- a/test/integration/client/transaction-tests.js +++ b/test/integration/client/transaction-tests.js @@ -5,9 +5,7 @@ var sink = new helper.Sink(2, function() { }); test('a single connection transaction', function() { - var connectionString = helper.connectionString(); - - helper.pg.connect(connectionString, assert.calls(function(err, client) { + helper.pg.connect(helper.config, assert.calls(function(err, client) { assert.isNull(err); client.query('begin'); @@ -48,8 +46,7 @@ test('a single connection transaction', function() { }) test('gh#36', function() { - var connectionString = helper.connectionString(); - helper.pg.connect(connectionString, function(err, client) { + helper.pg.connect(helper.config, function(err, client) { if(err) throw err; client.query("BEGIN"); client.query({ diff --git a/test/integration/client/type-coercion-tests.js b/test/integration/client/type-coercion-tests.js index 124f82e..c8f3999 100644 --- a/test/integration/client/type-coercion-tests.js +++ b/test/integration/client/type-coercion-tests.js @@ -1,9 +1,9 @@ var helper = require(__dirname + '/test-helper'); var sink; -var connectionString = helper.connectionString(); + var testForTypeCoercion = function(type){ - helper.pg.connect(connectionString, function(err, client) { - assert.isNull(err) + helper.pg.connect(helper.config, function(err, client) { + assert.isNull(err); client.query("create temp table test_type(col " + type.name + ")", assert.calls(function(err, result) { assert.isNull(err); test("Coerces " + type.name, function() { @@ -126,7 +126,7 @@ test("timestampz round trip", function() { 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); client.query('select null as res;', assert.calls(function(err, res) { assert.isNull(err); diff --git a/test/integration/connection-pool/ending-pool-tests.js b/test/integration/connection-pool/ending-pool-tests.js index c269042..e46c0fc 100644 --- a/test/integration/connection-pool/ending-pool-tests.js +++ b/test/integration/connection-pool/ending-pool-tests.js @@ -1,8 +1,4 @@ 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; test('disconnects', function() { @@ -11,8 +7,8 @@ test('disconnects', function() { //this should exit the process, killing each connection pool helper.pg.end(); }); - [conString1, conString2, conString3, conString4].forEach(function() { - helper.pg.connect(conString1, function(err, client) { + [helper.config, helper.config, helper.config, helper.config].forEach(function(config) { + helper.pg.connect(config, function(err, client) { assert.isNull(err); client.query("SELECT * FROM NOW()", function(err, result) { process.nextTick(function() { diff --git a/test/integration/connection-pool/error-tests.js b/test/integration/connection-pool/error-tests.js index c4653ef..11badf0 100644 --- a/test/integration/connection-pool/error-tests.js +++ b/test/integration/connection-pool/error-tests.js @@ -2,24 +2,22 @@ var helper = require(__dirname + "/../test-helper"); var pg = require(__dirname + "/../../../lib"); helper.pg = pg; -var conString = helper.connectionString(); - //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 \'\''; //get first client -pg.connect(conString, assert.success(function(client) { +helper.pg.connect(helper.config, assert.success(function(client) { client.id = 1; - pg.connect(conString, assert.success(function(client2) { + helper.pg.connect(helper.config, assert.success(function(client2) { client2.id = 2; //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(brokenClient); assert.equal(client.id, brokenClient.id); - pg.end(); + helper.pg.end(); }); //kill the connection from client client2.query(killIdleQuery, assert.success(function(res) { diff --git a/test/integration/connection-pool/idle-timeout-tests.js b/test/integration/connection-pool/idle-timeout-tests.js index 342442e..c6cbbd9 100644 --- a/test/integration/connection-pool/idle-timeout-tests.js +++ b/test/integration/connection-pool/idle-timeout-tests.js @@ -3,7 +3,7 @@ var helper = require(__dirname + '/test-helper'); helper.pg.defaults.poolIdleTimeout = 200; 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); client.query('SELECT NOW()'); //just let this one time out diff --git a/test/integration/connection-pool/test-helper.js b/test/integration/connection-pool/test-helper.js index 8345ea1..cc86677 100644 --- a/test/integration/connection-pool/test-helper.js +++ b/test/integration/connection-pool/test-helper.js @@ -1,18 +1,15 @@ var helper = require(__dirname + "/../test-helper"); -var pg = require(__dirname + "/../../../lib"); -helper.pg = pg; -var testPoolSize = function(max) { - var conString = helper.connectionString(); +helper.testPoolSize = function(max) { var sink = new helper.Sink(max, function() { - helper.pg.end(conString); + helper.pg.end(); }); test("can pool " + max + " times", function() { for(var i = 0; i < max; i++) { helper.pg.poolSize = 10; test("connection #" + i + " executes", function() { - helper.pg.connect(conString, function(err, client) { + helper.pg.connect(helper.config, function(err, client) { assert.isNull(err); client.query("select * from person", function(err, result) { assert.lengthIs(result.rows, 26) @@ -30,11 +27,5 @@ var testPoolSize = function(max) { }) } -module.exports = { - args: helper.args, - pg: helper.pg, - connectionString: helper.connectionString, - Sink: helper.Sink, - testPoolSize: testPoolSize -} +module.exports = helper; diff --git a/test/integration/connection-pool/unique-name-tests.js b/test/integration/connection-pool/unique-name-tests.js index 84dda43..6893fbd 100644 --- a/test/integration/connection-pool/unique-name-tests.js +++ b/test/integration/connection-pool/unique-name-tests.js @@ -7,35 +7,25 @@ helper.pg.defaults.database = helper.args.database; helper.pg.defaults.port = helper.args.port; helper.pg.defaults.host = helper.args.host; helper.pg.defaults.poolIdleTimeout = 100; -var args = { - user: helper.args.user, - password: helper.args.password, - database: helper.args.database, - port: helper.args.port, - host: helper.args.host + +var moreArgs = {}; +for (c in helper.config) { + moreArgs[c] = helper.config[c]; +} +moreArgs.zomg = true; + +var badArgs = {}; +for (c in helper.config) { + badArgs[c] = helper.config[c]; } -var moreArgs = { - database: helper.args.database, - password: helper.args.password, - 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 -} +badArgs.user = badArgs.user + 'laksdjfl'; +badArgs.password = badArgs.password + 'asldkfjlas'; +badArgs.zomg = true; 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); client.iGotAccessed = true; client.query("SELECT NOW()") diff --git a/test/native/callback-api-tests.js b/test/native/callback-api-tests.js index 56b2326..4500668 100644 --- a/test/native/callback-api-tests.js +++ b/test/native/callback-api-tests.js @@ -1,9 +1,8 @@ var helper = require(__dirname + "/../test-helper"); var Client = require(__dirname + "/../../lib/native"); -var conString = helper.connectionString(); test('fires callback with results', function() { - var client = new Client(conString); + var client = new Client(helper.config); client.connect(); client.query('SELECT 1 as num', assert.calls(function(err, result) { assert.isNull(err); diff --git a/test/native/connection-tests.js b/test/native/connection-tests.js index e1641a4..1cb0ed8 100644 --- a/test/native/connection-tests.js +++ b/test/native/connection-tests.js @@ -12,7 +12,7 @@ test('connecting with wrong parameters', function() { }); test('connects', function() { - var con = new Client(helper.connectionString()); + var con = new Client(helper.config); con.connect(); assert.emits(con, 'connect', function() { test('disconnects', function() { diff --git a/test/native/error-tests.js b/test/native/error-tests.js index 9d196e6..3184df5 100644 --- a/test/native/error-tests.js +++ b/test/native/error-tests.js @@ -1,9 +1,8 @@ var helper = require(__dirname + "/../test-helper"); var Client = require(__dirname + "/../../lib/native"); -var conString = helper.connectionString(); test('query with non-text as first parameter throws error', function() { - var client = new Client(conString); + var client = new Client(helper.config); client.connect(); assert.emits(client, 'connect', 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() { - var client = new Client(conString); + var client = new Client(helper.config); client.connect(); assert.emits(client, 'connect', 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 client = new Client(conString); + var client = new Client(helper.config); client.connect(); assert.emits(client, 'connect', function() { callback(client); diff --git a/test/native/evented-api-tests.js b/test/native/evented-api-tests.js index d70c824..db93f5b 100644 --- a/test/native/evented-api-tests.js +++ b/test/native/evented-api-tests.js @@ -1,9 +1,8 @@ var helper = require(__dirname + "/../test-helper"); var Client = require(__dirname + "/../../lib/native"); -var conString = helper.connectionString(); var setupClient = function() { - var client = new Client(conString); + var client = new Client(helper.config); client.connect(); client.query("CREATE TEMP TABLE boom(name varchar(10), age integer)"); client.query("INSERT INTO boom(name, age) VALUES('Aaron', 26)"); @@ -12,7 +11,7 @@ var setupClient = function() { } test('connects', function() { - var client = new Client(conString); + var client = new Client(helper.config); client.connect(); test('good query', function() { var query = client.query("SELECT 1 as num, 'HELLO' as str"); diff --git a/test/native/stress-tests.js b/test/native/stress-tests.js index 539e667..cac03d0 100644 --- a/test/native/stress-tests.js +++ b/test/native/stress-tests.js @@ -2,7 +2,7 @@ var helper = require(__dirname + "/../test-helper"); var Client = require(__dirname + "/../../lib/native"); test('many rows', function() { - var client = new Client(helper.connectionString()); + var client = new Client(helper.config); client.connect(); var q = client.query("SELECT * FROM person"); var rows = []; @@ -16,7 +16,7 @@ test('many rows', function() { }); test('many queries', function() { - var client = new Client(helper.connectionString()); + var client = new Client(helper.config); client.connect(); var count = 0; var expected = 100; @@ -35,7 +35,7 @@ test('many queries', function() { test('many clients', function() { var clients = []; for(var i = 0; i < 10; i++) { - clients.push(new Client(helper.connectionString())); + clients.push(new Client(helper.config)); } clients.forEach(function(client) { client.connect(); diff --git a/test/test-helper.js b/test/test-helper.js index dbc28b5..55a0a5c 100644 --- a/test/test-helper.js +++ b/test/test-helper.js @@ -225,9 +225,7 @@ module.exports = { args: args, Sink: Sink, pg: require(__dirname + '/../lib/'), - connectionString: function() { - return "pg"+(count++)+"://"+args.user+":"+args.password+"@"+args.host+":"+args.port+"/"+args.database; - }, + config: args, sys: sys, Client: Client };