2010-10-06 12:42:41 +08:00
|
|
|
require(__dirname+'/test-helper');
|
2010-10-07 10:34:51 +08:00
|
|
|
var buffers = require(__dirname+'/test-buffers');
|
2010-10-06 12:42:41 +08:00
|
|
|
|
|
|
|
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');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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() {
|
|
|
|
var stream = new MemoryStream();
|
|
|
|
stream.readyState = 'open';
|
|
|
|
var client = new Client({stream: stream});
|
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() {
|
|
|
|
var query = client.query('select * from bang');
|
|
|
|
assert.length(client.queryQueue, 1);
|
|
|
|
});
|
|
|
|
|
2010-10-07 10:34:51 +08:00
|
|
|
assert.empty(stream.packets);
|
2010-10-08 08:38:27 +08:00
|
|
|
|
2010-10-07 10:39:52 +08:00
|
|
|
stream.emit('data', buffers.readyForQuery());
|
2010-10-08 08:38:27 +08:00
|
|
|
assert.length(stream.packets, 1);
|
2010-10-07 10:34:51 +08:00
|
|
|
});
|