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

211 lines
7.7 KiB
JavaScript
Raw Normal View History

2013-05-27 23:34:05 +08:00
require('../../helper');
var server = require('../../../app/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
// 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) {
assert.response(server, {
2013-05-27 23:34:05 +08:00
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&format=geojson',
headers: {host: 'vizzuality.cartodb.com'},
method: 'GET'
},{ }, function(err, res){
2013-05-27 23:34:05 +08:00
assert.equal(res.statusCode, 200, res.body);
var cd = res.headers['content-disposition'];
2013-05-27 23:34:05 +08:00
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) {
assert.response(server, {
2013-05-27 23:34:05 +08:00
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){
2013-05-27 23:34:05 +08:00
assert.equal(res.statusCode, 200, res.body);
var cd = res.headers['content-disposition'];
2013-05-27 23:34:05 +08:00
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){
assert.response(server, {
2013-05-27 23:34:05 +08:00
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){
2013-05-27 23:34:05 +08:00
assert.equal(res.statusCode, 200, res.body);
var cd = res.headers['content-disposition'];
2013-05-27 23:34:05 +08:00
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){
assert.response(server, {
2013-05-27 23:34:05 +08:00
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){
2013-05-27 23:34:05 +08:00
assert.equal(res.statusCode, 200, res.body);
var cd = res.headers['content-disposition'];
2013-05-27 23:34:05 +08:00
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){
assert.response(server, {
2013-05-27 23:34:05 +08:00
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&format=geojson',
headers: {host: 'vizzuality.cartodb.com'},
method: 'GET'
},{ }, function(err, res){
2013-05-27 23:34:05 +08:00
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){
assert.response(server, {
2013-05-27 23:34:05 +08:00
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){
2013-05-27 23:34:05 +08:00
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){
assert.response(server, {
2013-05-27 23:34:05 +08:00
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){
2013-05-27 23:34:05 +08:00
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){
assert.response(server, {
2013-05-27 23:34:05 +08:00
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){
2013-05-27 23:34:05 +08:00
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){
assert.response(server, {
2013-05-27 23:34:05 +08:00
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){
2013-05-27 23:34:05 +08:00
assert.equal(res.statusCode, 200, res.body);
var cd = res.headers['content-disposition'];
2013-05-27 23:34:05 +08:00
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(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){
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(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){
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
});