node-postgres/test/parser-tests.js

137 lines
3.2 KiB
JavaScript
Raw Normal View History

require(__dirname+'/test-helper');
2010-09-29 15:46:10 +08:00
test('Parser on single messages', function() {
2010-09-30 13:14:41 +08:00
2010-09-29 15:46:10 +08:00
var authOkBuffer = new BufferList()
.addInt32(8)
.join(true, 'R');
var paramStatusBuffer = new BufferList()
.addCString("client_encoding")
.addCString("UTF8")
.join(true, 'S');
2010-09-30 13:14:41 +08:00
2010-09-29 15:46:10 +08:00
var backendKeyDataBuffer = new BufferList()
.addInt32(1)
.addInt32(2)
.join(true,'K');
2010-09-30 13:14:41 +08:00
2010-09-30 13:36:07 +08:00
2010-09-29 15:46:10 +08:00
var readyForQueryBuffer = new BufferList()
.add(Buffer('I'))
.join(true,'Z');
2010-09-30 13:36:07 +08:00
var expectedAuthenticationOkayMessage = {
name: 'AuthenticationOk',
id: 'R',
length: 8
};
2010-09-29 15:46:10 +08:00
2010-09-30 13:36:07 +08:00
var expectedParameterStatusMessage = {
2010-09-29 15:46:10 +08:00
name: 'ParameterStatus',
id: 'S',
length: 25,
parameterName: 'client_encoding',
parameterValue: 'UTF8'
2010-09-30 13:36:07 +08:00
};
2010-09-29 13:20:10 +08:00
2010-09-30 13:36:07 +08:00
var expectedBackendKeyDataMessage = {
2010-09-29 15:46:10 +08:00
name: 'BackendKeyData',
id: 'K',
processID: 1,
secretKey: 2
2010-09-30 13:36:07 +08:00
};
2010-09-30 13:14:41 +08:00
2010-09-30 13:36:07 +08:00
var expectedReadyForQueryMessage = {
2010-09-29 15:46:10 +08:00
name: 'ReadyForQuery',
id: 'Z',
length: 5,
status: 'I'
2010-09-30 13:36:07 +08:00
};
test('parses AuthenticationOk message', function() {
2010-09-29 15:46:10 +08:00
var result = new Parser(authOkBuffer).parse()[0];
2010-09-30 13:36:07 +08:00
assert.same(result, expectedAuthenticationOkayMessage);
});
test('parses ParameterStatus message', function() {
2010-09-29 15:46:10 +08:00
var result = new Parser(paramStatusBuffer).parse()[0];
2010-09-30 13:36:07 +08:00
assert.same(result, expectedParameterStatusMessage);
});
test('parses BackendKeyData message', function() {
2010-09-30 13:40:06 +08:00
var result = new Parser(backendKeyDataBuffer).parse()[0];
2010-09-30 13:36:07 +08:00
assert.same(result, expectedBackendKeyDataMessage);
});
test('parses ReadyForQuery message', function() {
2010-09-30 13:40:06 +08:00
var result = new Parser(readyForQueryBuffer).parse()[0];
2010-09-30 13:36:07 +08:00
assert.same(result, expectedReadyForQueryMessage);
2010-09-30 13:14:41 +08:00
});
2010-09-29 14:01:52 +08:00
2010-09-29 13:20:10 +08:00
test('parses normal CString', function() {
2010-09-29 13:30:35 +08:00
var result = new Parser(Buffer([33,0])).parseCString();
2010-09-29 13:20:10 +08:00
assert.equal(result,"!");
});
2010-09-29 15:46:10 +08:00
2010-09-29 14:01:52 +08:00
var resultText = stringToHex("SELECT 3\0");
var length = resultText.length + 4;
var commandCompleteData = [0x43, 0, 0, 0, length].concat(resultText);
2010-09-29 15:46:10 +08:00
2010-09-29 14:01:52 +08:00
test('parses CommandComplete message', function() {
var result = new Parser(Buffer(commandCompleteData)).parse()[0];
assert.same(result, {
length: 13,
id: 'C',
text: "SELECT 3"
});
});
2010-09-29 13:20:10 +08:00
2010-09-29 15:46:10 +08:00
var packet = {
BYTE: 'T',
LENGTH: null,
INT16: 0
};
var x = [0x54, 0, 0, 0, 26, 0, 1, 33, 0, 0, 0, 0, 2, 0, 3, 0, 0, 0, 4, 0, 5, 0, 6, 0, 0];
test('parses RowDescriptions', function() {
test('parses empty row description', function() {
var buffer = Buffer([0x54, 0, 0, 0, 6, 0, 0]);
var result = new Parser(buffer).parse()[0];
assert.same(result, {
name: 'RowDescription',
id: 'T',
length: 6,
rowCount: 0
});
assert.equal(result.rows.length, 0);
});
});
2010-09-29 13:20:10 +08:00
test('parses empty CString', function() {
2010-09-29 13:30:35 +08:00
var result = new Parser(Buffer([0])).parseCString();
2010-09-29 13:20:10 +08:00
assert.equal(result, '');
});
2010-09-29 13:30:35 +08:00
test('parses length', function() {
var parser = new Parser(Buffer([0,0,0,3]));
var result = parser.parseLength();
assert.equal(result, 3);
assert.equal(parser.offset, 4);
});
2010-09-29 13:43:28 +08:00
test('parsing empty buffer returns false', function() {
var parser = new Parser(Buffer(0));
assert.equal(parser.parse(), false);
});
});