2018-10-24 00:39:02 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-07 16:44:45 +08:00
|
|
|
require('../../support/test-helper');
|
2016-03-14 23:06:25 +08:00
|
|
|
|
|
|
|
var assert = require('../../support/assert');
|
|
|
|
var TestClient = require('../../support/test-client');
|
|
|
|
var dot = require('dot');
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
describe('analysis-layers', function () {
|
2016-03-14 23:16:27 +08:00
|
|
|
var IMAGE_TOLERANCE_PER_MIL = 20;
|
2016-03-14 23:06:25 +08:00
|
|
|
|
|
|
|
var multitypeStyleTemplate = dot.template([
|
|
|
|
"#points['mapnik::geometry_type'=1] {",
|
2019-10-22 01:07:24 +08:00
|
|
|
' marker-fill-opacity: {{=it._opacity}};',
|
|
|
|
' marker-line-color: #FFF;',
|
|
|
|
' marker-line-width: 0.5;',
|
|
|
|
' marker-line-opacity: {{=it._opacity}};',
|
|
|
|
' marker-placement: point;',
|
|
|
|
' marker-type: ellipse;',
|
|
|
|
' marker-width: 8;',
|
|
|
|
' marker-fill: {{=it._color}};',
|
|
|
|
' marker-allow-overlap: true;',
|
|
|
|
'}',
|
2016-03-14 23:06:25 +08:00
|
|
|
"#lines['mapnik::geometry_type'=2] {",
|
2019-10-22 01:07:24 +08:00
|
|
|
' line-color: {{=it._color}};',
|
|
|
|
' line-width: 2;',
|
|
|
|
' line-opacity: {{=it._opacity}};',
|
|
|
|
'}',
|
2016-03-14 23:06:25 +08:00
|
|
|
"#polygons['mapnik::geometry_type'=3] {",
|
2019-10-22 01:07:24 +08:00
|
|
|
' polygon-fill: {{=it._color}};',
|
|
|
|
' polygon-opacity: {{=it._opacity}};',
|
|
|
|
' line-color: #FFF;',
|
|
|
|
' line-width: 0.5;',
|
|
|
|
' line-opacity: {{=it._opacity}};',
|
|
|
|
'}'
|
2016-03-14 23:06:25 +08:00
|
|
|
].join('\n'));
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function cartocss (color, opacity) {
|
2016-03-14 23:06:25 +08:00
|
|
|
return multitypeStyleTemplate({
|
|
|
|
_color: color || '#F11810',
|
|
|
|
_opacity: Number.isFinite(opacity) ? opacity : 1
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function mapConfig (layers, dataviews, analysis) {
|
2016-03-14 23:06:25 +08:00
|
|
|
return {
|
|
|
|
version: '1.5.0',
|
|
|
|
layers: layers,
|
|
|
|
dataviews: dataviews || {},
|
|
|
|
analyses: analysis || []
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
var DEFAULT_MULTITYPE_STYLE = cartocss();
|
|
|
|
|
2016-03-17 19:50:42 +08:00
|
|
|
var TILE_ANALYSIS_TABLES = { z: 0, x: 0, y: 0 };
|
2016-03-14 23:06:25 +08:00
|
|
|
|
|
|
|
var useCases = [
|
|
|
|
{
|
|
|
|
desc: 'basic source-id mapnik layer',
|
2016-03-17 19:50:42 +08:00
|
|
|
fixture: 'basic-source-id-mapnik-layer.png',
|
2016-03-14 23:06:25 +08:00
|
|
|
mapConfig: mapConfig(
|
|
|
|
[
|
|
|
|
{
|
2019-10-22 01:07:24 +08:00
|
|
|
type: 'cartodb',
|
|
|
|
options: {
|
|
|
|
source: {
|
|
|
|
id: '2570e105-7b37-40d2-bdf4-1af889598745'
|
2016-03-14 23:06:25 +08:00
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
cartocss: DEFAULT_MULTITYPE_STYLE,
|
|
|
|
cartocss_version: '2.3.0'
|
2016-03-14 23:06:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
{},
|
|
|
|
[
|
|
|
|
{
|
2019-10-22 01:07:24 +08:00
|
|
|
id: '2570e105-7b37-40d2-bdf4-1af889598745',
|
|
|
|
type: 'source',
|
|
|
|
params: {
|
|
|
|
query: 'select * from populated_places_simple_reduced'
|
2016-03-14 23:06:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2016-03-17 19:50:42 +08:00
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
desc: 'buffer over source',
|
|
|
|
fixture: 'buffer-over-source.png',
|
|
|
|
tile: { z: 7, x: 61, y: 47 },
|
|
|
|
mapConfig: mapConfig(
|
|
|
|
[
|
|
|
|
{
|
2019-10-22 01:07:24 +08:00
|
|
|
type: 'cartodb',
|
|
|
|
options: {
|
|
|
|
source: {
|
|
|
|
id: 'HEAD'
|
2016-03-17 19:50:42 +08:00
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
cartocss: DEFAULT_MULTITYPE_STYLE,
|
|
|
|
cartocss_version: '2.3.0'
|
2016-03-17 19:50:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
{},
|
|
|
|
[
|
|
|
|
{
|
2019-10-22 01:07:24 +08:00
|
|
|
id: 'HEAD',
|
|
|
|
type: 'buffer',
|
|
|
|
params: {
|
|
|
|
source: {
|
|
|
|
id: '2570e105-7b37-40d2-bdf4-1af889598745',
|
|
|
|
type: 'source',
|
|
|
|
params: {
|
|
|
|
query: 'select * from populated_places_simple_reduced'
|
2016-03-17 19:50:42 +08:00
|
|
|
}
|
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
radius: 50000
|
2016-03-17 19:50:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
2016-03-14 23:06:25 +08:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
useCases.forEach(function (useCase) {
|
|
|
|
it('should implement use case: "' + useCase.desc + '"', function (done) {
|
2016-03-14 23:06:25 +08:00
|
|
|
var testClient = new TestClient(useCase.mapConfig, 1234);
|
|
|
|
|
|
|
|
var tile = useCase.tile || TILE_ANALYSIS_TABLES;
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
testClient.getTile(tile.z, tile.x, tile.y, function (err, res, image) {
|
2016-03-14 23:06:25 +08:00
|
|
|
assert.ok(!err, err);
|
|
|
|
|
2016-03-14 23:19:55 +08:00
|
|
|
// To generate images use:
|
|
|
|
// image.save('/tmp/' + useCase.desc.replace(/\s/g, '-') + '.png');
|
|
|
|
|
2016-03-17 19:50:42 +08:00
|
|
|
var fixturePath = './test/fixtures/analysis/' + useCase.fixture;
|
2019-10-22 01:07:24 +08:00
|
|
|
assert.imageIsSimilarToFile(image, fixturePath, IMAGE_TOLERANCE_PER_MIL, function (err) {
|
2016-03-14 23:16:27 +08:00
|
|
|
assert.ok(!err, err);
|
2016-03-14 23:06:25 +08:00
|
|
|
|
2016-03-14 23:16:27 +08:00
|
|
|
testClient.drain(done);
|
|
|
|
});
|
2016-03-14 23:06:25 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-03-14 23:42:51 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('should NOT fail for non-authenticated requests when it is just source', function (done) {
|
2016-03-14 23:42:51 +08:00
|
|
|
var useCase = useCases[0];
|
|
|
|
|
|
|
|
// No API key here
|
|
|
|
var testClient = new TestClient(useCase.mapConfig);
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
testClient.getLayergroup(function (err, layergroupResult) {
|
2016-03-17 19:50:42 +08:00
|
|
|
assert.ok(!err, err);
|
|
|
|
|
|
|
|
assert.equal(layergroupResult.metadata.layers.length, 1);
|
|
|
|
|
|
|
|
testClient.drain(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('should fail for non-authenticated requests that has a node other than "source"', function (done) {
|
2016-03-17 19:50:42 +08:00
|
|
|
var useCase = useCases[1];
|
|
|
|
|
|
|
|
// No API key here
|
|
|
|
var testClient = new TestClient(useCase.mapConfig);
|
|
|
|
|
2016-03-14 23:42:51 +08:00
|
|
|
var PERMISSION_DENIED_RESPONSE = {
|
|
|
|
status: 403,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json; charset=utf-8'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
testClient.getLayergroup({ response: PERMISSION_DENIED_RESPONSE }, function (err, layergroupResult) {
|
2016-03-14 23:42:51 +08:00
|
|
|
assert.ok(!err, err);
|
2016-04-14 18:56:20 +08:00
|
|
|
assert.deepEqual(
|
|
|
|
layergroupResult.errors,
|
2019-10-22 01:07:24 +08:00
|
|
|
['Analysis requires authentication with API key: permission denied.']
|
2016-04-14 18:56:20 +08:00
|
|
|
);
|
2016-03-14 23:42:51 +08:00
|
|
|
|
2016-03-17 19:50:42 +08:00
|
|
|
testClient.drain(done);
|
2016-03-14 23:42:51 +08:00
|
|
|
});
|
|
|
|
});
|
2016-04-21 23:03:41 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('should retrieve enough metadata about analyses', function (done) {
|
2016-04-21 23:03:41 +08:00
|
|
|
var useCase = useCases[1];
|
|
|
|
|
|
|
|
// No API key here
|
|
|
|
var testClient = new TestClient(useCase.mapConfig, 1234);
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
testClient.getLayergroup(function (err, layergroupResult) {
|
2016-04-21 23:03:41 +08:00
|
|
|
assert.ok(!err, err);
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
Array.isArray(layergroupResult.metadata.analyses),
|
2019-10-22 01:07:24 +08:00
|
|
|
'Missing "analyses" array metadata from: ' + JSON.stringify(layergroupResult)
|
2016-04-21 23:03:41 +08:00
|
|
|
);
|
|
|
|
var analyses = layergroupResult.metadata.analyses;
|
|
|
|
assert.equal(analyses.length, 1, 'Invalid number of analyses in metadata');
|
|
|
|
var nodes = analyses[0].nodes;
|
|
|
|
var nodesIds = Object.keys(nodes);
|
2016-07-05 17:26:52 +08:00
|
|
|
assert.deepEqual(nodesIds, ['HEAD', '2570e105-7b37-40d2-bdf4-1af889598745']);
|
2019-10-22 01:07:24 +08:00
|
|
|
nodesIds.forEach(function (nodeId) {
|
2016-04-21 23:03:41 +08:00
|
|
|
var node = nodes[nodeId];
|
|
|
|
assert.ok(node.hasOwnProperty('url'), 'Missing "url" attribute in node');
|
|
|
|
assert.ok(node.hasOwnProperty('status'), 'Missing "status" attribute in node');
|
|
|
|
assert.ok(node.hasOwnProperty('query'), 'Missing "status" attribute in node');
|
|
|
|
});
|
|
|
|
|
|
|
|
testClient.drain(done);
|
|
|
|
});
|
|
|
|
});
|
2016-04-29 18:51:48 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('should have analysis metadata when dataviews point to source node', function (done) {
|
2016-04-29 18:51:48 +08:00
|
|
|
var useCase = {
|
|
|
|
desc: 'basic source-id mapnik layer',
|
|
|
|
fixture: 'basic-source-id-mapnik-layer.png',
|
|
|
|
mapConfig: mapConfig(
|
|
|
|
[
|
|
|
|
{
|
2019-10-22 01:07:24 +08:00
|
|
|
type: 'cartodb',
|
|
|
|
options: {
|
|
|
|
sql: 'select * from populated_places_simple_reduced',
|
|
|
|
cartocss: DEFAULT_MULTITYPE_STYLE,
|
|
|
|
cartocss_version: '2.3.0'
|
2016-04-29 18:51:48 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2019-10-22 01:07:24 +08:00
|
|
|
type: 'cartodb',
|
|
|
|
options: {
|
|
|
|
source: {
|
|
|
|
id: '2570e105-7b37-40d2-bdf4-1af889598745'
|
2016-04-29 18:51:48 +08:00
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
cartocss: DEFAULT_MULTITYPE_STYLE,
|
|
|
|
cartocss_version: '2.3.0'
|
2016-04-29 18:51:48 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2019-10-22 01:07:24 +08:00
|
|
|
type: 'cartodb',
|
|
|
|
options: {
|
|
|
|
sql: 'select * from populated_places_simple_reduced',
|
|
|
|
cartocss: DEFAULT_MULTITYPE_STYLE,
|
|
|
|
cartocss_version: '2.3.0'
|
2016-04-29 18:51:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
{
|
|
|
|
pop_max_histogram: {
|
|
|
|
source: {
|
|
|
|
id: '2570e105-7b37-40d2-bdf4-1af889598745'
|
|
|
|
},
|
|
|
|
type: 'histogram',
|
|
|
|
options: {
|
|
|
|
column: 'pop_max'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
scalerank_histogram: {
|
|
|
|
source: {
|
|
|
|
id: '2570e105-7b37-40d2-bdf4-1af889598745'
|
|
|
|
},
|
|
|
|
type: 'histogram',
|
|
|
|
options: {
|
|
|
|
column: 'scalerank'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[
|
|
|
|
{
|
2019-10-22 01:07:24 +08:00
|
|
|
id: '2570e105-7b37-40d2-bdf4-1af889598745',
|
|
|
|
type: 'source',
|
|
|
|
params: {
|
|
|
|
query: 'select * from populated_places_simple_reduced'
|
2016-04-29 18:51:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
var testClient = new TestClient(useCase.mapConfig, 1234);
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
testClient.getLayergroup(function (err, layergroupResult) {
|
2016-04-29 18:51:48 +08:00
|
|
|
assert.ok(!err, err);
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
Array.isArray(layergroupResult.metadata.analyses),
|
2019-10-22 01:07:24 +08:00
|
|
|
'Missing "analyses" array metadata from: ' + JSON.stringify(layergroupResult)
|
2016-04-29 18:51:48 +08:00
|
|
|
);
|
|
|
|
var analyses = layergroupResult.metadata.analyses;
|
|
|
|
assert.equal(analyses.length, 1, 'Invalid number of analyses in metadata');
|
|
|
|
var nodes = analyses[0].nodes;
|
|
|
|
|
|
|
|
var nodesIds = Object.keys(nodes);
|
|
|
|
assert.deepEqual(nodesIds, ['2570e105-7b37-40d2-bdf4-1af889598745']);
|
2019-10-22 01:07:24 +08:00
|
|
|
nodesIds.forEach(function (nodeId) {
|
2016-04-29 18:51:48 +08:00
|
|
|
var node = nodes[nodeId];
|
|
|
|
assert.ok(node.hasOwnProperty('url'), 'Missing "url" attribute in node');
|
|
|
|
assert.ok(node.hasOwnProperty('status'), 'Missing "status" attribute in node');
|
|
|
|
assert.ok(node.hasOwnProperty('query'), 'Missing "status" attribute in node');
|
|
|
|
});
|
|
|
|
|
|
|
|
testClient.drain(done);
|
|
|
|
});
|
|
|
|
});
|
2016-05-05 23:52:37 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('should response with custom cache headers for node status endpoints', function (done) {
|
2016-05-05 23:52:37 +08:00
|
|
|
var useCase = useCases[1];
|
|
|
|
|
|
|
|
// No API key here
|
|
|
|
var testClient = new TestClient(useCase.mapConfig, 1234);
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
testClient.getNodeStatus('HEAD', function (err, response, nodeStatus) {
|
2016-05-05 23:52:37 +08:00
|
|
|
assert.ok(!err, err);
|
|
|
|
|
|
|
|
assert.equal(nodeStatus.status, 'ready');
|
|
|
|
|
|
|
|
var headers = response.headers;
|
|
|
|
|
|
|
|
assert.equal(headers['cache-control'], 'public,max-age=5');
|
|
|
|
|
|
|
|
var lastModified = new Date(headers['last-modified']);
|
|
|
|
var tenSecondsInMs = 1e5;
|
|
|
|
assert.ok(Date.now() - lastModified.getTime() < tenSecondsInMs);
|
|
|
|
|
|
|
|
testClient.drain(done);
|
|
|
|
});
|
|
|
|
});
|
2016-05-06 22:37:52 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('should wrap queries from analyses', function (done) {
|
2016-05-06 22:37:52 +08:00
|
|
|
var testClient = new TestClient(mapConfig(
|
|
|
|
[
|
|
|
|
{
|
2019-10-22 01:07:24 +08:00
|
|
|
type: 'cartodb',
|
|
|
|
options: {
|
|
|
|
source: {
|
|
|
|
id: '2570e105-7b37-40d2-bdf4-1af889598745'
|
2016-05-06 22:37:52 +08:00
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
sql_wrap: 'SELECT * FROM (<%= sql %>) __wrapped WHERE adm0cap = 1',
|
|
|
|
cartocss: DEFAULT_MULTITYPE_STYLE,
|
|
|
|
cartocss_version: '2.3.0'
|
2016-05-06 22:37:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
{},
|
|
|
|
[
|
|
|
|
{
|
2019-10-22 01:07:24 +08:00
|
|
|
id: '2570e105-7b37-40d2-bdf4-1af889598745',
|
|
|
|
type: 'source',
|
|
|
|
params: {
|
|
|
|
query: 'select * from populated_places_simple_reduced'
|
2016-05-06 22:37:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
), 1234);
|
|
|
|
|
|
|
|
var tile = TILE_ANALYSIS_TABLES;
|
2019-10-22 01:07:24 +08:00
|
|
|
testClient.getTile(tile.z, tile.x, tile.y, function (err, res, image) {
|
2016-05-06 22:37:52 +08:00
|
|
|
assert.ok(!err, err);
|
|
|
|
|
|
|
|
var fixturePath = './test/fixtures/analysis/adm0cap-source-id-mapnik-layer.png';
|
2019-10-22 01:07:24 +08:00
|
|
|
assert.imageIsSimilarToFile(image, fixturePath, IMAGE_TOLERANCE_PER_MIL, function (err) {
|
2016-05-06 22:37:52 +08:00
|
|
|
assert.ok(!err, err);
|
|
|
|
|
|
|
|
testClient.drain(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-03-14 23:06:25 +08:00
|
|
|
});
|