2018-10-24 00:39:02 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-07 16:44:45 +08:00
|
|
|
require('../../support/test-helper');
|
2017-06-09 18:17:16 +08:00
|
|
|
var assert = require('../../support/assert');
|
|
|
|
var TestClient = require('../../support/test-client');
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function createMapConfig (layers, dataviews, analysis) {
|
2017-06-09 18:17:16 +08:00
|
|
|
return {
|
|
|
|
version: '1.5.0',
|
|
|
|
layers: layers,
|
|
|
|
dataviews: dataviews || {},
|
|
|
|
analyses: analysis || []
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function getMapConfig (operation, lastNumber) {
|
2018-06-28 00:04:48 +08:00
|
|
|
return createMapConfig([
|
|
|
|
{
|
2019-10-22 01:07:24 +08:00
|
|
|
type: 'cartodb',
|
|
|
|
options: {
|
|
|
|
source: {
|
|
|
|
id: 'a0'
|
2018-06-28 00:04:48 +08:00
|
|
|
},
|
2019-10-22 01:07:24 +08:00
|
|
|
cartocss: '#points { marker-width: 10; marker-fill: red; }',
|
|
|
|
cartocss_version: '2.3.0'
|
2018-06-28 00:04:48 +08:00
|
|
|
}
|
2017-06-09 18:17:16 +08:00
|
|
|
}
|
2018-06-28 00:04:48 +08:00
|
|
|
],
|
|
|
|
{
|
|
|
|
val_formula: {
|
|
|
|
source: {
|
|
|
|
id: 'a0'
|
|
|
|
},
|
|
|
|
type: 'formula',
|
|
|
|
options: {
|
|
|
|
column: 'val',
|
|
|
|
operation: operation
|
2017-06-09 18:17:16 +08:00
|
|
|
}
|
2018-06-28 00:04:48 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
[
|
2017-06-09 18:17:16 +08:00
|
|
|
{
|
2019-10-22 01:07:24 +08:00
|
|
|
id: 'a0',
|
|
|
|
type: 'source',
|
2018-06-28 00:04:48 +08:00
|
|
|
params: {
|
|
|
|
query: `
|
|
|
|
SELECT
|
|
|
|
null::geometry the_geom_webmercator,
|
|
|
|
CASE
|
|
|
|
WHEN x % 5 = 1 THEN 'infinity'::float
|
|
|
|
WHEN x % 5 = 2 THEN '-infinity'::float
|
|
|
|
WHEN x % 5 = 3 THEN 'NaN'::float
|
|
|
|
WHEN x % 5 = 4 THEN NULL
|
|
|
|
ELSE x
|
|
|
|
END AS val
|
|
|
|
FROM generate_series(1, ${lastNumber}) x
|
|
|
|
`
|
2017-06-09 18:17:16 +08:00
|
|
|
}
|
2018-06-28 00:04:48 +08:00
|
|
|
}
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
describe('formula-dataview: special float values', function () {
|
|
|
|
describe('easy numbers', function () { // not needed, but I keep it here to help human calc
|
|
|
|
afterEach(function (done) {
|
2018-06-28 00:04:48 +08:00
|
|
|
if (this.testClient) {
|
|
|
|
this.testClient.drain(done);
|
|
|
|
} else {
|
|
|
|
done();
|
2017-06-09 18:17:16 +08:00
|
|
|
}
|
2018-06-28 00:04:48 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const lastNumber = 10;
|
|
|
|
|
|
|
|
[
|
2019-10-22 01:07:24 +08:00
|
|
|
{ operation: 'count', result: 2 },
|
|
|
|
{ operation: 'avg', result: 7.5 },
|
|
|
|
{ operation: 'sum', result: 15 },
|
|
|
|
{ operation: 'min', result: 5 },
|
|
|
|
{ operation: 'max', result: 10 }
|
2017-06-09 18:17:16 +08:00
|
|
|
]
|
2019-10-22 01:07:24 +08:00
|
|
|
.forEach(operationData => {
|
|
|
|
it(operationData.operation, function (done) {
|
|
|
|
this.testClient = new TestClient(getMapConfig(operationData.operation, lastNumber), 1234);
|
|
|
|
this.testClient.getDataview('val_formula', {}, function (err, dataview) {
|
|
|
|
assert.ok(!err, err);
|
|
|
|
assert.deepStrictEqual(dataview, {
|
|
|
|
type: 'formula',
|
|
|
|
operation: operationData.operation,
|
|
|
|
result: operationData.result,
|
|
|
|
nulls: lastNumber / 5,
|
|
|
|
nans: lastNumber / 5,
|
|
|
|
infinities: 2 * lastNumber / 5
|
|
|
|
});
|
|
|
|
done();
|
2018-06-28 00:04:48 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-06-09 18:17:16 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
describe('bigger numbers', function () {
|
|
|
|
afterEach(function (done) {
|
2018-06-28 00:04:48 +08:00
|
|
|
if (this.testClient) {
|
|
|
|
this.testClient.drain(done);
|
|
|
|
} else {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const lastNumber = 1000;
|
|
|
|
|
|
|
|
[
|
2019-10-22 01:07:24 +08:00
|
|
|
{ operation: 'count', result: 200 },
|
|
|
|
{ operation: 'avg', result: 502.5 },
|
|
|
|
{ operation: 'sum', result: 100500 },
|
|
|
|
{ operation: 'min', result: 5 },
|
|
|
|
{ operation: 'max', result: 1000 }
|
2018-06-28 00:04:48 +08:00
|
|
|
]
|
2019-10-22 01:07:24 +08:00
|
|
|
.forEach(operationData => {
|
|
|
|
it(operationData.operation, function (done) {
|
|
|
|
this.testClient = new TestClient(getMapConfig(operationData.operation, lastNumber), 1234);
|
|
|
|
this.testClient.getDataview('val_formula', {}, function (err, dataview) {
|
|
|
|
assert.ok(!err, err);
|
|
|
|
assert.deepStrictEqual(dataview, {
|
|
|
|
type: 'formula',
|
|
|
|
operation: operationData.operation,
|
|
|
|
result: operationData.result,
|
|
|
|
nulls: lastNumber / 5,
|
|
|
|
nans: lastNumber / 5,
|
|
|
|
infinities: 2 * lastNumber / 5
|
|
|
|
});
|
|
|
|
done();
|
2018-06-28 00:04:48 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-06-09 18:17:16 +08:00
|
|
|
});
|
|
|
|
});
|