Skip to content

Commit

Permalink
Re-organising type cast tests, to use the same test cases for both qu…
Browse files Browse the repository at this point in the history
…ery and execute.

Added a readTimeString method to Packet prototype, which parses the TIME type in binary parser
Added geometry parser to compile_binary_parser.
Added tests for type casting using execute instead of query, but disabled to test for type TIME, as it is failing at the moment.
  • Loading branch information
danieljuhl committed Apr 29, 2014
1 parent 51ca791 commit c0b3e57
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 48 deletions.
4 changes: 4 additions & 0 deletions lib/compile_binary_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,13 @@ function readCodeFor(field) {
case Types.TIMESTAMP:
case Types.NEWDATE:
return "packet.readDateTime();";
case Types.TIME:
return "packet.readTimeString()";
case Types.DECIMAL:
case Types.NEWDECIMAL:
return "packet.readLengthCodedString();";
case Types.GEOMETRY:
return "packet.parseGeometryValue();";
case Types.LONGLONG: // TODO: 8 bytes. Implement as two 4 bytes read for now (it's out of JavaScript int precision!)
return unsigned ? "packet.readInt64();" : "packet.readSInt64();";
default:
Expand Down
34 changes: 34 additions & 0 deletions lib/packets/packet.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,40 @@ Packet.prototype.readTime = function(convertTtoMs) {
return d*86400000 + H*3600000 + M*60000 + S*1000 + ms;
};

// TIME - value as a string, Can be negative
Packet.prototype.readTimeString = function(convertTtoMs) {
var length = this.readInt8();
if (length === 0)
return 0;

var result = 0;
var sign = this.readInt8() ? -1 : 1; // 'isNegative' flag byte
var d = 0;
var H = 0;
var M = 0;
var S = 0;
var ms = 0;
if (length > 7) {
d = this.readInt32();
H = this.readInt8();
M = this.readInt8();
S = this.readInt8();
}
if (length > 11)
ms = this.readInt32();

if (convertTtoMs) {
H += d * 24;
M += H * 60;
S += M * 60;
ms += S * 1000;
ms *= sign;
return ms;
}
return ( sign === -1 ? '-' : '' ) + [( d ? (d*24) + H : H ), M, S].join(':') + ( ms ? '.'+ ms : '' );
// return ( sign === -1 ? '-' : '' ) + ( d ? d+'d ' : '' ) + [H, M, S].join(':') + ( ms ? '.'+ ms : '' );
};

Packet.prototype.readLengthCodedString = function() {
var len = this.readLengthCodedNumber();
// TODO: check manually first byte here to avoid polymorphic return type?
Expand Down
72 changes: 72 additions & 0 deletions test/integration/connection/test-execute-type-casting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
var common = require('../../common');
var connection = common.createConnection();
var assert = require('assert');

common.useTestDb(connection);

var tests = require('./type-casting-tests')(connection);

var table = 'type_casting';

var schema = [];
var inserts = [];

tests.forEach(function(test, index) {
var escaped = test.insertRaw || connection.escape(test.insert);

test.columnName = test.type + '_' + index;

schema.push('`' + test.columnName + '` ' + test.type + ',');
inserts.push('`' + test.columnName + '` = ' + escaped);
});

var createTable = [
'CREATE TEMPORARY TABLE `' + table + '` (',
'`id` int(11) unsigned NOT NULL AUTO_INCREMENT,'
].concat(schema).concat([
'PRIMARY KEY (`id`)',
') ENGINE=InnoDB DEFAULT CHARSET=utf8'
]).join('\n');

connection.query(createTable);

connection.query('INSERT INTO ' + table + ' SET' + inserts.join(',\n'));

var row;
connection.execute('SELECT * FROM ' + table + ' WHERE id = ?;', [1], function(err, rows) {
if (err) throw err;

row = rows[0];
});

connection.end();

process.on('exit', function() {
tests.forEach(function(test) {
var expected = test.expect || test.insert;
var got = row[test.columnName];
var message;

if (expected instanceof Date) {
assert.equal(got instanceof Date, true, test.type);

expected = String(expected);
got = String(got);
} else if (Buffer.isBuffer(expected)) {
assert.equal(Buffer.isBuffer(got), true, test.type);

expected = String(Array.prototype.slice.call(expected));
got = String(Array.prototype.slice.call(got));
}

if (test.deep) {
message = 'got: "' + JSON.stringify(got) + '" expected: "' + JSON.stringify(expected) +
'" test: ' + test.type + '';
assert.deepEqual(expected, got, message);
} else {
message = 'got: "' + got + '" (' + (typeof got) + ') expected: "' + expected +
'" (' + (typeof expected) + ') test: ' + test.type + '';
assert.strictEqual(expected, got, message);
}
});
});
49 changes: 1 addition & 48 deletions test/integration/connection/test-type-casting.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,7 @@ var assert = require('assert');

common.useTestDb(connection);

var tests = [
{type: 'decimal(4,3)', insert: '1.234', expect: '1.234' },
// {type: 'decimal(3,3)', insert: 0.33},
{type: 'tinyint', insert: 1},
{type: 'smallint', insert: 2},
{type: 'int', insert: 3},
{type: 'float', insert: 4.5},
{type: 'double', insert: 5.5},
{type: 'bigint', insert: '6', expect: 6},
{type: 'bigint', insert: 6},
{type: 'mediumint', insert: 7},
{type: 'year', insert: 2012},
{type: 'timestamp', insert: new Date('2012-05-12 11:00:23')},
{type: 'datetime', insert: new Date('2012-05-12 12:00:23')},
{type: 'date', insert: new Date('2012-05-12 00:00:00')},
{type: 'time', insert: '13:13:23'},
{type: 'binary(4)', insert: new Buffer([0, 1, 254, 255])},
{type: 'varbinary(4)', insert: new Buffer([0, 1, 254, 255])},
{type: 'tinyblob', insert: new Buffer([0, 1, 254, 255])},
{type: 'mediumblob', insert: new Buffer([0, 1, 254, 255])},
{type: 'longblob', insert: new Buffer([0, 1, 254, 255])},
{type: 'blob', insert: new Buffer([0, 1, 254, 255])},
{type: 'bit(32)', insert: new Buffer([0, 1, 254, 255])},
{type: 'char(5)', insert: 'Hello'},
{type: 'varchar(5)', insert: 'Hello'},
{type: 'varchar(3) character set utf8 collate utf8_bin', insert: 'bin'},
{type: 'tinytext', insert: 'Hello World'},
{type: 'mediumtext', insert: 'Hello World'},
{type: 'longtext', insert: 'Hello World'},
{type: 'text', insert: 'Hello World'},
{type: 'point', insertRaw: 'POINT(1.2,-3.4)', expect: {x:1.2, y:-3.4}, deep: true},
{type: 'point', insertRaw: (function() {
var buffer = new Buffer(21);
buffer.writeUInt8(1, 0);
buffer.writeUInt32LE(1, 1);
buffer.writeDoubleLE(-5.6, 5);
buffer.writeDoubleLE(10.23, 13);
return 'GeomFromWKB(' + connection.escape(buffer) + ')';
})(), expect: {x:-5.6, y:10.23}, deep: true},
{type: 'point', insertRaw: '', insert: null, expect: null},
{type: 'linestring', insertRaw: 'LINESTRING(POINT(1.2,-3.4),POINT(-5.6,10.23),POINT(0.2,0.7))', expect: [{x:1.2, y:-3.4}, {x:-5.6, y:10.23}, {x:0.2, y:0.7}], deep: true},
{type: 'polygon', insertRaw: "GeomFromText('POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5))')", expect: [[{x:0,y:0},{x:10,y:0},{x:10,y:10},{x:0,y:10},{x:0,y:0}],[{x:5,y:5},{x:7,y:5},{x:7,y:7},{x:5,y:7},{x:5,y:5}]], deep: true},
{type: 'geometry', insertRaw: 'POINT(1.2,-3.4)', expect: {x:1.2, y:-3.4}, deep: true},
{type: 'multipoint', insertRaw: "GeomFromText('MULTIPOINT(0 0, 20 20, 60 60)')", expect: [{x:0, y:0}, {x:20, y:20}, {x:60, y:60}], deep: true},
{type: 'multilinestring', insertRaw: "GeomFromText('MULTILINESTRING((10 10, 20 20), (15 15, 30 15))')", expect: [[{x:10,y:10},{x:20,y:20}],[{x:15,y:15},{x:30,y:15}]], deep: true},
{type: 'multipolygon', insertRaw: "GeomFromText('MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)),((5 5,7 5,7 7,5 7, 5 5)))')", expect: [[[{x:0,y:0},{x:10,y:0},{x:10,y:10},{x:0,y:10},{x:0,y:0}]],[[{x:5,y:5},{x:7,y:5},{x:7,y:7},{x:5,y:7},{x:5,y:5}]]], deep: true},
{type: 'geometrycollection', insertRaw: "GeomFromText('GEOMETRYCOLLECTION(POINT(11 10), POINT(31 30), LINESTRING(15 15, 20 20))')", expect: [{x:11,y:10},{x:31,y:30},[{x:15,y:15},{x:20,y:20}]], deep: true}
];
var tests = require('./type-casting-tests')(connection);

var table = 'type_casting';

Expand Down
53 changes: 53 additions & 0 deletions test/integration/connection/type-casting-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module.exports = function(connection){
return [
{type: 'decimal(4,3)', insert: '1.234'},
// {type: 'decimal(3,3)', insert: 0.33},
{type: 'tinyint', insert: 1},
{type: 'smallint', insert: 2},
{type: 'int', insert: 3},
{type: 'float', insert: 4.5},
{type: 'double', insert: 5.5},
{type: 'bigint', insert: '6', expect: 6},
{type: 'bigint', insert: 6},
{type: 'mediumint', insert: 7},
{type: 'year', insert: 2012},
{type: 'timestamp', insert: new Date('2012-05-12 11:00:23')},
{type: 'datetime', insert: new Date('2012-05-12 12:00:23')},
{type: 'date', insert: new Date('2012-05-12 00:00:00')},
{type: 'time', insert: '13:13:23'},
{type: 'time', insert: '-13:13:23'},
{type: 'time', insert: '413:13:23'},
{type: 'time', insert: '-413:13:23'},
{type: 'binary(4)', insert: new Buffer([0, 1, 254, 255])},
{type: 'varbinary(4)', insert: new Buffer([0, 1, 254, 255])},
{type: 'tinyblob', insert: new Buffer([0, 1, 254, 255])},
{type: 'mediumblob', insert: new Buffer([0, 1, 254, 255])},
{type: 'longblob', insert: new Buffer([0, 1, 254, 255])},
{type: 'blob', insert: new Buffer([0, 1, 254, 255])},
{type: 'bit(32)', insert: new Buffer([0, 1, 254, 255])},
{type: 'char(5)', insert: 'Hello'},
{type: 'varchar(5)', insert: 'Hello'},
{type: 'varchar(3) character set utf8 collate utf8_bin', insert: 'bin'},
{type: 'tinytext', insert: 'Hello World'},
{type: 'mediumtext', insert: 'Hello World'},
{type: 'longtext', insert: 'Hello World'},
{type: 'text', insert: 'Hello World'},
{type: 'point', insertRaw: 'POINT(1.2,-3.4)', expect: {x:1.2, y:-3.4}, deep: true},
{type: 'point', insertRaw: (function() {
var buffer = new Buffer(21);
buffer.writeUInt8(1, 0);
buffer.writeUInt32LE(1, 1);
buffer.writeDoubleLE(-5.6, 5);
buffer.writeDoubleLE(10.23, 13);
return 'GeomFromWKB(' + connection.escape(buffer) + ')';
})(), expect: {x:-5.6, y:10.23}, deep: true},
{type: 'point', insertRaw: '', insert: null, expect: null},
{type: 'linestring', insertRaw: 'LINESTRING(POINT(1.2,-3.4),POINT(-5.6,10.23),POINT(0.2,0.7))', expect: [{x:1.2, y:-3.4}, {x:-5.6, y:10.23}, {x:0.2, y:0.7}], deep: true},
{type: 'polygon', insertRaw: "GeomFromText('POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5))')", expect: [[{x:0,y:0},{x:10,y:0},{x:10,y:10},{x:0,y:10},{x:0,y:0}],[{x:5,y:5},{x:7,y:5},{x:7,y:7},{x:5,y:7},{x:5,y:5}]], deep: true},
{type: 'geometry', insertRaw: 'POINT(1.2,-3.4)', expect: {x:1.2, y:-3.4}, deep: true},
{type: 'multipoint', insertRaw: "GeomFromText('MULTIPOINT(0 0, 20 20, 60 60)')", expect: [{x:0, y:0}, {x:20, y:20}, {x:60, y:60}], deep: true},
{type: 'multilinestring', insertRaw: "GeomFromText('MULTILINESTRING((10 10, 20 20), (15 15, 30 15))')", expect: [[{x:10,y:10},{x:20,y:20}],[{x:15,y:15},{x:30,y:15}]], deep: true},
{type: 'multipolygon', insertRaw: "GeomFromText('MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)),((5 5,7 5,7 7,5 7, 5 5)))')", expect: [[[{x:0,y:0},{x:10,y:0},{x:10,y:10},{x:0,y:10},{x:0,y:0}]],[[{x:5,y:5},{x:7,y:5},{x:7,y:7},{x:5,y:7},{x:5,y:5}]]], deep: true},
{type: 'geometrycollection', insertRaw: "GeomFromText('GEOMETRYCOLLECTION(POINT(11 10), POINT(31 30), LINESTRING(15 15, 20 20))')", expect: [{x:11,y:10},{x:31,y:30},[{x:15,y:15},{x:20,y:20}]], deep: true}
];
};

0 comments on commit c0b3e57

Please sign in to comment.