2010-10-24 06:36:04 +08:00
|
|
|
require(__dirname + "/test-helper");
|
2011-08-29 15:35:08 +08:00
|
|
|
var Connection = require(__dirname + '/../../../lib/connection');
|
2010-10-24 06:36:04 +08:00
|
|
|
var stream = new MemoryStream();
|
|
|
|
var con = new Connection({
|
|
|
|
stream: stream
|
|
|
|
});
|
|
|
|
|
2011-04-21 11:31:04 +08:00
|
|
|
assert.received = function(stream, buffer) {
|
2011-10-11 08:21:06 +08:00
|
|
|
assert.lengthIs(stream.packets, 1);
|
2010-10-24 06:36:04 +08:00
|
|
|
var packet = stream.packets.pop();
|
|
|
|
assert.equalBuffers(packet, buffer);
|
|
|
|
};
|
|
|
|
|
|
|
|
test("sends startup message", function() {
|
2010-10-25 10:28:10 +08:00
|
|
|
con.startup({
|
2010-10-24 06:36:04 +08:00
|
|
|
user: 'brian',
|
|
|
|
database: 'bang'
|
|
|
|
});
|
2011-04-21 11:31:04 +08:00
|
|
|
assert.received(stream, new BufferList()
|
2010-10-24 06:36:04 +08:00
|
|
|
.addInt16(3)
|
|
|
|
.addInt16(0)
|
|
|
|
.addCString('user')
|
|
|
|
.addCString('brian')
|
|
|
|
.addCString('database')
|
|
|
|
.addCString('bang')
|
|
|
|
.addCString('').join(true))
|
|
|
|
});
|
|
|
|
|
2010-10-25 10:26:18 +08:00
|
|
|
test('sends password message', function() {
|
|
|
|
con.password("!");
|
2011-04-21 11:31:04 +08:00
|
|
|
assert.received(stream, new BufferList().addCString("!").join(true,'p'));
|
2010-10-24 08:02:13 +08:00
|
|
|
});
|
|
|
|
|
2010-10-24 06:36:04 +08:00
|
|
|
test('sends query message', function() {
|
|
|
|
var txt = 'select * from boom';
|
|
|
|
con.query(txt);
|
2011-04-21 11:31:04 +08:00
|
|
|
assert.received(stream, new BufferList().addCString(txt).join(true,'Q'));
|
2010-10-24 06:36:04 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('sends parse message', function() {
|
|
|
|
con.parse({text: '!'});
|
|
|
|
var expected = new BufferList()
|
|
|
|
.addCString("")
|
|
|
|
.addCString("!")
|
|
|
|
.addInt16(0).join(true, 'P');
|
2011-04-21 11:31:04 +08:00
|
|
|
assert.received(stream, expected);
|
2010-10-24 06:36:04 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('sends parse message with named query', function() {
|
|
|
|
con.parse({
|
|
|
|
name: 'boom',
|
|
|
|
text: 'select * from boom',
|
|
|
|
types: []
|
|
|
|
});
|
|
|
|
var expected = new BufferList()
|
|
|
|
.addCString("boom")
|
|
|
|
.addCString("select * from boom")
|
|
|
|
.addInt16(0).join(true,'P');
|
2011-04-21 11:31:04 +08:00
|
|
|
assert.received(stream, expected);
|
2010-10-24 13:18:48 +08:00
|
|
|
|
|
|
|
test('with multiple parameters', function() {
|
|
|
|
con.parse({
|
|
|
|
name: 'force',
|
|
|
|
text: 'select * from bang where name = $1',
|
|
|
|
types: [1, 2, 3 ,4]
|
|
|
|
});
|
|
|
|
var expected = new BufferList()
|
|
|
|
.addCString("force")
|
|
|
|
.addCString("select * from bang where name = $1")
|
|
|
|
.addInt16(4)
|
|
|
|
.addInt32(1)
|
|
|
|
.addInt32(2)
|
|
|
|
.addInt32(3)
|
|
|
|
.addInt32(4).join(true,'P');
|
2011-04-21 11:31:04 +08:00
|
|
|
assert.received(stream, expected);
|
2010-10-24 13:18:48 +08:00
|
|
|
});
|
2010-10-24 06:36:04 +08:00
|
|
|
});
|
|
|
|
|
2010-10-25 02:46:50 +08:00
|
|
|
test('bind messages', function() {
|
|
|
|
test('with no values', function() {
|
|
|
|
con.bind();
|
2010-10-24 06:36:04 +08:00
|
|
|
|
2010-10-25 02:46:50 +08:00
|
|
|
var expectedBuffer = new BufferList()
|
|
|
|
.addCString("")
|
|
|
|
.addCString("")
|
|
|
|
.addInt16(0)
|
|
|
|
.addInt16(0)
|
|
|
|
.addInt16(0)
|
|
|
|
.join(true,"B");
|
2011-04-21 11:31:04 +08:00
|
|
|
assert.received(stream, expectedBuffer);
|
2010-10-25 02:46:50 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('with named statement, portal, and values', function() {
|
|
|
|
con.bind({
|
|
|
|
portal: 'bang',
|
|
|
|
statement: 'woo',
|
2012-07-06 11:08:18 +08:00
|
|
|
values: ['1', 'hi', null, 'zing']
|
2010-10-25 02:46:50 +08:00
|
|
|
});
|
|
|
|
var expectedBuffer = new BufferList()
|
|
|
|
.addCString('bang') //portal name
|
|
|
|
.addCString('woo') //statement name
|
|
|
|
.addInt16(0)
|
|
|
|
.addInt16(4)
|
|
|
|
.addInt32(1)
|
|
|
|
.add(Buffer("1"))
|
|
|
|
.addInt32(2)
|
|
|
|
.add(Buffer("hi"))
|
|
|
|
.addInt32(-1)
|
|
|
|
.addInt32(4)
|
|
|
|
.add(Buffer('zing'))
|
|
|
|
.addInt16(0)
|
|
|
|
.join(true, 'B');
|
2011-04-21 11:31:04 +08:00
|
|
|
assert.received(stream, expectedBuffer);
|
2010-10-25 02:46:50 +08:00
|
|
|
});
|
2010-10-24 06:36:04 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2010-10-25 02:46:50 +08:00
|
|
|
test("sends execute message", function() {
|
|
|
|
|
|
|
|
test("for unamed portal with no row limit", function() {
|
|
|
|
con.execute();
|
|
|
|
var expectedBuffer = new BufferList()
|
|
|
|
.addCString('')
|
|
|
|
.addInt32(0)
|
|
|
|
.join(true,'E');
|
2011-04-21 11:31:04 +08:00
|
|
|
assert.received(stream, expectedBuffer);
|
2010-10-25 02:46:50 +08:00
|
|
|
});
|
2010-10-24 06:36:04 +08:00
|
|
|
|
2010-10-25 02:46:50 +08:00
|
|
|
test("for named portal with row limit", function() {
|
|
|
|
con.execute({
|
|
|
|
portal: 'my favorite portal',
|
|
|
|
rows: 100
|
|
|
|
});
|
|
|
|
var expectedBuffer = new BufferList()
|
|
|
|
.addCString("my favorite portal")
|
|
|
|
.addInt32(100)
|
|
|
|
.join(true, 'E');
|
2011-04-21 11:31:04 +08:00
|
|
|
assert.received(stream, expectedBuffer);
|
2010-10-25 02:46:50 +08:00
|
|
|
});
|
|
|
|
});
|
2010-10-24 06:36:04 +08:00
|
|
|
|
|
|
|
test('sends flush command', function() {
|
|
|
|
con.flush();
|
|
|
|
var expected = new BufferList().join(true, 'H');
|
2011-04-21 11:31:04 +08:00
|
|
|
assert.received(stream, expected);
|
2010-10-24 06:36:04 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('sends sync command', function() {
|
|
|
|
con.sync();
|
|
|
|
var expected = new BufferList().join(true,'S');
|
2011-04-21 11:31:04 +08:00
|
|
|
assert.received(stream, expected);
|
2010-10-24 06:36:04 +08:00
|
|
|
});
|
2010-10-24 08:28:57 +08:00
|
|
|
|
|
|
|
test('sends end command', function() {
|
|
|
|
con.end();
|
|
|
|
var expected = new Buffer([0x58, 0, 0, 0, 4]);
|
2011-04-21 11:31:04 +08:00
|
|
|
assert.received(stream, expected);
|
2010-10-24 08:28:57 +08:00
|
|
|
});
|
2010-10-28 13:27:08 +08:00
|
|
|
|
|
|
|
test('sends describe command',function() {
|
|
|
|
test('describe statement', function() {
|
2010-10-28 13:50:45 +08:00
|
|
|
con.describe({type: 'S', name: 'bang'});
|
|
|
|
var expected = new BufferList().addChar('S').addCString('bang').join(true, 'D')
|
2011-04-21 11:31:04 +08:00
|
|
|
assert.received(stream, expected);
|
2010-10-28 13:50:45 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test("describe unnamed portal", function() {
|
|
|
|
con.describe({type: 'P'});
|
|
|
|
var expected = new BufferList().addChar('P').addCString("").join(true, "D");
|
2011-04-21 11:31:04 +08:00
|
|
|
assert.received(stream, expected);
|
2010-10-28 13:27:08 +08:00
|
|
|
});
|
|
|
|
});
|