2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2013-05-27 23:34:05 +08:00
|
|
|
require('../../helper');
|
|
|
|
|
2019-10-04 00:24:39 +08:00
|
|
|
var server = require('../../../lib/server')();
|
2015-05-13 17:53:14 +08:00
|
|
|
var assert = require('../../support/assert');
|
|
|
|
var querystring = require('querystring');
|
2013-05-27 23:34:05 +08:00
|
|
|
|
2019-12-26 23:10:41 +08:00
|
|
|
// use decSep for internationalization
|
|
|
|
var checkDecimals = function (x, decSep) {
|
2019-12-24 01:19:08 +08:00
|
|
|
var tmp = '' + x;
|
2019-12-26 23:10:41 +08:00
|
|
|
if (tmp.indexOf(decSep) > -1) {
|
|
|
|
return tmp.length - tmp.indexOf(decSep) - 1;
|
2015-05-13 17:53:14 +08:00
|
|
|
} else {
|
2013-05-27 23:34:05 +08:00
|
|
|
return 0;
|
2015-05-13 17:53:14 +08:00
|
|
|
}
|
|
|
|
};
|
2013-05-27 23:34:05 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
describe('export.geojson', function () {
|
2013-05-27 23:34:05 +08:00
|
|
|
// GEOJSON tests
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('GET /api/v1/sql with SQL parameter, ensuring content-disposition set to geojson', function (done) {
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&format=geojson',
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
method: 'GET'
|
|
|
|
}, { }, function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200, res.body);
|
2019-12-24 01:19:08 +08:00
|
|
|
var cd = res.headers['content-disposition'];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(true, /^attachment/.test(cd), 'GEOJSON is not disposed as attachment: ' + cd);
|
|
|
|
assert.strictEqual(true, /filename=cartodb-query.geojson/gi.test(cd));
|
2019-12-24 01:19:08 +08:00
|
|
|
done();
|
|
|
|
});
|
2013-05-27 23:34:05 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('POST /api/v1/sql with SQL parameter, ensuring content-disposition set to geojson', function (done) {
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql',
|
|
|
|
data: querystring.stringify({ q: 'SELECT * FROM untitle_table_4', format: 'geojson' }),
|
|
|
|
headers: { host: 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
|
|
method: 'POST'
|
|
|
|
}, { }, function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200, res.body);
|
2019-12-24 01:19:08 +08:00
|
|
|
var cd = res.headers['content-disposition'];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(true, /^attachment/.test(cd), 'GEOJSON is not disposed as attachment: ' + cd);
|
|
|
|
assert.strictEqual(true, /filename=cartodb-query.geojson/gi.test(cd));
|
2019-12-24 01:19:08 +08:00
|
|
|
done();
|
|
|
|
});
|
2013-05-27 23:34:05 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('uses the last format parameter when multiple are used', function (done) {
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql?format=csv&q=SELECT%20*%20FROM%20untitle_table_4&format=geojson',
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
method: 'GET'
|
|
|
|
}, { }, function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200, res.body);
|
2019-12-24 01:19:08 +08:00
|
|
|
var cd = res.headers['content-disposition'];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(true, /filename=cartodb-query.geojson/gi.test(cd));
|
2019-12-24 01:19:08 +08:00
|
|
|
done();
|
|
|
|
});
|
2013-05-27 23:34:05 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('uses custom filename', function (done) {
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&format=geojson&filename=x',
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
method: 'GET'
|
|
|
|
}, { }, function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200, res.body);
|
2019-12-24 01:19:08 +08:00
|
|
|
var cd = res.headers['content-disposition'];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(true, /filename=x.geojson/gi.test(cd), cd);
|
2019-12-24 01:19:08 +08:00
|
|
|
done();
|
|
|
|
});
|
2013-05-27 23:34:05 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('does not include the_geom and the_geom_webmercator properties by default', function (done) {
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&format=geojson',
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
method: 'GET'
|
|
|
|
}, { }, function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200, res.body);
|
2019-12-26 23:10:41 +08:00
|
|
|
var parsedBody = JSON.parse(res.body);
|
|
|
|
var row0 = parsedBody.features[0].properties;
|
2019-12-24 01:19:08 +08:00
|
|
|
var checkfields = { name: 1, cartodb_id: 1, the_geom: 0, the_geom_webmercator: 0 };
|
|
|
|
for (var f in checkfields) {
|
|
|
|
if (checkfields[f]) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ok(Object.prototype.hasOwnProperty.call(row0, f), "result does not include '" + f + "'");
|
2019-12-24 01:19:08 +08:00
|
|
|
} else {
|
2019-12-26 23:43:12 +08:00
|
|
|
assert.ok(!Object.prototype.hasOwnProperty.call(row0, f), "result includes '" + f + "'");
|
2019-12-24 01:19:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
});
|
2013-05-27 23:34:05 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('skipfields controls fields included in GeoJSON output', function (done) {
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&format=geojson&skipfields=unexistant,cartodb_id',
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
method: 'GET'
|
|
|
|
}, { }, function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200, res.body);
|
2019-12-26 23:10:41 +08:00
|
|
|
var parsedBody = JSON.parse(res.body);
|
|
|
|
var row0 = parsedBody.features[0].properties;
|
2019-12-24 01:19:08 +08:00
|
|
|
var checkfields = { name: 1, cartodb_id: 0, the_geom: 0, the_geom_webmercator: 0 };
|
|
|
|
for (var f in checkfields) {
|
|
|
|
if (checkfields[f]) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ok(Object.prototype.hasOwnProperty.call(row0, f), "result does not include '" + f + "'");
|
2019-12-24 01:19:08 +08:00
|
|
|
} else {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ok(!Object.prototype.hasOwnProperty.call(row0, f), "result includes '" + f + "'");
|
2019-12-24 01:19:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
});
|
2013-05-27 23:34:05 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('GET /api/v1/sql as geojson limiting decimal places', function (done) {
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
|
|
|
q: 'SELECT ST_MakePoint(0.123,2.3456) as the_geom',
|
|
|
|
format: 'geojson',
|
|
|
|
dp: '1'
|
|
|
|
}),
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
method: 'GET'
|
|
|
|
}, { }, function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200, res.body);
|
2019-12-24 01:19:08 +08:00
|
|
|
var result = JSON.parse(res.body);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(1, checkDecimals(result.features[0].geometry.coordinates[0], '.'));
|
2019-12-24 01:19:08 +08:00
|
|
|
done();
|
|
|
|
});
|
2013-05-27 23:34:05 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('GET /api/v1/sql as geojson with default dp as 6', function (done) {
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
|
|
|
q: 'SELECT ST_MakePoint(0.12345678,2.3456787654) as the_geom',
|
|
|
|
format: 'geojson'
|
|
|
|
}),
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
method: 'GET'
|
|
|
|
}, { }, function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200, res.body);
|
2019-12-24 01:19:08 +08:00
|
|
|
var result = JSON.parse(res.body);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(6, checkDecimals(result.features[0].geometry.coordinates[0], '.'));
|
2019-12-24 01:19:08 +08:00
|
|
|
done();
|
|
|
|
});
|
2013-05-27 23:34:05 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('null geometries in geojson output', function (done) {
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
|
|
|
q: "SELECT 1 as gid, 'U' as name, null::geometry as the_geom ",
|
|
|
|
format: 'geojson'
|
|
|
|
}),
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
method: 'GET'
|
|
|
|
}, { }, function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200, res.body);
|
2019-12-24 01:19:08 +08:00
|
|
|
var cd = res.headers['content-disposition'];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(true, /^attachment/.test(cd), 'GEOJSON is not disposed as attachment: ' + cd);
|
|
|
|
assert.strictEqual(true, /filename=cartodb-query.geojson/gi.test(cd));
|
2019-12-24 01:19:08 +08:00
|
|
|
var gjson = JSON.parse(res.body);
|
|
|
|
var expected = {
|
|
|
|
type: 'FeatureCollection',
|
|
|
|
features: [{
|
|
|
|
type: 'Feature',
|
|
|
|
properties: { gid: 1, name: 'U' },
|
|
|
|
geometry: null
|
|
|
|
}]
|
|
|
|
};
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.deepStrictEqual(gjson, expected);
|
2019-12-24 01:19:08 +08:00
|
|
|
done();
|
|
|
|
});
|
2014-08-05 06:47:14 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('stream response handle errors', function (done) {
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
|
|
|
q: 'SELECTT 1 as gid, null::geometry as the_geom ',
|
|
|
|
format: 'geojson'
|
|
|
|
}),
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
method: 'GET'
|
|
|
|
}, { }, function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.statusCode, 400, res.body);
|
2019-12-24 01:19:08 +08:00
|
|
|
var geoJson = JSON.parse(res.body);
|
|
|
|
assert.ok(geoJson.error);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(geoJson.error.length, 1);
|
2019-12-24 01:19:08 +08:00
|
|
|
assert.ok(geoJson.error[0].match(/^syntax error at or near.*/));
|
|
|
|
done();
|
|
|
|
});
|
2014-08-05 06:47:49 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('stream response with empty result set has valid output', function (done) {
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
|
|
|
q: 'SELECT 1 as gid, null::geometry as the_geom limit 0',
|
|
|
|
format: 'geojson'
|
|
|
|
}),
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
method: 'GET'
|
|
|
|
}, { }, function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200, res.body);
|
2019-12-24 01:19:08 +08:00
|
|
|
var geoJson = JSON.parse(res.body);
|
|
|
|
var expectedGeoJson = { type: 'FeatureCollection', features: [] };
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.deepStrictEqual(geoJson, expectedGeoJson);
|
2019-12-24 01:19:08 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-05-27 23:34:05 +08:00
|
|
|
});
|