2013-01-16 18:16:38 +08:00
|
|
|
require('../../helper');
|
|
|
|
|
2016-09-15 02:54:24 +08:00
|
|
|
var server = require('../../../app/server')();
|
2015-05-13 17:53:14 +08:00
|
|
|
var assert = require('../../support/assert');
|
|
|
|
var querystring = require('querystring');
|
2013-01-16 18:16:38 +08:00
|
|
|
|
2015-05-13 17:53:14 +08:00
|
|
|
describe('export.svg', function() {
|
2013-01-16 18:16:38 +08:00
|
|
|
|
2015-05-13 17:53:14 +08:00
|
|
|
it('GET /api/v1/sql with SVG format', function(done){
|
2013-01-16 18:16:38 +08:00
|
|
|
var query = querystring.stringify({
|
|
|
|
q: "SELECT 1 as cartodb_id, ST_MakeLine(ST_MakePoint(10, 10), ST_MakePoint(1034, 778)) AS the_geom ",
|
|
|
|
format: "svg"
|
|
|
|
});
|
2016-09-15 02:54:24 +08:00
|
|
|
assert.response(server, {
|
2013-01-16 18:16:38 +08:00
|
|
|
url: '/api/v1/sql?' + query,
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
2016-09-26 20:37:40 +08:00
|
|
|
},{ }, function(err, res){
|
2013-01-16 18:16:38 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var cd = res.header('Content-Disposition');
|
|
|
|
assert.ok(/filename=cartodb-query.svg/gi.test(cd), cd);
|
|
|
|
assert.equal(res.header('Content-Type'), 'image/svg+xml; charset=utf-8');
|
|
|
|
assert.ok( res.body.indexOf('<path d="M 0 768 L 1024 0" />') > 0, res.body );
|
|
|
|
// TODO: test viewBox
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:53:14 +08:00
|
|
|
it('POST /api/v1/sql with SVG format', function(done){
|
2013-01-16 18:16:38 +08:00
|
|
|
var query = querystring.stringify({
|
|
|
|
q: "SELECT 1 as cartodb_id, ST_MakeLine(ST_MakePoint(10, 10), ST_MakePoint(1034, 778)) AS the_geom ",
|
|
|
|
format: "svg"
|
|
|
|
});
|
2016-09-15 02:54:24 +08:00
|
|
|
assert.response(server, {
|
2013-01-16 18:16:38 +08:00
|
|
|
url: '/api/v1/sql',
|
|
|
|
data: query,
|
|
|
|
headers: {host: 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
|
|
method: 'POST'
|
2016-09-26 20:37:40 +08:00
|
|
|
},{ }, function(err, res){
|
2013-01-16 18:16:38 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var cd = res.header('Content-Disposition');
|
|
|
|
assert.equal(true, /^attachment/.test(cd), 'SVG is not disposed as attachment: ' + cd);
|
|
|
|
assert.ok(/filename=cartodb-query.svg/gi.test(cd), cd);
|
|
|
|
assert.equal(res.header('Content-Type'), 'image/svg+xml; charset=utf-8');
|
|
|
|
assert.ok( res.body.indexOf('<path d="M 0 768 L 1024 0" />') > 0, res.body );
|
|
|
|
// TODO: test viewBox
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:53:14 +08:00
|
|
|
it('GET /api/v1/sql with SVG format and custom filename', function(done){
|
2013-01-16 18:16:38 +08:00
|
|
|
var query = querystring.stringify({
|
|
|
|
q: "SELECT 1 as cartodb_id, ST_MakeLine(ST_MakePoint(10, 10), ST_MakePoint(1034, 778)) AS the_geom ",
|
|
|
|
format: "svg",
|
|
|
|
filename: 'mysvg'
|
|
|
|
});
|
2016-09-15 02:54:24 +08:00
|
|
|
assert.response(server, {
|
2013-01-16 18:16:38 +08:00
|
|
|
url: '/api/v1/sql?' + query,
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
2016-09-26 20:37:40 +08:00
|
|
|
},{ }, function(err, res){
|
2013-01-16 18:16:38 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var cd = res.header('Content-Disposition');
|
|
|
|
assert.ok(/filename=mysvg.svg/gi.test(cd), cd);
|
|
|
|
assert.equal(res.header('Content-Type'), 'image/svg+xml; charset=utf-8');
|
|
|
|
assert.ok( res.body.indexOf('<path d="M 0 768 L 1024 0" />') > 0, res.body );
|
|
|
|
// TODO: test viewBox
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:53:14 +08:00
|
|
|
it('GET /api/v1/sql with SVG format and centered point', function(done){
|
2013-01-16 18:16:38 +08:00
|
|
|
var query = querystring.stringify({
|
|
|
|
q: "SELECT 1 as cartodb_id, ST_MakePoint(5000, -54) AS the_geom ",
|
|
|
|
format: "svg"
|
|
|
|
});
|
2016-09-15 02:54:24 +08:00
|
|
|
assert.response(server, {
|
2013-01-16 18:16:38 +08:00
|
|
|
url: '/api/v1/sql?' + query,
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
2016-09-26 20:37:40 +08:00
|
|
|
},{ }, function(err, res){
|
2013-01-16 18:16:38 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var cd = res.header('Content-Disposition');
|
|
|
|
assert.ok(/filename=cartodb-query.svg/gi.test(cd), cd);
|
|
|
|
assert.equal(res.header('Content-Type'), 'image/svg+xml; charset=utf-8');
|
|
|
|
assert.ok( res.body.indexOf('cx="0" cy="0"') > 0, res.body );
|
|
|
|
// TODO: test viewBox
|
|
|
|
// TODO: test radius
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:53:14 +08:00
|
|
|
it('GET /api/v1/sql with SVG format and trimmed decimals', function(done){
|
2013-01-16 18:16:38 +08:00
|
|
|
var queryobj = {
|
|
|
|
q: "SELECT 1 as cartodb_id, 'LINESTRING(0 0, 1024 768, 500.123456 600.98765432)'::geometry AS the_geom ",
|
|
|
|
format: "svg",
|
|
|
|
dp: 2
|
|
|
|
};
|
2016-09-15 02:54:24 +08:00
|
|
|
assert.response(server, {
|
2013-01-16 18:16:38 +08:00
|
|
|
url: '/api/v1/sql?' + querystring.stringify(queryobj),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
2016-09-26 20:37:40 +08:00
|
|
|
},{ }, function(err, res){
|
2013-01-16 18:16:38 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var cd = res.header('Content-Disposition');
|
|
|
|
assert.ok(/filename=cartodb-query.svg/gi.test(cd), cd);
|
|
|
|
assert.equal(res.header('Content-Type'), 'image/svg+xml; charset=utf-8');
|
|
|
|
assert.ok( res.body.indexOf('<path d="M 0 768 L 1024 0 500.12 167.01" />') > 0, res.body );
|
|
|
|
// TODO: test viewBox
|
|
|
|
|
|
|
|
queryobj.dp = 3;
|
2016-09-15 02:54:24 +08:00
|
|
|
assert.response(server, {
|
2013-01-16 18:16:38 +08:00
|
|
|
url: '/api/v1/sql?' + querystring.stringify(queryobj),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
2016-09-26 20:37:40 +08:00
|
|
|
},{}, function(err, res) {
|
2013-01-16 18:16:38 +08:00
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var cd = res.header('Content-Disposition');
|
|
|
|
assert.equal(true, /^attachment/.test(cd), 'SVG is not disposed as attachment: ' + cd);
|
|
|
|
assert.ok(/filename=cartodb-query.svg/gi.test(cd), cd);
|
|
|
|
assert.equal(res.header('Content-Type'), 'image/svg+xml; charset=utf-8');
|
|
|
|
assert.ok( res.body.indexOf('<path d="M 0 768 L 1024 0 500.123 167.012" />') > 0, res.body );
|
|
|
|
// TODO: test viewBox
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-01-21 17:06:51 +08:00
|
|
|
// Test adding "the_geom" to skipfields
|
|
|
|
// See http://github.com/Vizzuality/CartoDB-SQL-API/issues/73
|
2015-05-13 17:53:14 +08:00
|
|
|
it('SVG format with "the_geom" in skipfields', function(done){
|
2013-01-21 17:06:51 +08:00
|
|
|
var query = querystring.stringify({
|
|
|
|
q: "SELECT 1 as cartodb_id, ST_MakePoint(5000, -54) AS the_geom ",
|
|
|
|
format: "svg",
|
|
|
|
skipfields: "the_geom"
|
|
|
|
});
|
2016-09-15 02:54:24 +08:00
|
|
|
assert.response(server, {
|
2013-01-21 17:06:51 +08:00
|
|
|
url: '/api/v1/sql?' + query,
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
2016-09-26 20:37:40 +08:00
|
|
|
},{ }, function(err, res){
|
2013-01-21 17:06:51 +08:00
|
|
|
assert.equal(res.statusCode, 400, res.statusCode + ': ' + res.body);
|
2013-02-13 20:32:34 +08:00
|
|
|
assert.deepEqual(res.headers['content-type'], 'application/json; charset=utf-8');
|
|
|
|
assert.deepEqual(res.headers['content-disposition'], 'inline');
|
2013-01-21 17:06:51 +08:00
|
|
|
assert.deepEqual(JSON.parse(res.body), {
|
|
|
|
error:['column "the_geom" does not exist']
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:53:14 +08:00
|
|
|
it('SVG format with missing "the_geom" field', function(done){
|
2013-01-21 17:06:51 +08:00
|
|
|
var query = querystring.stringify({
|
|
|
|
q: "SELECT 1 as cartodb_id, ST_MakePoint(5000, -54) AS something_else ",
|
|
|
|
format: "svg"
|
|
|
|
});
|
2016-09-15 02:54:24 +08:00
|
|
|
assert.response(server, {
|
2013-01-21 17:06:51 +08:00
|
|
|
url: '/api/v1/sql?' + query,
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
2016-09-26 20:37:40 +08:00
|
|
|
},{ }, function(err, res){
|
2013-01-21 17:06:51 +08:00
|
|
|
assert.equal(res.statusCode, 400, res.statusCode + ': ' + res.body);
|
|
|
|
assert.deepEqual(JSON.parse(res.body), {
|
|
|
|
error:['column "the_geom" does not exist']
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-14 00:15:38 +08:00
|
|
|
it('should close on error and error must be the only key in the body', function(done) {
|
|
|
|
assert.response(
|
2016-09-15 02:54:24 +08:00
|
|
|
server,
|
2015-05-14 00:15:38 +08:00
|
|
|
{
|
|
|
|
url: "/api/v1/sql?" + querystring.stringify({
|
|
|
|
q: "SELECT the_geom, 100/(cartodb_id - 3) cdb_ratio FROM untitle_table_4",
|
|
|
|
format: 'svg'
|
|
|
|
}),
|
|
|
|
headers: {
|
|
|
|
host: 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 400
|
|
|
|
},
|
2016-09-26 20:37:40 +08:00
|
|
|
function(err, res) {
|
2015-05-14 00:15:38 +08:00
|
|
|
var parsedBody = JSON.parse(res.body);
|
|
|
|
assert.deepEqual(Object.keys(parsedBody), ['error']);
|
|
|
|
assert.deepEqual(parsedBody.error, ["division by zero"]);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2013-01-21 17:06:51 +08:00
|
|
|
|
|
|
|
|
2013-01-16 18:16:38 +08:00
|
|
|
});
|