2016-03-11 03:45:00 +08:00
|
|
|
var assert = require('../support/assert');
|
|
|
|
var step = require('step');
|
|
|
|
var LayergroupToken = require('../../lib/cartodb/models/layergroup_token');
|
|
|
|
var testHelper = require(__dirname + '/../support/test_helper');
|
|
|
|
var CartodbWindshaft = require(__dirname + '/../../lib/cartodb/server');
|
|
|
|
var serverOptions = require(__dirname + '/../../lib/cartodb/server_options');
|
|
|
|
var server = new CartodbWindshaft(serverOptions);
|
2016-03-12 01:28:14 +08:00
|
|
|
var mapnik = require('windshaft').mapnik;
|
|
|
|
var IMAGE_TOLERANCE_PER_MIL = 1;
|
2016-03-11 03:45:00 +08:00
|
|
|
|
|
|
|
describe('turbo-cartocss for named maps', function() {
|
|
|
|
|
|
|
|
var keysToDelete;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
keysToDelete = {};
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function(done) {
|
|
|
|
testHelper.deleteRedisKeys(keysToDelete, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
var templateId = 'turbo-cartocss-template-1';
|
|
|
|
|
|
|
|
var template = {
|
|
|
|
version: '0.0.1',
|
|
|
|
name: templateId,
|
|
|
|
auth: { method: 'open' },
|
2016-03-12 01:28:14 +08:00
|
|
|
placeholders: {
|
|
|
|
color: {
|
|
|
|
type: "css_color",
|
|
|
|
default: "Reds"
|
|
|
|
}
|
|
|
|
},
|
2016-03-11 03:45:00 +08:00
|
|
|
layergroup: {
|
|
|
|
version: '1.0.0',
|
|
|
|
layers: [{
|
|
|
|
options: {
|
|
|
|
sql: [
|
|
|
|
'SELECT test_table.*, _prices.price FROM test_table JOIN (' +
|
|
|
|
' SELECT 1 AS cartodb_id, 10.00 AS price',
|
|
|
|
' UNION',
|
|
|
|
' SELECT 2, 10.50',
|
|
|
|
' UNION',
|
|
|
|
' SELECT 3, 11.00',
|
|
|
|
' UNION',
|
|
|
|
' SELECT 4, 12.00',
|
|
|
|
' UNION',
|
|
|
|
' SELECT 5, 21.00',
|
|
|
|
') _prices ON _prices.cartodb_id = test_table.cartodb_id'
|
|
|
|
].join('\n'),
|
2016-03-12 01:28:14 +08:00
|
|
|
cartocss: [
|
|
|
|
'#layer {',
|
|
|
|
' marker-fill: ramp([price], colorbrewer(<%= color %>));',
|
|
|
|
' marker-allow-overlap:true;',
|
|
|
|
'}'
|
|
|
|
].join('\n'),
|
2016-03-11 03:45:00 +08:00
|
|
|
cartocss_version: '2.0.2'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-12 01:28:14 +08:00
|
|
|
var templateParamsReds = { color: 'Reds' };
|
|
|
|
var templateParamsBlues = { color: 'Blues' };
|
2016-03-11 03:45:00 +08:00
|
|
|
|
|
|
|
it('should create a template with turbo-cartocss parsed properly', function (done) {
|
|
|
|
step(
|
|
|
|
function postTemplate() {
|
|
|
|
var next = this;
|
|
|
|
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/map/named?api_key=1234',
|
|
|
|
method: 'POST',
|
|
|
|
headers: { host: 'localhost', 'Content-Type': 'application/json' },
|
|
|
|
data: JSON.stringify(template)
|
|
|
|
}, {},
|
|
|
|
function (res, err) {
|
|
|
|
next(err, res);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function checkTemplate(err, res) {
|
|
|
|
assert.ifError(err);
|
|
|
|
assert.equal(res.statusCode, 200);
|
|
|
|
assert.deepEqual(JSON.parse(res.body), {
|
|
|
|
template_id: templateId
|
|
|
|
});
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
2016-03-12 01:28:14 +08:00
|
|
|
function instantiateTemplateWithReds(err) {
|
2016-03-11 03:45:00 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
|
|
|
|
var next = this;
|
|
|
|
assert.response(server, {
|
2016-03-12 01:28:14 +08:00
|
|
|
url: '/api/v1/map/named/' + templateId,
|
2016-03-11 03:45:00 +08:00
|
|
|
method: 'POST',
|
2016-03-12 01:28:14 +08:00
|
|
|
headers: {
|
|
|
|
host: 'localhost',
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
data: JSON.stringify(templateParamsReds)
|
2016-03-11 03:45:00 +08:00
|
|
|
}, {},
|
2016-03-12 01:28:14 +08:00
|
|
|
function(res, err) {
|
|
|
|
return next(err, res);
|
2016-03-11 03:45:00 +08:00
|
|
|
});
|
|
|
|
},
|
2016-03-12 01:28:14 +08:00
|
|
|
function checkInstanciationWithReds(err, res) {
|
2016-03-11 03:45:00 +08:00
|
|
|
assert.ifError(err);
|
2016-03-12 01:28:14 +08:00
|
|
|
|
2016-03-11 03:45:00 +08:00
|
|
|
assert.equal(res.statusCode, 200);
|
|
|
|
|
|
|
|
var parsedBody = JSON.parse(res.body);
|
|
|
|
|
|
|
|
keysToDelete['map_cfg|' + LayergroupToken.parse(parsedBody.layergroupid).token] = 0;
|
|
|
|
keysToDelete['user:localhost:mapviews:global'] = 5;
|
|
|
|
|
|
|
|
assert.ok(parsedBody.layergroupid);
|
|
|
|
assert.ok(parsedBody.last_updated);
|
|
|
|
|
|
|
|
return parsedBody.layergroupid;
|
|
|
|
},
|
2016-03-12 01:28:14 +08:00
|
|
|
function requestTileReds(err, layergroupId) {
|
2016-03-11 03:45:00 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
|
|
|
|
var next = this;
|
|
|
|
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/map/' + layergroupId + '/0/0/0.png',
|
|
|
|
method: 'GET',
|
|
|
|
headers: { host: 'localhost' },
|
|
|
|
encoding: 'binary'
|
|
|
|
}, {},
|
|
|
|
function(res, err) {
|
|
|
|
next(err, res);
|
|
|
|
});
|
|
|
|
},
|
2016-03-12 01:28:14 +08:00
|
|
|
function checkTileReds(err, res) {
|
2016-03-11 03:45:00 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
|
2016-03-12 01:28:14 +08:00
|
|
|
var next = this;
|
|
|
|
|
2016-03-11 03:45:00 +08:00
|
|
|
assert.equal(res.statusCode, 200);
|
|
|
|
assert.equal(res.headers['content-type'], 'image/png');
|
|
|
|
|
2016-03-12 01:28:14 +08:00
|
|
|
var fixturePath = './test/fixtures/turbo-cartocss-named-maps-reds.png';
|
|
|
|
var image = mapnik.Image.fromBytes(new Buffer(res.body, 'binary'));
|
2016-03-11 03:45:00 +08:00
|
|
|
|
2016-03-12 01:28:14 +08:00
|
|
|
assert.imageIsSimilarToFile(image, fixturePath, IMAGE_TOLERANCE_PER_MIL, next);
|
2016-03-11 03:45:00 +08:00
|
|
|
},
|
2016-03-12 01:28:14 +08:00
|
|
|
function instantiateTemplateWithBlues(err) {
|
|
|
|
assert.ifError(err);
|
2016-03-11 04:25:01 +08:00
|
|
|
|
2016-03-12 01:28:14 +08:00
|
|
|
var next = this;
|
2016-03-11 04:25:01 +08:00
|
|
|
assert.response(server, {
|
2016-03-12 01:28:14 +08:00
|
|
|
url: '/api/v1/map/named/' + templateId,
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
host: 'localhost',
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
data: JSON.stringify(templateParamsBlues)
|
2016-03-11 04:25:01 +08:00
|
|
|
}, {},
|
|
|
|
function(res, err) {
|
2016-03-12 01:28:14 +08:00
|
|
|
return next(err, res);
|
2016-03-11 04:25:01 +08:00
|
|
|
});
|
|
|
|
},
|
2016-03-12 01:28:14 +08:00
|
|
|
function checkInstanciationWithBlues(err, res) {
|
2016-03-11 04:25:01 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
assert.equal(res.statusCode, 200);
|
|
|
|
|
2016-03-12 01:28:14 +08:00
|
|
|
var parsedBody = JSON.parse(res.body);
|
2016-03-11 04:25:01 +08:00
|
|
|
|
2016-03-12 01:28:14 +08:00
|
|
|
keysToDelete['map_cfg|' + LayergroupToken.parse(parsedBody.layergroupid).token] = 0;
|
|
|
|
keysToDelete['user:localhost:mapviews:global'] = 5;
|
2016-03-11 18:06:51 +08:00
|
|
|
|
2016-03-12 01:28:14 +08:00
|
|
|
assert.ok(parsedBody.layergroupid);
|
|
|
|
assert.ok(parsedBody.last_updated);
|
2016-03-11 18:06:51 +08:00
|
|
|
|
2016-03-12 01:28:14 +08:00
|
|
|
return parsedBody.layergroupid;
|
2016-03-11 18:06:51 +08:00
|
|
|
},
|
2016-03-12 01:28:14 +08:00
|
|
|
function requestTileBlues(err, layergroupId) {
|
2016-03-11 18:06:51 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
|
|
|
|
var next = this;
|
|
|
|
|
|
|
|
assert.response(server, {
|
2016-03-12 01:28:14 +08:00
|
|
|
url: '/api/v1/map/' + layergroupId + '/0/0/0.png',
|
2016-03-11 18:06:51 +08:00
|
|
|
method: 'GET',
|
2016-03-12 01:28:14 +08:00
|
|
|
headers: { host: 'localhost' },
|
|
|
|
encoding: 'binary'
|
2016-03-11 18:06:51 +08:00
|
|
|
}, {},
|
|
|
|
function(res, err) {
|
|
|
|
next(err, res);
|
|
|
|
});
|
|
|
|
},
|
2016-03-12 01:28:14 +08:00
|
|
|
function checkTileBlues(err, res) {
|
2016-03-11 18:06:51 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
|
2016-03-12 01:28:14 +08:00
|
|
|
var next = this;
|
2016-03-11 18:06:51 +08:00
|
|
|
|
|
|
|
assert.equal(res.statusCode, 200);
|
2016-03-12 01:28:14 +08:00
|
|
|
assert.equal(res.headers['content-type'], 'image/png');
|
2016-03-11 18:06:51 +08:00
|
|
|
|
2016-03-12 01:28:14 +08:00
|
|
|
var fixturePath = './test/fixtures/turbo-cartocss-named-maps-blues.png';
|
|
|
|
var image = mapnik.Image.fromBytes(new Buffer(res.body, 'binary'));
|
|
|
|
|
|
|
|
assert.imageIsSimilarToFile(image, fixturePath, IMAGE_TOLERANCE_PER_MIL, next);
|
2016-03-11 18:06:51 +08:00
|
|
|
},
|
2016-03-11 03:45:00 +08:00
|
|
|
function deleteTemplate(err) {
|
|
|
|
assert.ifError(err);
|
|
|
|
|
|
|
|
var next = this;
|
|
|
|
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/map/named/' + templateId + '?api_key=1234',
|
|
|
|
method: 'DELETE',
|
|
|
|
headers: { host: 'localhost' }
|
|
|
|
}, {}, function (res, err) {
|
|
|
|
next(err, res);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function checkDeleteTemplate(err, res) {
|
|
|
|
assert.ifError(err);
|
|
|
|
assert.equal(res.statusCode, 204);
|
|
|
|
assert.ok(!res.body);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
function finish(err) {
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|