jshint fixes

This commit is contained in:
Raul Ochoa 2015-05-13 11:53:14 +02:00
parent 9486643f0a
commit 33731fc315
7 changed files with 272 additions and 259 deletions

View File

@ -2,34 +2,16 @@ require('../../helper');
require('../../support/assert');
var app = require(global.settings.app_root + '/app/controllers/app')()
, assert = require('assert')
, querystring = require('querystring')
, _ = require('underscore')
, zipfile = require('zipfile')
, fs = require('fs')
, libxmljs = require('libxmljs')
, Step = require('step')
;
var app = require(global.settings.app_root + '/app/controllers/app')();
var assert = require('assert');
var querystring = require('querystring');
// allow lots of emitters to be set to silence warning
app.setMaxListeners(0);
suite('export.arraybuffer', function() {
describe('export.arraybuffer', function() {
var expected_cache_control = 'no-cache,max-age=3600,must-revalidate,public';
var expected_cache_control_persist = 'public,max-age=31536000';
// use dec_sep for internationalization
var checkDecimals = function(x, dec_sep){
var tmp='' + x;
if (tmp.indexOf(dec_sep)>-1)
return tmp.length-tmp.indexOf(dec_sep)-1;
else
return 0;
}
test('GET /api/v1/sql as arraybuffer ', function(done){
it('GET /api/v1/sql as arraybuffer ', function(done){
assert.response(app, {
url: '/api/v1/sql?' + querystring.stringify({
q: 'SELECT cartodb_id,name,1::integer,187.9 FROM untitle_table_4',
@ -39,12 +21,12 @@ test('GET /api/v1/sql as arraybuffer ', function(done){
method: 'GET'
},{ }, function(res){
assert.equal(res.statusCode, 200, res.body);
assert.equal(res.headers['content-type'], "application/octet-stream")
assert.equal(res.headers['content-type'], "application/octet-stream");
done();
});
});
test('GET /api/v1/sql as arraybuffer does not support geometry types ', function(done){
it('GET /api/v1/sql as arraybuffer does not support geometry types ', function(done){
assert.response(app, {
url: '/api/v1/sql?' + querystring.stringify({
q: 'SELECT cartodb_id, the_geom FROM untitle_table_4',

View File

@ -2,21 +2,16 @@ require('../../helper');
require('../../support/assert');
var app = require(global.settings.app_root + '/app/controllers/app')()
, assert = require('assert')
, querystring = require('querystring')
, _ = require('underscore')
, zipfile = require('zipfile')
, fs = require('fs')
, libxmljs = require('libxmljs')
;
var app = require(global.settings.app_root + '/app/controllers/app')();
var assert = require('assert');
var querystring = require('querystring');
// allow lots of emitters to be set to silence warning
app.setMaxListeners(0);
suite('export.csv', function() {
describe('export.csv', function() {
test('CSV format', function(done){
it('CSV format', function(done){
assert.response(app, {
url: '/api/v1/sql?' + querystring.stringify({
q: 'SELECT * FROM untitle_table_4 WHERE cartodb_id = 1',
@ -43,7 +38,7 @@ test('CSV format', function(done){
});
});
test('CSV format, bigger than 81920 bytes', function(done){
it('CSV format, bigger than 81920 bytes', function(done){
assert.response(app, {
url: '/api/v1/sql',
data: querystring.stringify({
@ -59,7 +54,7 @@ test('CSV format, bigger than 81920 bytes', function(done){
});
test('CSV format from POST', function(done){
it('CSV format from POST', function(done){
assert.response(app, {
url: '/api/v1/sql',
data: querystring.stringify({q: "SELECT * FROM untitle_table_4 LIMIT 1", format: 'csv'}),
@ -76,7 +71,7 @@ test('CSV format from POST', function(done){
});
});
test('CSV format, custom filename', function(done){
it('CSV format, custom filename', function(done){
assert.response(app, {
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=csv&filename=mycsv.csv',
headers: {host: 'vizzuality.cartodb.com'},
@ -89,43 +84,45 @@ test('CSV format, custom filename', function(done){
var ct = res.header('Content-Type');
assert.equal(true, /header=present/.test(ct), "CSV doesn't advertise header presence: " + ct);
var row0 = res.body.substring(0, res.body.search(/[\n\r]/)).split(',');
var checkfields = {'name':1, 'cartodb_id':1, 'the_geom':1, 'the_geom_webmercator':1};
for ( var f in checkfields ) {
var idx = row0.indexOf(f);
if ( checkfields[f] ) {
assert.ok(idx != -1, "result does not include '" + f + "'");
} else {
assert.ok(idx == -1, "result includes '" + f + "' ("+idx+")");
}
}
var checkFields = { name: true, cartodb_id: true, the_geom: true, the_geom_webmercator: true };
Object.keys(checkFields).forEach(function(f) {
var idx = row0.indexOf(f);
if ( checkFields[f] ) {
assert.ok(idx !== -1, "result does not include '" + f + "'");
} else {
assert.ok(idx === -1, "result includes '" + f + "' ("+idx+")");
}
});
done();
});
});
test('skipfields controls fields included in CSV output', function(done){
it('skipfields controls fields included in CSV output', function(done){
assert.response(app, {
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=csv&skipfields=unexistant,cartodb_id',
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=csv' +
'&skipfields=unexistant,cartodb_id',
headers: {host: 'vizzuality.cartodb.com'},
method: 'GET'
},{ }, function(res){
assert.equal(res.statusCode, 200, res.body);
var row0 = res.body.substring(0, res.body.search(/[\n\r]/)).split(',');
var checkfields = {'name':1, 'cartodb_id':0, 'the_geom':1, 'the_geom_webmercator':1};
for ( var f in checkfields ) {
var idx = row0.indexOf(f);
if ( checkfields[f] ) {
assert.ok(idx != -1, "result does not include '" + f + "'");
} else {
assert.ok(idx == -1, "result includes '" + f + "' ("+idx+")");
}
}
var checkFields = { name: true, cartodb_id: false, the_geom: true, the_geom_webmercator: true };
Object.keys(checkFields).forEach(function(f) {
var idx = row0.indexOf(f);
if ( checkFields[f] ) {
assert.ok(idx !== -1, "result does not include '" + f + "'");
} else {
assert.ok(idx === -1, "result includes '" + f + "' ("+idx+")");
}
});
done();
});
});
test('GET /api/v1/sql as csv', function(done){
it('GET /api/v1/sql as csv', function(done){
assert.response(app, {
url: '/api/v1/sql?q=SELECT%20cartodb_id,ST_AsEWKT(the_geom)%20as%20geom%20FROM%20untitle_table_4%20LIMIT%201&format=csv',
url: '/api/v1/sql?q=SELECT%20cartodb_id,ST_AsEWKT(the_geom)%20as%20geom%20FROM%20untitle_table_4%20LIMIT%201' +
'&format=csv',
headers: {host: 'vizzuality.cartodb.com'},
method: 'GET'
},{ }, function(res){
@ -137,7 +134,7 @@ test('GET /api/v1/sql as csv', function(done){
});
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/60
test('GET /api/v1/sql as csv with no rows', function(done){
it('GET /api/v1/sql as csv with no rows', function(done){
assert.response(app, {
url: '/api/v1/sql?q=SELECT%20true%20WHERE%20false&format=csv',
headers: {host: 'vizzuality.cartodb.com'},
@ -147,13 +144,12 @@ test('GET /api/v1/sql as csv with no rows', function(done){
var obtained_lines = res.body.split('\r\n');
assert.ok(obtained_lines.length <= 2, // may or may not have an header
// See http://trac.osgeo.org/gdal/ticket/5234
'Too many lines in output (' + obtained_lines.length + '): '
+ obtained_lines.join('\n'));
'Too many lines in output (' + obtained_lines.length + '): ' + obtained_lines.join('\n'));
done();
});
});
test('GET /api/v1/sql as csv, properly escaped', function(done){
it('GET /api/v1/sql as csv, properly escaped', function(done){
assert.response(app, {
url: '/api/v1/sql?q=SELECT%20cartodb_id,%20address%20FROM%20untitle_table_4%20LIMIT%201&format=csv',
headers: {host: 'vizzuality.cartodb.com'},
@ -166,23 +162,29 @@ test('GET /api/v1/sql as csv, properly escaped', function(done){
});
});
test('GET /api/v1/sql as csv, concurrently', function(done){
it('GET /api/v1/sql as csv, concurrently', function(done){
var concurrency = 4;
var waiting = concurrency;
function validate(res){
var expected = 'cartodb_id,address\r\n1,"Calle de Pérez Galdós 9, Madrid, Spain"\r\n';
assert.equal(res.body, expected);
if ( ! --waiting ) {
done();
}
}
for (var i=0; i<concurrency; ++i) {
assert.response(app, {
url: '/api/v1/sql?q=SELECT%20cartodb_id,%20address%20FROM%20untitle_table_4%20LIMIT%201&format=csv',
headers: {host: 'vizzuality.cartodb.com'},
method: 'GET'
},{ }, function(res){
assert.equal(res.statusCode, 200, res.body);
var expected = 'cartodb_id,address\r\n1,"Calle de Pérez Galdós 9, Madrid, Spain"\r\n';
assert.equal(res.body, expected);
if ( ! --waiting ) done();
});
assert.response(app,
{
url: '/api/v1/sql?q=SELECT%20cartodb_id,%20address%20FROM%20untitle_table_4%20LIMIT%201&format=csv',
headers: {host: 'vizzuality.cartodb.com'},
method: 'GET'
},
{
status: 200
},
validate
);
}
});

View File

@ -1,15 +1,8 @@
require('../../helper');
require('../../support/assert');
var app = require(global.settings.app_root + '/app/controllers/app')()
, assert = require('assert')
, querystring = require('querystring')
, _ = require('underscore')
, zipfile = require('zipfile')
, fs = require('fs')
, libxmljs = require('libxmljs')
;
var app = require(global.settings.app_root + '/app/controllers/app')();
var assert = require('../../support/assert');
var querystring = require('querystring');
// allow lots of emitters to be set to silence warning
// TODO: check if still needed ...
@ -18,17 +11,18 @@ app.setMaxListeners(0);
// use dec_sep for internationalization
var checkDecimals = function(x, dec_sep){
var tmp='' + x;
if (tmp.indexOf(dec_sep)>-1)
return tmp.length-tmp.indexOf(dec_sep)-1;
else
if (tmp.indexOf(dec_sep)>-1) {
return tmp.length - tmp.indexOf(dec_sep) - 1;
} else {
return 0;
}
}
};
suite('export.geojson', function() {
describe('export.geojson', function() {
// GEOJSON tests
test('GET /api/v1/sql with SQL parameter and geojson format, ensuring content-disposition set to geojson', function(done){
it('GET /api/v1/sql with SQL parameter, ensuring content-disposition set to geojson', function(done) {
assert.response(app, {
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&format=geojson',
headers: {host: 'vizzuality.cartodb.com'},
@ -42,7 +36,7 @@ test('GET /api/v1/sql with SQL parameter and geojson format, ensuring content-di
});
});
test('POST /api/v1/sql with SQL parameter and geojson format, ensuring content-disposition set to geojson', function(done){
it('POST /api/v1/sql with SQL parameter, ensuring content-disposition set to geojson', function(done) {
assert.response(app, {
url: '/api/v1/sql',
data: querystring.stringify({q: "SELECT * FROM untitle_table_4", format: 'geojson' }),
@ -57,7 +51,7 @@ test('POST /api/v1/sql with SQL parameter and geojson format, ensuring content-d
});
});
test('uses the last format parameter when multiple are used', function(done){
it('uses the last format parameter when multiple are used', function(done){
assert.response(app, {
url: '/api/v1/sql?format=csv&q=SELECT%20*%20FROM%20untitle_table_4&format=geojson',
headers: {host: 'vizzuality.cartodb.com'},
@ -70,7 +64,7 @@ test('uses the last format parameter when multiple are used', function(done){
});
});
test('uses custom filename', function(done){
it('uses custom filename', function(done){
assert.response(app, {
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&format=geojson&filename=x',
headers: {host: 'vizzuality.cartodb.com'},
@ -83,7 +77,7 @@ test('uses custom filename', function(done){
});
});
test('does not include the_geom and the_geom_webmercator properties by default', function(done){
it('does not include the_geom and the_geom_webmercator properties by default', function(done){
assert.response(app, {
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&format=geojson',
headers: {host: 'vizzuality.cartodb.com'},
@ -104,7 +98,7 @@ test('does not include the_geom and the_geom_webmercator properties by default',
});
});
test('skipfields controls fields included in GeoJSON output', function(done){
it('skipfields controls fields included in GeoJSON output', function(done){
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'},
@ -126,7 +120,7 @@ test('skipfields controls fields included in GeoJSON output', function(done){
});
test('GET /api/v1/sql as geojson limiting decimal places', function(done){
it('GET /api/v1/sql as geojson limiting decimal places', function(done){
assert.response(app, {
url: '/api/v1/sql?' + querystring.stringify({
q: 'SELECT ST_MakePoint(0.123,2.3456) as the_geom',
@ -142,7 +136,7 @@ test('GET /api/v1/sql as geojson limiting decimal places', function(done){
});
});
test('GET /api/v1/sql as geojson with default dp as 6', function(done){
it('GET /api/v1/sql as geojson with default dp as 6', function(done){
assert.response(app, {
url: '/api/v1/sql?' + querystring.stringify({
q: 'SELECT ST_MakePoint(0.12345678,2.3456787654) as the_geom',
@ -157,7 +151,7 @@ test('GET /api/v1/sql as geojson with default dp as 6', function(done){
});
});
test('null geometries in geojson output', function(done){
it('null geometries in geojson output', function(done){
assert.response(app, {
url: '/api/v1/sql?' + querystring.stringify({
q: "SELECT 1 as gid, 'U' as name, null::geometry as the_geom ",
@ -182,7 +176,7 @@ test('null geometries in geojson output', function(done){
});
});
test('stream response handle errors', function(done) {
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 ",
@ -191,7 +185,6 @@ test('stream response handle errors', function(done) {
headers: {host: 'vizzuality.cartodb.com'},
method: 'GET'
},{ }, function(res){
console.log(res);
assert.equal(res.statusCode, 400, res.body);
var geoJson = JSON.parse(res.body);
assert.ok(geoJson.error);
@ -201,7 +194,7 @@ test('stream response handle errors', function(done) {
});
});
test('stream response with empty result set has valid output', function(done) {
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",

View File

@ -1,22 +1,16 @@
require('../../helper');
require('../../support/assert');
var app = require(global.settings.app_root + '/app/controllers/app')()
, assert = require('assert')
, querystring = require('querystring')
, _ = require('underscore')
, zipfile = require('zipfile')
, fs = require('fs')
, libxmljs = require('libxmljs')
, http = require('http')
, server_utils = require('../../support/server_utils')
;
var app = require(global.settings.app_root + '/app/controllers/app')();
var assert = require('../../support/assert');
var querystring = require('querystring');
var libxmljs = require('libxmljs');
var http = require('http');
var server_utils = require('../../support/server_utils');
// allow lots of emitters to be set to silence warning
app.setMaxListeners(0);
suite('export.kml', function() {
describe('export.kml', function() {
// Check if an attribute is in the KML output
//
@ -34,15 +28,21 @@ var hasAttribute = function(kml, att) {
var xpath;
xpath = "//SimpleField[@name='" + att + "']";
if ( doc.get(xpath) ) return true;
if ( doc.get(xpath) ) {
return true;
}
xpath = "//Placemark/" + att;
if ( doc.get(xpath) ) return true;
if ( doc.get(xpath) ) {
return true;
}
var lcatt = att.toLowerCase();
if ( lcatt == 'name' || lcatt == 'description' ) {
if ( lcatt === 'name' || lcatt === 'description' ) {
xpath = "//Placemark/" + lcatt;
if ( doc.get(xpath) ) return true;
if ( doc.get(xpath) ) {
return true;
}
}
//if ( lowerkml.indexOf('simplefield name="'+ loweratt + '"') != -1 ) return true;
@ -59,13 +59,19 @@ var extractCoordinates = function(kml) {
var doc = libxmljs.parseXmlString(kml);
//console.log("doc: " + doc);
if ( ! doc ) return;
if ( ! doc ) {
return;
}
var coo = doc.get("//coordinates");
//console.log("coo: " + coo);
if ( ! coo ) return;
if ( ! coo ) {
return;
}
coo = coo.text();
//console.log("coo: " + coo);
if ( ! coo ) return;
if ( ! coo ) {
return;
}
coo = coo.split(' ');
//console.log("coo: " + coo);
for (var i=0; i<coo.length; ++i) {
@ -84,19 +90,25 @@ var extractFolderName = function(kml) {
var doc = libxmljs.parseXmlString(kml);
//console.log("doc: " + doc);
if ( ! doc ) return;
if ( ! doc ) {
return;
}
var coo = doc.get("//Document/Folder/name");
//console.log("coo: " + coo);
if ( ! coo ) return;
if ( ! coo ) {
return;
}
coo = coo.text();
//console.log("coo: " + coo);
if ( ! coo ) return;
if ( ! coo ) {
return;
}
return coo;
};
// KML tests
test('KML format, unauthenticated', function(done){
it('KML format, unauthenticated', function(done){
assert.response(app, {
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=kml',
headers: {host: 'vizzuality.cartodb.com'},
@ -119,7 +131,7 @@ test('KML format, unauthenticated', function(done){
});
});
test('KML format, unauthenticated, POST', function(done){
it('KML format, unauthenticated, POST', function(done){
assert.response(app, {
url: '/api/v1/sql',
data: 'q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=kml',
@ -134,7 +146,7 @@ test('KML format, unauthenticated, POST', function(done){
});
});
test('KML format, bigger than 81920 bytes', function(done){
it('KML format, bigger than 81920 bytes', function(done){
assert.response(app, {
url: '/api/v1/sql',
data: querystring.stringify({
@ -153,7 +165,7 @@ test('KML format, bigger than 81920 bytes', function(done){
});
});
test('KML format, skipfields', function(done){
it('KML format, skipfields', function(done){
assert.response(app, {
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=kml&skipfields=address,cartodb_id',
headers: {host: 'vizzuality.cartodb.com'},
@ -176,7 +188,7 @@ test('KML format, skipfields', function(done){
});
});
test('KML format, unauthenticated, custom filename', function(done){
it('KML format, unauthenticated, custom filename', function(done){
assert.response(app, {
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=kml&filename=kmltest',
headers: {host: 'vizzuality.cartodb.com'},
@ -192,7 +204,7 @@ test('KML format, unauthenticated, custom filename', function(done){
});
});
test('KML format, authenticated', function(done){
it('KML format, authenticated', function(done){
assert.response(app, {
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=kml&api_key=1234',
headers: {host: 'vizzuality.cartodb.com'},
@ -205,15 +217,42 @@ test('KML format, authenticated', function(done){
});
});
test('KML format, unauthenticated, concurrent requests', function(done){
it('KML format, unauthenticated, concurrent requests', function(done){
var query = querystring.stringify({
q: "SELECT 'val', x, y, st_setsrid(st_makepoint(x,y),4326) as the_geom FROM generate_series(-180, 180) as x, generate_series(-90,90) y",
q: "SELECT 'val', x, y, st_setsrid(st_makepoint(x,y),4326) as the_geom " +
"FROM generate_series(-180, 180) as x, generate_series(-90,90) y",
format: 'kml',
filename: 'multi'
});
var concurrency = 4;
var waiting = concurrency;
function onResponse(res) {
//console.log("Response started");
res.body = '';
//res.setEncoding('binary');
res.on('data', function(chunk){ res.body += chunk; });
res.on('end', function(){
//console.log("Response ended");
assert.equal(res.statusCode, 200, res.body);
assert.ok(res.body);
var snippet = res.body.substr(0, 5);
assert.equal(snippet, "<?xml");
var cd = res.headers['content-disposition'];
assert.equal(true, /^attachment/.test(cd), 'KML is not disposed as attachment: ' + cd);
assert.equal(true, /filename=multi.kml/gi.test(cd), 'Unexpected KML filename: ' + cd);
if ( ! --waiting ) {
app.close();
done();
}
});
}
function onError(err) {
console.log("Response error" + err);
}
server_utils.startOnNextPort(app, function() {
var port = app.address().port;
//console.log("Listening on port " + port);
@ -225,34 +264,16 @@ test('KML format, unauthenticated, concurrent requests', function(done){
path: '/api/v1/sql?' + query,
headers: {host: 'vizzuality.cartodb.com'},
agent: false // or should this be true ?
}).on('response', function(res) {
//console.log("Response started");
res.body = '';
//res.setEncoding('binary');
res.on('data', function(chunk){ res.body += chunk; });
res.on('end', function(){
//console.log("Response ended");
assert.equal(res.statusCode, 200, res.body);
assert.ok(res.body);
var snippet = res.body.substr(0, 5);
assert.equal(snippet, "<?xml");
var cd = res.headers['content-disposition'];
assert.equal(true, /^attachment/.test(cd), 'KML is not disposed as attachment: ' + cd);
assert.equal(true, /filename=multi.kml/gi.test(cd), 'Unexpected KML filename: ' + cd);
if ( ! --waiting ) {
app.close();
done();
}
});
}).on('error', function(err) {
console.log("Response error" + err);
}).end();
})
.on('response', onResponse)
.on('error', onError)
.end();
}
});
});
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/60
test('GET /api/v1/sql as kml with no rows', function(done){
it('GET /api/v1/sql as kml with no rows', function(done){
assert.response(app, {
url: '/api/v1/sql?q=SELECT%20true%20WHERE%20false&format=kml',
headers: {host: 'vizzuality.cartodb.com'},
@ -260,7 +281,10 @@ test('GET /api/v1/sql as kml with no rows', function(done){
},{ }, function(res){
assert.equal(res.statusCode, 200, res.body);
// NOTE: GDAL-1.11+ added 'id="root_doc"' attribute to the output
var pat = new RegExp('^<\\?xml version="1.0" encoding="utf-8" \\?><kml xmlns="http://www.opengis.net/kml/2.2"><Document( id="root_doc")?><Folder><name>cartodb_query</name></Folder></Document></kml>$');
var pat = new RegExp('^<\\?xml version="1.0" encoding="utf-8" \\?>' +
'<kml xmlns="http://www.opengis.net/kml/2.2">' +
'<Document( id="root_doc")?><Folder><name>cartodb_query</name></Folder></Document>' +
'</kml>$');
var body = res.body.replace(/\n/g,'');
assert.ok(body.match(pat),
"Response:\n" + body + '\ndoes not match pattern:\n' + pat);
@ -269,7 +293,7 @@ test('GET /api/v1/sql as kml with no rows', function(done){
});
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/90
test('GET /api/v1/sql as kml with ending semicolon', function(done){
it('GET /api/v1/sql as kml with ending semicolon', function(done){
assert.response(app, {
url: '/api/v1/sql?' + querystring.stringify({
q: 'SELECT true WHERE false;',
@ -280,7 +304,10 @@ test('GET /api/v1/sql as kml with ending semicolon', function(done){
},{ }, function(res){
assert.equal(res.statusCode, 200, res.body);
// NOTE: GDAL-1.11+ added 'id="root_doc"' attribute to the output
var pat = new RegExp('^<\\?xml version="1.0" encoding="utf-8" \\?><kml xmlns="http://www.opengis.net/kml/2.2"><Document( id="root_doc")?><Folder><name>cartodb_query</name></Folder></Document></kml>$');
var pat = new RegExp('^<\\?xml version="1.0" encoding="utf-8" \\?>' +
'<kml xmlns="http://www.opengis.net/kml/2.2">' +
'<Document( id="root_doc")?><Folder><name>cartodb_query</name></Folder></Document>' +
'</kml>$');
var body = res.body.replace(/\n/g,'');
assert.ok(body.match(pat),
"Response:\n" + body + '\ndoes not match pattern:\n' + pat);
@ -289,7 +316,7 @@ test('GET /api/v1/sql as kml with ending semicolon', function(done){
});
// See https://github.com/CartoDB/cartodb/issues/276
test('check point coordinates, unauthenticated', function(done){
it('check point coordinates, unauthenticated', function(done){
assert.response(app, {
url: '/api/v1/sql?' + querystring.stringify({
q: 'SELECT * from untitle_table_4 WHERE cartodb_id = -1',
@ -307,7 +334,7 @@ test('check point coordinates, unauthenticated', function(done){
});
// See https://github.com/CartoDB/cartodb/issues/276
test('check point coordinates, authenticated', function(done){
it('check point coordinates, authenticated', function(done){
assert.response(app, {
url: '/api/v1/sql?' + querystring.stringify({
q: 'SELECT * from untitle_table_4 WHERE cartodb_id = -1',
@ -325,7 +352,7 @@ test('check point coordinates, authenticated', function(done){
});
});
test('expects 1000 placemarks in public table', function(done){
it('expects 1000 placemarks in public table', function(done){
var numberOfRowsInPublicTable = 6,
seriesLimit = 200,
expectedRows = numberOfRowsInPublicTable * seriesLimit;
@ -349,7 +376,7 @@ test('check point coordinates, authenticated', function(done){
);
});
test('expects 1000 placemarks in private table using the API KEY', function(done){
it('expects 1000 placemarks in private table using the API KEY', function(done){
var numberOfRowsInPrivateTable = 5,
seriesLimit = 200,
expectedRows = numberOfRowsInPrivateTable * seriesLimit;

View File

@ -1,24 +1,20 @@
require('../../helper');
require('../../support/assert');
var app = require(global.settings.app_root + '/app/controllers/app')()
, assert = require('assert')
, querystring = require('querystring')
, _ = require('underscore')
, zipfile = require('zipfile')
, fs = require('fs')
, libxmljs = require('libxmljs')
;
var app = require(global.settings.app_root + '/app/controllers/app')();
var assert = require('../../support/assert');
var querystring = require('querystring');
var _ = require('underscore');
var zipfile = require('zipfile');
var fs = require('fs');
// allow lots of emitters to be set to silence warning
app.setMaxListeners(0);
suite('export.shapefile', function() {
describe('export.shapefile', function() {
// SHP tests
test('SHP format, unauthenticated', function(done){
it('SHP format, unauthenticated', function(done){
assert.response(app, {
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=shp',
headers: {host: 'vizzuality.cartodb.com'},
@ -31,7 +27,9 @@ test('SHP format, unauthenticated', function(done){
assert.equal(true, /filename=cartodb-query.zip/gi.test(cd));
var tmpfile = '/tmp/myshape.zip';
var err = fs.writeFileSync(tmpfile, res.body, 'binary');
if (err) { done(err); return }
if (err) {
return done(err);
}
var zf = new zipfile.ZipFile(tmpfile);
assert.ok(_.contains(zf.names, 'cartodb-query.shp'), 'SHP zipfile does not contain .shp: ' + zf.names);
assert.ok(_.contains(zf.names, 'cartodb-query.shx'), 'SHP zipfile does not contain .shx: ' + zf.names);
@ -43,7 +41,7 @@ test('SHP format, unauthenticated', function(done){
});
});
test('SHP format, unauthenticated, POST', function(done){
it('SHP format, unauthenticated, POST', function(done){
assert.response(app, {
url: '/api/v1/sql',
data: 'q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=shp',
@ -58,7 +56,7 @@ test('SHP format, unauthenticated, POST', function(done){
});
});
test('SHP format, big size, POST', function(done){
it('SHP format, big size, POST', function(done){
assert.response(app, {
url: '/api/v1/sql',
data: querystring.stringify({
@ -77,7 +75,7 @@ test('SHP format, big size, POST', function(done){
});
});
test('SHP format, unauthenticated, with custom filename', function(done){
it('SHP format, unauthenticated, with custom filename', function(done){
assert.response(app, {
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=shp&filename=myshape',
headers: {host: 'vizzuality.cartodb.com'},
@ -90,7 +88,9 @@ test('SHP format, unauthenticated, with custom filename', function(done){
assert.equal(true, /filename=myshape.zip/gi.test(cd));
var tmpfile = '/tmp/myshape.zip';
var err = fs.writeFileSync(tmpfile, res.body, 'binary');
if (err) { done(err); return }
if (err) {
return done(err);
}
var zf = new zipfile.ZipFile(tmpfile);
assert.ok(_.contains(zf.names, 'myshape.shp'), 'SHP zipfile does not contain .shp: ' + zf.names);
assert.ok(_.contains(zf.names, 'myshape.shx'), 'SHP zipfile does not contain .shx: ' + zf.names);
@ -101,7 +101,7 @@ test('SHP format, unauthenticated, with custom filename', function(done){
});
});
test('SHP format, unauthenticated, with custom, dangerous filename', function(done){
it('SHP format, unauthenticated, with custom, dangerous filename', function(done){
assert.response(app, {
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=shp&filename=b;"%20()[]a',
headers: {host: 'vizzuality.cartodb.com'},
@ -115,7 +115,9 @@ test('SHP format, unauthenticated, with custom, dangerous filename', function(do
assert.equal(true, /filename=b_______a.zip/gi.test(cd), 'Unexpected SHP filename: ' + cd);
var tmpfile = '/tmp/myshape.zip';
var err = fs.writeFileSync(tmpfile, res.body, 'binary');
if (err) { done(err); return }
if (err) {
return done(err);
}
var zf = new zipfile.ZipFile(tmpfile);
assert.ok(_.contains(zf.names, fname + '.shp'), 'SHP zipfile does not contain .shp: ' + zf.names);
assert.ok(_.contains(zf.names, fname + '.shx'), 'SHP zipfile does not contain .shx: ' + zf.names);
@ -126,7 +128,7 @@ test('SHP format, unauthenticated, with custom, dangerous filename', function(do
});
});
test('SHP format, authenticated', function(done){
it('SHP format, authenticated', function(done){
assert.response(app, {
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=shp&api_key=1234',
headers: {host: 'vizzuality.cartodb.com'},
@ -138,7 +140,9 @@ test('SHP format, authenticated', function(done){
assert.equal(true, /filename=cartodb-query.zip/gi.test(cd));
var tmpfile = '/tmp/myshape.zip';
var err = fs.writeFileSync(tmpfile, res.body, 'binary');
if (err) { done(err); return }
if (err) {
return done(err);
}
var zf = new zipfile.ZipFile(tmpfile);
assert.ok(_.contains(zf.names, 'cartodb-query.shp'), 'SHP zipfile does not contain .shp: ' + zf.names);
assert.ok(_.contains(zf.names, 'cartodb-query.shx'), 'SHP zipfile does not contain .shx: ' + zf.names);
@ -152,7 +156,7 @@ test('SHP format, authenticated', function(done){
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/66
test('SHP format, unauthenticated, with utf8 data', function(done){
it('SHP format, unauthenticated, with utf8 data', function(done){
var query = querystring.stringify({
q: "SELECT '♥♦♣♠' as f, st_makepoint(0,0,4326) as the_geom",
format: 'shp',
@ -167,7 +171,9 @@ test('SHP format, unauthenticated, with utf8 data', function(done){
assert.equal(res.statusCode, 200, res.body);
var tmpfile = '/tmp/myshape.zip';
var err = fs.writeFileSync(tmpfile, res.body, 'binary');
if (err) { done(err); return }
if (err) {
return done(err);
}
var zf = new zipfile.ZipFile(tmpfile);
var buffer = zf.readFileSync('myshape.dbf');
fs.unlinkSync(tmpfile);
@ -178,10 +184,9 @@ test('SHP format, unauthenticated, with utf8 data', function(done){
});
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/66
test('mixed type geometry', function(done){
it('mixed type geometry', function(done){
var query = querystring.stringify({
q: "SELECT 'POINT(0 0)'::geometry as g UNION ALL "
+ "SELECT 'LINESTRING(0 0, 1 0)'::geometry",
q: "SELECT 'POINT(0 0)'::geometry as g UNION ALL SELECT 'LINESTRING(0 0, 1 0)'::geometry",
format: 'shp'
});
assert.response(app, {
@ -194,19 +199,19 @@ test('mixed type geometry', function(done){
assert.deepEqual(res.headers['content-disposition'], 'inline');
assert.equal(res.statusCode, 400, res.statusCode + ': ' +res.body);
var parsedBody = JSON.parse(res.body);
var expectedBody = {"error":["ERROR 1: Attempt to write non-point (LINESTRING) geometry to point shapefile."]}
var expectedBody = {"error":["ERROR 1: Attempt to write non-point (LINESTRING) geometry to point shapefile."]};
assert.deepEqual(parsedBody, expectedBody);
done();
});
});
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/87
test('errors are not confused with warnings', function(done){
it('errors are not confused with warnings', function(done){
var query = querystring.stringify({
q: "SELECT 'POINT(0 0)'::geometry as g"
+ ", 1 as a_very_very_very_long_field_name"
+ " UNION ALL "
+ "SELECT 'LINESTRING(0 0, 1 0)'::geometry, 2",
q: [
"SELECT 'POINT(0 0)'::geometry as g, 1 as a_very_very_very_long_field_name",
"SELECT 'LINESTRING(0 0, 1 0)'::geometry, 2"
].join(" UNION ALL "),
format: 'shp'
});
assert.response(app, {
@ -219,13 +224,13 @@ test('errors are not confused with warnings', function(done){
assert.deepEqual(res.headers['content-disposition'], 'inline');
assert.equal(res.statusCode, 400, res.statusCode + ': ' +res.body);
var parsedBody = JSON.parse(res.body);
var expectedBody = {"error":["ERROR 1: Attempt to write non-point (LINESTRING) geometry to point shapefile."]}
var expectedBody = {"error":["ERROR 1: Attempt to write non-point (LINESTRING) geometry to point shapefile."]};
assert.deepEqual(parsedBody, expectedBody);
done();
});
});
test('skipfields controls fields included in SHP output', function(done){
it('skipfields controls fields included in SHP output', function(done){
var query = querystring.stringify({
q: "SELECT 111 as skipme, 222 as keepme, 'POINT(0 0)'::geometry as g",
format: 'shp',
@ -241,7 +246,9 @@ test('skipfields controls fields included in SHP output', function(done){
assert.equal(res.statusCode, 200, res.body);
var tmpfile = '/tmp/myshape.zip';
var err = fs.writeFileSync(tmpfile, res.body, 'binary');
if (err) { done(err); return }
if (err) {
return done(err);
}
var zf = new zipfile.ZipFile(tmpfile);
var buffer = zf.readFileSync('myshape.dbf');
fs.unlinkSync(tmpfile);
@ -251,37 +258,48 @@ test('skipfields controls fields included in SHP output', function(done){
});
});
test('SHP format, concurrently', function(done){
it('SHP format, concurrently', function(done){
var concurrency = 1;
var waiting = concurrency;
function validate(res){
var cd = res.header('Content-Disposition');
assert.equal(true, /^attachment/.test(cd), 'SHP is not disposed as attachment: ' + cd);
assert.equal(true, /filename=cartodb-query.zip/gi.test(cd));
var tmpfile = '/tmp/myshape.zip';
var err = fs.writeFileSync(tmpfile, res.body, 'binary');
if (err) {
return done(err);
}
var zf = new zipfile.ZipFile(tmpfile);
assert.ok(_.contains(zf.names, 'cartodb-query.shp'), 'SHP zipfile does not contain .shp: ' + zf.names);
assert.ok(_.contains(zf.names, 'cartodb-query.shx'), 'SHP zipfile does not contain .shx: ' + zf.names);
assert.ok(_.contains(zf.names, 'cartodb-query.dbf'), 'SHP zipfile does not contain .dbf: ' + zf.names);
assert.ok(_.contains(zf.names, 'cartodb-query.prj'), 'SHP zipfile does not contain .prj: ' + zf.names);
// TODO: check DBF contents
fs.unlinkSync(tmpfile);
if ( ! --waiting ) {
done();
}
}
for (var i=0; i<concurrency; ++i) {
assert.response(app, {
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=shp',
headers: {host: 'vizzuality.cartodb.com'},
encoding: 'binary',
method: 'GET'
},{ }, function(res){
assert.equal(res.statusCode, 200, res.body);
var cd = res.header('Content-Disposition');
assert.equal(true, /^attachment/.test(cd), 'SHP is not disposed as attachment: ' + cd);
assert.equal(true, /filename=cartodb-query.zip/gi.test(cd));
var tmpfile = '/tmp/myshape.zip';
var err = fs.writeFileSync(tmpfile, res.body, 'binary');
if (err) { done(err); return }
var zf = new zipfile.ZipFile(tmpfile);
assert.ok(_.contains(zf.names, 'cartodb-query.shp'), 'SHP zipfile does not contain .shp: ' + zf.names);
assert.ok(_.contains(zf.names, 'cartodb-query.shx'), 'SHP zipfile does not contain .shx: ' + zf.names);
assert.ok(_.contains(zf.names, 'cartodb-query.dbf'), 'SHP zipfile does not contain .dbf: ' + zf.names);
assert.ok(_.contains(zf.names, 'cartodb-query.prj'), 'SHP zipfile does not contain .prj: ' + zf.names);
// TODO: check DBF contents
fs.unlinkSync(tmpfile);
if ( ! --waiting ) done();
});
assert.response(
app,
{
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=shp',
headers: {host: 'vizzuality.cartodb.com'},
encoding: 'binary',
method: 'GET'
},
{
status: 200
},
validate
);
}
});
// See https://github.com/CartoDB/CartoDB-SQL-API/issues/111
test('point with null first', function(done){
it('point with null first', function(done){
var query = querystring.stringify({
q: "SELECT null::geometry as g UNION ALL SELECT 'SRID=4326;POINT(0 0)'::geometry",
format: 'shp'
@ -297,7 +315,9 @@ test('point with null first', function(done){
assert.equal(true, /filename=cartodb-query.zip/gi.test(cd));
var tmpfile = '/tmp/myshape.zip';
var err = fs.writeFileSync(tmpfile, res.body, 'binary');
if (err) { done(err); return }
if (err) {
return done(err);
}
var zf = new zipfile.ZipFile(tmpfile);
assert.ok(_.contains(zf.names, 'cartodb-query.shp'), 'SHP zipfile does not contain .shp: ' + zf.names);
assert.ok(_.contains(zf.names, 'cartodb-query.shx'), 'SHP zipfile does not contain .shx: ' + zf.names);

View File

@ -1,22 +1,15 @@
require('../../helper');
require('../../support/assert');
var app = require(global.settings.app_root + '/app/controllers/app')()
, assert = require('assert')
, querystring = require('querystring')
, _ = require('underscore')
, zipfile = require('zipfile')
, fs = require('fs')
, libxmljs = require('libxmljs')
;
var app = require(global.settings.app_root + '/app/controllers/app')();
var assert = require('../../support/assert');
var querystring = require('querystring');
// allow lots of emitters to be set to silence warning
app.setMaxListeners(0);
suite('export.svg', function() {
describe('export.svg', function() {
test('GET /api/v1/sql with SVG format', function(done){
it('GET /api/v1/sql with SVG format', function(done){
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"
@ -36,7 +29,7 @@ test('GET /api/v1/sql with SVG format', function(done){
});
});
test('POST /api/v1/sql with SVG format', function(done){
it('POST /api/v1/sql with SVG format', function(done){
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"
@ -58,7 +51,7 @@ test('POST /api/v1/sql with SVG format', function(done){
});
});
test('GET /api/v1/sql with SVG format and custom filename', function(done){
it('GET /api/v1/sql with SVG format and custom filename', function(done){
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",
@ -79,7 +72,7 @@ test('GET /api/v1/sql with SVG format and custom filename', function(done){
});
});
test('GET /api/v1/sql with SVG format and centered point', function(done){
it('GET /api/v1/sql with SVG format and centered point', function(done){
var query = querystring.stringify({
q: "SELECT 1 as cartodb_id, ST_MakePoint(5000, -54) AS the_geom ",
format: "svg"
@ -100,7 +93,7 @@ test('GET /api/v1/sql with SVG format and centered point', function(done){
});
});
test('GET /api/v1/sql with SVG format and trimmed decimals', function(done){
it('GET /api/v1/sql with SVG format and trimmed decimals', function(done){
var queryobj = {
q: "SELECT 1 as cartodb_id, 'LINESTRING(0 0, 1024 768, 500.123456 600.98765432)'::geometry AS the_geom ",
format: "svg",
@ -138,7 +131,7 @@ test('GET /api/v1/sql with SVG format and trimmed decimals', function(done){
// Test adding "the_geom" to skipfields
// See http://github.com/Vizzuality/CartoDB-SQL-API/issues/73
test('SVG format with "the_geom" in skipfields', function(done){
it('SVG format with "the_geom" in skipfields', function(done){
var query = querystring.stringify({
q: "SELECT 1 as cartodb_id, ST_MakePoint(5000, -54) AS the_geom ",
format: "svg",
@ -159,7 +152,7 @@ test('SVG format with "the_geom" in skipfields', function(done){
});
});
test('SVG format with missing "the_geom" field', function(done){
it('SVG format with missing "the_geom" field', function(done){
var query = querystring.stringify({
q: "SELECT 1 as cartodb_id, ST_MakePoint(5000, -54) AS something_else ",
format: "svg"

View File

@ -1,15 +1,9 @@
require('../../helper');
require('../../support/assert');
var app = require(global.settings.app_root + '/app/controllers/app')()
, assert = require('assert')
, querystring = require('querystring')
, _ = require('underscore')
, zipfile = require('zipfile')
, fs = require('fs')
, libxmljs = require('libxmljs')
;
var app = require(global.settings.app_root + '/app/controllers/app')();
var assert = require('../../support/assert');
var querystring = require('querystring');
var _ = require('underscore');
// allow lots of emitters to be set to silence warning
app.setMaxListeners(0);
@ -81,8 +75,8 @@ test('GET two polygons sharing an edge as topojson', function(done){
assert.equal(shell[1], 1); /* non-shared arc */
var props = obj.properties;
assert.equal(_.keys(props).length, 2); // gid, name
assert.equal(props['gid'], 1);
assert.equal(props['name'], 'U');
assert.equal(props.gid, 1);
assert.equal(props.name, 'U');
obj = topojson.objects[1];
//console.dir(obj);
@ -99,8 +93,8 @@ test('GET two polygons sharing an edge as topojson', function(done){
assert.equal(shell[1], 2); /* non-shared arc */
props = obj.properties;
assert.equal(_.keys(props).length, 2); // gid, name
assert.equal(props['gid'], 2);
assert.equal(props['name'], 'D');
assert.equal(props.gid, 2);
assert.equal(props.name, 'D');
// Check arcs
assert.ok(topojson.hasOwnProperty('arcs'));
@ -185,8 +179,8 @@ test('null geometries', function(done){
assert.equal(shell[0], 0); /* non-shared arc */
var props = obj.properties;
assert.equal(_.keys(props).length, 2); // gid, name
assert.equal(props['gid'], 1);
assert.equal(props['name'], 'U');
assert.equal(props.gid, 1);
assert.equal(props.name, 'U');
// Check arcs
assert.ok(topojson.hasOwnProperty('arcs'));
@ -233,10 +227,12 @@ test('null geometries', function(done){
function(res) {
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
var didRunJsonCallback = false;
// jshint ignore:start
function foo_jsonp(body) {
didRunJsonCallback = true;
}
eval(res.body);
// jshint ignore:end
assert.ok(didRunJsonCallback);
done();
}