From 653beb19529fb606a932dab188a372542f9f0469 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Mon, 6 Jun 2016 11:46:27 +0200 Subject: [PATCH] Dataviews/widgets adapter working with non sql, non source, and non widgets layers Ref #480 --- NEWS.md | 3 + .../adapter/dataviews-widgets-adapter.js | 9 +- test/acceptance/widgets/ported/aggregation.js | 112 --------- test/acceptance/widgets/regressions.js | 223 ++++++++++++++++++ 4 files changed, 233 insertions(+), 114 deletions(-) create mode 100644 test/acceptance/widgets/regressions.js diff --git a/NEWS.md b/NEWS.md index eb1beed1..357ec784 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,9 @@ Released 2016-mm-dd +Bug fixes: + - Dataviews/widgets adapter working with non sql, non source, and non widgets layers + ## 2.45.0 diff --git a/lib/cartodb/models/mapconfig/adapter/dataviews-widgets-adapter.js b/lib/cartodb/models/mapconfig/adapter/dataviews-widgets-adapter.js index a87ef0b9..8ae13272 100644 --- a/lib/cartodb/models/mapconfig/adapter/dataviews-widgets-adapter.js +++ b/lib/cartodb/models/mapconfig/adapter/dataviews-widgets-adapter.js @@ -14,11 +14,16 @@ DataviewsWidgetsMapConfigAdapter.prototype.getMapConfig = function(user, request requestMapConfig.dataviews = requestMapConfig.dataviews || {}; requestMapConfig.layers.forEach(function(layer, index) { - if (!layer.options.widgets || !layer.options.sql) { + var layerSourceId = getLayerSourceId(layer); + + if (!layer.options.widgets) { + return; + } + + if (!layerSourceId && !layer.options.sql) { return; } - var layerSourceId = getLayerSourceId(layer); var dataviewSourceId = layerSourceId || 'cdb-layer-source-' + index; // Append a new analysis if layer has no source id but sql. if (!layerSourceId) { diff --git a/test/acceptance/widgets/ported/aggregation.js b/test/acceptance/widgets/ported/aggregation.js index ccb7a31f..9329f039 100644 --- a/test/acceptance/widgets/ported/aggregation.js +++ b/test/acceptance/widgets/ported/aggregation.js @@ -162,118 +162,6 @@ describe('widgets', function() { }); }); - it('should work when there is a mix of layers with and without widgets', function(done) { - - var aggregationSumMapConfig = { - version: '1.5.0', - layers: [ - { - type: 'mapnik', - options: { - sql: 'select * from populated_places_simple_reduced', - cartocss: '#layer0 { marker-fill: red; marker-width: 10; }', - cartocss_version: '2.0.1', - widgets: { - adm0name: { - type: 'aggregation', - options: { - column: 'adm0name', - aggregation: 'sum', - aggregationColumn: 'pop_max' - } - } - } - } - }, - { - type: 'mapnik', - options: { - sql: 'select * from populated_places_simple_reduced limit 100', - cartocss: '#layer0 { marker-fill: red; marker-width: 10; }', - cartocss_version: '2.0.1' - } - } - ] - }; - - this.testClient = new TestClient(aggregationSumMapConfig); - this.testClient.getWidget('adm0name', { own_filter: 0 }, function (err, res, aggregation) { - assert.ok(!err, err); - assert.ok(aggregation); - assert.equal(aggregation.type, 'aggregation'); - - assert.equal(aggregation.categories.length, 6); - - assert.deepEqual( - aggregation.categories[0], - { category: 'China', value: 374537585, agg: false } - ); - - assert.deepEqual( - aggregation.categories[aggregation.categories.length - 1], - { category: 'Other', value: 1412626289, agg: true } - ); - - done(); - }); - }); - - it('should work with layers not containing sql', function(done) { - var basemapUrl = "https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png"; - - var aggregationSumMapConfig = { - version: '1.5.0', - layers: [ - { - type: 'mapnik', - options: { - sql: 'select * from populated_places_simple_reduced', - cartocss: '#layer0 { marker-fill: red; marker-width: 10; }', - cartocss_version: '2.0.1', - widgets: { - adm0name: { - type: 'aggregation', - options: { - column: 'adm0name', - aggregation: 'sum', - aggregationColumn: 'pop_max' - } - } - } - } - }, - { - "type": "http", - "options": { - "urlTemplate": basemapUrl, - "subdomains": "abcd" - } - } - ] - }; - - this.testClient = new TestClient(aggregationSumMapConfig); - this.testClient.getWidget('adm0name', { own_filter: 0 }, function (err, res, aggregation) { - assert.ok(!err, err); - assert.ok(aggregation); - assert.equal(aggregation.type, 'aggregation'); - - assert.equal(aggregation.categories.length, 6); - - assert.deepEqual( - aggregation.categories[0], - { category: 'China', value: 374537585, agg: false } - ); - - assert.deepEqual( - aggregation.categories[aggregation.categories.length - 1], - { category: 'Other', value: 1412626289, agg: true } - ); - - done(); - }); - }); - var filteredCategoriesSumScenarios = [ { accept: [], values: [] }, { accept: ['Canada'], values: [23955084] }, diff --git a/test/acceptance/widgets/regressions.js b/test/acceptance/widgets/regressions.js new file mode 100644 index 00000000..1d006ac4 --- /dev/null +++ b/test/acceptance/widgets/regressions.js @@ -0,0 +1,223 @@ +require('../../support/test_helper'); + +var assert = require('../../support/assert'); +var TestClient = require('../../support/test-client'); + +describe('widgets-regressions', function() { + + describe('aggregations', function() { + + afterEach(function(done) { + if (this.testClient) { + this.testClient.drain(done); + } else { + done(); + } + }); + + it('should work when there is a mix of layers with and without widgets', function(done) { + var layersWithNoWidgetsMapConfig = { + version: '1.5.0', + layers: [ + { + type: 'mapnik', + options: { + sql: 'select * from populated_places_simple_reduced', + cartocss: '#layer0 { marker-fill: red; marker-width: 10; }', + cartocss_version: '2.0.1', + widgets: { + adm0name: { + type: 'aggregation', + options: { + column: 'adm0name', + aggregation: 'sum', + aggregationColumn: 'pop_max' + } + } + } + } + }, + { + type: 'mapnik', + options: { + sql: 'select * from populated_places_simple_reduced limit 100', + cartocss: '#layer0 { marker-fill: red; marker-width: 10; }', + cartocss_version: '2.0.1' + } + } + ] + }; + + this.testClient = new TestClient(layersWithNoWidgetsMapConfig); + this.testClient.getWidget('adm0name', { own_filter: 0 }, function (err, res, aggregation) { + assert.ok(!err, err); + assert.ok(aggregation); + assert.equal(aggregation.type, 'aggregation'); + + assert.equal(aggregation.categories.length, 6); + + assert.deepEqual( + aggregation.categories[0], + { category: 'China', value: 374537585, agg: false } + ); + + assert.deepEqual( + aggregation.categories[aggregation.categories.length - 1], + { category: 'Other', value: 1412626289, agg: true } + ); + + done(); + }); + }); + + it('should work when there is a mix of layers with and without widgets, source and sql', function(done) { + var mixOfLayersMapConfig = { + version: '1.5.0', + layers: [ + { + type: 'mapnik', + options: { + sql: 'select * from populated_places_simple_reduced', + cartocss: '#layer0 { marker-fill: red; marker-width: 10; }', + cartocss_version: '2.0.1', + widgets: { + adm0name_categories: { + type: 'aggregation', + options: { + column: 'adm0name', + aggregation: 'sum', + aggregationColumn: 'pop_max' + } + }, + adm1name_categories: { + type: 'aggregation', + options: { + column: 'adm1name', + aggregation: 'sum', + aggregationColumn: 'pop_max' + } + } + } + } + }, + { + type: 'mapnik', + options: { + source: {id: 'head-limited'}, + cartocss: '#layer0 { marker-fill: red; marker-width: 10; }', + cartocss_version: '2.0.1', + widgets: { + pop_max_histogram: { + type: 'histogram', + options: { + column: 'pop_max' + } + } + } + } + }, + { + "type": "http", + "options": { + "urlTemplate": "http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png", + "subdomains": "abcd" + } + } + ], + analyses: [ + { + id: 'head-limited', + type: 'source', + params: { + query: 'select * from populated_places_simple_reduced limit 100' + } + } + ], + dataviews: { + wadus: { + type: 'histogram', + source: { + id: 'head-limited' + }, + options: { + column: 'population' + } + } + } + }; + + this.testClient = new TestClient(mixOfLayersMapConfig); + this.testClient.getLayergroup(function(err, layergroup) { + assert.ok(!err, err); + assert.ok(layergroup.metadata); + var metadata = layergroup.metadata; + assert.equal(metadata.layers.length, 3); + assert.equal(metadata.analyses.length, 2); + assert.equal(Object.keys(metadata.dataviews).length, 4); + assert.deepEqual( + Object.keys(metadata.dataviews), + ['wadus', 'adm0name_categories', 'adm1name_categories', 'pop_max_histogram'] + ); + + + done(); + }); + }); + + it('should work with layers not containing sql', function(done) { + var nonSqlLayersMapConfig = { + version: '1.5.0', + layers: [ + { + type: 'mapnik', + options: { + sql: 'select * from populated_places_simple_reduced', + cartocss: '#layer0 { marker-fill: red; marker-width: 10; }', + cartocss_version: '2.0.1', + widgets: { + adm0name: { + type: 'aggregation', + options: { + column: 'adm0name', + aggregation: 'sum', + aggregationColumn: 'pop_max' + } + } + } + } + }, + { + "type": "http", + "options": { + "urlTemplate": "http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png", + "subdomains": "abcd" + } + } + ] + }; + + this.testClient = new TestClient(nonSqlLayersMapConfig); + this.testClient.getWidget('adm0name', { own_filter: 0 }, function (err, res, aggregation) { + assert.ok(!err, err); + assert.ok(aggregation); + assert.equal(aggregation.type, 'aggregation'); + + assert.equal(aggregation.categories.length, 6); + + assert.deepEqual( + aggregation.categories[0], + { category: 'China', value: 374537585, agg: false } + ); + + assert.deepEqual( + aggregation.categories[aggregation.categories.length - 1], + { category: 'Other', value: 1412626289, agg: true } + ); + + done(); + }); + }); + + }); + +});