Named map updates does not emit update event if template didn't change

closes #323
This commit is contained in:
Raul Ochoa 2015-09-23 19:59:39 +02:00
parent 857548bbe4
commit 5e1bc3e199
3 changed files with 121 additions and 27 deletions

View File

@ -314,15 +314,18 @@ o.updTemplate = function(owner, tpl_id, template, callback) {
var userTemplatesKey = this.key_usr_tpl({ owner:owner });
var previousTemplate = null;
step(
function getExistingTemplate() {
self._redisCmd('HGET', [ userTemplatesKey, tpl_id ], this);
},
function updateTemplate(err, currentTemplate) {
function updateTemplate(err, _currentTemplate) {
assert.ifError(err);
if (!currentTemplate) {
if (!_currentTemplate) {
throw new Error("Template '" + tpl_id + "' of user '" + owner + "' does not exist");
}
previousTemplate = _currentTemplate;
self._redisCmd('HSET', [ userTemplatesKey, templateName, JSON.stringify(template) ], this);
},
function handleTemplateUpdate(err, didSetNewField) {
@ -334,7 +337,9 @@ o.updTemplate = function(owner, tpl_id, template, callback) {
},
function finish(err) {
if (!err) {
self.emit('update', owner, templateName, template);
if (self.fingerPrint(JSON.parse(previousTemplate)) !== self.fingerPrint(template)) {
self.emit('update', owner, templateName, template);
}
}
callback(err, template);

View File

@ -4,6 +4,7 @@ var assert = require('../../support/assert');
var redis = require('redis');
var step = require('step');
var FastlyPurge = require('fastly-purge');
var _ = require('underscore');
var NamedMapsCacheEntry = require(__dirname + '/../../../lib/cartodb/cache/model/named_maps_entry');
var CartodbWindshaft = require(__dirname + '/../../../lib/cartodb/server');
@ -34,29 +35,35 @@ describe('templates surrogate keys', function() {
var server = new CartodbWindshaft(serverOptions);
var templateOwner = 'localhost',
templateName = 'acceptance',
expectedTemplateId = templateName,
template = {
version: '0.0.1',
name: templateName,
auth: {
method: 'open'
},
layergroup: {
version: '1.2.0',
layers: [
{
options: {
sql: 'select 1 cartodb_id, null::geometry as the_geom_webmercator',
cartocss: '#layer { marker-fill:blue; }',
cartocss_version: '2.3.0'
}
}
]
}
var templateOwner = 'localhost';
var templateName = 'acceptance';
var expectedTemplateId = templateName;
var template = {
version: '0.0.1',
name: templateName,
auth: {
method: 'open'
},
expectedBody = { template_id: expectedTemplateId };
layergroup: {
version: '1.2.0',
layers: [
{
options: {
sql: 'select 1 cartodb_id, null::geometry as the_geom_webmercator',
cartocss: '#layer { marker-fill:blue; }',
cartocss_version: '2.3.0'
}
}
]
}
};
var templateUpdated = _.extend({}, template, {layergroup: {layers: [{
type: 'plain',
options: {
color: 'red'
}
}]} });
var expectedBody = { template_id: expectedTemplateId };
var varnishHttpUrl = [
'http://', serverOptions.varnish_host, ':', serverOptions.varnish_http_port
@ -149,7 +156,7 @@ describe('templates surrogate keys', function() {
host: templateOwner,
'Content-Type': 'application/json'
},
data: JSON.stringify(template)
data: JSON.stringify(templateUpdated)
};
var next = this;
assert.response(server,
@ -284,7 +291,7 @@ describe('templates surrogate keys', function() {
host: templateOwner,
'Content-Type': 'application/json'
},
data: JSON.stringify(template)
data: JSON.stringify(templateUpdated)
};
var next = this;
assert.response(server,

View File

@ -26,6 +26,13 @@ describe('template_maps', function() {
}
};
var LAYER_PLAIN = {
type: 'plain',
options: {
color: 'red'
}
};
it('does not accept template with unsupported version', function(done) {
var tmap = new TemplateMaps(redis_pool);
assert.ok(tmap);
@ -550,4 +557,79 @@ describe('template_maps', function() {
);
});
describe('emit', function() {
var owner = 'me';
var templateName = 'emit';
var template = {
version:'0.0.1',
name: templateName,
auth: {
method: 'open'
},
placeholders: {},
layergroup: {
layers: [
wadusLayer
]
}
};
var templateUpdated = _.extend({}, template, { layergroup: { layers: [LAYER_PLAIN] } });
var templateMaps;
beforeEach(function() {
templateMaps = new TemplateMaps(redis_pool);
});
it('should emit on template update', function(done) {
templateMaps.on('update', function(_owner, _templateName, _template) {
assert.equal(_owner, owner);
assert.equal(_templateName, templateName);
assert.deepEqual(_template, templateUpdated);
templateMaps.delTemplate(owner, templateName, done);
});
templateMaps.addTemplate(owner, template, function(err, templateName, _template) {
assert.ok(!err, err);
assert.deepEqual(_template, template);
templateMaps.updTemplate(owner, templateName, templateUpdated, function() {});
});
});
it('should emit on template deletion', function(done) {
templateMaps.on('delete', function(_owner, _templateName) {
assert.equal(_owner, owner);
assert.equal(_templateName, templateName);
done();
});
templateMaps.addTemplate(owner, template, function(err, templateName, _template) {
assert.ok(!err, err);
assert.deepEqual(_template, template);
templateMaps.delTemplate(owner, templateName, function() {});
});
});
it('should NOT emit on template update when template is the same', function(done) {
templateMaps.on('update', function(_owner, _templateName, _template) {
assert.equal(_owner, owner);
assert.equal(_templateName, templateName);
assert.deepEqual(_template, templateUpdated);
templateMaps.delTemplate(owner, templateName, done);
});
templateMaps.addTemplate(owner, template, function(err, templateName, _template) {
assert.ok(!err, err);
assert.deepEqual(_template, template);
templateMaps.updTemplate(owner, templateName, template, function() {
templateMaps.updTemplate(owner, templateName, templateUpdated, function() {});
});
});
});
});
});