From 80ee829ba0afe8b5fbd49fa3a0ef03cc1b20581f Mon Sep 17 00:00:00 2001 From: brianc Date: Sun, 24 Oct 2010 23:32:18 -0500 Subject: [PATCH] testing out the query interface --- lib/client.js | 34 +++++++++++++++--- test/unit/client/simple-query-tests.js | 50 +++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 6 deletions(-) diff --git a/lib/client.js b/lib/client.js index 9eb38699..b851c38f 100644 --- a/lib/client.js +++ b/lib/client.js @@ -67,22 +67,46 @@ p.pulseQueryQueue = function() { if(this.readyForQuery===true && this.queryQueue.length > 0) { this.readyForQuery = false; var query = this.queryQueue.shift(); - if(typeof query === 'string') { - this.connection.query(query); - } + query.submit(this.connection); } }; p.query = function(config) { - - this.queryQueue.push(config); + var query = new Query({text: config}); + this.queryQueue.push(query); this.pulseQueryQueue(); + return query; }; Client.md5 = function(string) { return crypto.createHash('md5').update(string).digest('hex'); }; +var Query = function(config) { + this.text = config.text; + EventEmitter.call(this); +}; +sys.inherits(Query, EventEmitter);p +var p = Query.prototype; + +p.submit = function(connection) { + var self = this; + connection.query(this.text); + var handleRowDescription = function(msg) { + + }; + var handleDatarow = function(msg) { + self.emit('row', msg); + }; + connection.on('rowDescription', handleRowDescription); + connection.on('dataRow', handleDatarow); + connection.once('readyForQuery', function() { + //remove all listeners + connection.removeListener('rowDescription', handleRowDescription); + connection.removeListener('dataRow', handleDatarow); + self.emit('end'); + }); +}; // var intParser = { // fromDbValue: parseInt // }; diff --git a/test/unit/client/simple-query-tests.js b/test/unit/client/simple-query-tests.js index 8970c838..98827e75 100644 --- a/test/unit/client/simple-query-tests.js +++ b/test/unit/client/simple-query-tests.js @@ -42,6 +42,7 @@ test('executing query', function() { }); }); + test("multiple in the queue", function() { var client = makeClient(); var connection = client.connection; @@ -72,6 +73,53 @@ test('executing query', function() { assert.equal(queries[2], 'three'); }); }); - }) + }); + + test("query event binding and flow", function() { + var client = makeClient(); + var con = client.connection; + var query = client.query('whatever'); + + test("has no queries sent before ready", function() { + assert.empty(con.queries); + }); + + test('sends query on readyForQuery event', function() { + con.emit('readyForQuery'); + assert.length(con.queries, 1); + assert.equal(con.queries[0], 'whatever'); + }); + + test('handles rowDescription message', function() { + var handled = con.emit('rowDescription',{}); + assert.ok(handled, "should have handlded rowDescritpion"); + }); + + test('handles dataRow messages', function() { + assert.raises(query, 'row', function(row) { + assert.equal(row.fields[0], "hi"); + }); + var handled = con.emit('dataRow', { fields: ["hi"] }); + assert.ok(handled, "should have handled first data row message"); + + assert.raises(query, 'row', function(row) { + assert.equal(row.fields[0], "bye"); + }); + var handledAgain = con.emit('dataRow', { fields: ["bye"] }); + assert.ok(handledAgain, "should have handled seciond data row message"); + + }); + + test('removes itself after another readyForQuery message', function() { + assert.raises(query, "end"); + con.emit("readyForQuery"); + //this would never actually happen + ['dataRow','rowDescritpion', 'commandComplete'].forEach(function(msg) { + assert.equal(con.emit(msg), false, "Should no longer be picking up '"+ msg +"' messages"); + }); + }); + + }); + });