Skip to content

Add geometry typecast test #1428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 88 additions & 13 deletions test/integration/connection/test-typecast.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,29 @@ const connection = common.createConnection();
const assert = require('assert');

connection.query('CREATE TEMPORARY TABLE json_test (json_test JSON)');
connection.query('INSERT INTO json_test VALUES (?)', JSON.stringify({ test: 42 }));
connection.query(
'INSERT INTO json_test VALUES (?)',
JSON.stringify({ test: 42 })
);

connection.query(
'CREATE TEMPORARY TABLE geom_test (p POINT SRID 0, g GEOMETRY NOT NULL SRID 0)'
);
connection.query(
'INSERT INTO geom_test VALUES (ST_GeomFromText("POINT(1 1)"), ' +
'ST_GeomFromText("LINESTRING(-71.160281 42.258729,-71.160837 42.259113,-71.161144 42.25932)"))'
);

connection.query(
{
sql: 'select "foo uppercase" as foo',
typeCast: function(field, next) {
assert.equal("number", typeof field.length);
typeCast: function (field, next) {
assert.equal('number', typeof field.length);
if (field.type === 'VAR_STRING') {

return field.string().toUpperCase();
}
return next();
}
},
},
(err, res) => {
assert.ifError(err);
Expand All @@ -28,7 +38,7 @@ connection.query(
connection.query(
{
sql: 'select "foobar" as foo',
typeCast: false
typeCast: false,
},
(err, res) => {
assert.ifError(err);
Expand All @@ -40,9 +50,9 @@ connection.query(
connection.query(
{
sql: 'SELECT NULL as test, 6 as value;',
typeCast: function(field, next) {
typeCast: function (field, next) {
return next();
}
},
},
(err, _rows) => {
assert.ifError(err);
Expand All @@ -51,13 +61,12 @@ connection.query(
}
);


connection.query(
{
sql: 'SELECT * from json_test',
typeCast: function(_field, next) {
typeCast: function (_field, next) {
return next();
}
},
},
(err, _rows) => {
assert.ifError(err);
Expand All @@ -68,14 +77,80 @@ connection.query(
connection.execute(
{
sql: 'SELECT * from json_test',
typeCast: function(_field, next) {
typeCast: function (_field, next) {
return next();
}
},
},
(err, _rows) => {
assert.ifError(err);
assert.equal(_rows[0].json_test.test, 42);
}
);

// read geo fields
connection.query(
{
sql: 'select * from geom_test',
},
(err, res) => {
assert.ifError(err);
assert.deepEqual({ x: 1, y: 1 }, res[0].p);
assert.deepEqual(
[
{ x: -71.160281, y: 42.258729 },
{ x: -71.160837, y: 42.259113 },
{ x: -71.161144, y: 42.25932 },
],
res[0].g
);
}
);

connection.query(
{
sql: 'select * from geom_test',
typeCast: function (field, next) {
assert.equal("geom_test", field.table);

if (field.name === 'p' && field.type === 'GEOMETRY') {
assert.deepEqual({ x: 1, y: 1 }, field.geometry());
return { x: 2, y: 2 };
}

if (field.name === 'g' && field.type === 'GEOMETRY') {
assert.deepEqual(
[
{ x: -71.160281, y: 42.258729 },
{ x: -71.160837, y: 42.259113 },
{ x: -71.161144, y: 42.25932 },
],
field.geometry()
);

return [
{ x: -70, y: 40 },
{ x: -60, y: 50 },
{ x: -50, y: 60 },
];
}

assert.fail('should not reach here');

return next();
},
},
(err, res) => {
assert.ifError(err);
assert.deepEqual({ x: 2, y: 2 }, res[0].p);
assert.deepEqual(
[
{ x: -70, y: 40 },
{ x: -60, y: 50 },
{ x: -50, y: 60 },
],
res[0].g
);
}
);

connection.end();