From 73478ed0e909332e021119946ea5c28914c8bf05 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Fri, 5 Feb 2016 12:35:01 +0100 Subject: [PATCH 1/5] Rename widgets tests file --- test/acceptance/{ => widgets}/widgets.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) rename test/acceptance/{ => widgets}/widgets.js (98%) diff --git a/test/acceptance/widgets.js b/test/acceptance/widgets/widgets.js similarity index 98% rename from test/acceptance/widgets.js rename to test/acceptance/widgets/widgets.js index 2f4483f5..0b2b5eb1 100644 --- a/test/acceptance/widgets.js +++ b/test/acceptance/widgets/widgets.js @@ -1,14 +1,13 @@ -var assert = require('../support/assert'); +var assert = require('../../support/assert'); var step = require('step'); var qs = require('querystring'); -var helper = require(__dirname + '/../support/test_helper'); -var LayergroupToken = require('../../lib/cartodb/models/layergroup_token'); +var helper = require('../../support/test_helper'); +var LayergroupToken = require('../../../lib/cartodb/models/layergroup_token'); -var CartodbWindshaft = require('../../lib/cartodb/server'); -var serverOptions = require('../../lib/cartodb/server_options'); +var CartodbWindshaft = require('../../../lib/cartodb/server'); +var serverOptions = require('../../../lib/cartodb/server_options'); var server = new CartodbWindshaft(serverOptions); -server.setMaxListeners(0); describe('widgets', function() { From d78e01b7a49805f3adf1d6602881a61179cd2a8e Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Fri, 5 Feb 2016 12:59:33 +0100 Subject: [PATCH 2/5] Extract getWidget to TestClient --- test/acceptance/widgets/widgets.js | 167 ++++++----------------------- test/support/test-client.js | 119 ++++++++++++++++++++ 2 files changed, 153 insertions(+), 133 deletions(-) create mode 100644 test/support/test-client.js diff --git a/test/acceptance/widgets/widgets.js b/test/acceptance/widgets/widgets.js index 0b2b5eb1..501829ed 100644 --- a/test/acceptance/widgets/widgets.js +++ b/test/acceptance/widgets/widgets.js @@ -1,120 +1,8 @@ var assert = require('../../support/assert'); -var step = require('step'); -var qs = require('querystring'); - -var helper = require('../../support/test_helper'); -var LayergroupToken = require('../../../lib/cartodb/models/layergroup_token'); - -var CartodbWindshaft = require('../../../lib/cartodb/server'); -var serverOptions = require('../../../lib/cartodb/server_options'); -var server = new CartodbWindshaft(serverOptions); - +var TestClient = require('../../support/test-client'); describe('widgets', function() { - var keysToDelete; - - beforeEach(function() { - keysToDelete = {}; - }); - - afterEach(function(done) { - helper.deleteRedisKeys(keysToDelete, done); - }); - - function getWidget(mapConfig, widgetName, params, callback) { - if (!callback) { - callback = params; - params = {}; - } - - var url = '/api/v1/map'; - if (params && params.filters) { - url += '?' + qs.stringify({ filters: JSON.stringify(params.filters) }); - } - - var layergroupId; - step( - function createLayergroup() { - var next = this; - assert.response(server, - { - url: url, - method: 'POST', - headers: { - host: 'localhost', - 'Content-Type': 'application/json' - }, - data: JSON.stringify(mapConfig) - }, - { - status: 200, - headers: { - 'Content-Type': 'application/json; charset=utf-8' - } - }, - function(res, err) { - if (err) { - return next(err); - } - var parsedBody = JSON.parse(res.body); - var expectedWidgetURLS = { - http: "/api/v1/map/" + parsedBody.layergroupid + "/0/widget/" + widgetName - }; - assert.ok(parsedBody.metadata.layers[0].widgets[widgetName]); - assert.ok( - parsedBody.metadata.layers[0].widgets[widgetName].url.http.match(expectedWidgetURLS.http) - ); - return next(null, parsedBody.layergroupid); - } - ); - }, - function getWidgetResult(err, _layergroupId) { - assert.ifError(err); - - var next = this; - layergroupId = _layergroupId; - - var urlParams = { - own_filter: params.hasOwnProperty('own_filter') ? params.own_filter : 1 - }; - if (params && params.bbox) { - urlParams.bbox = params.bbox; - } - url = '/api/v1/map/' + layergroupId + '/0/widget/' + widgetName + '?' + qs.stringify(urlParams); - - assert.response(server, - { - url: url, - method: 'GET', - headers: { - host: 'localhost' - } - }, - { - status: 200, - headers: { - 'Content-Type': 'application/json; charset=utf-8' - } - }, - function(res, err) { - if (err) { - return next(err); - } - - next(null, res); - } - ); - }, - function finish(err, res) { - keysToDelete['map_cfg|' + LayergroupToken.parse(layergroupId).token] = 0; - keysToDelete['user:localhost:mapviews:global'] = 5; - return callback(err, res); - } - ); - } - - it("should expose layer list", function(done) { var listWidgetMapConfig = { @@ -139,7 +27,9 @@ describe('widgets', function() { ] }; - getWidget(listWidgetMapConfig, 'names', function(err, res) { + var testClient = new TestClient(listWidgetMapConfig); + + testClient.getWidget('names', function(err, res) { if (err) { return done(err); } @@ -153,7 +43,7 @@ describe('widgets', function() { ]; assert.deepEqual(JSON.parse(res.body).rows, expectedList); - done(); + testClient.drain(done); }); }); @@ -179,7 +69,10 @@ describe('widgets', function() { } ] }; - getWidget(histogramMapConfig, 'pop_max', function(err, res) { + + var testClient = new TestClient(histogramMapConfig); + + testClient.getWidget('pop_max', function(err, res) { if (err) { return done(err); } @@ -187,7 +80,7 @@ describe('widgets', function() { var histogram = JSON.parse(res.body); assert.ok(histogram.bins.length); - done(); + testClient.drain(done); }); }); @@ -218,7 +111,8 @@ describe('widgets', function() { }; it("should expose an aggregation", function(done) { - getWidget(aggregationMapConfig, 'country_places_count', { own_filter: 0 }, function(err, res) { + var testClient = new TestClient(aggregationMapConfig); + testClient.getWidget('country_places_count', { own_filter: 0 }, function(err, res) { if (err) { return done(err); } @@ -227,7 +121,7 @@ describe('widgets', function() { assert.equal(aggregation.categories.length, 6); assert.deepEqual(aggregation.categories[0], { value: 769, category: 'USA', agg: false }); - done(); + testClient.drain(done); }); }); @@ -239,7 +133,8 @@ describe('widgets', function() { ] } }; - getWidget(aggregationMapConfig, 'country_places_count', params, function(err, res) { + var testClient = new TestClient(aggregationMapConfig); + testClient.getWidget('country_places_count', params, function(err, res) { if (err) { return done(err); } @@ -248,7 +143,7 @@ describe('widgets', function() { assert.equal(aggregation.categories.length, 1); assert.deepEqual(aggregation.categories[0], { value: 256, category: 'CAN', agg: false }); - done(); + testClient.drain(done); }); }); }); @@ -277,7 +172,8 @@ describe('widgets', function() { }; it("should expose an histogram", function(done) { - getWidget(histogramMapConfig, 'country_places_histogram', { own_filter: 0 }, function(err, res) { + var testClient = new TestClient(histogramMapConfig); + testClient.getWidget('country_places_histogram', { own_filter: 0 }, function(err, res) { if (err) { return done(err); } @@ -289,7 +185,7 @@ describe('widgets', function() { { bin: 0, freq: 6497, min: 0, max: 742572, avg: 113511.16823149147 } ); - done(); + testClient.drain(done); }); }); @@ -303,7 +199,8 @@ describe('widgets', function() { ] } }; - getWidget(histogramMapConfig, 'country_places_histogram', params, function(err, res) { + var testClient = new TestClient(histogramMapConfig); + testClient.getWidget('country_places_histogram', params, function(err, res) { if (err) { return done(err); } @@ -318,7 +215,7 @@ describe('widgets', function() { avg: 5815009.596774193 }); - done(); + testClient.drain(done); }); }); }); @@ -363,7 +260,8 @@ describe('widgets', function() { ] } }; - getWidget(combinedWidgetsMapConfig, 'country_places_count', params, function(err, res) { + var testClient = new TestClient(combinedWidgetsMapConfig); + testClient.getWidget('country_places_count', params, function(err, res) { if (err) { return done(err); } @@ -378,7 +276,7 @@ describe('widgets', function() { return sum + (row.category === 'CHN' ? 1 : 0); }, 0), 0); - done(); + testClient.drain(done); }); }); @@ -393,7 +291,8 @@ describe('widgets', function() { ] } }; - getWidget(combinedWidgetsMapConfig, 'country_places_count', params, function(err, res) { + var testClient = new TestClient(combinedWidgetsMapConfig); + testClient.getWidget('country_places_count', params, function(err, res) { if (err) { return done(err); } @@ -408,7 +307,7 @@ describe('widgets', function() { return sum + (row.category === 'CHN' ? 1 : 0); }, 0), 0); - done(); + testClient.drain(done); }); }); @@ -423,7 +322,8 @@ describe('widgets', function() { }, bbox: '-20,0,45,60' }; - getWidget(combinedWidgetsMapConfig, 'country_places_count', params, function(err, res) { + var testClient = new TestClient(combinedWidgetsMapConfig); + testClient.getWidget('country_places_count', params, function(err, res) { if (err) { return done(err); } @@ -438,7 +338,7 @@ describe('widgets', function() { return sum + (row.category === 'CHN' ? 1 : 0); }, 0), 0); - done(); + testClient.drain(done); }); }); @@ -454,7 +354,8 @@ describe('widgets', function() { }, bbox: '-20,0,45,60' }; - getWidget(combinedWidgetsMapConfig, 'country_places_count', params, function(err, res) { + var testClient = new TestClient(combinedWidgetsMapConfig); + testClient.getWidget('country_places_count', params, function(err, res) { if (err) { return done(err); } @@ -469,7 +370,7 @@ describe('widgets', function() { return sum + (row.category === 'CHN' ? 1 : 0); }, 0), 0); - done(); + testClient.drain(done); }); }); }); diff --git a/test/support/test-client.js b/test/support/test-client.js new file mode 100644 index 00000000..d2df611b --- /dev/null +++ b/test/support/test-client.js @@ -0,0 +1,119 @@ +'use strict'; + +var qs = require('querystring'); +var step = require('step'); + +var LayergroupToken = require('../../lib/cartodb/models/layergroup_token'); + +var assert = require('./assert'); +var helper = require('./test_helper'); + +var CartodbWindshaft = require('../../lib/cartodb/server'); +var serverOptions = require('../../lib/cartodb/server_options'); +var server = new CartodbWindshaft(serverOptions); + + +function TestClient(mapConfig) { + this.mapConfig = mapConfig; + this.keysToDelete = {}; +} + +module.exports = TestClient; + +TestClient.prototype.getWidget = function(widgetName, params, callback) { + var self = this; + + if (!callback) { + callback = params; + params = {}; + } + + var url = '/api/v1/map'; + if (params && params.filters) { + url += '?' + qs.stringify({ filters: JSON.stringify(params.filters) }); + } + + var layergroupId; + step( + function createLayergroup() { + var next = this; + assert.response(server, + { + url: url, + method: 'POST', + headers: { + host: 'localhost', + 'Content-Type': 'application/json' + }, + data: JSON.stringify(self.mapConfig) + }, + { + status: 200, + headers: { + 'Content-Type': 'application/json; charset=utf-8' + } + }, + function(res, err) { + if (err) { + return next(err); + } + var parsedBody = JSON.parse(res.body); + var expectedWidgetURLS = { + http: "/api/v1/map/" + parsedBody.layergroupid + "/0/widget/" + widgetName + }; + assert.ok(parsedBody.metadata.layers[0].widgets[widgetName]); + assert.ok( + parsedBody.metadata.layers[0].widgets[widgetName].url.http.match(expectedWidgetURLS.http) + ); + return next(null, parsedBody.layergroupid); + } + ); + }, + function getWidgetResult(err, _layergroupId) { + assert.ifError(err); + + var next = this; + layergroupId = _layergroupId; + + var urlParams = { + own_filter: params.hasOwnProperty('own_filter') ? params.own_filter : 1 + }; + if (params && params.bbox) { + urlParams.bbox = params.bbox; + } + url = '/api/v1/map/' + layergroupId + '/0/widget/' + widgetName + '?' + qs.stringify(urlParams); + + assert.response(server, + { + url: url, + method: 'GET', + headers: { + host: 'localhost' + } + }, + { + status: 200, + headers: { + 'Content-Type': 'application/json; charset=utf-8' + } + }, + function(res, err) { + if (err) { + return next(err); + } + + next(null, res); + } + ); + }, + function finish(err, res) { + self.keysToDelete['map_cfg|' + LayergroupToken.parse(layergroupId).token] = 0; + self.keysToDelete['user:localhost:mapviews:global'] = 5; + return callback(err, res); + } + ); +}; + +TestClient.prototype.drain = function(callback) { + helper.deleteRedisKeys(this.keysToDelete, callback); +}; From 74cb8767715124b11cb5c37013822c38ef0d60ac Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Fri, 5 Feb 2016 13:01:34 +0100 Subject: [PATCH 3/5] Move list to own file --- test/acceptance/widgets/list.js | 49 ++++++++++++++++++++++++++++++ test/acceptance/widgets/widgets.js | 44 --------------------------- 2 files changed, 49 insertions(+), 44 deletions(-) create mode 100644 test/acceptance/widgets/list.js diff --git a/test/acceptance/widgets/list.js b/test/acceptance/widgets/list.js new file mode 100644 index 00000000..8d5aab07 --- /dev/null +++ b/test/acceptance/widgets/list.js @@ -0,0 +1,49 @@ +var assert = require('../../support/assert'); +var TestClient = require('../../support/test-client'); + +describe('widget list', function() { + + it("should expose layer list", function(done) { + + var listWidgetMapConfig = { + version: '1.5.0', + layers: [ + { + type: 'mapnik', + options: { + sql: 'select * from test_table', + cartocss: '#layer { marker-fill: red; marker-width: 32; marker-allow-overlap: true; }', + cartocss_version: '2.3.0', + widgets: { + names: { + type: 'list', + options: { + columns: ['name'] + } + } + } + } + } + ] + }; + + var testClient = new TestClient(listWidgetMapConfig); + + testClient.getWidget('names', function(err, res) { + if (err) { + return done(err); + } + + var expectedList = [ + {name:"Hawai"}, + {name:"El Estocolmo"}, + {name:"El Rey del Tallarín"}, + {name:"El Lacón"}, + {name:"El Pico"} + ]; + assert.deepEqual(JSON.parse(res.body).rows, expectedList); + + testClient.drain(done); + }); + }); +}); diff --git a/test/acceptance/widgets/widgets.js b/test/acceptance/widgets/widgets.js index 501829ed..fc6263e8 100644 --- a/test/acceptance/widgets/widgets.js +++ b/test/acceptance/widgets/widgets.js @@ -3,50 +3,6 @@ var TestClient = require('../../support/test-client'); describe('widgets', function() { - it("should expose layer list", function(done) { - - var listWidgetMapConfig = { - version: '1.5.0', - layers: [ - { - type: 'mapnik', - options: { - sql: 'select * from test_table', - cartocss: '#layer { marker-fill: red; marker-width: 32; marker-allow-overlap: true; }', - cartocss_version: '2.3.0', - widgets: { - names: { - type: 'list', - options: { - columns: ['name'] - } - } - } - } - } - ] - }; - - var testClient = new TestClient(listWidgetMapConfig); - - testClient.getWidget('names', function(err, res) { - if (err) { - return done(err); - } - - var expectedList = [ - {name:"Hawai"}, - {name:"El Estocolmo"}, - {name:"El Rey del Tallarín"}, - {name:"El Lacón"}, - {name:"El Pico"} - ]; - assert.deepEqual(JSON.parse(res.body).rows, expectedList); - - testClient.drain(done); - }); - }); - it("should expose layer histogram", function(done) { var histogramMapConfig = { version: '1.5.0', From f42d20f2c3250b2e2b8e3130bb8baf2d4fbd8432 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Fri, 5 Feb 2016 13:24:39 +0100 Subject: [PATCH 4/5] Histograms in their own file --- test/acceptance/widgets/histogram.js | 120 +++++++++++++++++++++++++++ test/acceptance/widgets/list.js | 4 +- test/acceptance/widgets/widgets.js | 111 +------------------------ 3 files changed, 125 insertions(+), 110 deletions(-) create mode 100644 test/acceptance/widgets/histogram.js diff --git a/test/acceptance/widgets/histogram.js b/test/acceptance/widgets/histogram.js new file mode 100644 index 00000000..0eeb4ca6 --- /dev/null +++ b/test/acceptance/widgets/histogram.js @@ -0,0 +1,120 @@ +require('../../support/test_helper'); + +var assert = require('../../support/assert'); +var TestClient = require('../../support/test-client'); + +describe('histogram widgets', function() { + + it("should expose layer histogram", function(done) { + var histogramMapConfig = { + version: '1.5.0', + layers: [ + { + type: 'mapnik', + options: { + sql: 'select * from populated_places_simple_reduced', + cartocss: '#layer { marker-fill: red; marker-width: 32; marker-allow-overlap: true; }', + cartocss_version: '2.3.0', + widgets: { + pop_max: { + type: 'histogram', + options: { + column: 'pop_max' + } + } + } + } + } + ] + }; + + var testClient = new TestClient(histogramMapConfig); + + testClient.getWidget('pop_max', function(err, res) { + if (err) { + return done(err); + } + + var histogram = JSON.parse(res.body); + assert.ok(histogram.bins.length); + + testClient.drain(done); + }); + }); + + describe('filters', function() { + + describe('range', function() { + var histogramMapConfig = { + version: '1.5.0', + layers: [ + { + type: 'mapnik', + options: { + sql: 'select * from populated_places_simple_reduced', + cartocss: '#layer { marker-fill: red; marker-width: 32; marker-allow-overlap: true; }', + cartocss_version: '2.3.0', + widgets: { + country_places_histogram: { + type: 'histogram', + options: { + column: 'pop_max' + } + } + } + } + } + ] + }; + + it("should expose an histogram", function(done) { + var testClient = new TestClient(histogramMapConfig); + testClient.getWidget('country_places_histogram', { own_filter: 0 }, function(err, res) { + if (err) { + return done(err); + } + + var histogram = JSON.parse(res.body); + // notice min value + assert.deepEqual( + histogram.bins[0], + { bin: 0, freq: 6497, min: 0, max: 742572, avg: 113511.16823149147 } + ); + + testClient.drain(done); + }); + }); + + it("should expose a filtered histogram", function(done) { + var params = { + filters: { + layers: [ + { + country_places_histogram: { min: 4000000 } + } + ] + } + }; + var testClient = new TestClient(histogramMapConfig); + testClient.getWidget('country_places_histogram', params, function(err, res) { + if (err) { + return done(err); + } + + var histogram = JSON.parse(res.body); + // notice min value + assert.deepEqual(histogram.bins[0], { + bin: 0, + freq: 62, + min: 4000000, + max: 9276403, + avg: 5815009.596774193 + }); + + testClient.drain(done); + }); + }); + }); + }); + +}); diff --git a/test/acceptance/widgets/list.js b/test/acceptance/widgets/list.js index 8d5aab07..87fbf7df 100644 --- a/test/acceptance/widgets/list.js +++ b/test/acceptance/widgets/list.js @@ -1,7 +1,9 @@ +require('../../support/test_helper'); + var assert = require('../../support/assert'); var TestClient = require('../../support/test-client'); -describe('widget list', function() { +describe('list widgets', function() { it("should expose layer list", function(done) { diff --git a/test/acceptance/widgets/widgets.js b/test/acceptance/widgets/widgets.js index fc6263e8..7639561c 100644 --- a/test/acceptance/widgets/widgets.js +++ b/test/acceptance/widgets/widgets.js @@ -1,45 +1,10 @@ +require('../../support/test_helper'); + var assert = require('../../support/assert'); var TestClient = require('../../support/test-client'); describe('widgets', function() { - it("should expose layer histogram", function(done) { - var histogramMapConfig = { - version: '1.5.0', - layers: [ - { - type: 'mapnik', - options: { - sql: 'select * from populated_places_simple_reduced', - cartocss: '#layer { marker-fill: red; marker-width: 32; marker-allow-overlap: true; }', - cartocss_version: '2.3.0', - widgets: { - pop_max: { - type: 'histogram', - options: { - column: 'pop_max' - } - } - } - } - } - ] - }; - - var testClient = new TestClient(histogramMapConfig); - - testClient.getWidget('pop_max', function(err, res) { - if (err) { - return done(err); - } - - var histogram = JSON.parse(res.body); - assert.ok(histogram.bins.length); - - testClient.drain(done); - }); - }); - describe('filters', function() { describe('category', function() { @@ -104,78 +69,6 @@ describe('widgets', function() { }); }); - describe('range', function() { - var histogramMapConfig = { - version: '1.5.0', - layers: [ - { - type: 'mapnik', - options: { - sql: 'select * from populated_places_simple_reduced', - cartocss: '#layer { marker-fill: red; marker-width: 32; marker-allow-overlap: true; }', - cartocss_version: '2.3.0', - widgets: { - country_places_histogram: { - type: 'histogram', - options: { - column: 'pop_max' - } - } - } - } - } - ] - }; - - it("should expose an histogram", function(done) { - var testClient = new TestClient(histogramMapConfig); - testClient.getWidget('country_places_histogram', { own_filter: 0 }, function(err, res) { - if (err) { - return done(err); - } - - var histogram = JSON.parse(res.body); - // notice min value - assert.deepEqual( - histogram.bins[0], - { bin: 0, freq: 6497, min: 0, max: 742572, avg: 113511.16823149147 } - ); - - testClient.drain(done); - }); - }); - - it("should expose a filtered histogram", function(done) { - var params = { - filters: { - layers: [ - { - country_places_histogram: { min: 4000000 } - } - ] - } - }; - var testClient = new TestClient(histogramMapConfig); - testClient.getWidget('country_places_histogram', params, function(err, res) { - if (err) { - return done(err); - } - - var histogram = JSON.parse(res.body); - // notice min value - assert.deepEqual(histogram.bins[0], { - bin: 0, - freq: 62, - min: 4000000, - max: 9276403, - avg: 5815009.596774193 - }); - - testClient.drain(done); - }); - }); - }); - describe('combine widget filters', function() { var combinedWidgetsMapConfig = { version: '1.5.0', From b571b39b38cdb1b0c18bc577adb5ed86524f0680 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Fri, 5 Feb 2016 13:32:38 +0100 Subject: [PATCH 5/5] Aggregations in their own file --- test/acceptance/widgets/aggregation.js | 74 ++++++ test/acceptance/widgets/widgets.js | 310 ++++++++++--------------- 2 files changed, 196 insertions(+), 188 deletions(-) create mode 100644 test/acceptance/widgets/aggregation.js diff --git a/test/acceptance/widgets/aggregation.js b/test/acceptance/widgets/aggregation.js new file mode 100644 index 00000000..0b58fb65 --- /dev/null +++ b/test/acceptance/widgets/aggregation.js @@ -0,0 +1,74 @@ +require('../../support/test_helper'); + +var assert = require('../../support/assert'); +var TestClient = require('../../support/test-client'); + +describe('aggregation widgets', function() { + + var aggregationMapConfig = { + version: '1.5.0', + layers: [ + { + type: 'mapnik', + options: { + sql: 'select * from populated_places_simple_reduced', + cartocss: '#layer { marker-fill: red; marker-width: 32; marker-allow-overlap: true; }', + cartocss_version: '2.3.0', + widgets: { + country_places_count: { + type: 'aggregation', + options: { + column: 'adm0_a3', + aggregation: 'count' + } + } + } + } + } + ] + }; + + it("should expose an aggregation", function(done) { + var testClient = new TestClient(aggregationMapConfig); + testClient.getWidget('country_places_count', { own_filter: 0 }, function(err, res) { + if (err) { + return done(err); + } + + var aggregation = JSON.parse(res.body); + assert.equal(aggregation.categories.length, 6); + assert.deepEqual(aggregation.categories[0], { value: 769, category: 'USA', agg: false }); + + testClient.drain(done); + }); + }); + + describe('filters', function() { + + describe('category', function () { + + it("should expose a filtered aggregation", function (done) { + var params = { + filters: { + layers: [ + {country_places_count: {accept: ['CAN']}} + ] + } + }; + var testClient = new TestClient(aggregationMapConfig); + testClient.getWidget('country_places_count', params, function (err, res) { + if (err) { + return done(err); + } + + var aggregation = JSON.parse(res.body); + assert.equal(aggregation.categories.length, 1); + assert.deepEqual(aggregation.categories[0], { value: 256, category: 'CAN', agg: false }); + + testClient.drain(done); + }); + }); + }); + + }); +}); diff --git a/test/acceptance/widgets/widgets.js b/test/acceptance/widgets/widgets.js index 7639561c..6fee7a29 100644 --- a/test/acceptance/widgets/widgets.js +++ b/test/acceptance/widgets/widgets.js @@ -3,226 +3,160 @@ require('../../support/test_helper'); var assert = require('../../support/assert'); var TestClient = require('../../support/test-client'); -describe('widgets', function() { +describe('widget filters', function() { - describe('filters', function() { - - describe('category', function() { - var aggregationMapConfig = { - version: '1.5.0', - layers: [ - { - type: 'mapnik', - options: { - sql: 'select * from populated_places_simple_reduced', - cartocss: '#layer { marker-fill: red; marker-width: 32; marker-allow-overlap: true; }', - cartocss_version: '2.3.0', - widgets: { - country_places_count: { - type: 'aggregation', - options: { - column: 'adm0_a3', - aggregation: 'count' - } + describe('combine widget filters', function() { + var combinedWidgetsMapConfig = { + version: '1.5.0', + layers: [ + { + type: 'mapnik', + options: { + sql: 'select * from populated_places_simple_reduced', + cartocss: '#layer { marker-fill: red; marker-width: 32; marker-allow-overlap: true; }', + cartocss_version: '2.3.0', + widgets: { + country_places_count: { + type: 'aggregation', + options: { + column: 'adm0_a3', + aggregation: 'count' + } + }, + country_places_histogram: { + type: 'histogram', + options: { + column: 'pop_max' } } } } - ] + } + ] + }; + + it("should expose a filtered aggregation", function(done) { + var params = { + filters: { + layers: [ + { + country_places_count: { reject: ['CHN'] } + } + ] + } }; + var testClient = new TestClient(combinedWidgetsMapConfig); + testClient.getWidget('country_places_count', params, function(err, res) { + if (err) { + return done(err); + } - it("should expose an aggregation", function(done) { - var testClient = new TestClient(aggregationMapConfig); - testClient.getWidget('country_places_count', { own_filter: 0 }, function(err, res) { - if (err) { - return done(err); - } + var aggregation = JSON.parse(res.body); - var aggregation = JSON.parse(res.body); - assert.equal(aggregation.categories.length, 6); - assert.deepEqual(aggregation.categories[0], { value: 769, category: 'USA', agg: false }); + // first one would be CHN if reject filter wasn't applied + assert.deepEqual(aggregation.categories[0], { value: 769, category: "USA", agg: false }); - testClient.drain(done); - }); - }); + // confirm 'CHN' was filtered out (reject) + assert.equal(aggregation.categories.reduce(function(sum, row) { + return sum + (row.category === 'CHN' ? 1 : 0); + }, 0), 0); - it("should expose a filtered aggregation", function(done) { - var params = { - filters: { - layers: [ - {country_places_count: {accept: ['CAN']}} - ] - } - }; - var testClient = new TestClient(aggregationMapConfig); - testClient.getWidget('country_places_count', params, function(err, res) { - if (err) { - return done(err); - } - - var aggregation = JSON.parse(res.body); - assert.equal(aggregation.categories.length, 1); - assert.deepEqual(aggregation.categories[0], { value: 256, category: 'CAN', agg: false }); - - testClient.drain(done); - }); + testClient.drain(done); }); }); - describe('combine widget filters', function() { - var combinedWidgetsMapConfig = { - version: '1.5.0', - layers: [ - { - type: 'mapnik', - options: { - sql: 'select * from populated_places_simple_reduced', - cartocss: '#layer { marker-fill: red; marker-width: 32; marker-allow-overlap: true; }', - cartocss_version: '2.3.0', - widgets: { - country_places_count: { - type: 'aggregation', - options: { - column: 'adm0_a3', - aggregation: 'count' - } - }, - country_places_histogram: { - type: 'histogram', - options: { - column: 'pop_max' - } - } - } + it("should expose a filtered aggregation", function(done) { + var params = { + filters: { + layers: [ + { + country_places_count: { reject: ['CHN'] }, + country_places_histogram: { min: 7000000 } } - } - ] + ] + } }; + var testClient = new TestClient(combinedWidgetsMapConfig); + testClient.getWidget('country_places_count', params, function(err, res) { + if (err) { + return done(err); + } - it("should expose a filtered aggregation", function(done) { - var params = { - filters: { - layers: [ - { - country_places_count: { reject: ['CHN'] } - } - ] - } - }; - var testClient = new TestClient(combinedWidgetsMapConfig); - testClient.getWidget('country_places_count', params, function(err, res) { - if (err) { - return done(err); - } + var aggregation = JSON.parse(res.body); - var aggregation = JSON.parse(res.body); + // first one would be CHN if reject filter wasn't applied + assert.deepEqual(aggregation.categories[0], { value: 4, category: 'IND', agg: false }); - // first one would be CHN if reject filter wasn't applied - assert.deepEqual(aggregation.categories[0], { value: 769, category: "USA", agg: false }); + // confirm 'CHN' was filtered out (reject) + assert.equal(aggregation.categories.reduce(function(sum, row) { + return sum + (row.category === 'CHN' ? 1 : 0); + }, 0), 0); - // confirm 'CHN' was filtered out (reject) - assert.equal(aggregation.categories.reduce(function(sum, row) { - return sum + (row.category === 'CHN' ? 1 : 0); - }, 0), 0); - - testClient.drain(done); - }); + testClient.drain(done); }); + }); - it("should expose a filtered aggregation", function(done) { - var params = { - filters: { - layers: [ - { - country_places_count: { reject: ['CHN'] }, - country_places_histogram: { min: 7000000 } - } - ] - } - }; - var testClient = new TestClient(combinedWidgetsMapConfig); - testClient.getWidget('country_places_count', params, function(err, res) { - if (err) { - return done(err); - } + it("should allow to filter by bounding box a filtered aggregation", function(done) { + var params = { + filters: { + layers: [ + { + country_places_histogram: { min: 50000 } + } + ] + }, + bbox: '-20,0,45,60' + }; + var testClient = new TestClient(combinedWidgetsMapConfig); + testClient.getWidget('country_places_count', params, function(err, res) { + if (err) { + return done(err); + } - var aggregation = JSON.parse(res.body); + var aggregation = JSON.parse(res.body); - // first one would be CHN if reject filter wasn't applied - assert.deepEqual(aggregation.categories[0], { value: 4, category: 'IND', agg: false }); + // first one would be CHN if reject filter wasn't applied + assert.deepEqual(aggregation.categories[0], { value: 96, category: "RUS", agg: false }); - // confirm 'CHN' was filtered out (reject) - assert.equal(aggregation.categories.reduce(function(sum, row) { - return sum + (row.category === 'CHN' ? 1 : 0); - }, 0), 0); + // confirm 'CHN' was filtered out (reject) + assert.equal(aggregation.categories.reduce(function(sum, row) { + return sum + (row.category === 'CHN' ? 1 : 0); + }, 0), 0); - testClient.drain(done); - }); + testClient.drain(done); }); + }); - it("should allow to filter by bounding box a filtered aggregation", function(done) { - var params = { - filters: { - layers: [ - { - country_places_histogram: { min: 50000 } - } - ] - }, - bbox: '-20,0,45,60' - }; - var testClient = new TestClient(combinedWidgetsMapConfig); - testClient.getWidget('country_places_count', params, function(err, res) { - if (err) { - return done(err); - } + it("should allow to filter by bounding box a filtered aggregation, with reject", function(done) { + var params = { + filters: { + layers: [ + { + country_places_count: { reject: ['RUS'] }, + country_places_histogram: { min: 50000 } + } + ] + }, + bbox: '-20,0,45,60' + }; + var testClient = new TestClient(combinedWidgetsMapConfig); + testClient.getWidget('country_places_count', params, function(err, res) { + if (err) { + return done(err); + } - var aggregation = JSON.parse(res.body); + var aggregation = JSON.parse(res.body); - // first one would be CHN if reject filter wasn't applied - assert.deepEqual(aggregation.categories[0], { value: 96, category: "RUS", agg: false }); + // first one would be CHN if reject filter wasn't applied + assert.deepEqual(aggregation.categories[0], { value: 77, category: "TUR", agg: false }); - // confirm 'CHN' was filtered out (reject) - assert.equal(aggregation.categories.reduce(function(sum, row) { - return sum + (row.category === 'CHN' ? 1 : 0); - }, 0), 0); + // confirm 'CHN' was filtered out (reject) + assert.equal(aggregation.categories.reduce(function(sum, row) { + return sum + (row.category === 'CHN' ? 1 : 0); + }, 0), 0); - testClient.drain(done); - }); - }); - - it("should allow to filter by bounding box a filtered aggregation, with reject", function(done) { - var params = { - filters: { - layers: [ - { - country_places_count: { reject: ['RUS'] }, - country_places_histogram: { min: 50000 } - } - ] - }, - bbox: '-20,0,45,60' - }; - var testClient = new TestClient(combinedWidgetsMapConfig); - testClient.getWidget('country_places_count', params, function(err, res) { - if (err) { - return done(err); - } - - var aggregation = JSON.parse(res.body); - - // first one would be CHN if reject filter wasn't applied - assert.deepEqual(aggregation.categories[0], { value: 77, category: "TUR", agg: false }); - - // confirm 'CHN' was filtered out (reject) - assert.equal(aggregation.categories.reduce(function(sum, row) { - return sum + (row.category === 'CHN' ? 1 : 0); - }, 0), 0); - - testClient.drain(done); - }); + testClient.drain(done); }); }); }); - });