diff --git a/test/acceptance/regressions.js b/test/acceptance/regressions.js index 421df02f..8ac3b4dc 100644 --- a/test/acceptance/regressions.js +++ b/test/acceptance/regressions.js @@ -37,4 +37,78 @@ describe('regressions', function() { testClient.drain(done); }); }); + + it('should create and instantiate a named map with filters', function (done) { + const apikeyToken = '1234'; + + const template = { + version: '0.0.1', + name: 'regression-dataview-filter-template', + placeholders: { + buffersize: { + type: 'number', + default: 0 + } + }, + layergroup: { + version: '1.6.0', + layers: [ + { + type: 'cartodb', + options: { + source: { + id: 'a1' + }, + cartocss: TestClient.CARTOCSS.POINTS, + cartocss_version: '2.3.0' + } + } + ], + dataviews: { + country_places_count: { + source: { + id: 'a1' + }, + type: 'aggregation', + options: { + column: 'adm0_a3', + aggregation: 'count' + } + } + }, + analyses: [ + { + id: 'a1', + type: 'source', + params: { + query: 'select * from populated_places_simple_reduced' + } + } + ] + } + }; + + const testClient = new TestClient(template, apikeyToken); + + const params = { + own_filter: 1, + filters: { + dataviews: { + country_places_count: { + accept: ['CAN'] + } + } + } + }; + + testClient.getDataview('country_places_count', params, (err, dataview) => { + assert.ifError(err); + + assert.equal(dataview.type, 'aggregation'); + assert.equal(dataview.categories.length, 1); + assert.deepEqual(dataview.categories[0], { value: 256, category: 'CAN', agg: false }); + + testClient.drain(done); + }); + }); }); diff --git a/test/support/test-client.js b/test/support/test-client.js index 7b068901..487dbe99 100644 --- a/test/support/test-client.js +++ b/test/support/test-client.js @@ -358,6 +358,8 @@ TestClient.prototype.getDataview = function(dataviewName, params, callback) { } var url = '/api/v1/map'; + var urlNamed = url + '/named'; + if (Object.keys(extraParams).length > 0) { url += '?' + qs.stringify(extraParams); } @@ -370,17 +372,73 @@ TestClient.prototype.getDataview = function(dataviewName, params, callback) { }; step( - function createLayergroup() { + function createTemplate () { var next = this; + + if (!self.template) { + return next(); + } + + if (!self.apiKey) { + return next(new Error('apiKey param is mandatory to create a new template')); + } + + params.placeholders = params.placeholders || {}; + assert.response(self.server, { - url: url, + url: urlNamed + '?' + qs.stringify({ api_key: self.apiKey }), method: 'POST', headers: { host: 'localhost', 'Content-Type': 'application/json' }, - data: JSON.stringify(self.mapConfig) + data: JSON.stringify(self.template) + }, + { + status: 200, + headers: { + 'Content-Type': 'application/json; charset=utf-8' + } + }, + function (res, err) { + if (err) { + return next(err); + } + return next(null, JSON.parse(res.body).template_id); + } + ); + }, + function createLayergroup(err, templateId) { + assert.ifError(err); + + var next = this; + + var data = templateId ? params.placeholders : self.mapConfig; + + const queryParams = {}; + + if (self.apiKey) { + queryParams.api_key = self.apiKey; + } + + if (params.filters !== undefined) { + queryParams.filters = JSON.stringify(params.filters); + } + + var path = templateId ? + urlNamed + '/' + templateId + '?' + qs.stringify(queryParams) : + url; + + assert.response(self.server, + { + url: path, + method: 'POST', + headers: { + host: 'localhost', + 'Content-Type': 'application/json' + }, + data: JSON.stringify(data) }, { status: 200,