node-postgres/test/unit/connection/outbound-sending-tests.js

173 lines
4.3 KiB
JavaScript
Raw Normal View History

2010-10-24 06:36:04 +08:00
require(__dirname + "/test-helper");
2011-02-24 09:50:43 +08:00
var Connection = require('connection');
2010-10-24 06:36:04 +08:00
var stream = new MemoryStream();
var con = new Connection({
stream: stream
});
assert.recieved = function(stream, buffer) {
assert.length(stream.packets, 1);
var packet = stream.packets.pop();
assert.equalBuffers(packet, buffer);
};
test("sends startup message", function() {
con.startup({
2010-10-24 06:36:04 +08:00
user: 'brian',
database: 'bang'
});
assert.recieved(stream, new BufferList()
.addInt16(3)
.addInt16(0)
.addCString('user')
.addCString('brian')
.addCString('database')
.addCString('bang')
.addCString('').join(true))
});
test('sends password message', function() {
con.password("!");
2010-10-24 08:02:13 +08:00
assert.recieved(stream, new BufferList().addCString("!").join(true,'p'));
});
2010-10-24 06:36:04 +08:00
test('sends query message', function() {
var txt = 'select * from boom';
con.query(txt);
assert.recieved(stream, new BufferList().addCString(txt).join(true,'Q'));
});
test('sends parse message', function() {
con.parse({text: '!'});
var expected = new BufferList()
.addCString("")
.addCString("!")
.addInt16(0).join(true, 'P');
assert.recieved(stream, expected);
});
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');
assert.recieved(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');
assert.recieved(stream, expected);
});
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");
assert.recieved(stream, expectedBuffer);
});
test('with named statement, portal, and values', function() {
con.bind({
portal: 'bang',
statement: 'woo',
values: [1, 'hi', null, 'zing']
});
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');
assert.recieved(stream, expectedBuffer);
});
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');
assert.recieved(stream, expectedBuffer);
});
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');
assert.recieved(stream, expectedBuffer);
});
});
2010-10-24 06:36:04 +08:00
test('sends flush command', function() {
con.flush();
var expected = new BufferList().join(true, 'H');
assert.recieved(stream, expected);
});
test('sends sync command', function() {
con.sync();
var expected = new BufferList().join(true,'S');
assert.recieved(stream, expected);
});
2010-10-24 08:28:57 +08:00
test('sends end command', function() {
con.end();
var expected = new Buffer([0x58, 0, 0, 0, 4]);
assert.recieved(stream, expected);
});
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')
assert.recieved(stream, expected);
});
test("describe unnamed portal", function() {
con.describe({type: 'P'});
var expected = new BufferList().addChar('P').addCString("").join(true, "D");
2010-10-28 13:27:08 +08:00
assert.recieved(stream, expected);
});
});