node-postgres/test/unit/simple-query-tests.js

59 lines
1.2 KiB
JavaScript
Raw Normal View History

2010-10-11 11:42:13 +08:00
require(__dirname+'/test-helper');
var dataTypes = {
char: 18
};
test('simple query', 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++;
});
2010-10-18 05:59:33 +08:00
stream.emit('data', buffers.dataRow(["!"]));
2010-10-11 11:42:13 +08:00
test('row has correct data', function() {
2010-10-18 05:59:33 +08:00
assert.length(rowData, 1);
2010-10-11 11:42:13 +08:00
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());
2010-10-18 05:59:33 +08:00
assert.length(rowData, 1);
2010-10-11 11:42:13 +08:00
assert.equal(ended, 1);
});
});