Windshaft-cartodb/test/acceptance/aggregation.js

398 lines
14 KiB
JavaScript
Raw Normal View History

require('../support/test_helper');
const assert = require('../support/assert');
const TestClient = require('../support/test-client');
const serverOptions = require('../../lib/cartodb/server_options');
2017-12-06 00:44:52 +08:00
const MISSING_AGGREGATION_COLUMNS = 'Missing columns in the aggregation. The map-config defines cartocss expressions,'+
' interactivity fields or attributes that are not present in the aggregation';
const suites = [{
desc: 'mvt (mapnik)',
usePostGIS: false
}];
if (process.env.POSTGIS_VERSION === '2.4') {
suites.push({
desc: 'mvt (postgis)',
usePostGIS: true
});
}
describe('aggregation', function () {
const POINTS_SQL_1 = `
select
st_setsrid(st_makepoint(x*10, x*10), 4326) as the_geom,
st_transform(st_setsrid(st_makepoint(x*10, x*10), 4326), 3857) as the_geom_webmercator,
x as value
from generate_series(-3, 3) x
`;
const POINTS_SQL_2 = `
select
st_setsrid(st_makepoint(x*10, x*10*(-1)), 4326) as the_geom,
st_transform(st_setsrid(st_makepoint(x*10, x*10*(-1)), 4326), 3857) as the_geom_webmercator,
x as value,
x*x as sqrt_value
from generate_series(-3, 3) x
`;
2017-12-06 03:21:20 +08:00
const POLYGONS_SQL_1 = `
2017-12-06 03:21:20 +08:00
select
st_buffer(st_setsrid(st_makepoint(x*10, x*10), 4326)::geography, 100000)::geometry as the_geom,
st_transform(
st_buffer(st_setsrid(st_makepoint(x*10, x*10), 4326)::geography, 100000)::geometry,
3857
) as the_geom_webmercator,
x as value
2017-12-06 03:21:20 +08:00
from generate_series(-3, 3) x
`;
function createVectorMapConfig (layers = [
{
type: 'cartodb',
options: {
sql: POINTS_SQL_1,
aggregation: true
}
},
{
type: 'cartodb',
options: {
sql: POINTS_SQL_2,
aggregation: true
}
}
]) {
return {
version: '1.6.0',
layers: layers
};
}
suites.forEach((suite) => {
const { desc, usePostGIS } = suite;
2017-12-06 00:44:52 +08:00
describe(desc, function () {
const originalUsePostGIS = serverOptions.renderer.mvt.usePostGIS;
before(function () {
serverOptions.renderer.mvt.usePostGIS = usePostGIS;
});
after(function (){
serverOptions.renderer.mvt.usePostGIS = originalUsePostGIS;
});
afterEach(function (done) {
this.testClient.drain(done);
});
it('should return a layergroup indicating the mapconfig was aggregated', function (done) {
this.mapConfig = createVectorMapConfig([
{
type: 'cartodb',
options: {
sql: POINTS_SQL_1,
aggregation: {
threshold: 1
}
}
},
{
type: 'cartodb',
options: {
sql: POINTS_SQL_2,
aggregation: {
threshold: 1
}
}
}
]);
this.testClient = new TestClient(this.mapConfig);
this.testClient.getLayergroup((err, body) => {
if (err) {
return done(err);
}
assert.equal(typeof body.metadata, 'object');
assert.ok(Array.isArray(body.metadata.layers));
2017-12-05 23:52:15 +08:00
body.metadata.layers.forEach(layer => assert.ok(layer.meta.aggregation.mvt));
2017-12-06 03:21:20 +08:00
body.metadata.layers.forEach(layer => assert.ok(!layer.meta.aggregation.png));
done();
});
});
it('should return a layergroup with aggregation and cartocss compatible', function (done) {
this.mapConfig = createVectorMapConfig([
{
type: 'cartodb',
options: {
sql: POINTS_SQL_1,
aggregation: {
columns: {
total: {
aggregate_function: 'sum',
aggregated_column: 'value'
}
},
threshold: 1
},
cartocss: '#layer { marker-width: [total]; }',
cartocss_version: '2.3.0'
}
}
]);
this.testClient = new TestClient(this.mapConfig);
2017-12-05 23:52:15 +08:00
this.testClient.getLayergroup((err, body) => {
if (err) {
return done(err);
}
2017-12-05 23:52:15 +08:00
assert.equal(typeof body.metadata, 'object');
assert.ok(Array.isArray(body.metadata.layers));
body.metadata.layers.forEach(layer => assert.ok(layer.meta.aggregation.mvt));
2017-12-06 03:21:20 +08:00
body.metadata.layers.forEach(layer => assert.ok(layer.meta.aggregation.png));
2017-12-05 23:52:15 +08:00
done();
});
});
it('should fail when aggregation and cartocss are not compatible', function (done) {
const response = {
status: 400,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
};
this.mapConfig = createVectorMapConfig([
{
type: 'cartodb',
options: {
sql: POINTS_SQL_1,
aggregation: {
columns: {
total: {
aggregate_function: 'sum',
aggregated_column: 'value'
}
},
threshold: 1
},
cartocss: '#layer { marker-width: [value]; }',
cartocss_version: '2.3.0'
}
}
]);
this.testClient = new TestClient(this.mapConfig);
this.testClient.getLayergroup({ response }, (err, body) => {
if (err) {
return done(err);
}
assert.equal(body.errors[0], MISSING_AGGREGATION_COLUMNS);
done();
});
});
2017-12-05 23:52:15 +08:00
it('should fail if cartocss uses "value" column and it\'s not defined in the aggregation',
function (done) {
const response = {
status: 400,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
};
this.mapConfig = createVectorMapConfig([
{
type: 'cartodb',
options: {
sql: POINTS_SQL_2,
aggregation: true,
cartocss: '#layer { marker-width: [value]; }',
cartocss_version: '2.3.0'
}
}
]);
this.testClient = new TestClient(this.mapConfig);
this.testClient.getLayergroup({ response }, (err, body) => {
if (err) {
return done(err);
}
assert.equal(body.errors[0], MISSING_AGGREGATION_COLUMNS);
done();
});
});
it('should fail if aggregation misses a column defined in interactivity',
function (done) {
const response = {
status: 400,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
};
this.mapConfig = createVectorMapConfig([
{
type: 'cartodb',
options: {
sql: POINTS_SQL_2,
aggregation: {
columns: {
total: {
aggregate_function: 'sum',
aggregated_column: 'value'
}
}
},
cartocss: '#layer { marker-width: [value]; }',
cartocss_version: '2.3.0',
interactivity: ['sqrt_value']
}
}
]);
this.testClient = new TestClient(this.mapConfig);
this.testClient.getLayergroup({ response }, (err, body) => {
if (err) {
return done(err);
}
assert.equal(body.errors[0], MISSING_AGGREGATION_COLUMNS);
done();
});
});
it('should skip aggregation to create a layergroup with aggregation defined already', function (done) {
const mapConfig = createVectorMapConfig([
{
type: 'cartodb',
options: {
sql: POINTS_SQL_1,
aggregation: {
columns: {
total: {
aggregate_function: 'sum',
aggregated_column: 'value'
}
}
}
}
}
]);
this.testClient = new TestClient(mapConfig);
const options = { aggregation: false };
this.testClient.getLayergroup(options, (err, body) => {
if (err) {
return done(err);
}
assert.equal(typeof body.metadata, 'object');
assert.ok(Array.isArray(body.metadata.layers));
body.metadata.layers.forEach(layer => assert.ok(layer.meta.aggregation === undefined));
done();
});
});
2017-12-06 03:21:20 +08:00
it('when the layer\'s row count is lower than threshold should skip aggregation', function (done) {
const mapConfig = createVectorMapConfig([
2017-12-06 03:21:20 +08:00
{
type: 'cartodb',
options: {
sql: POINTS_SQL_1,
aggregation: {
columns: {
total: {
aggregate_function: 'sum',
aggregated_column: 'value'
}
},
threshold: 1001
}
2017-12-06 03:21:20 +08:00
}
}
]);
this.testClient = new TestClient(mapConfig);
const options = {};
this.testClient.getLayergroup(options, (err, body) => {
2017-12-06 03:21:20 +08:00
if (err) {
return done(err);
}
assert.equal(typeof body.metadata, 'object');
assert.ok(Array.isArray(body.metadata.layers));
body.metadata.layers.forEach(layer =>{
assert.deepEqual(layer.meta.aggregation, { png: false, mvt: false });
});
2017-12-06 03:21:20 +08:00
done();
});
});
it('when the layer\'s geometry type is not point should responds with error', function (done) {
const mapConfig = createVectorMapConfig([
2017-12-06 03:21:20 +08:00
{
type: 'cartodb',
options: {
sql: POLYGONS_SQL_1,
aggregation: {
threshold: 1
}
2017-12-06 03:21:20 +08:00
}
}
]);
this.testClient = new TestClient(mapConfig);
const options = {
response: {
status: 400
}
};
this.testClient.getLayergroup(options, (err, body) => {
2017-12-06 03:21:20 +08:00
if (err) {
return done(err);
}
assert.deepEqual(body, {
errors: [
2017-12-12 19:39:12 +08:00
'Unsupported geometry type: ST_Polygon.' +
' Aggregation is available only for geometry type: ST_Point'
],
errors_with_context:[{
type: 'unknown',
2017-12-12 19:39:12 +08:00
message: 'Unsupported geometry type: ST_Polygon.' +
' Aggregation is available only for geometry type: ST_Point'
}]
});
2017-12-06 03:21:20 +08:00
done();
});
});
});
});
});