Windshaft-cartodb/test/acceptance/widgets/widgets.js

380 lines
14 KiB
JavaScript
Raw Normal View History

2016-02-05 19:35:01 +08:00
var assert = require('../../support/assert');
2016-02-05 19:59:33 +08:00
var TestClient = require('../../support/test-client');
2015-10-07 01:47:44 +08:00
2015-10-27 19:50:27 +08:00
describe('widgets', function() {
2015-10-07 01:47:44 +08:00
it("should expose layer list", function(done) {
2015-10-28 02:08:46 +08:00
var listWidgetMapConfig = {
version: '1.5.0',
layers: [
{
type: 'mapnik',
options: {
sql: 'select * from test_table',
cartocss: '#layer { marker-fill: red; marker-width: 32; marker-allow-overlap: true; }',
cartocss_version: '2.3.0',
widgets: {
names: {
type: 'list',
options: {
columns: ['name']
}
}
}
}
}
]
};
2016-02-05 19:59:33 +08:00
var testClient = new TestClient(listWidgetMapConfig);
testClient.getWidget('names', function(err, res) {
if (err) {
return done(err);
}
var expectedList = [
{name:"Hawai"},
{name:"El Estocolmo"},
{name:"El Rey del Tallarín"},
{name:"El Lacón"},
{name:"El Pico"}
];
2015-11-17 01:34:36 +08:00
assert.deepEqual(JSON.parse(res.body).rows, expectedList);
2016-02-05 19:59:33 +08:00
testClient.drain(done);
});
2015-10-07 01:47:44 +08:00
});
2015-10-27 19:50:27 +08:00
it("should expose layer histogram", function(done) {
2015-10-28 02:08:46 +08:00
var histogramMapConfig = {
2015-10-27 19:50:27 +08:00
version: '1.5.0',
layers: [
{
type: 'mapnik',
options: {
sql: 'select * from populated_places_simple_reduced',
cartocss: '#layer { marker-fill: red; marker-width: 32; marker-allow-overlap: true; }',
cartocss_version: '2.3.0',
widgets: {
pop_max: {
type: 'histogram',
options: {
column: 'pop_max'
}
}
}
}
}
]
};
2016-02-05 19:59:33 +08:00
var testClient = new TestClient(histogramMapConfig);
testClient.getWidget('pop_max', function(err, res) {
if (err) {
return done(err);
}
2015-10-27 19:50:27 +08:00
var histogram = JSON.parse(res.body);
2015-11-17 01:34:36 +08:00
assert.ok(histogram.bins.length);
2015-10-27 19:50:27 +08:00
2016-02-05 19:59:33 +08:00
testClient.drain(done);
});
2015-10-27 19:50:27 +08:00
});
2015-10-27 20:18:53 +08:00
describe('filters', function() {
2015-10-28 02:08:46 +08:00
describe('category', function() {
var aggregationMapConfig = {
version: '1.5.0',
layers: [
{
type: 'mapnik',
options: {
sql: 'select * from populated_places_simple_reduced',
cartocss: '#layer { marker-fill: red; marker-width: 32; marker-allow-overlap: true; }',
cartocss_version: '2.3.0',
widgets: {
country_places_count: {
type: 'aggregation',
options: {
column: 'adm0_a3',
aggregation: 'count'
}
2015-10-27 20:18:53 +08:00
}
}
}
}
2015-10-28 02:08:46 +08:00
]
};
2015-10-27 20:18:53 +08:00
2015-10-28 02:08:46 +08:00
it("should expose an aggregation", function(done) {
2016-02-05 19:59:33 +08:00
var testClient = new TestClient(aggregationMapConfig);
testClient.getWidget('country_places_count', { own_filter: 0 }, function(err, res) {
2015-10-28 02:08:46 +08:00
if (err) {
return done(err);
}
2015-10-27 20:18:53 +08:00
2015-10-28 02:08:46 +08:00
var aggregation = JSON.parse(res.body);
2016-01-18 21:08:01 +08:00
assert.equal(aggregation.categories.length, 6);
2015-11-17 01:34:36 +08:00
assert.deepEqual(aggregation.categories[0], { value: 769, category: 'USA', agg: false });
2015-10-27 20:18:53 +08:00
2016-02-05 19:59:33 +08:00
testClient.drain(done);
2015-10-28 02:08:46 +08:00
});
});
it("should expose a filtered aggregation", function(done) {
2015-10-29 23:25:56 +08:00
var params = {
filters: {
layers: [
{country_places_count: {accept: ['CAN']}}
]
}
2015-10-28 02:08:46 +08:00
};
2016-02-05 19:59:33 +08:00
var testClient = new TestClient(aggregationMapConfig);
testClient.getWidget('country_places_count', params, function(err, res) {
2015-10-28 02:08:46 +08:00
if (err) {
return done(err);
}
var aggregation = JSON.parse(res.body);
2015-11-17 01:34:36 +08:00
assert.equal(aggregation.categories.length, 1);
assert.deepEqual(aggregation.categories[0], { value: 256, category: 'CAN', agg: false });
2015-10-28 02:08:46 +08:00
2016-02-05 19:59:33 +08:00
testClient.drain(done);
2015-10-28 02:08:46 +08:00
});
2015-10-27 20:18:53 +08:00
});
});
2015-10-28 02:08:46 +08:00
describe('range', function() {
var histogramMapConfig = {
version: '1.5.0',
2015-10-27 20:18:53 +08:00
layers: [
2015-10-28 02:08:46 +08:00
{
type: 'mapnik',
options: {
sql: 'select * from populated_places_simple_reduced',
cartocss: '#layer { marker-fill: red; marker-width: 32; marker-allow-overlap: true; }',
cartocss_version: '2.3.0',
widgets: {
country_places_histogram: {
type: 'histogram',
options: {
column: 'pop_max'
}
}
}
}
}
2015-10-27 20:18:53 +08:00
]
};
2016-01-18 21:08:01 +08:00
it("should expose an histogram", function(done) {
2016-02-05 19:59:33 +08:00
var testClient = new TestClient(histogramMapConfig);
testClient.getWidget('country_places_histogram', { own_filter: 0 }, function(err, res) {
2015-10-28 02:08:46 +08:00
if (err) {
return done(err);
}
var histogram = JSON.parse(res.body);
// notice min value
2015-11-05 00:21:33 +08:00
assert.deepEqual(
2015-11-17 01:34:36 +08:00
histogram.bins[0],
2016-01-18 21:08:01 +08:00
{ bin: 0, freq: 6497, min: 0, max: 742572, avg: 113511.16823149147 }
2015-11-05 00:21:33 +08:00
);
2015-10-28 02:08:46 +08:00
2016-02-05 19:59:33 +08:00
testClient.drain(done);
2015-10-28 02:08:46 +08:00
});
});
2015-10-28 19:06:06 +08:00
it("should expose a filtered histogram", function(done) {
2015-10-29 23:25:56 +08:00
var params = {
filters: {
layers: [
{
country_places_histogram: { min: 4000000 }
}
]
}
2015-10-28 02:08:46 +08:00
};
2016-02-05 19:59:33 +08:00
var testClient = new TestClient(histogramMapConfig);
testClient.getWidget('country_places_histogram', params, function(err, res) {
2015-10-28 02:08:46 +08:00
if (err) {
return done(err);
}
var histogram = JSON.parse(res.body);
// notice min value
2015-11-17 01:34:36 +08:00
assert.deepEqual(histogram.bins[0], {
2015-11-05 00:21:33 +08:00
bin: 0,
2016-01-18 21:08:01 +08:00
freq: 62,
min: 4000000,
max: 9276403,
avg: 5815009.596774193
2015-11-05 00:21:33 +08:00
});
2015-10-27 20:18:53 +08:00
2016-02-05 19:59:33 +08:00
testClient.drain(done);
2015-10-28 02:08:46 +08:00
});
2015-10-27 20:18:53 +08:00
});
});
describe('combine widget filters', function() {
var combinedWidgetsMapConfig = {
version: '1.5.0',
layers: [
{
type: 'mapnik',
options: {
sql: 'select * from populated_places_simple_reduced',
cartocss: '#layer { marker-fill: red; marker-width: 32; marker-allow-overlap: true; }',
cartocss_version: '2.3.0',
widgets: {
country_places_count: {
type: 'aggregation',
options: {
column: 'adm0_a3',
aggregation: 'count'
}
},
country_places_histogram: {
type: 'histogram',
options: {
column: 'pop_max'
}
}
}
}
}
]
};
it("should expose a filtered aggregation", function(done) {
2015-10-29 23:25:56 +08:00
var params = {
filters: {
layers: [
{
country_places_count: { reject: ['CHN'] }
}
]
}
};
2016-02-05 19:59:33 +08:00
var testClient = new TestClient(combinedWidgetsMapConfig);
testClient.getWidget('country_places_count', params, function(err, res) {
2015-10-29 23:25:56 +08:00
if (err) {
return done(err);
}
var aggregation = JSON.parse(res.body);
// first one would be CHN if reject filter wasn't applied
2015-11-17 01:34:36 +08:00
assert.deepEqual(aggregation.categories[0], { value: 769, category: "USA", agg: false });
2015-10-29 23:25:56 +08:00
// confirm 'CHN' was filtered out (reject)
2015-11-17 01:34:36 +08:00
assert.equal(aggregation.categories.reduce(function(sum, row) {
return sum + (row.category === 'CHN' ? 1 : 0);
2015-10-29 23:25:56 +08:00
}, 0), 0);
2016-02-05 19:59:33 +08:00
testClient.drain(done);
2015-10-29 23:25:56 +08:00
});
});
it("should expose a filtered aggregation", function(done) {
var params = {
filters: {
layers: [
{
country_places_count: { reject: ['CHN'] },
country_places_histogram: { min: 7000000 }
}
]
}
};
2016-02-05 19:59:33 +08:00
var testClient = new TestClient(combinedWidgetsMapConfig);
testClient.getWidget('country_places_count', params, function(err, res) {
if (err) {
return done(err);
}
var aggregation = JSON.parse(res.body);
// first one would be CHN if reject filter wasn't applied
2015-11-17 01:34:36 +08:00
assert.deepEqual(aggregation.categories[0], { value: 4, category: 'IND', agg: false });
// confirm 'CHN' was filtered out (reject)
2015-11-17 01:34:36 +08:00
assert.equal(aggregation.categories.reduce(function(sum, row) {
return sum + (row.category === 'CHN' ? 1 : 0);
}, 0), 0);
2016-02-05 19:59:33 +08:00
testClient.drain(done);
});
});
it("should allow to filter by bounding box a filtered aggregation", function(done) {
var params = {
filters: {
layers: [
{
country_places_histogram: { min: 50000 }
}
]
},
bbox: '-20,0,45,60'
};
2016-02-05 19:59:33 +08:00
var testClient = new TestClient(combinedWidgetsMapConfig);
testClient.getWidget('country_places_count', params, function(err, res) {
if (err) {
return done(err);
}
var aggregation = JSON.parse(res.body);
// first one would be CHN if reject filter wasn't applied
2015-11-17 01:34:36 +08:00
assert.deepEqual(aggregation.categories[0], { value: 96, category: "RUS", agg: false });
// confirm 'CHN' was filtered out (reject)
2015-11-17 01:34:36 +08:00
assert.equal(aggregation.categories.reduce(function(sum, row) {
return sum + (row.category === 'CHN' ? 1 : 0);
}, 0), 0);
2016-02-05 19:59:33 +08:00
testClient.drain(done);
});
});
it("should allow to filter by bounding box a filtered aggregation, with reject", function(done) {
var params = {
filters: {
layers: [
{
country_places_count: { reject: ['RUS'] },
country_places_histogram: { min: 50000 }
}
]
},
bbox: '-20,0,45,60'
};
2016-02-05 19:59:33 +08:00
var testClient = new TestClient(combinedWidgetsMapConfig);
testClient.getWidget('country_places_count', params, function(err, res) {
if (err) {
return done(err);
}
var aggregation = JSON.parse(res.body);
// first one would be CHN if reject filter wasn't applied
2015-11-17 01:34:36 +08:00
assert.deepEqual(aggregation.categories[0], { value: 77, category: "TUR", agg: false });
// confirm 'CHN' was filtered out (reject)
2015-11-17 01:34:36 +08:00
assert.equal(aggregation.categories.reduce(function(sum, row) {
return sum + (row.category === 'CHN' ? 1 : 0);
}, 0), 0);
2016-02-05 19:59:33 +08:00
testClient.drain(done);
});
});
});
2015-10-27 20:18:53 +08:00
});
2015-10-07 01:47:44 +08:00
});