CartoDB-SQL-API/test/acceptance/export/geojson.js

215 lines
7.8 KiB
JavaScript
Raw Normal View History

2013-05-27 23:34:05 +08:00
require('../../helper');
2015-05-13 17:53:14 +08:00
var app = require(global.settings.app_root + '/app/controllers/app')();
var assert = require('../../support/assert');
var querystring = require('querystring');
2013-05-27 23:34:05 +08:00
// allow lots of emitters to be set to silence warning
// TODO: check if still needed ...
app.setMaxListeners(0);
// use dec_sep for internationalization
var checkDecimals = function(x, dec_sep){
var tmp='' + x;
2015-05-13 17:53:14 +08:00
if (tmp.indexOf(dec_sep)>-1) {
return tmp.length - tmp.indexOf(dec_sep) - 1;
} 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
2015-05-13 17:53:14 +08:00
describe('export.geojson', function() {
2013-05-27 23:34:05 +08:00
// GEOJSON tests
2015-05-13 17:53:14 +08:00
it('GET /api/v1/sql with SQL parameter, ensuring content-disposition set to geojson', function(done) {
2013-05-27 23:34:05 +08:00
assert.response(app, {
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&format=geojson',
headers: {host: 'vizzuality.cartodb.com'},
method: 'GET'
},{ }, function(res){
assert.equal(res.statusCode, 200, res.body);
var cd = res.header('Content-Disposition');
assert.equal(true, /^attachment/.test(cd), 'GEOJSON is not disposed as attachment: ' + cd);
assert.equal(true, /filename=cartodb-query.geojson/gi.test(cd));
done();
});
});
2015-05-13 17:53:14 +08:00
it('POST /api/v1/sql with SQL parameter, ensuring content-disposition set to geojson', function(done) {
2013-05-27 23:34:05 +08:00
assert.response(app, {
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(res){
assert.equal(res.statusCode, 200, res.body);
var cd = res.header('Content-Disposition');
assert.equal(true, /^attachment/.test(cd), 'GEOJSON is not disposed as attachment: ' + cd);
assert.equal(true, /filename=cartodb-query.geojson/gi.test(cd));
done();
});
});
2015-05-13 17:53:14 +08:00
it('uses the last format parameter when multiple are used', function(done){
2013-05-27 23:34:05 +08:00
assert.response(app, {
url: '/api/v1/sql?format=csv&q=SELECT%20*%20FROM%20untitle_table_4&format=geojson',
headers: {host: 'vizzuality.cartodb.com'},
method: 'GET'
},{ }, function(res){
assert.equal(res.statusCode, 200, res.body);
var cd = res.header('Content-Disposition');
assert.equal(true, /filename=cartodb-query.geojson/gi.test(cd));
done();
});
});
2015-05-13 17:53:14 +08:00
it('uses custom filename', function(done){
2013-05-27 23:34:05 +08:00
assert.response(app, {
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&format=geojson&filename=x',
headers: {host: 'vizzuality.cartodb.com'},
method: 'GET'
},{ }, function(res){
assert.equal(res.statusCode, 200, res.body);
var cd = res.header('Content-Disposition');
assert.equal(true, /filename=x.geojson/gi.test(cd), cd);
done();
});
});
2015-05-13 17:53:14 +08:00
it('does not include the_geom and the_geom_webmercator properties by default', function(done){
2013-05-27 23:34:05 +08:00
assert.response(app, {
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&format=geojson',
headers: {host: 'vizzuality.cartodb.com'},
method: 'GET'
},{ }, function(res){
assert.equal(res.statusCode, 200, res.body);
var parsed_body = JSON.parse(res.body);
var row0 = parsed_body.features[0].properties;
var checkfields = {'name':1, 'cartodb_id':1, 'the_geom':0, 'the_geom_webmercator':0};
for ( var f in checkfields ) {
if ( checkfields[f] ) {
assert.ok(row0.hasOwnProperty(f), "result does not include '" + f + "'");
} else {
assert.ok(!row0.hasOwnProperty(f), "result includes '" + f + "'");
}
}
done();
});
});
2015-05-13 17:53:14 +08:00
it('skipfields controls fields included in GeoJSON output', function(done){
2013-05-27 23:34:05 +08:00
assert.response(app, {
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(res){
assert.equal(res.statusCode, 200, res.body);
var parsed_body = JSON.parse(res.body);
var row0 = parsed_body.features[0].properties;
var checkfields = {'name':1, 'cartodb_id':0, 'the_geom':0, 'the_geom_webmercator':0};
for ( var f in checkfields ) {
if ( checkfields[f] ) {
assert.ok(row0.hasOwnProperty(f), "result does not include '" + f + "'");
} else {
assert.ok(!row0.hasOwnProperty(f), "result includes '" + f + "'");
}
}
done();
});
});
2015-05-13 17:53:14 +08:00
it('GET /api/v1/sql as geojson limiting decimal places', function(done){
2013-05-27 23:34:05 +08:00
assert.response(app, {
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(res){
assert.equal(res.statusCode, 200, res.body);
var result = JSON.parse(res.body);
assert.equal(1, checkDecimals(result.features[0].geometry.coordinates[0], '.'));
done();
});
});
2015-05-13 17:53:14 +08:00
it('GET /api/v1/sql as geojson with default dp as 6', function(done){
2013-05-27 23:34:05 +08:00
assert.response(app, {
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(res){
assert.equal(res.statusCode, 200, res.body);
var result = JSON.parse(res.body);
assert.equal(6, checkDecimals(result.features[0].geometry.coordinates[0], '.'));
done();
});
});
2015-05-13 17:53:14 +08:00
it('null geometries in geojson output', function(done){
2013-05-27 23:34:05 +08:00
assert.response(app, {
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(res){
assert.equal(res.statusCode, 200, res.body);
var cd = res.header('Content-Disposition');
assert.equal(true, /^attachment/.test(cd), 'GEOJSON is not disposed as attachment: ' + cd);
assert.equal(true, /filename=cartodb-query.geojson/gi.test(cd));
var gjson = JSON.parse(res.body);
var expected = {
type: 'FeatureCollection',
features: [ { type: 'Feature',
properties: { gid: 1, name: 'U' },
geometry: null } ]
};
assert.deepEqual(gjson, expected);
done();
});
});
2015-05-13 17:53:14 +08:00
it('stream response handle errors', function(done) {
assert.response(app, {
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(res){
assert.equal(res.statusCode, 400, res.body);
var geoJson = JSON.parse(res.body);
2015-03-01 20:19:51 +08:00
assert.ok(geoJson.error);
assert.equal(geoJson.error.length, 1);
assert.ok(geoJson.error[0].match(/^syntax error at or near.*/));
done();
});
});
2015-05-13 17:53:14 +08:00
it('stream response with empty result set has valid output', function(done) {
assert.response(app, {
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(res){
assert.equal(res.statusCode, 200, res.body);
var geoJson = JSON.parse(res.body);
var expectedGeoJson = {"type": "FeatureCollection", "features": []};
assert.deepEqual(geoJson, expectedGeoJson);
done();
});
});
2013-05-27 23:34:05 +08:00
});