Windshaft-cartodb/test/unit/backends/layer-stats/mapnik-layer-stats-test.js

155 lines
5.4 KiB
JavaScript
Raw Normal View History

'use strict';
var assert = require('assert');
2019-10-07 17:29:07 +08:00
var MapnikLayerStats = require('../../../../lib/backends/layer-stats/mapnik-layer-stats');
var MapConfig = require('windshaft').model.MapConfig;
function getDbConnectionMock () {
return {
2019-10-22 01:07:24 +08:00
query: function (sql, callback) {
return callback(null, {
2019-10-22 01:07:24 +08:00
rows: [{ rows: 1 }]
});
}
};
}
2019-10-22 01:07:24 +08:00
describe('mapnik-layer-stats', function () {
beforeEach(function () {
this.dbConnectionMock = getDbConnectionMock();
this.rendererCacheMock = {};
this.params = {};
});
var testMapConfigOneLayer = {
version: '1.5.0',
layers: [
{
type: 'mapnik',
options: {
sql: 'select * from test_table limit 2',
cartocss: '#layer { marker-fill:red; marker-width:32; marker-allow-overlap:true; }',
cartocss_version: '2.3.0'
}
}
]
};
var testMapConfigTwoLayers = {
version: '1.5.0',
layers: [
{
type: 'mapnik',
options: {
sql: 'select * from test_table limit 2',
cartocss: '#layer { marker-fill:red; marker-width:32; marker-allow-overlap:true; }',
cartocss_version: '2.3.0'
}
},
{
type: 'mapnik',
options: {
sql: 'select * from test_table limit 2',
cartocss: '#layer { marker-fill:red; marker-width:32; marker-allow-overlap:true; }',
cartocss_version: '2.3.0'
}
2019-10-22 01:07:24 +08:00
}
]
};
var testMapConfigOneLayerTwoTables = {
version: '1.5.0',
layers: [
{
type: 'mapnik',
options: {
sql: 'select * from test_table limit 2',
cartocss: '#layer { marker-fill:red; marker-width:32; marker-allow-overlap:true; }',
cartocss_version: '2.3.0',
affected_tables: ['test_table_1', 'test_table_2']
}
2019-10-22 01:07:24 +08:00
}
]
};
var testMapConfigTwoLayerTwoTables = {
version: '1.5.0',
layers: [
{
type: 'mapnik',
options: {
sql: 'select * from test_table limit 2',
cartocss: '#layer { marker-fill:red; marker-width:32; marker-allow-overlap:true; }',
cartocss_version: '2.3.0',
affected_tables: ['test_table_1', 'test_table_2']
}
},
{
type: 'mapnik',
options: {
sql: 'select * from test_table limit 2',
cartocss: '#layer { marker-fill:red; marker-width:32; marker-allow-overlap:true; }',
cartocss_version: '2.3.0',
affected_tables: ['test_table_3', 'test_table_4']
}
2019-10-22 01:07:24 +08:00
}
]
};
2019-10-22 01:07:24 +08:00
it('should return 1 feature for one layer', function (done) {
var mapConfig = MapConfig.create(testMapConfigOneLayer);
var layer = mapConfig.getLayer(0);
var testSubject = new MapnikLayerStats();
2019-10-22 01:07:24 +08:00
testSubject.getStats(layer, this.dbConnectionMock, function (err, result) {
assert.ifError(err);
assert.strictEqual(result.estimatedFeatureCount, 1);
done();
});
});
2019-10-22 01:07:24 +08:00
it('should return 1 feature for two layers', function (done) {
var self = this;
var mapConfig = MapConfig.create(testMapConfigTwoLayers);
var layer0 = mapConfig.getLayer(0);
var layer1 = mapConfig.getLayer(1);
var testSubject = new MapnikLayerStats();
2019-10-22 01:07:24 +08:00
testSubject.getStats(layer0, self.dbConnectionMock, function (err, result) {
assert.ifError(err);
assert.strictEqual(result.estimatedFeatureCount, 1);
2019-10-22 01:07:24 +08:00
testSubject.getStats(layer1, self.dbConnectionMock, function (err, result) {
assert.ifError(err);
assert.strictEqual(result.estimatedFeatureCount, 1);
done();
});
});
});
2019-10-22 01:07:24 +08:00
it('should return 1 feature for one layers with two tables', function (done) {
var mapConfig = MapConfig.create(testMapConfigOneLayerTwoTables);
var layer = mapConfig.getLayer(0);
var testSubject = new MapnikLayerStats();
2019-10-22 01:07:24 +08:00
testSubject.getStats(layer, this.dbConnectionMock, function (err, result) {
assert.ifError(err);
assert.strictEqual(result.estimatedFeatureCount, 1);
done();
});
});
2019-10-22 01:07:24 +08:00
it('should return 1 feature for two layers and two tables', function (done) {
var self = this;
var mapConfig = MapConfig.create(testMapConfigTwoLayerTwoTables);
var layer0 = mapConfig.getLayer(0);
var layer1 = mapConfig.getLayer(1);
var testSubject = new MapnikLayerStats();
2019-10-22 01:07:24 +08:00
testSubject.getStats(layer0, self.dbConnectionMock, function (err, result) {
assert.ifError(err);
assert.strictEqual(result.estimatedFeatureCount, 1);
2019-10-22 01:07:24 +08:00
testSubject.getStats(layer1, self.dbConnectionMock, function (err, result) {
assert.ifError(err);
assert.strictEqual(result.estimatedFeatureCount, 1);
done();
});
});
});
});