Windshaft-cartodb/test/unit/cartodb/ported/tile_stats.test.js

91 lines
3.0 KiB
JavaScript
Raw Normal View History

2015-07-08 06:19:11 +08:00
require('../../../support/test_helper.js');
var assert = require('assert');
var LayergroupController = require('../../../../lib/cartodb/controllers/layergroup');
2015-07-08 06:19:11 +08:00
describe('tile stats', function() {
beforeEach(function () {
this.statsClient = global.statsClient;
2015-07-08 06:19:11 +08:00
});
afterEach(function() {
global.statsClient = this.statsClient;
});
2015-07-08 06:19:11 +08:00
it('finalizeGetTileOrGrid does not call statsClient when format is not supported', function() {
var expectedCalls = 2, // it will call increment once for the general error
invalidFormat = 'png2',
invalidFormatRegexp = new RegExp('invalid'),
formatMatched = false;
mockStatsClientGetInstance({
increment: function(label) {
formatMatched = formatMatched || !!label.match(invalidFormatRegexp);
expectedCalls--;
}
});
2015-10-01 00:00:54 +08:00
var layergroupController = new LayergroupController();
2015-07-08 06:19:11 +08:00
var reqMock = {
profiler: { toJSONString:function() {} },
2015-07-08 06:19:11 +08:00
params: {
format: invalidFormat
}
};
var resMock = {
status: function() { return this; },
set: function() {},
json: function() {},
jsonp: function() {},
send: function() {}
};
var next = function () {};
layergroupController.finalizeGetTileOrGrid('Unsupported format png2', reqMock, resMock, null, null, next);
2015-07-08 06:19:11 +08:00
assert.ok(formatMatched, 'Format was never matched in increment method');
assert.equal(expectedCalls, 0, 'Unexpected number of calls to increment method');
});
it('finalizeGetTileOrGrid calls statsClient when format is supported', function() {
var expectedCalls = 2, // general error + format error
validFormat = 'png',
validFormatRegexp = new RegExp(validFormat),
formatMatched = false;
mockStatsClientGetInstance({
increment: function(label) {
formatMatched = formatMatched || !!label.match(validFormatRegexp);
expectedCalls--;
}
});
var reqMock = {
profiler: { toJSONString:function() {} },
2015-07-08 06:19:11 +08:00
params: {
format: validFormat
}
};
var resMock = {
status: function() { return this; },
set: function() {},
json: function() {},
jsonp: function() {},
send: function() {}
};
2015-07-08 06:19:11 +08:00
2015-10-01 00:00:54 +08:00
var layergroupController = new LayergroupController();
2015-07-08 06:19:11 +08:00
var next = function () {};
layergroupController.finalizeGetTileOrGrid('Another error happened', reqMock, resMock, null, null, next);
2015-07-08 06:19:11 +08:00
assert.ok(formatMatched, 'Format was never matched in increment method');
assert.equal(expectedCalls, 0, 'Unexpected number of calls to increment method');
});
function mockStatsClientGetInstance(instance) {
global.statsClient = Object.assign(global.statsClient, instance);
2015-07-08 06:19:11 +08:00
}
});