Skip to content

Commit b505634

Browse files
wellwelweltestn
andauthored
ci: add geometry typecast test (#2395)
* Add geometry typecast test * ci: compatibility with MySQL 5.7 in typeCast tests --------- Co-authored-by: testn <[email protected]>
1 parent 819863e commit b505634

File tree

1 file changed

+93
-18
lines changed

1 file changed

+93
-18
lines changed

test/integration/connection/test-typecast.js

+93-18
Original file line numberDiff line numberDiff line change
@@ -5,77 +5,152 @@ const connection = common.createConnection();
55
const assert = require('assert');
66

77
connection.query('CREATE TEMPORARY TABLE json_test (json_test JSON)');
8-
connection.query('INSERT INTO json_test VALUES (?)', JSON.stringify({ test: 42 }));
8+
connection.query(
9+
'INSERT INTO json_test VALUES (?)',
10+
JSON.stringify({ test: 42 }),
11+
);
12+
13+
connection.query(
14+
'CREATE TEMPORARY TABLE geom_test (p POINT, g GEOMETRY NOT NULL)',
15+
);
16+
connection.query(
17+
'INSERT INTO geom_test VALUES (ST_GeomFromText("POINT(1 1)"), ' +
18+
'ST_GeomFromText("LINESTRING(-71.160281 42.258729,-71.160837 42.259113,-71.161144 42.25932)"))',
19+
);
920

1021
connection.query(
1122
{
1223
sql: 'select "foo uppercase" as foo',
13-
typeCast: function(field, next) {
14-
assert.equal("number", typeof field.length);
24+
typeCast: function (field, next) {
25+
assert.equal('number', typeof field.length);
1526
if (field.type === 'VAR_STRING') {
16-
1727
return field.string().toUpperCase();
1828
}
1929
return next();
20-
}
30+
},
2131
},
2232
(err, res) => {
2333
assert.ifError(err);
2434
assert.equal(res[0].foo, 'FOO UPPERCASE');
25-
}
35+
},
2636
);
2737

2838
connection.query(
2939
{
3040
sql: 'select "foobar" as foo',
31-
typeCast: false
41+
typeCast: false,
3242
},
3343
(err, res) => {
3444
assert.ifError(err);
3545
assert(Buffer.isBuffer(res[0].foo));
3646
assert.equal(res[0].foo.toString('utf8'), 'foobar');
37-
}
47+
},
3848
);
3949

4050
connection.query(
4151
{
4252
sql: 'SELECT NULL as test, 6 as value;',
43-
typeCast: function(field, next) {
53+
typeCast: function (field, next) {
4454
return next();
45-
}
55+
},
4656
},
4757
(err, _rows) => {
4858
assert.ifError(err);
4959
assert.equal(_rows[0].test, null);
5060
assert.equal(_rows[0].value, 6);
51-
}
61+
},
5262
);
5363

54-
5564
connection.query(
5665
{
5766
sql: 'SELECT * from json_test',
58-
typeCast: function(_field, next) {
67+
typeCast: function (_field, next) {
5968
return next();
60-
}
69+
},
6170
},
6271
(err, _rows) => {
6372
assert.ifError(err);
6473
assert.equal(_rows[0].json_test.test, 42);
65-
}
74+
},
6675
);
6776

6877
connection.execute(
6978
{
7079
sql: 'SELECT * from json_test',
71-
typeCast: function(_field, next) {
80+
typeCast: function (_field, next) {
7281
return next();
73-
}
82+
},
7483
},
7584
(err, _rows) => {
7685
assert.ifError(err);
7786
assert.equal(_rows[0].json_test.test, 42);
78-
}
87+
},
88+
);
89+
90+
// read geo fields
91+
connection.query(
92+
{
93+
sql: 'select * from geom_test',
94+
},
95+
(err, res) => {
96+
assert.ifError(err);
97+
assert.deepEqual({ x: 1, y: 1 }, res[0].p);
98+
assert.deepEqual(
99+
[
100+
{ x: -71.160281, y: 42.258729 },
101+
{ x: -71.160837, y: 42.259113 },
102+
{ x: -71.161144, y: 42.25932 },
103+
],
104+
res[0].g,
105+
);
106+
},
107+
);
108+
109+
connection.query(
110+
{
111+
sql: 'select * from geom_test',
112+
typeCast: function (field, next) {
113+
assert.equal('geom_test', field.table);
114+
115+
if (field.name === 'p' && field.type === 'GEOMETRY') {
116+
assert.deepEqual({ x: 1, y: 1 }, field.geometry());
117+
return { x: 2, y: 2 };
118+
}
119+
120+
if (field.name === 'g' && field.type === 'GEOMETRY') {
121+
assert.deepEqual(
122+
[
123+
{ x: -71.160281, y: 42.258729 },
124+
{ x: -71.160837, y: 42.259113 },
125+
{ x: -71.161144, y: 42.25932 },
126+
],
127+
field.geometry(),
128+
);
129+
130+
return [
131+
{ x: -70, y: 40 },
132+
{ x: -60, y: 50 },
133+
{ x: -50, y: 60 },
134+
];
135+
}
136+
137+
assert.fail('should not reach here');
138+
139+
return next();
140+
},
141+
},
142+
(err, res) => {
143+
assert.ifError(err);
144+
assert.deepEqual({ x: 2, y: 2 }, res[0].p);
145+
assert.deepEqual(
146+
[
147+
{ x: -70, y: 40 },
148+
{ x: -60, y: 50 },
149+
{ x: -50, y: 60 },
150+
],
151+
res[0].g,
152+
);
153+
},
79154
);
80155

81156
connection.end();

0 commit comments

Comments
 (0)