Windshaft-cartodb/test/acceptance/cluster.js

430 lines
14 KiB
JavaScript
Raw Normal View History

2019-02-27 02:19:44 +08:00
'use strict';
require('../support/test_helper');
2019-02-27 19:43:26 +08:00
const assert = require('../support/assert');
2019-02-27 02:19:44 +08:00
const TestClient = require('../support/test-client');
const POINTS_SQL_1 = `
select
x + 4 as cartodb_id,
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,
2019-03-01 18:21:18 +08:00
x as value,
CASE
WHEN x % 2 = 0 THEN 'even'
ELSE 'odd'
END AS type
2019-02-27 02:19:44 +08:00
from generate_series(-3, 3) x
`;
const defaultLayers = [{
type: 'cartodb',
options: {
sql: POINTS_SQL_1,
2019-02-27 19:43:26 +08:00
aggregation: {
threshold: 1
}
2019-02-27 02:19:44 +08:00
}
}];
function createVectorMapConfig (layers = defaultLayers) {
return {
version: '1.8.0',
layers: layers
};
}
describe('cluster', function () {
describe('map-config w/o aggregation', function () {
it('should return error while fetching disaggregated features', function (done) {
const mapConfig = createVectorMapConfig([{
type: 'cartodb',
options: {
sql: POINTS_SQL_1,
cartocss: TestClient.CARTOCSS.POINTS,
cartocss_version: '2.3.0'
}
}]);
const testClient = new TestClient(mapConfig);
const zoom = 0;
const cartodb_id = 1;
const layerId = 0;
const params = {
response: {
status: 400
}
};
testClient.getClusterFeatures(zoom, cartodb_id, layerId, params, (err, body) => {
if (err) {
return done(err);
}
assert.deepStrictEqual(body, {
2019-03-01 18:21:18 +08:00
errors:[ 'Map c502fc8fc1cb0d5e412db3deabffeee5 has no aggregation defined for layer 0' ],
errors_with_context:[
{
2019-02-28 19:13:15 +08:00
layer: {
index: '0',
type: 'cartodb'
},
2019-03-01 18:21:18 +08:00
message: 'Map c502fc8fc1cb0d5e412db3deabffeee5 has no aggregation defined for layer 0',
2019-02-28 19:13:15 +08:00
subtype: 'aggregation',
type: 'layer'
}
]
});
2019-02-28 19:13:15 +08:00
testClient.drain(done);
});
});
it('with aggregation disabled should return error while fetching disaggregated features', function (done) {
const mapConfig = createVectorMapConfig([{
type: 'cartodb',
options: {
sql: POINTS_SQL_1,
aggregation: false
}
}]);
const testClient = new TestClient(mapConfig);
const zoom = 0;
const cartodb_id = 1;
const layerId = 0;
const params = {
response: {
status: 400
}
};
testClient.getClusterFeatures(zoom, cartodb_id, layerId, params, (err, body) => {
if (err) {
return done(err);
}
assert.deepStrictEqual(body, {
2019-03-01 18:21:18 +08:00
errors:[ 'Map 18792467ae296929d04e32dfe7f81a80 has no aggregation defined for layer 0' ],
2019-02-28 19:13:15 +08:00
errors_with_context:[
{
layer: {
index: '0',
type: 'cartodb'
},
2019-03-01 18:21:18 +08:00
message: 'Map 18792467ae296929d04e32dfe7f81a80 has no aggregation defined for layer 0',
2019-02-28 19:13:15 +08:00
subtype: 'aggregation',
type: 'layer'
}
]
});
testClient.drain(done);
});
});
});
describe('map-config with aggregation', function () {
2019-02-27 19:43:26 +08:00
const suite = [
{
zoom: 0,
2019-02-27 19:43:26 +08:00
cartodb_id: 1,
resolution: 0.5,
2019-03-01 18:21:18 +08:00
expected: [ { cartodb_id: 1, value: -3, type: 'odd' } ]
2019-02-27 19:43:26 +08:00
},
{
zoom: 0,
2019-02-27 19:43:26 +08:00
cartodb_id: 2,
resolution: 0.5,
2019-03-01 18:21:18 +08:00
expected: [ { cartodb_id: 2, value: -2, type: 'even' } ]
2019-02-27 19:43:26 +08:00
},
{
zoom: 0,
2019-02-27 19:43:26 +08:00
cartodb_id: 3,
resolution: 0.5,
2019-03-01 18:21:18 +08:00
expected: [ { cartodb_id: 3, value: -1, type: 'odd' } ]
2019-02-27 19:43:26 +08:00
},
{
zoom: 0,
2019-02-27 19:43:26 +08:00
cartodb_id: 4,
resolution: 0.5,
2019-03-01 18:21:18 +08:00
expected: [ { cartodb_id: 4, value: 0, type: 'even' } ]
2019-02-27 19:43:26 +08:00
},
{
zoom: 0,
2019-02-27 19:43:26 +08:00
cartodb_id: 5,
resolution: 0.5,
2019-03-01 18:21:18 +08:00
expected: [ { cartodb_id: 5, value: 1, type: 'odd' } ]
2019-02-27 19:43:26 +08:00
},
{
zoom: 0,
2019-02-27 19:43:26 +08:00
cartodb_id: 6,
resolution: 0.5,
2019-03-01 18:21:18 +08:00
expected: [ { cartodb_id: 6, value: 2, type: 'even' } ]
},
{
zoom: 0,
cartodb_id: 7,
resolution: 0.5,
2019-03-01 18:21:18 +08:00
expected: [ { cartodb_id: 7, value: 3, type: 'odd' } ]
},
{
zoom: 0,
cartodb_id: 1,
resolution: 1,
2019-03-01 18:21:18 +08:00
expected: [ { cartodb_id: 1, value: -3, type: 'odd' } ]
},
{
zoom: 0,
cartodb_id: 2,
resolution: 1,
2019-03-01 18:21:18 +08:00
expected: [ { cartodb_id: 2, value: -2, type: 'even' } ]
},
{
zoom: 0,
cartodb_id: 3,
resolution: 1,
2019-03-01 18:21:18 +08:00
expected: [ { cartodb_id: 3, value: -1, type: 'odd' } ]
},
{
zoom: 0,
cartodb_id: 4,
resolution: 1,
2019-03-01 18:21:18 +08:00
expected: [ { cartodb_id: 4, value: 0, type: 'even' } ]
},
{
zoom: 0,
cartodb_id: 5,
resolution: 1,
2019-03-01 18:21:18 +08:00
expected: [ { cartodb_id: 5, value: 1, type: 'odd' } ]
},
{
zoom: 0,
cartodb_id: 6,
resolution: 1,
2019-03-01 18:21:18 +08:00
expected: [ { cartodb_id: 6, value: 2, type: 'even' } ]
},
2019-02-27 19:43:26 +08:00
{
zoom: 0,
cartodb_id: 7,
resolution: 1,
2019-03-01 18:21:18 +08:00
expected: [ { cartodb_id: 7, value: 3, type: 'odd' } ]
},
{
zoom: 0,
2019-02-27 19:43:26 +08:00
cartodb_id: 1,
resolution: 50,
expected: [
2019-03-01 18:21:18 +08:00
{ cartodb_id: 1, value: -3, type: 'odd' },
{ cartodb_id: 2, value: -2, type: 'even' },
{ cartodb_id: 3, value: -1, type: 'odd' },
{ cartodb_id: 4, value: 0, type: 'even' },
2019-02-27 19:43:26 +08:00
]
},
{
zoom: 0,
cartodb_id: 5,
resolution: 50,
expected: [
2019-03-01 18:21:18 +08:00
{ cartodb_id: 5, value: 1, type: 'odd' },
{ cartodb_id: 6, value: 2, type: 'even' },
{ cartodb_id: 7, value: 3, type: 'odd' }
]
},
{
zoom: 1,
cartodb_id: 1,
resolution: 1,
2019-03-01 18:21:18 +08:00
expected: [ { cartodb_id: 1, value: -3, type: 'odd' } ]
},
{
zoom: 1,
cartodb_id: 2,
resolution: 1,
2019-03-01 18:21:18 +08:00
expected: [ { cartodb_id: 2, value: -2, type: 'even' } ]
},
{
zoom: 1,
cartodb_id: 3,
resolution: 1,
2019-03-01 18:21:18 +08:00
expected: [ { cartodb_id: 3, value: -1, type: 'odd' } ]
},
{
zoom: 1,
cartodb_id: 4,
resolution: 1,
2019-03-01 18:21:18 +08:00
expected: [ { cartodb_id: 4, value: 0, type: 'even' } ]
},
{
zoom: 1,
cartodb_id: 5,
resolution: 1,
2019-03-01 18:21:18 +08:00
expected: [ { cartodb_id: 5, value: 1, type: 'odd' } ]
},
{
zoom: 1,
cartodb_id: 6,
resolution: 1,
2019-03-01 18:21:18 +08:00
expected: [ { cartodb_id: 6, value: 2, type: 'even' } ]
},
{
zoom: 1,
cartodb_id: 7,
resolution: 1,
2019-03-01 18:21:18 +08:00
expected: [ { cartodb_id: 7, value: 3, type: 'odd' } ]
},
{
zoom: 1,
cartodb_id: 1,
resolution: 50,
expected: [
2019-03-01 18:21:18 +08:00
{ cartodb_id: 1, value: -3, type: 'odd' },
{ cartodb_id: 2, value: -2, type: 'even'},
{ cartodb_id: 3, value: -1, type: 'odd' },
{ cartodb_id: 4, value: 0, type: 'even' },
]
},
{
zoom: 1,
2019-02-27 19:43:26 +08:00
cartodb_id: 5,
resolution: 50,
expected: [
2019-03-01 18:21:18 +08:00
{ cartodb_id: 5, value: 1, type: 'odd' },
{ cartodb_id: 6, value: 2, type: 'even' },
{ cartodb_id: 7, value: 3, type: 'odd' }
2019-02-27 19:43:26 +08:00
]
}
];
suite.forEach(({ zoom, cartodb_id, resolution, expected }) => {
const description = `should get features for z: ${zoom} cartodb_id: ${cartodb_id}, res: ${resolution}`;
it(description, function (done) {
2019-02-27 19:43:26 +08:00
const mapConfig = createVectorMapConfig([{
type: 'cartodb',
options: {
sql: POINTS_SQL_1,
aggregation: {
threshold: 1,
resolution: resolution
}
}
}]);
const testClient = new TestClient(mapConfig);
const layerId = 0;
const params = {};
testClient.getClusterFeatures(zoom, cartodb_id, layerId, params, (err, body) => {
2019-02-27 19:43:26 +08:00
if (err) {
return done(err);
}
assert.deepStrictEqual(body.rows, expected);
testClient.drain(done);
});
});
2019-02-27 02:19:44 +08:00
});
});
2019-03-01 18:21:18 +08:00
describe('map-config w/o aggregation', function () {
const suite = [
{
zoom: 0,
cartodb_id: 1,
resolution: 1,
aggregation: { columns: ['type'] },
expected: [ { _cdb_feature_count: 1, type: 'odd' } ]
},
{
zoom: 0,
cartodb_id: 2,
resolution: 1,
aggregation: { columns: ['type'] },
expected: [ { _cdb_feature_count: 1, type: 'even' } ]
},
{
zoom: 0,
cartodb_id: 3,
resolution: 1,
aggregation: { columns: ['type'] },
expected: [ { _cdb_feature_count: 1, type: 'odd' } ]
},
{
zoom: 0,
cartodb_id: 4,
resolution: 1,
aggregation: { columns: ['type'] },
expected: [ { _cdb_feature_count: 1, type: 'even' } ]
},
{
zoom: 0,
cartodb_id: 5,
resolution: 1,
aggregation: { columns: ['type'] },
expected: [ { _cdb_feature_count: 1, type: 'odd' } ]
},
{
zoom: 0,
cartodb_id: 6,
resolution: 1,
aggregation: { columns: ['type'] },
expected: [ { _cdb_feature_count: 1, type: 'even' } ]
},
{
zoom: 0,
cartodb_id: 7,
resolution: 1,
aggregation: { columns: ['type'] },
expected: [ { _cdb_feature_count: 1, type: 'odd' } ]
},
{
zoom: 0,
cartodb_id: 1,
resolution: 50,
aggregation: { columns: ['type'] },
expected: [
{ _cdb_feature_count: 2, type: 'even' },
{ _cdb_feature_count: 2, type: 'odd' }
]
},
{
zoom: 0,
cartodb_id: 5,
resolution: 50,
aggregation: { columns: ['type'] },
expected: [
{ _cdb_feature_count: 1, type: 'even' },
{ _cdb_feature_count: 2, type: 'odd' }
]
}
];
suite.forEach(({ zoom, cartodb_id, resolution, aggregation, expected }) => {
it('should return features aggregated by type', function (done) {
const mapConfig = createVectorMapConfig([{
type: 'cartodb',
options: {
sql: POINTS_SQL_1,
aggregation: {
threshold: 1,
resolution
}
}
}]);
const testClient = new TestClient(mapConfig);
const layerId = 0;
const params = { aggregation };
testClient.getClusterFeatures(zoom, cartodb_id, layerId, params, (err, body) => {
if (err) {
return done(err);
}
assert.deepStrictEqual(body.rows, expected);
testClient.drain(done);
});
});
});
});
2019-02-27 02:19:44 +08:00
});