refactored tests to use BufferList

This commit is contained in:
brianc 2010-09-29 02:46:10 -05:00
parent bf03dbf3f8
commit 7478b74d9a

View File

@ -1,19 +1,29 @@
require(__dirname+'/test-helper');
test('Parser on single messages', function() {
var authOkData = [0x52, 00, 00, 00, 08, 00, 00, 00, 00];
var authenticationOkBuffer = Buffer(authOkData);
var firstString = [0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0];
var secondString = [0x55, 0x54, 0x46, 0x38, 0];
var paramStatusData = [0x53, 0, 0, 0, 0x19].concat(firstString).concat(secondString);
var parameterStatusBuffer = Buffer(paramStatusData);
var authOkBuffer = new BufferList()
.addInt32(8)
.join(true, 'R');
var backendKeyData = [0x4b, 0, 0, 0, 0x0c, 0, 0, 0, 1, 0, 0, 0, 2];
var backendKeyDataBuffer = Buffer(backendKeyData);
var readyForQueryData = [0x5a, 0, 0, 0, 5, 'I'.charCodeAt(0)];
var readyForQueryBuffer = Buffer(readyForQueryData)
var paramStatusBuffer = new BufferList()
.addCString("client_encoding")
.addCString("UTF8")
.join(true, 'S');
var backendKeyDataBuffer = new BufferList()
.addInt32(1)
.addInt32(2)
.join(true,'K');
var readyForQueryBuffer = new BufferList()
.add(Buffer('I'))
.join(true,'Z');
var expectedAuthenticationOkayMessage = {
name: 'AuthenticationOk',
@ -44,12 +54,12 @@ test('Parser on single messages', function() {
};
test('parses AuthenticationOk message', function() {
var result = new Parser(authenticationOkBuffer).parse()[0];
var result = new Parser(authOkBuffer).parse()[0];
assert.same(result, expectedAuthenticationOkayMessage);
});
test('parses ParameterStatus message', function() {
var result = new Parser(parameterStatusBuffer).parse()[0];
var result = new Parser(paramStatusBuffer).parse()[0];
assert.same(result, expectedParameterStatusMessage);
});
@ -63,19 +73,6 @@ test('Parser on single messages', function() {
assert.same(result, expectedReadyForQueryMessage);
});
test('parses multiple messages', function() {
var message = authOkData
.concat(paramStatusData)
.concat(backendKeyData)
.concat(readyForQueryData);
var buffer = Buffer(message);
var result = new Parser(buffer).parse();
assert.equal(result.length, 4);
assert.same(result[0], expectedAuthenticationOkayMessage);
assert.same(result[1], expectedParameterStatusMessage);
assert.same(result[2], expectedBackendKeyDataMessage);
assert.same(result[3], expectedReadyForQueryMessage);
});
test('parses normal CString', function() {
var result = new Parser(Buffer([33,0])).parseCString();
@ -95,6 +92,30 @@ test('Parser on single messages', function() {
});
});
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);
});
});
test('parses empty CString', function() {
var result = new Parser(Buffer([0])).parseCString();
assert.equal(result, '');