From fb463923d8a29f546f8aa0c5d54519d621cb9b2b Mon Sep 17 00:00:00 2001 From: Brian Carlson Date: Tue, 14 Dec 2010 19:23:01 -0600 Subject: [PATCH] test refactoring --- test/integration/client/simple-query-tests.js | 1 - test/integration/client/test-helper.js | 3 +- .../integration/client/type-coercion-tests.js | 66 +++++++++++-------- .../connection-pool/test-helper.js | 17 +++-- test/test-helper.js | 1 + 5 files changed, 55 insertions(+), 33 deletions(-) diff --git a/test/integration/client/simple-query-tests.js b/test/integration/client/simple-query-tests.js index 99b07478..0452bc9f 100644 --- a/test/integration/client/simple-query-tests.js +++ b/test/integration/client/simple-query-tests.js @@ -51,4 +51,3 @@ test("multiple select statements", function() { }); client.on('drain', client.end.bind(client)); }); - diff --git a/test/integration/client/test-helper.js b/test/integration/client/test-helper.js index 6371171c..03ace913 100644 --- a/test/integration/client/test-helper.js +++ b/test/integration/client/test-helper.js @@ -14,5 +14,6 @@ module.exports = { return client; }, connectionString: helper.connectionString, - Sink: helper.Sink + Sink: helper.Sink, + pg: helper.pg }; diff --git a/test/integration/client/type-coercion-tests.js b/test/integration/client/type-coercion-tests.js index 09def7f6..53a2c9dc 100644 --- a/test/integration/client/type-coercion-tests.js +++ b/test/integration/client/type-coercion-tests.js @@ -1,37 +1,39 @@ var helper = require(__dirname + '/test-helper'); - -var client = helper.client(); -client.on('drain', client.end.bind(client)); - +var sink; +var connectionString = helper.connectionString(); var testForTypeCoercion = function(type){ - client.query("create temp table test_type(col " + type.name + ")"); + helper.pg.connect(connectionString, function(err, client) { + assert.isNull(err) + client.query("create temp table test_type(col " + type.name + ")"); - test("Coerces " + type.name, function() { - type.values.forEach(function(val) { + test("Coerces " + type.name, function() { + type.values.forEach(function(val) { - var insertQuery = client.query({ - name: 'insert type test ' + type.name, - text: 'insert into test_type(col) VALUES($1)', - values: [val] + var insertQuery = client.query({ + name: 'insert type test ' + type.name, + text: 'insert into test_type(col) VALUES($1)', + values: [val] + }); + + var query = client.query({ + name: 'get type ' + type.name , + text: 'select col from test_type' + }); + + assert.emits(query, 'row', function(row) { + assert.strictEqual(row.col, val, "expected " + type.name + " of " + val + " but got " + row[0]); + }); + + client.query({ + name: 'delete values', + text: 'delete from test_type' + }); + sink.add(); }); - var query = client.query({ - name: 'get type ' + type.name , - text: 'select col from test_type' - }); - - assert.emits(query, 'row', function(row) { - assert.strictEqual(row.col, val, "expected " + type.name + " of " + val + " but got " + row[0]); - }); - - client.query({ - name: 'delete values', - text: 'delete from test_type' - }); + client.query('drop table test_type'); }); - - client.query('drop table test_type'); - }); + }) }; var types = [{ @@ -76,9 +78,18 @@ var types = [{ values: ['13:12:12.321', null] }]; +var valueCount = 0; +types.forEach(function(type) { + valueCount += type.values.length; +}) +sink = new helper.Sink(valueCount, function() { + helper.pg.end(); +}) + types.forEach(testForTypeCoercion); test("timestampz round trip", function() { + var now = new Date(); var client = helper.client(); client.on('error', function(err) { @@ -112,6 +123,7 @@ test("timestampz round trip", function() { }); }); + client.on('drain', client.end.bind(client)); }); diff --git a/test/integration/connection-pool/test-helper.js b/test/integration/connection-pool/test-helper.js index aebea427..41a45fc2 100644 --- a/test/integration/connection-pool/test-helper.js +++ b/test/integration/connection-pool/test-helper.js @@ -11,10 +11,19 @@ var testPoolSize = function(max) { test("can pool " + max + " times", function() { for(var i = 0; i < max; i++) { helper.pg.poolSize = 10; - helper.pg.connect(conString, function(err, client) { - assert.isNull(err); - client.query("select * from NOW()", function() { - sink.add(); + test("connection #" + i + " executes", function() { + helper.pg.connect(conString, function(err, client) { + assert.isNull(err); + client.query("select * from person", function(err, result) { + assert.length(result.rows, 26) + }) + client.query("select count(*) as c from person", function(err, result) { + assert.equal(result.rows[0].c, 26) + }) + var query = client.query("SELECT * FROM NOW()") + query.on('end',function() { + sink.add() + }) }) }) } diff --git a/test/test-helper.js b/test/test-helper.js index 6d94b49b..7e32022b 100644 --- a/test/test-helper.js +++ b/test/test-helper.js @@ -173,6 +173,7 @@ var Sink = function(expected, timeout, callback) { module.exports = { args: args, Sink: Sink, + pg: require('index'), connectionString: function() { return "pg"+(count++)+"://"+args.user+":"+args.password+"@"+args.host+":"+args.port+"/"+args.database; }