Windshaft-cartodb/test/acceptance/dataviews/error-cases-test.js

212 lines
7.1 KiB
JavaScript
Raw Normal View History

'use strict';
require('../../support/test-helper');
var assert = require('../../support/assert');
var TestClient = require('../../support/test-client');
2019-10-22 01:07:24 +08:00
describe('dataview error cases', function () {
describe('generic errors', function () {
afterEach(function (done) {
2018-05-11 00:35:52 +08:00
if (this.testClient) {
this.testClient.drain(done);
} else {
done();
}
});
2018-05-10 21:59:38 +08:00
var ERROR_RESPONSE = {
status: 400,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
};
2019-10-22 01:07:24 +08:00
function createMapConfig (dataviews) {
2018-05-10 21:59:38 +08:00
return {
version: '1.5.0',
layers: [
{
2019-10-22 01:07:24 +08:00
type: 'cartodb',
options: {
source: {
id: 'HEAD'
2018-05-10 21:59:38 +08:00
},
2019-10-22 01:07:24 +08:00
cartocss: '#points { marker-width: 10; marker-fill: red; }',
cartocss_version: '2.3.0'
2018-05-10 21:59:38 +08:00
}
}
2018-05-10 21:59:38 +08:00
],
dataviews: dataviews,
analyses: [
{
2019-10-22 01:07:24 +08:00
id: 'HEAD',
type: 'source',
params: {
query: 'select null::geometry the_geom_webmercator, x from generate_series(0,1000) x'
2018-05-10 21:59:38 +08:00
}
}
2018-05-10 21:59:38 +08:00
]
};
}
2019-10-22 01:07:24 +08:00
it('should fail when invalid dataviews object is provided, string case', function (done) {
var mapConfig = createMapConfig('wadus-string');
2018-05-10 21:59:38 +08:00
this.testClient = new TestClient(mapConfig, 1234);
2019-10-22 01:07:24 +08:00
this.testClient.getLayergroup({ response: ERROR_RESPONSE }, function (err, errObj) {
2018-05-10 21:59:38 +08:00
assert.ok(!err, err);
assert.deepStrictEqual(errObj.errors, ['"dataviews" must be a valid JSON object: "string" type found']);
2018-05-10 21:59:38 +08:00
done();
});
});
2019-10-22 01:07:24 +08:00
it('should fail when invalid dataviews object is provided, array case', function (done) {
2018-05-10 21:59:38 +08:00
var mapConfig = createMapConfig([]);
this.testClient = new TestClient(mapConfig, 1234);
2019-10-22 01:07:24 +08:00
this.testClient.getLayergroup({ response: ERROR_RESPONSE }, function (err, errObj) {
2018-05-10 21:59:38 +08:00
assert.ok(!err, err);
assert.deepStrictEqual(errObj.errors, ['"dataviews" must be a valid JSON object: "array" type found']);
2018-05-10 21:59:38 +08:00
done();
});
});
2019-10-22 01:07:24 +08:00
it('should work with empty but valid objects', function (done) {
2018-05-10 21:59:38 +08:00
var mapConfig = createMapConfig({});
this.testClient = new TestClient(mapConfig, 1234);
2019-10-22 01:07:24 +08:00
this.testClient.getLayergroup(function (err, layergroup) {
2018-05-10 21:59:38 +08:00
assert.ok(!err, err);
2018-05-10 21:59:38 +08:00
assert.ok(layergroup);
assert.ok(layergroup.layergroupid);
2018-05-10 21:59:38 +08:00
done();
});
});
});
2019-10-22 01:07:24 +08:00
describe('pg_typeof', function () {
afterEach(function (done) {
2018-05-10 21:59:38 +08:00
if (this.testClient) {
this.testClient.drain(done);
} else {
done();
}
});
2019-10-22 01:07:24 +08:00
function createMapConfig (query) {
2018-05-10 21:59:38 +08:00
query = query || 'select * from populated_places_simple_reduced';
2018-05-10 21:59:38 +08:00
return {
version: '1.5.0',
layers: [
{
type: 'mapnik',
options: {
sql: query,
cartocss: '#layer0 { marker-fill: red; marker-width: 10; }',
cartocss_version: '2.0.1',
2019-10-22 01:07:24 +08:00
source: { id: 'a0' }
2018-05-10 21:59:38 +08:00
}
}
],
analyses: [{
2019-10-22 01:07:24 +08:00
id: 'a0',
type: 'source',
2018-05-10 21:59:38 +08:00
params: {
query: query
}
}],
2019-10-22 01:07:24 +08:00
dataviews: {
2018-05-10 21:59:38 +08:00
aggregation_count_dataview: {
2019-10-22 01:07:24 +08:00
type: 'aggregation',
source: { id: 'a0' },
2018-05-10 21:59:38 +08:00
options: {
2019-10-22 01:07:24 +08:00
column: 'adm0name',
aggregation: 'count',
aggregationColumn: 'adm0name'
2018-05-10 21:59:38 +08:00
}
}
2018-05-10 21:59:38 +08:00
}
};
2018-05-10 21:59:38 +08:00
}
2019-10-22 01:07:24 +08:00
it('should work without filters', function (done) {
2018-05-10 21:59:38 +08:00
this.testClient = new TestClient(createMapConfig());
2019-10-22 01:07:24 +08:00
this.testClient.getDataview('aggregation_count_dataview', { own_filter: 0 }, function (err) {
assert.ifError(err);
2018-05-10 21:59:38 +08:00
done();
});
});
2019-10-22 01:07:24 +08:00
it('should work with filters', function (done) {
2018-05-10 21:59:38 +08:00
var params = {
filters: {
2019-10-22 01:07:24 +08:00
dataviews: { aggregation_count_dataview: { accept: ['Canada'] } }
2018-05-10 21:59:38 +08:00
}
};
2018-05-10 21:59:38 +08:00
this.testClient = new TestClient(createMapConfig());
2019-10-22 01:07:24 +08:00
this.testClient.getDataview('aggregation_count_dataview', params, function (err) {
assert.ifError(err);
2018-05-10 21:59:38 +08:00
done();
});
});
2019-10-22 01:07:24 +08:00
it('should return an error if the column used by dataview does not exist', function (done) {
const query = 'select cartodb_id, the_geom, the_geom_webmercator from populated_places_simple_reduced';
const params = {
response: {
status: 404,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
}
};
const expectedResponseBody = {
2019-10-22 01:07:24 +08:00
errors: ['column "adm0name" does not exist'],
errors_with_context: [{
type: 'unknown',
message: 'column "adm0name" does not exist'
}]
};
this.testClient = new TestClient(createMapConfig(query));
2019-10-22 01:07:24 +08:00
this.testClient.getDataview('aggregation_count_dataview', params, function (err, result) {
assert.ifError(err);
assert.deepStrictEqual(result, expectedResponseBody);
done();
});
});
2019-10-22 01:07:24 +08:00
it('should not fail if query does not return rows', function (done) {
2018-05-10 21:59:38 +08:00
const query = 'select * from populated_places_simple_reduced limit 0';
const expectedResponseBody = {
aggregation: 'count',
count: 0,
nulls: 0,
nans: 0,
infinities: 0,
min: 0,
max: 0,
categoriesCount: 0,
categories: [],
type: 'aggregation'
};
2018-05-10 21:59:38 +08:00
this.testClient = new TestClient(createMapConfig(query));
2019-10-22 01:07:24 +08:00
this.testClient.getDataview('aggregation_count_dataview', { own_filter: 0 }, function (err, result) {
assert.ifError(err);
assert.deepStrictEqual(result, expectedResponseBody);
2018-05-10 21:59:38 +08:00
done();
});
});
});
});