2018-10-24 00:39:02 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-07 16:44:45 +08:00
|
|
|
require('../../support/test-helper');
|
2016-06-06 17:46:27 +08:00
|
|
|
|
|
|
|
var assert = require('../../support/assert');
|
|
|
|
var TestClient = require('../../support/test-client');
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
describe('widgets-regressions', function () {
|
|
|
|
describe('aggregations', function () {
|
|
|
|
afterEach(function (done) {
|
2016-06-06 17:46:27 +08:00
|
|
|
if (this.testClient) {
|
|
|
|
this.testClient.drain(done);
|
|
|
|
} else {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('should work when there is a mix of layers with and without widgets', function (done) {
|
2016-06-06 17:46:27 +08:00
|
|
|
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);
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(aggregation.type, 'aggregation');
|
2016-06-06 17:46:27 +08:00
|
|
|
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(aggregation.categories.length, 6);
|
2016-06-06 17:46:27 +08:00
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('should work when there is a mix of layers with and without widgets, source and sql', function (done) {
|
2016-06-06 17:46:27 +08:00
|
|
|
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: {
|
2019-10-22 01:07:24 +08:00
|
|
|
source: { id: 'head-limited' },
|
2016-06-06 17:46:27 +08:00
|
|
|
cartocss: '#layer0 { marker-fill: red; marker-width: 10; }',
|
|
|
|
cartocss_version: '2.0.1',
|
|
|
|
widgets: {
|
|
|
|
pop_max_histogram: {
|
|
|
|
type: 'histogram',
|
|
|
|
options: {
|
|
|
|
column: 'pop_max'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2019-10-22 01:07:24 +08:00
|
|
|
type: 'http',
|
|
|
|
options: {
|
|
|
|
urlTemplate: 'http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png',
|
|
|
|
subdomains: 'abcd'
|
2016-06-06 17:46:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
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);
|
2019-10-22 01:07:24 +08:00
|
|
|
this.testClient.getLayergroup(function (err, layergroup) {
|
2016-06-06 17:46:27 +08:00
|
|
|
assert.ok(!err, err);
|
|
|
|
assert.ok(layergroup.metadata);
|
|
|
|
var metadata = layergroup.metadata;
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(metadata.layers.length, 3);
|
|
|
|
assert.strictEqual(metadata.analyses.length, 2);
|
|
|
|
assert.strictEqual(Object.keys(metadata.dataviews).length, 4);
|
2016-06-06 17:46:27 +08:00
|
|
|
assert.deepEqual(
|
|
|
|
Object.keys(metadata.dataviews),
|
|
|
|
['wadus', 'adm0name_categories', 'adm1name_categories', 'pop_max_histogram']
|
|
|
|
);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('should work with layers not containing sql', function (done) {
|
2016-06-06 17:46:27 +08:00
|
|
|
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'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2019-10-22 01:07:24 +08:00
|
|
|
type: 'http',
|
|
|
|
options: {
|
|
|
|
urlTemplate: 'http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png',
|
|
|
|
subdomains: 'abcd'
|
2016-06-06 17:46:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
this.testClient = new TestClient(nonSqlLayersMapConfig);
|
|
|
|
this.testClient.getWidget('adm0name', { own_filter: 0 }, function (err, res, aggregation) {
|
|
|
|
assert.ok(!err, err);
|
|
|
|
assert.ok(aggregation);
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(aggregation.type, 'aggregation');
|
2016-06-06 17:46:27 +08:00
|
|
|
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(aggregation.categories.length, 6);
|
2016-06-06 17:46:27 +08:00
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('should not count the polygons outside the bounding box', function (done) {
|
2017-08-11 00:16:53 +08:00
|
|
|
// $ % $ = not intersecting left triangle
|
|
|
|
// $$ **VVVVV** %% % = not intersecting right triangle
|
|
|
|
// $$$ *VVVVV* %%% * = intersecting triangle
|
|
|
|
// $$$$ ***** %%%% V = bounding box
|
|
|
|
// $$$$$ *** %%%%%
|
|
|
|
// $$$$$$ * %%%%%%
|
|
|
|
// $$$$$$$ %%%%%%%
|
|
|
|
// $$$$$$$$ %%%%%%%%
|
|
|
|
|
2017-08-16 16:07:27 +08:00
|
|
|
const notIntersectingLeftTriangle = {
|
2019-10-22 01:07:24 +08:00
|
|
|
type: 'Polygon',
|
|
|
|
coordinates: [[
|
|
|
|
[-161.015625, 69.28725695167886],
|
|
|
|
[-162.7734375, -7.710991655433217],
|
|
|
|
[-40.78125, -8.059229627200192],
|
|
|
|
[-161.015625, 69.28725695167886]
|
2017-08-11 00:09:18 +08:00
|
|
|
]]
|
|
|
|
};
|
|
|
|
|
2017-08-16 16:07:27 +08:00
|
|
|
const notIntersectingRightTriangle = {
|
2019-10-22 01:07:24 +08:00
|
|
|
type: 'Polygon',
|
2017-08-11 00:09:18 +08:00
|
|
|
coordinates: [[
|
2019-10-22 01:07:24 +08:00
|
|
|
[-29.179687499999996, -7.01366792756663],
|
|
|
|
[103.71093749999999, -6.664607562172573],
|
|
|
|
[105.46875, 69.16255790810501],
|
|
|
|
[-29.179687499999996, -7.01366792756663]
|
2017-08-11 00:09:18 +08:00
|
|
|
]]
|
|
|
|
};
|
|
|
|
|
2017-08-16 16:07:27 +08:00
|
|
|
const intersectingTriangle = {
|
2019-10-22 01:07:24 +08:00
|
|
|
type: 'Polygon',
|
|
|
|
coordinates: [[
|
|
|
|
[-117.42187500000001, 68.13885164925573],
|
|
|
|
[-35.859375, 20.96143961409684],
|
|
|
|
[59.4140625, 68.52823492039876],
|
|
|
|
[-117.42187500000001, 68.13885164925573]
|
2017-08-11 00:09:18 +08:00
|
|
|
]]
|
|
|
|
};
|
|
|
|
|
2018-10-24 00:39:02 +08:00
|
|
|
const query = `
|
2017-08-11 00:09:18 +08:00
|
|
|
SELECT
|
|
|
|
ST_TRANSFORM(ST_SETSRID(ST_GeomFromGeoJSON(
|
|
|
|
'${JSON.stringify(notIntersectingLeftTriangle)}'
|
|
|
|
), 4326), 3857) AS the_geom_webmercator, 1 AS cartodb_id, 'notIntersectingLeftTriangle' AS name
|
2018-07-17 18:43:29 +08:00
|
|
|
UNION ALL
|
2017-08-11 00:09:18 +08:00
|
|
|
SELECT
|
|
|
|
ST_TRANSFORM(ST_SETSRID(ST_GeomFromGeoJSON(
|
|
|
|
'${JSON.stringify(notIntersectingRightTriangle)}'
|
|
|
|
), 4326), 3857), 2, 'notIntersectingRightTriangle'
|
2018-07-17 18:43:29 +08:00
|
|
|
UNION ALL
|
2017-08-11 00:09:18 +08:00
|
|
|
SELECT
|
|
|
|
ST_TRANSFORM(ST_SETSRID(ST_GeomFromGeoJSON(
|
|
|
|
'${JSON.stringify(intersectingTriangle)}'
|
|
|
|
), 4326), 3857), 3, 'intersectingTriangle'
|
2017-08-11 00:20:15 +08:00
|
|
|
`;
|
2017-08-11 00:09:18 +08:00
|
|
|
|
2017-08-16 16:07:27 +08:00
|
|
|
const mapConfig = {
|
2017-08-11 00:09:18 +08:00
|
|
|
version: '1.5.0',
|
|
|
|
layers: [
|
|
|
|
{
|
2019-10-22 01:07:24 +08:00
|
|
|
type: 'cartodb',
|
|
|
|
options: {
|
|
|
|
source: {
|
|
|
|
id: 'a0'
|
2017-08-11 00:09:18 +08:00
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
cartocss: '#points { marker-width: 10; marker-fill: red; }',
|
|
|
|
cartocss_version: '2.3.0'
|
2017-08-11 00:09:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
dataviews: {
|
|
|
|
val_formula: {
|
|
|
|
source: {
|
|
|
|
id: 'a0'
|
|
|
|
},
|
|
|
|
type: 'aggregation',
|
|
|
|
options: {
|
2019-10-22 01:07:24 +08:00
|
|
|
column: 'name',
|
|
|
|
aggregation: 'count'
|
2017-08-11 00:09:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
analyses: [
|
|
|
|
{
|
2019-10-22 01:07:24 +08:00
|
|
|
id: 'a0',
|
|
|
|
type: 'source',
|
|
|
|
params: {
|
|
|
|
query: query
|
2017-08-11 00:09:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
this.testClient = new TestClient(mapConfig, 1234);
|
2017-08-16 16:07:27 +08:00
|
|
|
const params = {
|
2017-08-11 00:09:18 +08:00
|
|
|
bbox: '-77.34374999999999,45.82879925192134,17.578125,55.97379820507658'
|
|
|
|
};
|
2019-10-22 01:07:24 +08:00
|
|
|
this.testClient.getDataview('val_formula', params, function (err, dataview) {
|
2017-08-16 16:07:27 +08:00
|
|
|
assert.ifError(err);
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(dataview.categories.length, 1);
|
|
|
|
assert.strictEqual(dataview.categories[0].category, 'intersectingTriangle');
|
2017-08-11 00:09:18 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2016-06-06 17:46:27 +08:00
|
|
|
});
|
|
|
|
});
|