From 1c43930ba1bb72cb0f4d6cd5c2d4c70ce14a16bd Mon Sep 17 00:00:00 2001 From: brianc Date: Mon, 10 Dec 2012 22:44:58 -0600 Subject: [PATCH] cleanup & fix failing tests to allow for green merge of pull #228 --- lib/client.js | 3 +- lib/query.js | 17 +++++- test/integration/client/cancel-query-tests.js | 60 +++++++++---------- test/integration/client/empty-query-tests.js | 4 +- test/integration/client/simple-query-tests.js | 8 +-- test/test-helper.js | 2 +- 6 files changed, 54 insertions(+), 40 deletions(-) diff --git a/lib/client.js b/lib/client.js index 581a673..c861daf 100644 --- a/lib/client.js +++ b/lib/client.js @@ -166,8 +166,9 @@ p.cancel = function(client, query) { con.cancel(client.processID, client.secretKey); }); } - else if (client.queryQueue.indexOf(query) != -1) + else if (client.queryQueue.indexOf(query) != -1) { client.queryQueue.splice(client.queryQueue.indexOf(query), 1); + } }; p._pulseQueryQueue = function() { diff --git a/lib/query.js b/lib/query.js index 16012ad..6bdc1b0 100644 --- a/lib/query.js +++ b/lib/query.js @@ -40,7 +40,18 @@ util.inherits(Query, EventEmitter); var p = Query.prototype; p.requiresPreparation = function() { - return (this.values || 0).length > 0 || this.name || this.rows || this.binary; + //named queries must always be prepared + if(this.name) return true; + //always prepare if there are max number of rows expected per + //portal execution + if(this.rows) return true; + //don't prepare empty text queries + if(!this.text) return false; + //binary should be prepared to specify results should be in binary + //unless there are no parameters + if(this.binary && !this.values) return false; + //prepare if there are values + return (this.values || 0).length > 0; }; @@ -139,7 +150,9 @@ p.prepare = function(connection) { name: self.name, types: self.types }, true); - connection.parsedStatements[this.name] = true; + if(this.name) { + connection.parsedStatements[this.name] = true; + } } //TODO is there some better way to prepare values for the database? diff --git a/test/integration/client/cancel-query-tests.js b/test/integration/client/cancel-query-tests.js index 842b471..80b05b2 100644 --- a/test/integration/client/cancel-query-tests.js +++ b/test/integration/client/cancel-query-tests.js @@ -5,42 +5,42 @@ test("cancellation of a query", function() { var client = helper.client(); - var qry = client.query("select name from person order by name"); + var qry = "select name from person order by name"; client.on('drain', client.end.bind(client)); - var rows1 = 0, rows2 = 0, rows3 = 0, rows4 = 0; + var rows1 = 0, rows2 = 0, rows3 = 0, rows4 = 0; - var query1 = client.query(qry); - query1.on('row', function(row) { - rows1++; - }); - var query2 = client.query(qry); - query2.on('row', function(row) { - rows2++; - }); - var query3 = client.query(qry); - query3.on('row', function(row) { - rows3++; - }); - var query4 = client.query(qry); - query4.on('row', function(row) { - rows4++; - }); + var query1 = client.query(qry); + query1.on('row', function(row) { + rows1++; + }); + var query2 = client.query(qry); + query2.on('row', function(row) { + rows2++; + }); + var query3 = client.query(qry); + query3.on('row', function(row) { + rows3++; + }); + var query4 = client.query(qry); + query4.on('row', function(row) { + rows4++; + }); - helper.pg.cancel(helper.config, client, query1); - helper.pg.cancel(helper.config, client, query2); - helper.pg.cancel(helper.config, 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); - assert.equal(rows2, 0); - assert.equal(rows4, 0); - }, 2000); + setTimeout(function() { + assert.equal(rows1, 0); + assert.equal(rows2, 0); + assert.equal(rows4, 0); + }, 2000); assert.emits(query3, 'end', function() { - test("returned right number of rows", function() { - assert.equal(rows3, 26); - }); - }); + test("returned right number of rows", function() { + assert.equal(rows3, 26); + }); + }); }); diff --git a/test/integration/client/empty-query-tests.js b/test/integration/client/empty-query-tests.js index 3eb207c..6f0d574 100644 --- a/test/integration/client/empty-query-tests.js +++ b/test/integration/client/empty-query-tests.js @@ -5,11 +5,11 @@ test("empty query message handling", function() { assert.emits(client, 'drain', function() { client.end(); }); - client.query({text: "", binary: false}); + client.query({text: ""}); }); test('callback supported', assert.calls(function() { - client.query({text: "", binary: false}, function(err, result) { + client.query("", function(err, result) { assert.isNull(err); assert.empty(result.rows); }) diff --git a/test/integration/client/simple-query-tests.js b/test/integration/client/simple-query-tests.js index 2e77910..f8ef1ad 100644 --- a/test/integration/client/simple-query-tests.js +++ b/test/integration/client/simple-query-tests.js @@ -38,7 +38,7 @@ test("simple query interface", function() { test("multiple simple queries", function() { var client = helper.client(); - client.query({ text: "create temp table bang(id serial, name varchar(5));insert into bang(name) VALUES('boom');", binary: false }) + client.query({ text: "create temp table bang(id serial, name varchar(5));insert into bang(name) VALUES('boom');"}) client.query("insert into bang(name) VALUES ('yes');"); var query = client.query("select name from bang"); assert.emits(query, 'row', function(row) { @@ -52,9 +52,9 @@ test("multiple simple queries", function() { test("multiple select statements", function() { var client = helper.client(); - client.query({text: "create temp table boom(age integer); insert into boom(age) values(1); insert into boom(age) values(2); insert into boom(age) values(3)", binary: false}); - client.query({text: "create temp table bang(name varchar(5)); insert into bang(name) values('zoom');", binary: false}); - var result = client.query({text: "select age from boom where age < 2; select name from bang", binary: false}); + client.query("create temp table boom(age integer); insert into boom(age) values(1); insert into boom(age) values(2); insert into boom(age) values(3)"); + client.query({text: "create temp table bang(name varchar(5)); insert into bang(name) values('zoom');"}); + var result = client.query({text: "select age from boom where age < 2; select name from bang"}); assert.emits(result, 'row', function(row) { assert.strictEqual(row['age'], 1); assert.emits(result, 'row', function(row) { diff --git a/test/test-helper.js b/test/test-helper.js index ed0fdcc..4ad7b7b 100644 --- a/test/test-helper.js +++ b/test/test-helper.js @@ -28,7 +28,7 @@ assert.same = function(actual, expected) { assert.emits = function(item, eventName, callback, message) { var called = false; var id = setTimeout(function() { - test("Should have called " + eventName, function() { + test("Should have called '" + eventName + "' event", function() { assert.ok(called, message || "Expected '" + eventName + "' to be called.") }); },5000);