node-postgres/test/unit/communication-tests.js

143 lines
3.3 KiB
JavaScript
Raw Normal View History

require(__dirname+'/test-helper');
2010-10-08 09:00:49 +08:00
2010-10-07 10:34:51 +08:00
var buffers = require(__dirname+'/test-buffers');
test('client can take existing stream', function() {
var stream = new MemoryStream();
var client = new Client({
stream: stream
});
assert.equal(client.stream, stream);
});
2010-10-07 08:54:02 +08:00
test('using closed stream', function() {
var stream = new MemoryStream();
stream.readyState = 'closed';
stream.connect = function(port, host) {
this.connectCalled = true;
this.port = port;
this.host = host;
}
var client = new Client({
stream: stream,
host: 'bang',
port: 1234
});
client.connect();
test('makes stream connect', function() {
assert.equal(stream.connectCalled, true);
});
test('uses configured port', function() {
assert.equal(stream.port, 1234);
});
test('uses configured host', function() {
assert.equal(stream.host, 'bang');
});
2010-10-11 06:30:33 +08:00
test('after stream connects', function() {
stream.emit('connect');
//TODO - test more of the connection packets
2010-10-11 06:30:33 +08:00
});
2010-10-07 08:54:02 +08:00
});
test('using opened stream', function() {
var stream = new MemoryStream();
stream.readyState = 'open';
stream.connect = function() {
assert.ok(false, "Should not call open");
};
var client = new Client({stream: stream});
test('does not call open', function() {
client.connect();
});
});
2010-10-07 10:34:51 +08:00
test('query queue', function() {
2010-10-08 09:00:49 +08:00
2010-10-07 10:34:51 +08:00
var stream = new MemoryStream();
2010-10-08 09:00:49 +08:00
2010-10-07 10:34:51 +08:00
stream.readyState = 'open';
2010-10-08 09:00:49 +08:00
var client = new Client({
stream: stream
});
client.connect();
2010-10-08 08:38:27 +08:00
test('new client has empty queue', function() {
assert.empty(client.queryQueue);
});
test('calling query queues the query object', function() {
2010-10-09 12:17:09 +08:00
var query = client.query('!');
2010-10-08 08:38:27 +08:00
assert.length(client.queryQueue, 1);
});
2010-10-08 09:00:49 +08:00
test('sends query after stream emits ready for query packet', function() {
assert.empty(stream.packets);
var handled = stream.emit('data', buffers.readyForQuery());
assert.ok(handled, "Stream should have had data handled");
assert.length(stream.packets, 1);
assert.equalBuffers(stream.packets[0], [0x51,0,0,0,6,33,0])
2010-10-08 09:00:49 +08:00
});
2010-10-08 08:38:27 +08:00
2010-10-07 10:34:51 +08:00
});
2010-10-09 15:48:41 +08:00
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);
});
2010-10-11 06:30:33 +08:00
2010-10-09 15:48:41 +08:00
});