2018-10-24 00:39:02 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-07 16:44:45 +08:00
|
|
|
require('../../../support/test-helper');
|
2016-06-01 17:48:37 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
var assert = require('../../../support/assert');
|
2016-06-01 17:48:37 +08:00
|
|
|
var TestClient = require('../../../support/test-client');
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
describe('widgets', function () {
|
|
|
|
describe('formula', function () {
|
|
|
|
afterEach(function (done) {
|
2016-06-01 17:48:37 +08:00
|
|
|
if (this.testClient) {
|
|
|
|
this.testClient.drain(done);
|
|
|
|
} else {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function widgetsMapConfig (widgets) {
|
2016-06-01 17:48:37 +08:00
|
|
|
return {
|
|
|
|
version: '1.5.0',
|
|
|
|
layers: [
|
|
|
|
{
|
|
|
|
type: 'mapnik',
|
|
|
|
options: {
|
|
|
|
sql: 'select * from populated_places_simple_reduced where pop_max > 0 and pop_max < 600000',
|
|
|
|
cartocss: '#layer0 { marker-fill: red; marker-width: 10; }',
|
|
|
|
cartocss_version: '2.0.1',
|
|
|
|
widgets: widgets
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
var operations = {
|
|
|
|
min: [10, 0],
|
|
|
|
max: [599579, 0],
|
|
|
|
count: [5822, 0],
|
|
|
|
avg: [112246.00893163861, 0],
|
|
|
|
sum: [653496264, 0]
|
|
|
|
};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
Object.keys(operations).forEach(function (operation) {
|
|
|
|
it('should do ' + operation + ' over column', function (done) {
|
2016-06-01 17:48:37 +08:00
|
|
|
var widgets = {
|
|
|
|
pop_max_f: {
|
|
|
|
type: 'formula',
|
|
|
|
options: {
|
|
|
|
column: 'pop_max',
|
|
|
|
operation: operation
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.testClient = new TestClient(widgetsMapConfig(widgets));
|
|
|
|
this.testClient.getWidget('pop_max_f', function (err, res, result) {
|
|
|
|
assert.ok(!err, err);
|
|
|
|
assert.equal(result.operation, operation);
|
|
|
|
assert.equal(result.result, operations[operation][0]);
|
|
|
|
assert.equal(result.nulls, operations[operation][1]);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('does not require column for count formula', function (done) {
|
|
|
|
var operation = 'count';
|
2016-06-01 17:48:37 +08:00
|
|
|
var widgets = {
|
|
|
|
pop_max_count_f: {
|
|
|
|
type: 'formula',
|
|
|
|
options: {
|
|
|
|
operation: operation
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.testClient = new TestClient(widgetsMapConfig(widgets));
|
|
|
|
this.testClient.getWidget('pop_max_count_f', function (err, res, result) {
|
|
|
|
assert.ok(!err, err);
|
|
|
|
assert.equal(result.operation, operation);
|
|
|
|
assert.equal(result.result, operations[operation][0]);
|
|
|
|
assert.equal(result.nulls, operations[operation][1]);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|