diff --git a/test/acceptance/export/arraybuffer.js b/test/acceptance/export/arraybuffer.js index 5d2f6330..2ecaa36b 100644 --- a/test/acceptance/export/arraybuffer.js +++ b/test/acceptance/export/arraybuffer.js @@ -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', diff --git a/test/acceptance/export/csv.js b/test/acceptance/export/csv.js index af5e8e2f..07b1ee74 100644 --- a/test/acceptance/export/csv.js +++ b/test/acceptance/export/csv.js @@ -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-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", diff --git a/test/acceptance/export/kml.js b/test/acceptance/export/kml.js index 68a8fe0b..4bf5b685 100644 --- a/test/acceptance/export/kml.js +++ b/test/acceptance/export/kml.js @@ -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; icartodb_query$'); + var pat = new RegExp('^<\\?xml version="1.0" encoding="utf-8" \\?>' + + '' + + 'cartodb_query' + + '$'); 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" \\?>cartodb_query$'); + var pat = new RegExp('^<\\?xml version="1.0" encoding="utf-8" \\?>' + + '' + + 'cartodb_query' + + '$'); 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; diff --git a/test/acceptance/export/shapefile.js b/test/acceptance/export/shapefile.js index 4e9070c1..882a0572 100644 --- a/test/acceptance/export/shapefile.js +++ b/test/acceptance/export/shapefile.js @@ -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