2016-04-07 22:18:48 +08:00
|
|
|
var assert = require('../../support/assert');
|
|
|
|
|
|
|
|
var helper = require('../../support/test_helper');
|
|
|
|
|
|
|
|
var CartodbWindshaft = require('../../../lib/cartodb/server');
|
|
|
|
var serverOptions = require('../../../lib/cartodb/server_options');
|
|
|
|
var server = new CartodbWindshaft(serverOptions);
|
2016-05-13 22:56:52 +08:00
|
|
|
var TestClient = require('../../support/test-client');
|
2016-04-07 22:18:48 +08:00
|
|
|
|
2016-05-23 19:01:23 +08:00
|
|
|
var LayergroupToken = require('../../support/layergroup-token');
|
2016-04-07 22:18:48 +08:00
|
|
|
|
|
|
|
describe('named-maps analysis', function() {
|
|
|
|
|
|
|
|
var IMAGE_TOLERANCE_PER_MIL = 20;
|
|
|
|
|
|
|
|
var username = 'localhost';
|
|
|
|
var widgetsTemplateName = 'widgets-template';
|
|
|
|
|
2016-05-13 22:56:52 +08:00
|
|
|
var widgetsTemplate = {
|
|
|
|
version: '0.0.1',
|
|
|
|
name: widgetsTemplateName,
|
|
|
|
layergroup: {
|
|
|
|
version: '1.5.0',
|
|
|
|
layers: [
|
|
|
|
{
|
|
|
|
"type": "cartodb",
|
|
|
|
"options": {
|
|
|
|
"source": {
|
|
|
|
"id": "HEAD"
|
2016-04-21 21:35:45 +08:00
|
|
|
},
|
2016-05-13 22:56:52 +08:00
|
|
|
"cartocss": '#buffer { polygon-fill: red; }',
|
|
|
|
"cartocss_version": "2.3.0"
|
2016-04-07 22:18:48 +08:00
|
|
|
}
|
2016-05-13 22:56:52 +08:00
|
|
|
}
|
|
|
|
],
|
|
|
|
dataviews: {
|
|
|
|
pop_max_histogram: {
|
|
|
|
source: {
|
|
|
|
id: 'HEAD'
|
2016-04-07 22:18:48 +08:00
|
|
|
},
|
2016-05-13 22:56:52 +08:00
|
|
|
type: 'histogram',
|
|
|
|
options: {
|
|
|
|
column: 'pop_max'
|
2016-04-07 22:18:48 +08:00
|
|
|
}
|
2016-05-13 22:56:52 +08:00
|
|
|
}
|
2016-04-07 22:18:48 +08:00
|
|
|
},
|
2016-05-13 22:56:52 +08:00
|
|
|
analyses: [
|
|
|
|
{
|
|
|
|
"id": "HEAD",
|
|
|
|
"type": "buffer",
|
|
|
|
"params": {
|
|
|
|
"source": {
|
|
|
|
"id": "2570e105-7b37-40d2-bdf4-1af889598745",
|
|
|
|
"type": "source",
|
|
|
|
"params": {
|
|
|
|
"query": "select * from populated_places_simple_reduced"
|
|
|
|
}
|
2016-04-07 22:18:48 +08:00
|
|
|
},
|
2016-05-13 22:56:52 +08:00
|
|
|
"radius": 50000
|
2016-04-07 22:18:48 +08:00
|
|
|
}
|
2016-05-13 22:56:52 +08:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
2016-04-07 22:18:48 +08:00
|
|
|
|
2016-05-13 22:56:52 +08:00
|
|
|
beforeEach(function createTemplate(done) {
|
2016-04-07 22:18:48 +08:00
|
|
|
assert.response(
|
|
|
|
server,
|
|
|
|
{
|
2016-05-13 22:56:52 +08:00
|
|
|
url: '/api/v1/map/named?api_key=1234',
|
|
|
|
method: 'POST',
|
2016-04-07 22:18:48 +08:00
|
|
|
headers: {
|
2016-05-13 22:56:52 +08:00
|
|
|
host: username,
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
data: JSON.stringify(widgetsTemplate)
|
2016-04-07 22:18:48 +08:00
|
|
|
},
|
|
|
|
{
|
2016-05-13 22:56:52 +08:00
|
|
|
status: 200
|
2016-04-07 22:18:48 +08:00
|
|
|
},
|
|
|
|
function(res, err) {
|
2016-05-13 22:56:52 +08:00
|
|
|
assert.deepEqual(JSON.parse(res.body), { template_id: widgetsTemplateName });
|
|
|
|
return done(err);
|
2016-04-07 22:18:48 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2016-05-13 22:56:52 +08:00
|
|
|
afterEach(function deleteTemplate(done) {
|
2016-04-21 21:35:45 +08:00
|
|
|
assert.response(
|
|
|
|
server,
|
|
|
|
{
|
2016-05-13 22:56:52 +08:00
|
|
|
url: '/api/v1/map/named/' + widgetsTemplateName + '?api_key=1234',
|
|
|
|
method: 'DELETE',
|
2016-04-21 21:35:45 +08:00
|
|
|
headers: {
|
|
|
|
host: username
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2016-05-13 22:56:52 +08:00
|
|
|
status: 204
|
2016-04-21 21:35:45 +08:00
|
|
|
},
|
|
|
|
function(res, err) {
|
2016-05-13 22:56:52 +08:00
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('layergroup', function() {
|
|
|
|
var layergroupid;
|
|
|
|
var layergroup;
|
|
|
|
var keysToDelete;
|
|
|
|
|
|
|
|
beforeEach(function(done) {
|
|
|
|
keysToDelete = {};
|
|
|
|
|
|
|
|
assert.response(
|
|
|
|
server,
|
|
|
|
{
|
|
|
|
url: '/api/v1/map/named/' + widgetsTemplateName,
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
host: username,
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
data: JSON.stringify({})
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 200
|
|
|
|
},
|
|
|
|
function(res, err) {
|
|
|
|
assert.ifError(err);
|
|
|
|
|
|
|
|
layergroup = JSON.parse(res.body);
|
|
|
|
assert.ok(layergroup.hasOwnProperty('layergroupid'), "Missing 'layergroupid' from: " + res.body);
|
|
|
|
layergroupid = layergroup.layergroupid;
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
Array.isArray(layergroup.metadata.analyses),
|
|
|
|
'Missing "analyses" array metadata from: ' + res.body
|
|
|
|
);
|
|
|
|
var analyses = layergroup.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']);
|
2016-05-13 22:56:52 +08:00
|
|
|
nodesIds.forEach(function(nodeId) {
|
|
|
|
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'), 'Unexpected "query" attribute in node');
|
|
|
|
});
|
|
|
|
|
|
|
|
keysToDelete['map_cfg|' + LayergroupToken.parse(layergroup.layergroupid).token] = 0;
|
|
|
|
keysToDelete['user:localhost:mapviews:global'] = 5;
|
|
|
|
|
|
|
|
return done();
|
2016-04-21 21:35:45 +08:00
|
|
|
}
|
2016-05-13 22:56:52 +08:00
|
|
|
);
|
2016-04-21 21:35:45 +08:00
|
|
|
|
2016-05-13 22:56:52 +08:00
|
|
|
});
|
2016-04-21 21:35:45 +08:00
|
|
|
|
2016-05-13 22:56:52 +08:00
|
|
|
afterEach(function(done) {
|
|
|
|
helper.deleteRedisKeys(keysToDelete, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to retrieve images from analysis', function(done) {
|
|
|
|
assert.response(
|
|
|
|
server,
|
|
|
|
{
|
|
|
|
url: '/api/v1/map/' + layergroupid + '/6/31/24.png',
|
|
|
|
method: 'GET',
|
|
|
|
encoding: 'binary',
|
|
|
|
headers: {
|
|
|
|
host: username
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 200,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'image/png'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function(res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
var fixturePath = './test/fixtures/analysis/named-map-buffer.png';
|
|
|
|
assert.imageBufferIsSimilarToFile(res.body, fixturePath, IMAGE_TOLERANCE_PER_MIL, function(err) {
|
|
|
|
assert.ok(!err, err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to retrieve dataviews from analysis', function(done) {
|
|
|
|
assert.response(
|
|
|
|
server,
|
|
|
|
{
|
|
|
|
url: '/api/v1/map/' + layergroupid + '/dataview/pop_max_histogram',
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
host: username
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 200,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json; charset=utf-8'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function(res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
var dataview = JSON.parse(res.body);
|
|
|
|
assert.equal(dataview.type, 'histogram');
|
|
|
|
assert.equal(dataview.bins_start, 0);
|
|
|
|
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to retrieve static map preview via layergroup', function(done) {
|
|
|
|
assert.response(
|
|
|
|
server,
|
|
|
|
{
|
|
|
|
url: '/api/v1/map/static/center/' + layergroupid + '/4/42/-3/320/240.png',
|
|
|
|
method: 'GET',
|
|
|
|
encoding: 'binary',
|
|
|
|
headers: {
|
|
|
|
host: username
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 200,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'image/png'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function(res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
var fixturePath = './test/fixtures/analysis/named-map-buffer-layergroup-static-preview.png';
|
|
|
|
assert.imageBufferIsSimilarToFile(res.body, fixturePath, IMAGE_TOLERANCE_PER_MIL, function(err) {
|
|
|
|
assert.ok(!err, err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('auto-instantiation', function() {
|
|
|
|
it('should be able to retrieve static map preview via fixed url', function(done) {
|
|
|
|
TestClient.getStaticMap(widgetsTemplateName, function(err, image) {
|
|
|
|
assert.ok(!err, err);
|
|
|
|
var fixturePath = './test/fixtures/analysis/named-map-buffer-static-preview.png';
|
|
|
|
assert.imageIsSimilarToFile(image, fixturePath, IMAGE_TOLERANCE_PER_MIL, function(err) {
|
|
|
|
assert.ok(!err, err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-04-21 21:35:45 +08:00
|
|
|
});
|
|
|
|
|
2016-04-07 22:18:48 +08:00
|
|
|
});
|