testing out the query interface

This commit is contained in:
brianc 2010-10-24 23:32:18 -05:00
parent cea0b49eaa
commit 80ee829ba0
2 changed files with 78 additions and 6 deletions

View File

@ -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
// };

View File

@ -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");
});
});
});
});