From 98856f0e9c8a710d156229d37707fd65afb15a38 Mon Sep 17 00:00:00 2001 From: brianc Date: Sat, 9 Oct 2010 02:48:41 -0500 Subject: [PATCH] simple query scenario tested --- lib/index.js | 22 +++++++++++--- test/communication-tests.js | 58 +++++++++++++++++++++++++++++++++++++ test/test-helper.js | 10 +++++-- 3 files changed, 84 insertions(+), 6 deletions(-) diff --git a/lib/index.js b/lib/index.js index cc8b3f0f..289ebe11 100644 --- a/lib/index.js +++ b/lib/index.js @@ -56,6 +56,7 @@ Client.prototype.connect = function() { }); this.on('ReadyForQuery', function() { + self.readyForQuery = true; self.pulseQueryQueue(); }); }; @@ -70,22 +71,35 @@ Client.prototype.query = function(text) { query.client = this; query.text = text; this.queryQueue.push(query); + this.pulseQueryQueue(); return query; }; Client.prototype.pulseQueryQueue = function() { + if(!this.readyForQuery) { + return; + }; var query = this.queryQueue.shift(); if(query) { + var self = this; + this.readyForQuery = false; this.stream.write(query.toBuffer()); + var rowHandler = function(msg) { + query.emit('row',msg.fields) + }; + var endHandler; + endHandler = function(msg) { + query.emit('end'); + self.removeListener('CommandComplete', endHandler); + self.removeListener('DataRow', rowHandler); + }; + this.on('DataRow', rowHandler); + this.on('CommandComplete', endHandler); } }; var Query = function() { EventEmitter.call(this); - var self = this; - process.nextTick(function() { - self.emit('end'); - }); }; sys.inherits(Query, EventEmitter); diff --git a/test/communication-tests.js b/test/communication-tests.js index dd4af00c..a69254fe 100644 --- a/test/communication-tests.js +++ b/test/communication-tests.js @@ -76,3 +76,61 @@ test('query queue', function() { }); }); + +var dataTypes = { + char: 18 +}; + +test('simple query scenario', function() { + var stream = new MemoryStream(); + stream.readyState = 'open'; + var client = new Client({ + stream: stream + }); + client.connect(); + assert.ok(stream.emit('data', buffers.readyForQuery())); + + var query = client.query('!'); + test('stream got packet', function() { + assert.length(stream.packets, 1); + }); + + stream.emit('data', buffers.rowDescription([{ + name: 'id', + dataTypeID: dataTypes.char, + dataTypeSize: 1 + }])); + + var rowData = []; + query.on('row',function(data) { + rowData = data; + }); + + var ended = 0; + query.on('end', function() { + ended++; + }); + + stream.emit('data', buffers.dataRow(["!"])); + + test('row has one item', function() { + assert.length(rowData, 1); + }); + + test('row has correct data', function() { + assert.equal(rowData[0], "!"); + }); + + + test('query ends', function() { + stream.emit('data', buffers.commandComplete()); + assert.equal(ended, 1); + }); + + test('after query is ended, it emits nothing else', function() { + stream.emit('data', buffers.dataRow(["X","Y","Z"])); + stream.emit('data', buffers.commandComplete()); + assert.length(rowData, 1); + assert.equal(ended, 1); + }); +}); diff --git a/test/test-helper.js b/test/test-helper.js index 62cb0c7c..c526c6d9 100644 --- a/test/test-helper.js +++ b/test/test-helper.js @@ -39,8 +39,14 @@ test = function(name, action) { name = ' ' + name; } test.tabout += 2; - console.log(name); - action(); + process.stdout.write('.'); + try{ + action(); + }catch(e) { + console.log(name); + console.log(e.toString()); + } + test.tabout -= 2; };