Initial commit
This commit is contained in:
@@ -0,0 +1,292 @@
|
||||
var $ = require('jquery');
|
||||
var _ = require('underscore');
|
||||
var util = require('cdb.core.util');
|
||||
var WindshaftClient = require('../../../src/windshaft/client');
|
||||
var Request = require('../../../src/windshaft/request');
|
||||
var LZMA = require('../../../vendor/lzma');
|
||||
|
||||
describe('windshaft/client', function () {
|
||||
describe('instantiateMap', function () {
|
||||
var ajaxSpy;
|
||||
|
||||
function getRecentCall () {
|
||||
const args = ajaxSpy.calls.mostRecent().args[0];
|
||||
const url = args.url;
|
||||
|
||||
return {
|
||||
url,
|
||||
method: args.method,
|
||||
path: url.split('?')[0],
|
||||
params: url.split('?')[1].split('&'),
|
||||
dataType: args.dataType,
|
||||
contentType: args.contentType,
|
||||
jsonpCallback: args.jsonpCallback,
|
||||
cache: args.cache,
|
||||
crossOrigin: args.crossOrigin,
|
||||
success: args.success,
|
||||
error: args.error
|
||||
};
|
||||
}
|
||||
|
||||
beforeEach(function () {
|
||||
ajaxSpy = spyOn($, 'ajax');
|
||||
|
||||
spyOn(util, 'uniqueCallbackName').and.callFake(function () { return 'callbackName'; });
|
||||
|
||||
this.client = new WindshaftClient({
|
||||
urlTemplate: 'https://{user}.carto.com:443',
|
||||
userName: 'cartojs-test'
|
||||
});
|
||||
});
|
||||
|
||||
it('should trigger a GET request to instantiate a map', function () {
|
||||
var request = new Request({ some: 'json that must be encoded' }, {}, {});
|
||||
this.client.instantiateMap(request);
|
||||
|
||||
const { path, method, dataType, jsonpCallback, cache } = getRecentCall();
|
||||
|
||||
expect(path).toEqual('https://cartojs-test.carto.com:443/api/v1/map');
|
||||
expect(method).toEqual('GET');
|
||||
expect(dataType).toEqual('jsonp');
|
||||
expect(jsonpCallback).toMatch('_cdbc_callbackName');
|
||||
expect(cache).toEqual(true);
|
||||
});
|
||||
|
||||
it('should use the endpoint for named maps', function () {
|
||||
var request = new Request({ some: 'json that must be encoded' }, {}, {});
|
||||
this.client = new WindshaftClient({
|
||||
urlTemplate: 'https://{user}.carto.com:443',
|
||||
userName: 'cartojs-test',
|
||||
templateName: 'tpl123456789'
|
||||
});
|
||||
this.client.instantiateMap(request);
|
||||
|
||||
const { path } = getRecentCall();
|
||||
|
||||
expect(path).toEqual('https://cartojs-test.carto.com:443/api/v1/map/named/tpl123456789/jsonp');
|
||||
});
|
||||
|
||||
it('should include the given params and handle JSON objects correctly', function () {
|
||||
var request = new Request({ some: 'json that must be encoded' }, {
|
||||
client: 'client_version',
|
||||
filters: { some: 'filters that will be applied' }
|
||||
}, {});
|
||||
this.client.instantiateMap(request);
|
||||
|
||||
const { path, params, method, dataType, jsonpCallback, cache } = getRecentCall();
|
||||
|
||||
expect(path).toEqual('https://cartojs-test.carto.com:443/api/v1/map');
|
||||
expect(params[0]).toEqual('config=%7B%22some%22%3A%22json%20that%20must%20be%20encoded%22%7D');
|
||||
expect(params[1]).toEqual('client=client_version');
|
||||
expect(params[2]).toEqual('filters=%7B%22some%22%3A%22filters%20that%20will%20be%20applied%22%7D');
|
||||
expect(method).toEqual('GET');
|
||||
expect(dataType).toEqual('jsonp');
|
||||
expect(jsonpCallback).toMatch('_cdbc_callbackName');
|
||||
expect(cache).toEqual(true);
|
||||
});
|
||||
|
||||
it('should invoke the success callback', function () {
|
||||
var successCallback = jasmine.createSpy('successCallback');
|
||||
var request = new Request('mapDefinition', {}, { success: successCallback });
|
||||
this.client.instantiateMap(request);
|
||||
|
||||
const { success } = getRecentCall();
|
||||
success({ layergroupid: '123456789' });
|
||||
|
||||
expect(successCallback).toHaveBeenCalled();
|
||||
var dasboardInstance = successCallback.calls.mostRecent().args[0];
|
||||
|
||||
expect(dasboardInstance).toEqual({ layergroupid: '123456789' });
|
||||
});
|
||||
|
||||
it('should invoke the error callback if Windshaft returns some errors under a success response code', function () {
|
||||
var errorCallback = jasmine.createSpy('errorCallback');
|
||||
var request = new Request('mapDefinition', {}, { error: errorCallback });
|
||||
|
||||
this.client.instantiateMap(request);
|
||||
|
||||
var errors = {
|
||||
errors: ['the error message'],
|
||||
errors_with_context: [
|
||||
{
|
||||
type: 'unknown',
|
||||
message: 'the error message'
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
const { success } = getRecentCall();
|
||||
success(errors);
|
||||
|
||||
var callArgs = errorCallback.calls.mostRecent().args;
|
||||
expect(callArgs[0][0]).toEqual(jasmine.objectContaining({
|
||||
message: 'the error message',
|
||||
origin: 'windshaft'
|
||||
}));
|
||||
});
|
||||
|
||||
it('should invoke the error callback if ajax request goes wrong. If it is not a windshaft error it returns an empty array', function () {
|
||||
var errorCallback = jasmine.createSpy('errorCallback');
|
||||
var request = new Request('mapDefinition', {}, { error: errorCallback });
|
||||
this.client.instantiateMap(request);
|
||||
|
||||
const { error } = getRecentCall();
|
||||
error({ responseText: JSON.stringify({ something: 'else' }) });
|
||||
|
||||
var callArgs = errorCallback.calls.mostRecent().args;
|
||||
expect(callArgs[0] instanceof Array).toBe(true);
|
||||
expect(callArgs[0].length).toBe(0);
|
||||
});
|
||||
|
||||
it('should ignore the error callback if request was aborted', function () {
|
||||
var errorCallback = jasmine.createSpy('errorCallback');
|
||||
var request = new Request('mapDefinition', {}, { error: errorCallback });
|
||||
|
||||
this.client.instantiateMap(request);
|
||||
|
||||
$.ajax.calls.argsFor(0)[0].error({ xhr: { responseText: 'something' } }, 'abort');
|
||||
|
||||
expect(errorCallback).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
describe('request tracking', function () {
|
||||
var successCallback;
|
||||
var errorCallback;
|
||||
var request;
|
||||
|
||||
beforeEach(function () {
|
||||
successCallback = jasmine.createSpy('successCallback');
|
||||
errorCallback = jasmine.createSpy('errorCallback');
|
||||
|
||||
request = new Request('mapDefinition', {}, {
|
||||
success: successCallback,
|
||||
error: errorCallback
|
||||
});
|
||||
});
|
||||
|
||||
it('should make a request only if the request service allows it', function () {
|
||||
expect($.ajax).not.toHaveBeenCalled();
|
||||
|
||||
spyOn(this.client._requestTracker, 'canRequestBePerformed').and.returnValue(true);
|
||||
|
||||
this.client.instantiateMap(request);
|
||||
|
||||
expect($.ajax).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not make a request when the request service does not allow it', function () {
|
||||
expect($.ajax).not.toHaveBeenCalled();
|
||||
|
||||
spyOn(this.client._requestTracker, 'canRequestBePerformed').and.returnValue(false);
|
||||
|
||||
this.client.instantiateMap(request);
|
||||
expect($.ajax).not.toHaveBeenCalled();
|
||||
expect(errorCallback).toHaveBeenCalledWith({});
|
||||
});
|
||||
});
|
||||
|
||||
xdescribe('HTTP method:', function () {
|
||||
it('should use GET to URL with encoded config when the payload is small enough', function (done) {
|
||||
var smallPayload = new Array(1933).join('x');
|
||||
var request = new Request(smallPayload, { a: 'a sentence' }, {});
|
||||
this.client.instantiateMap(request);
|
||||
|
||||
_.defer(function () {
|
||||
const { url, method, path, params } = getRecentCall();
|
||||
|
||||
expect(url.length).toBeLessThan(2033);
|
||||
expect(path).toEqual('https://cartojs-test.carto.com:443/api/v1/map');
|
||||
expect(method).toEqual('GET');
|
||||
expect(params[0]).toMatch('^config=');
|
||||
expect(params[0]).not.toMatch('^lzma=');
|
||||
expect(params[1]).toEqual('a=a%20sentence');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should use GET with compressed payload when payload is too big', function (done) {
|
||||
var mediumPayload = new Array(2033).join('x');
|
||||
var request = new Request(mediumPayload, { a: 'a sentence' }, {});
|
||||
this.client.instantiateMap(request);
|
||||
|
||||
_.defer(function () {
|
||||
const { url, method, path, params } = getRecentCall();
|
||||
|
||||
expect(url.length).toBeLessThan(2033);
|
||||
expect(path).toEqual('https://cartojs-test.carto.com:443/api/v1/map');
|
||||
expect(method).toEqual('GET');
|
||||
|
||||
expect(params[0]).toMatch('^lzma=');
|
||||
expect(params[0]).not.toMatch('^config=');
|
||||
expect(params[1]).toEqual('a=a%20sentence');
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should use POST when URL even the compressed payload is too big', function (done) {
|
||||
// simulate a compression that generates something BIG
|
||||
spyOn(LZMA, 'compress').and.callFake(function (data, level, callback) {
|
||||
callback(new Array(2500).join('x'));
|
||||
});
|
||||
var bigPayload = new Array(2033).join('x');
|
||||
var request = new Request(bigPayload, { a: 'a sentence' }, {});
|
||||
this.client.instantiateMap(request);
|
||||
|
||||
_.defer(function () {
|
||||
const { method, path, params, dataType, contentType, crossOrigin } = getRecentCall();
|
||||
|
||||
expect(path).toEqual('https://cartojs-test.carto.com:443/api/v1/map');
|
||||
expect(crossOrigin).toEqual(true);
|
||||
expect(method).toEqual('POST');
|
||||
expect(dataType).toEqual('json');
|
||||
expect(contentType).toEqual('application/json');
|
||||
expect(params[0]).toEqual('a=a%20sentence');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('cancelling previous requests', function () {
|
||||
beforeEach(function () {
|
||||
this.fakeXHR = jasmine.createSpyObj('fakeXHR', ['abort']);
|
||||
$.ajax.and.returnValues(this.fakeXHR, undefined);
|
||||
});
|
||||
|
||||
it('should cancel previous requests when using GET requests', function () {
|
||||
var errorCallback = jasmine.createSpy('errorCallback');
|
||||
var request = new Request({ some: 'json that must be encoded' }, {}, { error: errorCallback });
|
||||
this.client.instantiateMap(request);
|
||||
|
||||
expect($.ajax.calls.argsFor(0)[0].method).toEqual('GET');
|
||||
expect(this.fakeXHR.abort).not.toHaveBeenCalled();
|
||||
|
||||
this.client.instantiateMap(request);
|
||||
|
||||
expect(this.fakeXHR.abort).toHaveBeenCalled();
|
||||
|
||||
expect(errorCallback).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should cancel previous requests when using POST requests', function (done) {
|
||||
// simulate a compression that generates something BIG
|
||||
spyOn(LZMA, 'compress').and.callFake(function (data, level, callback) {
|
||||
callback(new Array(2500).join('x'));
|
||||
});
|
||||
var request = new Request({ something: new Array(3000).join('x') }, {}, {});
|
||||
this.client.instantiateMap(request);
|
||||
|
||||
_.defer(function () {
|
||||
expect($.ajax.calls.argsFor(0)[0].method).toEqual('POST');
|
||||
expect(this.fakeXHR.abort).not.toHaveBeenCalled();
|
||||
|
||||
this.client.instantiateMap(request);
|
||||
|
||||
expect(this.fakeXHR.abort).toHaveBeenCalled();
|
||||
|
||||
done();
|
||||
}.bind(this));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,82 @@
|
||||
var parseWindshaftErrors = require('../../../src/windshaft/error-parser');
|
||||
|
||||
describe('error-parser', function () {
|
||||
function testErrorsWithContext (response) {
|
||||
var parsedErrors = parseWindshaftErrors(response);
|
||||
|
||||
expect(parsedErrors.length).toBe(1);
|
||||
expect(parsedErrors[0].message).toEqual('Your princess is in another castle');
|
||||
expect(parsedErrors[0].layerId).toEqual('6675c6a3-8c57-4033-9fba-072215565521');
|
||||
}
|
||||
|
||||
function testErrors (response) {
|
||||
var parsedErrors = parseWindshaftErrors(response);
|
||||
|
||||
expect(parsedErrors.length).toBe(1);
|
||||
expect(parsedErrors[0].message).toEqual('You must defeat Sheng Long to stand a chance.');
|
||||
}
|
||||
|
||||
var errorsWithContext = [{
|
||||
type: 'layer',
|
||||
message: 'Your princess is in another castle',
|
||||
layer: {
|
||||
id: '6675c6a3-8c57-4033-9fba-072215565521'
|
||||
}
|
||||
}];
|
||||
|
||||
var errors = [
|
||||
'You must defeat Sheng Long to stand a chance.',
|
||||
'You did quite well, but you need more training to defeat me!'
|
||||
];
|
||||
|
||||
it('should return errors with context if available inside a responseJSON object', function () {
|
||||
var response = {
|
||||
responseJSON: {
|
||||
errors_with_context: errorsWithContext,
|
||||
errors: errors
|
||||
}
|
||||
};
|
||||
|
||||
testErrorsWithContext(response);
|
||||
});
|
||||
|
||||
it('should return one simple error if no one with context is available inside a responseJSON object', function () {
|
||||
var response = {
|
||||
responseJSON: {
|
||||
errors: errors
|
||||
}
|
||||
};
|
||||
|
||||
testErrors(response);
|
||||
});
|
||||
|
||||
it('should return errors with context if available', function () {
|
||||
var response = {
|
||||
errors_with_context: errorsWithContext,
|
||||
errors: errors
|
||||
};
|
||||
|
||||
testErrorsWithContext(response);
|
||||
});
|
||||
|
||||
it('should return one simple error if no one with context is available', function () {
|
||||
var response = {
|
||||
errors: errors
|
||||
};
|
||||
|
||||
testErrors(response);
|
||||
});
|
||||
|
||||
it('should pass the error object if it is not a string', function () {
|
||||
var response = {
|
||||
errors: [{
|
||||
message: 'You must defeat Sheng Long to stand a chance.'
|
||||
}]
|
||||
};
|
||||
|
||||
var parsedErrors = parseWindshaftErrors(response);
|
||||
|
||||
expect(parsedErrors.length).toBe(1);
|
||||
expect(parsedErrors[0].message).toEqual('You must defeat Sheng Long to stand a chance.');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,3 @@
|
||||
module.exports = {
|
||||
responseText: '{ "errors": ["Postgis Plugin: ERROR: transform: couldnt project point (242 611 0): latitude or longitude exceeded limits."]}'
|
||||
};
|
||||
@@ -0,0 +1,55 @@
|
||||
var WindshaftError = require('../../../src/windshaft/error');
|
||||
|
||||
describe('windshaft/error', function () {
|
||||
it('should parse properly the input error and return it', function () {
|
||||
var inputError = {
|
||||
type: 'tile',
|
||||
subtype: 'torque',
|
||||
message: 'an error happened',
|
||||
context: 'the context'
|
||||
};
|
||||
|
||||
var windshaftError = new WindshaftError(inputError);
|
||||
|
||||
expect(windshaftError.origin).toEqual('windshaft');
|
||||
expect(windshaftError.type).toEqual('tile');
|
||||
expect(windshaftError.subtype).toEqual('torque');
|
||||
expect(windshaftError.message).toEqual('an error happened');
|
||||
expect(windshaftError.context).toEqual('the context');
|
||||
});
|
||||
|
||||
describe('analysis error', function () {
|
||||
it('should fill context property is present', function () {
|
||||
var inputError = {
|
||||
type: 'analysis',
|
||||
analysis: {
|
||||
context: 'analysis context'
|
||||
}
|
||||
};
|
||||
|
||||
var windshaftError = new WindshaftError(inputError);
|
||||
|
||||
expect(windshaftError.context).toEqual('analysis context');
|
||||
});
|
||||
|
||||
it('should fill analysisId with analysis node_id or id', function () {
|
||||
var inputError = {
|
||||
type: 'analysis',
|
||||
analysis: {
|
||||
node_id: 'F1'
|
||||
}
|
||||
};
|
||||
|
||||
var windshaftError = new WindshaftError(inputError);
|
||||
|
||||
expect(windshaftError.analysisId).toEqual('F1');
|
||||
|
||||
delete inputError.analysis.node_id;
|
||||
inputError.analysis.id = 'G1';
|
||||
|
||||
windshaftError = new WindshaftError(inputError);
|
||||
|
||||
expect(windshaftError.analysisId).toEqual('G1');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
var FilterBase = require('../../../../src/windshaft/filters/base');
|
||||
|
||||
describe('windshaft/filters/base', function () {
|
||||
beforeEach(function () {
|
||||
this.filter = new FilterBase();
|
||||
this.filter.toJSON = jasmine.createSpy('toJSON').and.returnValue({});
|
||||
this.filter.isEmpty = jasmine.createSpy('isEmpty').and.returnValue(false);
|
||||
});
|
||||
|
||||
describe('.remove', function () {
|
||||
beforeEach(function () {
|
||||
this.removeSpy = jasmine.createSpy('remove');
|
||||
this.filter.on('destroy', this.removeSpy);
|
||||
spyOn(this.filter, 'stopListening');
|
||||
this.filter.remove();
|
||||
});
|
||||
|
||||
it('should trigger a destroy event', function () {
|
||||
expect(this.removeSpy).toHaveBeenCalledWith(this.filter);
|
||||
});
|
||||
|
||||
it('should stop listening to events', function () {
|
||||
expect(this.filter.stopListening).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,226 @@
|
||||
var WindshaftFiltersCategory = require('../../../../src/windshaft/filters/category');
|
||||
|
||||
describe('windshaft/filters/category', function () {
|
||||
var data;
|
||||
|
||||
beforeEach(function () {
|
||||
this.filter = new WindshaftFiltersCategory({
|
||||
dataviewId: 'category_dataview'
|
||||
});
|
||||
|
||||
data = [];
|
||||
for (var i = 0; i < 12; i++) {
|
||||
data.push(i);
|
||||
}
|
||||
});
|
||||
|
||||
it('should generate two internal collections for adding rejected and accepted items', function () {
|
||||
expect(this.filter.rejectedCategories).toBeDefined();
|
||||
expect(this.filter.acceptedCategories).toBeDefined();
|
||||
});
|
||||
|
||||
describe('isEmpty', function () {
|
||||
it('should be empty when there is no rejected and accepted items', function () {
|
||||
expect(this.filter.isEmpty()).toBeTruthy();
|
||||
});
|
||||
it('should not be empty when there is rejected items', function () {
|
||||
this.filter.reject(1);
|
||||
expect(this.filter.isEmpty()).toBeFalsy();
|
||||
});
|
||||
it('should not be empty when there is accepted items', function () {
|
||||
this.filter.accept(1);
|
||||
expect(this.filter.isEmpty()).toBeFalsy();
|
||||
});
|
||||
it('should not be empty when rejectAll is true', function () {
|
||||
this.filter.set('rejectAll', true);
|
||||
expect(this.filter.isEmpty()).toBeFalsy();
|
||||
});
|
||||
});
|
||||
|
||||
describe('accept', function () {
|
||||
it('should accept an array or a string as an argument', function () {
|
||||
var acceptedCats = this.filter.acceptedCategories;
|
||||
this.filter.accept(1);
|
||||
expect(acceptedCats.size()).toBe(1);
|
||||
this.filter.accept(2);
|
||||
expect(acceptedCats.size()).toBe(2);
|
||||
});
|
||||
|
||||
it('should trigger change event when value is accepted', function () {
|
||||
var callback = jasmine.createSpy('callback');
|
||||
this.filter.bind('change', callback, this.filter);
|
||||
this.filter.accept(1);
|
||||
expect(callback).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should remove value from rejected if it is present when it is accepted', function () {
|
||||
this.filter.reject(1);
|
||||
var rejectedCats = this.filter.rejectedCategories;
|
||||
var acceptedCats = this.filter.acceptedCategories;
|
||||
expect(rejectedCats.size()).toBe(1);
|
||||
this.filter.accept(1);
|
||||
expect(rejectedCats.size()).toBe(0);
|
||||
expect(acceptedCats.size()).toBe(0);
|
||||
});
|
||||
|
||||
it('should not accept a value if it is already present', function () {
|
||||
this.filter.accept(1);
|
||||
var acceptedCats = this.filter.acceptedCategories;
|
||||
expect(acceptedCats.size()).toBe(1);
|
||||
this.filter.accept(1);
|
||||
expect(acceptedCats.size()).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
it('should accept all cleaning accepted and rejected', function () {
|
||||
this.filter.accept([1, 2, 3]);
|
||||
this.filter.reject([4, 5, 6]);
|
||||
this.filter.acceptAll();
|
||||
expect(this.filter.rejectedCategories.size()).toBe(0);
|
||||
expect(this.filter.acceptedCategories.size()).toBe(0);
|
||||
});
|
||||
|
||||
describe('reject', function () {
|
||||
it('should reject an array or a string as an argument', function () {
|
||||
var rejectedCats = this.filter.rejectedCategories;
|
||||
this.filter.reject([1]);
|
||||
expect(rejectedCats.size()).toBe(1);
|
||||
this.filter.reject(2);
|
||||
expect(rejectedCats.size()).toBe(2);
|
||||
});
|
||||
|
||||
it('should trigger change event when value is accepted', function () {
|
||||
var callback = jasmine.createSpy('callback');
|
||||
this.filter.bind('change', callback, this.filter);
|
||||
this.filter.accept(1);
|
||||
expect(callback).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should remove value from accepted if it is present when it is rejected', function () {
|
||||
this.filter.accept([1, 2]);
|
||||
var acceptedCats = this.filter.acceptedCategories;
|
||||
var rejectedCats = this.filter.rejectedCategories;
|
||||
expect(acceptedCats.size()).toBe(2);
|
||||
this.filter.reject(1);
|
||||
expect(rejectedCats.size()).toBe(0);
|
||||
expect(acceptedCats.size()).toBe(1);
|
||||
this.filter.reject(2);
|
||||
expect(rejectedCats.size()).toBe(0);
|
||||
expect(acceptedCats.size()).toBe(0);
|
||||
this.filter.reject(3);
|
||||
expect(rejectedCats.size()).toBe(1);
|
||||
expect(acceptedCats.size()).toBe(0);
|
||||
});
|
||||
|
||||
it('should not reject a value if it is already present', function () {
|
||||
this.filter.reject(1);
|
||||
var rejectedCats = this.filter.rejectedCategories;
|
||||
expect(rejectedCats.size()).toBe(1);
|
||||
this.filter.reject(1);
|
||||
expect(rejectedCats.size()).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
it('should reject all cleaning accepted and rejected, also changing rejectAll attribute', function () {
|
||||
var callback = jasmine.createSpy('callback');
|
||||
this.filter.bind('change', callback, this.filter);
|
||||
this.filter.accept([1, 2, 3]);
|
||||
this.filter.reject([4]);
|
||||
this.filter.rejectAll();
|
||||
expect(this.filter.rejectedCategories.size()).toBe(0);
|
||||
expect(this.filter.acceptedCategories.size()).toBe(0);
|
||||
expect(this.filter.get('rejectAll')).toBeTruthy();
|
||||
expect(callback).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
describe('isRejected', function () {
|
||||
it('should be rejected it is included in reject collection', function () {
|
||||
this.filter.reject(1);
|
||||
expect(this.filter.isRejected(1)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should be rejected it is not included in any of both (accept or reject) and there are accepted', function () {
|
||||
this.filter.accept(4);
|
||||
expect(this.filter.isRejected(1)).toBeTruthy();
|
||||
this.filter.reject(2);
|
||||
expect(this.filter.isRejected(1)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should not be rejected if both collections are empty', function () {
|
||||
expect(this.filter.isRejected(1)).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should not be rejected if it is present in accepted collection', function () {
|
||||
this.filter.accept(1);
|
||||
expect(this.filter.isRejected(1)).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should be rejected if rejectAll is enabled', function () {
|
||||
this.filter.set('rejectAll', true);
|
||||
expect(this.filter.isRejected(1)).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe('toJSON', function () {
|
||||
it('should generate an object with attributes when it is serialized', function () {
|
||||
this.filter.reject([1, 2]);
|
||||
var result = this.filter.toJSON();
|
||||
expect(result['category_dataview']).toBeDefined();
|
||||
expect(result['category_dataview']['reject']).toBeDefined();
|
||||
});
|
||||
|
||||
it('should not generate any value when accept and reject are empty', function () {
|
||||
var result = this.filter.toJSON();
|
||||
expect(result['category_dataview']).toBeDefined();
|
||||
expect(result['category_dataview']['reject']).not.toBeDefined();
|
||||
expect(result['category_dataview']['accept']).not.toBeDefined();
|
||||
});
|
||||
|
||||
it('should send accept when there is any accept, no matter rejects', function () {
|
||||
this.filter.reject([1, 2]);
|
||||
this.filter.accept(3);
|
||||
var result = this.filter.toJSON();
|
||||
expect(result['category_dataview']).toBeDefined();
|
||||
expect(result['category_dataview']['reject']).not.toBeDefined();
|
||||
expect(result['category_dataview']['accept']).toBeDefined();
|
||||
var accept = result['category_dataview']['accept'];
|
||||
expect(accept.length).toBe(1);
|
||||
expect(accept[0]).toBe(3);
|
||||
});
|
||||
|
||||
it('should send reject when accept is empty but reject', function () {
|
||||
this.filter.reject([1, 2]);
|
||||
var result = this.filter.toJSON();
|
||||
expect(result['category_dataview']).toBeDefined();
|
||||
expect(result['category_dataview']['reject']).toBeDefined();
|
||||
expect(result['category_dataview']['accept']).not.toBeDefined();
|
||||
var reject = result['category_dataview']['reject'];
|
||||
expect(reject.length).toBe(2);
|
||||
expect(reject[0]).toBe(1);
|
||||
expect(reject[1]).toBe(2);
|
||||
});
|
||||
|
||||
it('should send accept when both (accept and reject) are not empty', function () {
|
||||
this.filter.reject([1, 2]);
|
||||
this.filter.accept([3]);
|
||||
var result = this.filter.toJSON();
|
||||
expect(result['category_dataview']).toBeDefined();
|
||||
expect(result['category_dataview']['reject']).not.toBeDefined();
|
||||
expect(result['category_dataview']['accept']).toBeDefined();
|
||||
var accept = result['category_dataview']['accept'];
|
||||
expect(accept.length).toBe(1);
|
||||
expect(accept[0]).toBe(3);
|
||||
});
|
||||
|
||||
// TODO: change this spec when it is fixed in the API
|
||||
it('should send a special character in accept when all categories are rejected', function () {
|
||||
this.filter.rejectAll(data);
|
||||
var result = this.filter.toJSON();
|
||||
expect(result['category_dataview']).toBeDefined();
|
||||
expect(result['category_dataview']['reject']).not.toBeDefined();
|
||||
expect(result['category_dataview']['accept']).toBeDefined();
|
||||
var accept = result['category_dataview']['accept'];
|
||||
expect(accept.length).toBe(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,65 @@
|
||||
var RangeFilter = require('../../../../src/windshaft/filters/range');
|
||||
|
||||
describe('windshaft/filters/range', function () {
|
||||
beforeEach(function () {
|
||||
this.dataviewId = 'range-filter-uuid';
|
||||
this.filter = new RangeFilter({
|
||||
dataviewId: this.dataviewId
|
||||
});
|
||||
});
|
||||
|
||||
it('should have a undefined range values by default', function () {
|
||||
expect(this.filter.get('min')).toBeUndefined();
|
||||
expect(this.filter.get('max')).toBeUndefined();
|
||||
});
|
||||
|
||||
describe('.setRange', function () {
|
||||
it('should set the range', function () {
|
||||
this.filter.setRange(2, 4);
|
||||
expect(this.filter.get('min')).toEqual(2);
|
||||
expect(this.filter.get('max')).toEqual(4);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.isEmpty', function () {
|
||||
it('should return true if there are no range values set', function () {
|
||||
expect(this.filter.isEmpty()).toBe(true);
|
||||
this.filter.setRange(1, 2);
|
||||
expect(this.filter.isEmpty()).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.unsetRange', function () {
|
||||
beforeEach(function () {
|
||||
this.filter.setRange(2, 4);
|
||||
this.changeSpy = jasmine.createSpy('change');
|
||||
this.filter.bind('change', this.changeSpy);
|
||||
this.filter.unsetRange();
|
||||
});
|
||||
|
||||
it('should remove range', function () {
|
||||
expect(this.filter.get('min')).toBeUndefined();
|
||||
expect(this.filter.get('max')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should only trigger change event once', function () {
|
||||
expect(this.changeSpy).toHaveBeenCalled();
|
||||
expect(this.changeSpy.calls.count()).toEqual(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.toJSON', function () {
|
||||
beforeEach(function () {
|
||||
this.filter.setRange(2, 4);
|
||||
this.res = this.filter.toJSON();
|
||||
});
|
||||
|
||||
it('should return a JSON object', function () {
|
||||
expect(this.res).toEqual(jasmine.any(Object));
|
||||
var res = this.res[this.dataviewId];
|
||||
expect(res).toEqual(jasmine.any(Object));
|
||||
expect(res.min).toEqual(2);
|
||||
expect(res.max).toEqual(4);
|
||||
});
|
||||
});
|
||||
});
|
||||
+356
@@ -0,0 +1,356 @@
|
||||
var Backbone = require('backbone');
|
||||
var AnalysisService = require('../../../../../src/analysis/analysis-service.js');
|
||||
var CartoDBLayer = require('../../../../../src/geo/map/cartodb-layer');
|
||||
var DataviewModel = require('../../../../../src/dataviews/dataview-model-base');
|
||||
var AnalysisSerializer = require('../../../../../src/windshaft/map-serializer/anonymous-map-serializer/analysis-serializer');
|
||||
var createEngine = require('../../../fixtures/engine.fixture.js');
|
||||
|
||||
describe('analysis-serializer', function () {
|
||||
var engineMock;
|
||||
var mapModel;
|
||||
var layersCollection;
|
||||
var dataviewsCollection;
|
||||
var analysisService;
|
||||
var analysisDefinition;
|
||||
var analysisModel;
|
||||
|
||||
beforeEach(function () {
|
||||
mapModel = new Backbone.Model();
|
||||
engineMock = createEngine();
|
||||
layersCollection = new Backbone.Collection();
|
||||
dataviewsCollection = new Backbone.Collection();
|
||||
analysisService = new AnalysisService({
|
||||
engine: engineMock,
|
||||
camshaftReference: fakeCamshaftReference
|
||||
});
|
||||
analysisDefinition = {
|
||||
id: 'd0',
|
||||
type: 'source',
|
||||
params: {
|
||||
query: 'select * from subway_stops'
|
||||
}
|
||||
};
|
||||
analysisModel = analysisService.analyse(analysisDefinition);
|
||||
});
|
||||
|
||||
describe('.serialize', function () {
|
||||
describe('layer sources', function () {
|
||||
it('should serialize a single analysis', function () {
|
||||
var cartoDBLayer = new CartoDBLayer({
|
||||
id: 'l1',
|
||||
source: analysisModel,
|
||||
cartocss: 'cartocssMock',
|
||||
cartocss_version: '2.0'
|
||||
}, {
|
||||
engine: engineMock
|
||||
});
|
||||
layersCollection.reset([ cartoDBLayer ]);
|
||||
|
||||
var actual = AnalysisSerializer.serialize(layersCollection, dataviewsCollection);
|
||||
var expected = [ analysisDefinition ];
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it("should NOT include an analysis if it's part of the analysis of another layer", function () {
|
||||
var analysis1 = analysisService.analyse({
|
||||
id: 'b1',
|
||||
type: 'union',
|
||||
params: {
|
||||
join_on: 'cartodb_id',
|
||||
source: {
|
||||
id: 'a2',
|
||||
type: 'estimated-population',
|
||||
params: {
|
||||
columnName: 'estimated_people',
|
||||
source: {
|
||||
id: 'a1',
|
||||
type: 'trade-area',
|
||||
params: {
|
||||
kind: 'walk',
|
||||
time: 300,
|
||||
source: {
|
||||
id: 'a0',
|
||||
type: 'source',
|
||||
params: {
|
||||
query: 'select * from subway_stops'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
var analysis2 = analysisService.analyse({
|
||||
id: 'a2',
|
||||
type: 'estimated-population',
|
||||
params: {
|
||||
columnName: 'estimated_people',
|
||||
source: {
|
||||
id: 'a1',
|
||||
type: 'trade-area',
|
||||
params: {
|
||||
kind: 'walk',
|
||||
time: 300,
|
||||
source: {
|
||||
id: 'a0',
|
||||
type: 'source',
|
||||
params: {
|
||||
query: 'select * from subway_stops'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var cartoDBLayer1 = new CartoDBLayer({
|
||||
id: 'l1',
|
||||
source: analysis1,
|
||||
cartocss: 'cartocssMock',
|
||||
cartocss_version: '2.0'
|
||||
}, {
|
||||
engine: engineMock
|
||||
});
|
||||
|
||||
var cartoDBLayer2 = new CartoDBLayer({
|
||||
id: 'l2',
|
||||
source: analysis2,
|
||||
cartocss: 'cartocssMock',
|
||||
cartocss_version: '2.0'
|
||||
}, {
|
||||
engine: engineMock
|
||||
});
|
||||
|
||||
layersCollection.reset([
|
||||
cartoDBLayer1, cartoDBLayer2
|
||||
]);
|
||||
|
||||
var actual = AnalysisSerializer.serialize(layersCollection, dataviewsCollection);
|
||||
var expected = [{
|
||||
'id': 'b1',
|
||||
'type': 'union',
|
||||
'params': {
|
||||
'join_on': 'cartodb_id',
|
||||
'source': {
|
||||
'id': 'a2',
|
||||
'type': 'estimated-population',
|
||||
'params': {
|
||||
'columnName': 'estimated_people',
|
||||
'source': {
|
||||
'id': 'a1',
|
||||
'type': 'trade-area',
|
||||
'params': {
|
||||
'kind': 'walk',
|
||||
'time': 300,
|
||||
'source': {
|
||||
'id': 'a0',
|
||||
'type': 'source',
|
||||
'params': {
|
||||
'query': 'select * from subway_stops'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}];
|
||||
expect(actual.length).toEqual(1);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('dataview sources', function () {
|
||||
it('should serialize a single analysis', function () {
|
||||
var dataview = new DataviewModel({
|
||||
source: analysisModel
|
||||
}, {
|
||||
map: mapModel,
|
||||
engine: engineMock
|
||||
});
|
||||
|
||||
dataviewsCollection.reset([ dataview ]);
|
||||
|
||||
var actual = AnalysisSerializer.serialize(layersCollection, dataviewsCollection);
|
||||
var expected = [analysisDefinition];
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it("should NOT include an analysis if it's part of the analysis of another dataview", function () {
|
||||
var analysis1 = analysisService.analyse({
|
||||
id: 'b1',
|
||||
type: 'union',
|
||||
params: {
|
||||
join_on: 'cartodb_id',
|
||||
source: {
|
||||
id: 'a2',
|
||||
type: 'estimated-population',
|
||||
params: {
|
||||
columnName: 'estimated_people',
|
||||
source: {
|
||||
id: 'a1',
|
||||
type: 'trade-area',
|
||||
params: {
|
||||
kind: 'walk',
|
||||
time: 300,
|
||||
source: {
|
||||
id: 'a0',
|
||||
type: 'source',
|
||||
params: {
|
||||
query: 'select * from subway_stops'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
var analysis2 = analysisService.analyse({
|
||||
id: 'a2',
|
||||
type: 'estimated-population',
|
||||
params: {
|
||||
columnName: 'estimated_people',
|
||||
source: {
|
||||
id: 'a1',
|
||||
type: 'trade-area',
|
||||
params: {
|
||||
kind: 'walk',
|
||||
time: 300,
|
||||
source: {
|
||||
id: 'a0',
|
||||
type: 'source',
|
||||
params: {
|
||||
query: 'select * from subway_stops'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var dataview1 = new DataviewModel({
|
||||
source: analysis1
|
||||
}, {
|
||||
map: mapModel,
|
||||
engine: engineMock
|
||||
});
|
||||
|
||||
var dataview2 = new DataviewModel({
|
||||
source: analysis2
|
||||
}, {
|
||||
map: mapModel,
|
||||
engine: engineMock
|
||||
});
|
||||
|
||||
dataviewsCollection.reset([
|
||||
dataview1, dataview2
|
||||
]);
|
||||
|
||||
var actual = AnalysisSerializer.serialize(layersCollection, dataviewsCollection);
|
||||
var expected = [{
|
||||
'id': 'b1',
|
||||
'type': 'union',
|
||||
'params': {
|
||||
'join_on': 'cartodb_id',
|
||||
'source': {
|
||||
'id': 'a2',
|
||||
'type': 'estimated-population',
|
||||
'params': {
|
||||
'columnName': 'estimated_people',
|
||||
'source': {
|
||||
'id': 'a1',
|
||||
'type': 'trade-area',
|
||||
'params': {
|
||||
'kind': 'walk',
|
||||
'time': 300,
|
||||
'source': {
|
||||
'id': 'a0',
|
||||
'type': 'source',
|
||||
'params': {
|
||||
'query': 'select * from subway_stops'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}];
|
||||
expect(actual.length).toEqual(1);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
it('should only include an analysis once when more than one layer or dataview point to the same analysis', function () {
|
||||
var cartoDBLayer1 = new CartoDBLayer({
|
||||
id: 'l1',
|
||||
source: analysisModel,
|
||||
cartocss: 'cartocssMock',
|
||||
cartocss_version: '2.0'
|
||||
}, {
|
||||
engine: engineMock
|
||||
});
|
||||
|
||||
var cartoDBLayer2 = new CartoDBLayer({
|
||||
id: 'l2',
|
||||
source: analysisModel,
|
||||
cartocss: 'cartocssMock',
|
||||
cartocss_version: '2.0'
|
||||
}, {
|
||||
engine: engineMock
|
||||
});
|
||||
|
||||
var dataview = new DataviewModel({
|
||||
source: analysisModel
|
||||
}, {
|
||||
map: mapModel,
|
||||
engine: engineMock
|
||||
});
|
||||
|
||||
layersCollection.reset([
|
||||
cartoDBLayer1, cartoDBLayer2
|
||||
]);
|
||||
dataviewsCollection.reset([
|
||||
dataview
|
||||
]);
|
||||
|
||||
var actual = AnalysisSerializer.serialize(layersCollection, dataviewsCollection);
|
||||
var expected = [ analysisDefinition ];
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// We use a "fake" reference instead of the one in src/analysis/camshaft-reference
|
||||
// to ensure that tests won't break if the real thing changes
|
||||
var fakeCamshaftReference = {
|
||||
getSourceNamesForAnalysisType: function (analysisType) {
|
||||
var map = {
|
||||
'source': [],
|
||||
'trade-area': ['source'],
|
||||
'estimated-population': ['source'],
|
||||
'point-in-polygon': ['points_source', 'polygons_source'],
|
||||
'union': ['source']
|
||||
};
|
||||
if (!map[analysisType]) {
|
||||
throw new Error('analysis type ' + analysisType + ' not supported');
|
||||
}
|
||||
return map[analysisType];
|
||||
},
|
||||
|
||||
getParamNamesForAnalysisType: function (analysisType) {
|
||||
var map = {
|
||||
'source': ['query'],
|
||||
'trade-area': ['kind', 'time'],
|
||||
'estimated-population': ['columnName'],
|
||||
'point-in-polygon': [],
|
||||
'union': ['join_on']
|
||||
};
|
||||
if (!map[analysisType]) {
|
||||
throw new Error('analysis type ' + analysisType + ' not supported');
|
||||
}
|
||||
return map[analysisType];
|
||||
}
|
||||
};
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
var Backbone = require('backbone');
|
||||
var AnalysisService = require('../../../../../src/analysis/analysis-service.js');
|
||||
var CartoDBLayer = require('../../../../../src/geo/map/cartodb-layer');
|
||||
var DataviewModelBase = require('../../../../../src/dataviews/dataview-model-base');
|
||||
var AnonymousMapSerializer = require('../../../../../src/windshaft/map-serializer/anonymous-map-serializer/anonymous-map-serializer');
|
||||
|
||||
var MyDataviewModel = DataviewModelBase.extend({
|
||||
toJSON: function () {
|
||||
return {};
|
||||
}
|
||||
});
|
||||
|
||||
describe('anonymous-map-serializer', function () {
|
||||
describe('.serialize', function () {
|
||||
var engineMock;
|
||||
var mapModel;
|
||||
var layersCollection;
|
||||
var dataviewsCollection;
|
||||
var analysisService;
|
||||
var analysisModel;
|
||||
var payload;
|
||||
|
||||
beforeEach(function () {
|
||||
mapModel = new Backbone.Model();
|
||||
engineMock = new Backbone.Model();
|
||||
layersCollection = new Backbone.Collection();
|
||||
dataviewsCollection = new Backbone.Collection();
|
||||
|
||||
// Analyses
|
||||
analysisService = new AnalysisService({
|
||||
engine: engineMock,
|
||||
camshaftReference: fakeCamshaftReference
|
||||
});
|
||||
analysisModel = analysisService.analyse({
|
||||
id: 'ANALYSIS_ID',
|
||||
type: 'source',
|
||||
params: {
|
||||
query: 'select * from subway_stops'
|
||||
}
|
||||
});
|
||||
|
||||
// Layers
|
||||
var cartoDBLayer = new CartoDBLayer({
|
||||
id: 'LAYER_ID',
|
||||
source: analysisModel,
|
||||
cartocss: 'cartocssMock',
|
||||
cartocss_version: '2.0'
|
||||
}, {
|
||||
engine: engineMock
|
||||
});
|
||||
layersCollection.add(cartoDBLayer);
|
||||
|
||||
// Dataviews
|
||||
var dataview = new MyDataviewModel({
|
||||
id: 'DATAVIEW_ID',
|
||||
source: analysisModel
|
||||
}, {
|
||||
map: mapModel,
|
||||
engine: engineMock
|
||||
});
|
||||
dataviewsCollection.add(dataview);
|
||||
|
||||
// Serialized payload
|
||||
payload = AnonymousMapSerializer.serialize(layersCollection, dataviewsCollection);
|
||||
});
|
||||
|
||||
it('should include buffersize', function () {
|
||||
expect(payload.buffersize).toEqual({
|
||||
mvt: 0
|
||||
});
|
||||
});
|
||||
|
||||
it('should include one layer', function () {
|
||||
expect(payload.layers.length).toEqual(1);
|
||||
expect(payload.layers[0].id).toEqual('LAYER_ID');
|
||||
});
|
||||
|
||||
it('should include one dataview', function () {
|
||||
expect(Object.keys(payload.dataviews).length).toEqual(1);
|
||||
expect(payload.dataviews['DATAVIEW_ID']).toBeDefined();
|
||||
});
|
||||
|
||||
it('should include one analysis', function () {
|
||||
expect(payload.analyses.length).toEqual(1);
|
||||
expect(payload.analyses[0].id).toEqual('ANALYSIS_ID');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
var fakeCamshaftReference = {
|
||||
getSourceNamesForAnalysisType: function (analysisType) {
|
||||
var map = {
|
||||
'source': []
|
||||
};
|
||||
if (!map[analysisType]) {
|
||||
throw new Error('analysis type ' + analysisType + ' not supported');
|
||||
}
|
||||
return map[analysisType];
|
||||
},
|
||||
|
||||
getParamNamesForAnalysisType: function (analysisType) {
|
||||
var map = {
|
||||
'source': ['query']
|
||||
};
|
||||
if (!map[analysisType]) {
|
||||
throw new Error('analysis type ' + analysisType + ' not supported');
|
||||
}
|
||||
return map[analysisType];
|
||||
}
|
||||
};
|
||||
+116
@@ -0,0 +1,116 @@
|
||||
var Backbone = require('backbone');
|
||||
var CategoryDataviewModel = require('../../../../../src/dataviews/category-dataview-model');
|
||||
var HistogramDataviewModel = require('../../../../../src/dataviews/histogram-dataview-model');
|
||||
var FormulaDataviewModel = require('../../../../../src/dataviews/formula-dataview-model');
|
||||
var MockFactory = require('../../../../helpers/mockFactory');
|
||||
var DataviewsSerializer = require('../../../../../src/windshaft/map-serializer/anonymous-map-serializer/dataviews-serializer');
|
||||
var createEngine = require('../../../fixtures/engine.fixture.js');
|
||||
|
||||
describe('dataviews-serializer', function () {
|
||||
describe('.serialize', function () {
|
||||
var engineMock;
|
||||
var map;
|
||||
var layer;
|
||||
var analysis;
|
||||
|
||||
beforeEach(function () {
|
||||
engineMock = createEngine();
|
||||
map = new Backbone.Model();
|
||||
layer = new Backbone.Model();
|
||||
analysis = MockFactory.createAnalysisModel({ id: 'a0' });
|
||||
});
|
||||
|
||||
it('serialises histogram dataviews', function () {
|
||||
var histogramDataview = new HistogramDataviewModel({
|
||||
id: 'dataview1',
|
||||
column: 'column1',
|
||||
column_type: 'date',
|
||||
aggregation: 'week',
|
||||
source: analysis
|
||||
}, {
|
||||
map: map,
|
||||
engine: engineMock,
|
||||
layer: layer
|
||||
});
|
||||
|
||||
var dataviewsCollection = new Backbone.Collection([
|
||||
histogramDataview
|
||||
]);
|
||||
|
||||
expect(DataviewsSerializer.serialize(dataviewsCollection)).toEqual({
|
||||
dataview1: {
|
||||
type: 'histogram',
|
||||
source: {
|
||||
id: 'a0'
|
||||
},
|
||||
options: {
|
||||
column: 'column1',
|
||||
aggregation: 'week'
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('serialises category dataviews', function () {
|
||||
var categoryDataview = new CategoryDataviewModel({
|
||||
id: 'dataview2',
|
||||
column: 'column2',
|
||||
aggregation: 'aggregation2',
|
||||
aggregation_column: 'aggregation_column2',
|
||||
source: analysis
|
||||
}, {
|
||||
map: map,
|
||||
engine: engineMock,
|
||||
layer: layer
|
||||
});
|
||||
|
||||
var dataviewsCollection = new Backbone.Collection([
|
||||
categoryDataview
|
||||
]);
|
||||
|
||||
expect(DataviewsSerializer.serialize(dataviewsCollection)).toEqual({
|
||||
dataview2: {
|
||||
type: 'aggregation',
|
||||
source: {
|
||||
id: 'a0'
|
||||
},
|
||||
options: {
|
||||
column: 'column2',
|
||||
aggregation: 'aggregation2',
|
||||
aggregationColumn: 'aggregation_column2'
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('serialises formula dataviews', function () {
|
||||
var formulaDataview = new FormulaDataviewModel({
|
||||
id: 'dataview3',
|
||||
column: 'column3',
|
||||
operation: 'sum',
|
||||
source: analysis
|
||||
}, {
|
||||
map: map,
|
||||
engine: engineMock,
|
||||
layer: layer
|
||||
});
|
||||
|
||||
var dataviewsCollection = new Backbone.Collection([
|
||||
formulaDataview
|
||||
]);
|
||||
|
||||
expect(DataviewsSerializer.serialize(dataviewsCollection)).toEqual({
|
||||
dataview3: {
|
||||
type: 'formula',
|
||||
source: {
|
||||
id: 'a0'
|
||||
},
|
||||
options: {
|
||||
column: 'column3',
|
||||
operation: 'sum'
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
+214
@@ -0,0 +1,214 @@
|
||||
var Backbone = require('backbone');
|
||||
var CartoDBLayer = require('../../../../../src/geo/map/cartodb-layer');
|
||||
var PlainLayer = require('../../../../../src/geo/map/plain-layer');
|
||||
var TileLayer = require('../../../../../src/geo/map/tile-layer');
|
||||
var TorqueLayer = require('../../../../../src/geo/map/torque-layer');
|
||||
var GMapsBaseLayer = require('../../../../../src/geo/map/gmaps-base-layer');
|
||||
var LayersSerializer = require('../../../../../src/windshaft/map-serializer/anonymous-map-serializer/layers-serializer');
|
||||
var MockFactory = require('../../../../helpers/mockFactory');
|
||||
var createEngine = require('../../../fixtures/engine.fixture.js');
|
||||
|
||||
describe('layers-serializer', function () {
|
||||
describe('.serialize', function () {
|
||||
var engineMock;
|
||||
var sourceMock;
|
||||
var layersCollection;
|
||||
|
||||
// Create all test objects once
|
||||
beforeAll(function () {
|
||||
layersCollection = new Backbone.Collection();
|
||||
engineMock = createEngine();
|
||||
sourceMock = MockFactory.createAnalysisModel({ id: 'a1' });
|
||||
});
|
||||
|
||||
describe('mapnik layer', function () {
|
||||
it('should serialize a cartodb layer with no aggregation', function () {
|
||||
var cartoDBLayer = new CartoDBLayer({
|
||||
id: 'l1',
|
||||
source: sourceMock,
|
||||
cartocss: 'cartoCSS1',
|
||||
cartocss_version: '2.0'
|
||||
}, {
|
||||
engine: engineMock
|
||||
});
|
||||
layersCollection.reset([cartoDBLayer]);
|
||||
|
||||
var actual = LayersSerializer.serialize(layersCollection);
|
||||
var expected = [{
|
||||
'id': 'l1',
|
||||
'type': 'mapnik',
|
||||
'options': {
|
||||
'cartocss': 'cartoCSS1',
|
||||
'cartocss_version': '2.0',
|
||||
'interactivity': ['cartodb_id'],
|
||||
'source': { id: 'a1' }
|
||||
}
|
||||
}];
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should serialize a cartodb layer with aggregation', function () {
|
||||
var cartoDBLayer = new CartoDBLayer({
|
||||
id: 'l1',
|
||||
source: sourceMock,
|
||||
cartocss: 'cartoCSS1',
|
||||
cartocss_version: '2.0'
|
||||
}, {
|
||||
engine: engineMock,
|
||||
aggregation: {
|
||||
threshold: 1000,
|
||||
resolution: 4,
|
||||
placement: 'point-sample',
|
||||
columns: {
|
||||
'population': {
|
||||
aggregate_function: 'sum',
|
||||
aggregated_column: 'pop_max'
|
||||
},
|
||||
'states': {
|
||||
aggregate_function: 'sum',
|
||||
aggregated_column: 'states'
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
layersCollection.reset([cartoDBLayer]);
|
||||
|
||||
var actual = LayersSerializer.serialize(layersCollection);
|
||||
|
||||
var expected = [{
|
||||
'id': 'l1',
|
||||
'type': 'mapnik',
|
||||
'options': {
|
||||
'cartocss': 'cartoCSS1',
|
||||
'cartocss_version': '2.0',
|
||||
'interactivity': ['cartodb_id'],
|
||||
'source': { id: 'a1' },
|
||||
'aggregation': {
|
||||
'threshold': 1000,
|
||||
'resolution': 4,
|
||||
'placement': 'point-sample',
|
||||
'columns': {
|
||||
'population': {
|
||||
'aggregate_function': 'sum',
|
||||
'aggregated_column': 'pop_max'
|
||||
},
|
||||
'states': {
|
||||
'aggregate_function': 'sum',
|
||||
'aggregated_column': 'states'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}];
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should serialize a cartodb layer with propper zoom options', function () {
|
||||
var cartoDBLayer = new CartoDBLayer({
|
||||
id: 'l1',
|
||||
source: sourceMock,
|
||||
cartocss: 'cartoCSS1',
|
||||
cartocss_version: '2.0',
|
||||
minzoom: 5,
|
||||
maxzoom: 9
|
||||
},
|
||||
{
|
||||
engine: engineMock
|
||||
});
|
||||
layersCollection.reset([cartoDBLayer]);
|
||||
|
||||
var actual = LayersSerializer.serialize(layersCollection);
|
||||
var expected = [{
|
||||
'id': 'l1',
|
||||
'type': 'mapnik',
|
||||
'options': {
|
||||
'cartocss': 'cartoCSS1',
|
||||
'cartocss_version': '2.0',
|
||||
'interactivity': ['cartodb_id'],
|
||||
'source': { id: 'a1' },
|
||||
'minzoom': 5,
|
||||
'maxzoom': 9
|
||||
}
|
||||
}];
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
it('should serialize a plain layer', function () {
|
||||
var plainLayer = new PlainLayer({
|
||||
id: 'l2',
|
||||
color: 'COLOR',
|
||||
image: 'http://carto.com/image.png'
|
||||
}, { engine: engineMock });
|
||||
layersCollection.reset([plainLayer]);
|
||||
|
||||
var actual = LayersSerializer.serialize(layersCollection);
|
||||
var expected = [{
|
||||
'id': 'l2',
|
||||
'type': 'plain',
|
||||
'options': {
|
||||
'color': 'COLOR',
|
||||
'imageUrl': 'http://carto.com/image.png'
|
||||
}
|
||||
}];
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should serialize a torque layer', function () {
|
||||
var torqueLayer = new TorqueLayer({
|
||||
id: 'l3',
|
||||
source: sourceMock,
|
||||
cartocss: 'cartocss'
|
||||
}, {
|
||||
engine: engineMock
|
||||
});
|
||||
layersCollection.reset([torqueLayer]);
|
||||
|
||||
var actual = LayersSerializer.serialize(layersCollection);
|
||||
var expected = [{
|
||||
'id': 'l3',
|
||||
'type': 'torque',
|
||||
'options': {
|
||||
'source': { id: 'a1' },
|
||||
'cartocss': 'cartocss',
|
||||
'cartocss_version': '2.1.0'
|
||||
}
|
||||
}];
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should serialize a tile layer', function () {
|
||||
var tileLayer = new TileLayer({
|
||||
id: 'l4',
|
||||
urlTemplate: 'URL_TEMPLATE',
|
||||
subdomains: 'abc',
|
||||
tms: false
|
||||
}, { engine: engineMock });
|
||||
layersCollection.reset([tileLayer]);
|
||||
|
||||
var actual = LayersSerializer.serialize(layersCollection);
|
||||
var expected = [{
|
||||
'id': 'l4',
|
||||
'type': 'http',
|
||||
'options': {
|
||||
'urlTemplate': 'URL_TEMPLATE',
|
||||
'subdomains': 'abc',
|
||||
'tms': false
|
||||
}
|
||||
}];
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should not serialize GMapsBase layers', function () {
|
||||
var gmapsBaseLayer = new GMapsBaseLayer({
|
||||
id: 'l4',
|
||||
baseType: 'roadmap'
|
||||
}, { engine: engineMock });
|
||||
layersCollection.reset([gmapsBaseLayer]);
|
||||
|
||||
var actual = LayersSerializer.serialize(layersCollection);
|
||||
var expected = [];
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,60 @@
|
||||
var Backbone = require('backbone');
|
||||
var TileLayer = require('../../../../../src/geo/map/tile-layer');
|
||||
var CartoDBLayer = require('../../../../../src/geo/map/cartodb-layer');
|
||||
var TorqueLayer = require('../../../../../src/geo/map/torque-layer');
|
||||
var GMapsBaseLayer = require('../../../../../src/geo/map/gmaps-base-layer');
|
||||
var NamedMapSerializer = require('../../../../../src/windshaft/map-serializer/named-map-serializer/named-map-serializer');
|
||||
|
||||
describe('named-map-serializer', function () {
|
||||
var engineMock;
|
||||
describe('.serialize', function () {
|
||||
beforeEach(function () {
|
||||
engineMock = new Backbone.Model();
|
||||
this.layersCollection = new Backbone.Collection([]);
|
||||
this.dataviewsCollection = new Backbone.Collection([]);
|
||||
});
|
||||
|
||||
it('should include the buffersize', function () {
|
||||
var payload = NamedMapSerializer.serialize(this.layersCollection, this.dataviewsCollection);
|
||||
expect(payload.buffersize).toEqual({
|
||||
mvt: 0
|
||||
});
|
||||
});
|
||||
|
||||
it('should send styles of CartoDB and Torque layers using the right indexes', function () {
|
||||
this.layersCollection.reset([
|
||||
new TileLayer({}, { engine: engineMock }),
|
||||
new CartoDBLayer({ cartocss: 'cartoCSS1' }, { engine: engineMock }),
|
||||
new TorqueLayer({ cartocss: 'cartoCSS2' }, { engine: engineMock })
|
||||
]);
|
||||
|
||||
var payload = NamedMapSerializer.serialize(this.layersCollection, this.dataviewsCollection);
|
||||
|
||||
// TileLayer doesn't have cartoCSS but Maps API is aware
|
||||
// of them so they're taken into account when calculating
|
||||
// indexes for CartoDB and Torque layers
|
||||
expect(payload.styles).toEqual({
|
||||
1: 'cartoCSS1',
|
||||
2: 'cartoCSS2'
|
||||
});
|
||||
});
|
||||
|
||||
it('should ignore GMapsBase layers when calculating indexes', function () {
|
||||
this.layersCollection.reset([
|
||||
new GMapsBaseLayer({}, { engine: engineMock }),
|
||||
new CartoDBLayer({ cartocss: 'cartoCSS1' }, { engine: engineMock }),
|
||||
new TorqueLayer({ cartocss: 'cartoCSS2' }, { engine: engineMock })
|
||||
]);
|
||||
|
||||
var payload = NamedMapSerializer.serialize(this.layersCollection, this.dataviewsCollection);
|
||||
|
||||
// GMapsBaseLayer doesn't have cartoCSS and Maps API is
|
||||
// NOT aware of them so they're NOT taken into account
|
||||
// when calculating indexes for CartoDB and Torque layers
|
||||
expect(payload.styles).toEqual({
|
||||
0: 'cartoCSS1',
|
||||
1: 'cartoCSS2'
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,42 @@
|
||||
var RequestTracker = require('../../../src/windshaft/request-tracker');
|
||||
var Request = require('../../../src/windshaft/request');
|
||||
|
||||
describe('windshaft/request-tracker', function () {
|
||||
var MAX_NUMBER_OF_REQUESTS = 3;
|
||||
var requestTracker;
|
||||
var requestMock = new Request('payloadMock', 'paramsMock');
|
||||
|
||||
beforeEach(function () {
|
||||
requestTracker = new RequestTracker(MAX_NUMBER_OF_REQUESTS);
|
||||
});
|
||||
|
||||
describe('.canRequestBePerformed', function () {
|
||||
it('should allow "MAX_NUMBER_OF_REQUESTS" equal requests when the response is the same', function () {
|
||||
expect(requestTracker.canRequestBePerformed(requestMock)).toEqual(true);
|
||||
requestTracker.track(requestMock, 'responseMock');
|
||||
expect(requestTracker.canRequestBePerformed(requestMock)).toEqual(true);
|
||||
requestTracker.track(requestMock, 'responseMock');
|
||||
expect(requestTracker.canRequestBePerformed(requestMock)).toEqual(true);
|
||||
requestTracker.track(requestMock, 'responseMock');
|
||||
expect(requestTracker.canRequestBePerformed(requestMock)).toEqual(false);
|
||||
});
|
||||
|
||||
it('should allow more than "MAX_NUMBER_OF_REQUESTS" equal requests when the response is different', function () {
|
||||
// Reach the request limit (3)
|
||||
requestTracker.track(requestMock, 'responseMock');
|
||||
requestTracker.track(requestMock, 'responseMock1');
|
||||
requestTracker.track(requestMock, 'responseMock2');
|
||||
expect(requestTracker.canRequestBePerformed(requestMock)).toEqual(true);
|
||||
});
|
||||
|
||||
it('should allow more than "MAX_NUMBER_OF_REQUESTS" when the requests are different ', function () {
|
||||
// Reach the request limit (3)
|
||||
requestTracker.track(requestMock, 'responseMock');
|
||||
requestTracker.track(requestMock, 'responseMock');
|
||||
requestTracker.track(requestMock, 'responseMock');
|
||||
// Perform a different request
|
||||
var requestMock1 = new Request('payloadMock1', 'paramsMock1');
|
||||
expect(requestTracker.canRequestBePerformed(requestMock1)).toEqual(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,60 @@
|
||||
module.exports = {
|
||||
'layergroupid': '2edba0a73a790c4afb83222183782123:1508164637676',
|
||||
'metadata': {
|
||||
'layers': [
|
||||
{
|
||||
'type': 'mapnik',
|
||||
'id': '6d21d64e-8216-4645-9301-d57f991c02a3',
|
||||
'meta': {
|
||||
'cartocss': '#layer {\nmarker-color: red;\n}',
|
||||
'stats': {
|
||||
'estimatedFeatureCount': 10031
|
||||
},
|
||||
'cartocss_meta': {
|
||||
'rules': []
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
'dataviews': {},
|
||||
'analyses': [
|
||||
{
|
||||
'nodes': {
|
||||
'a1': {
|
||||
'status': 'ready',
|
||||
'url': {
|
||||
'http': 'http://3.ashbu.cartocdn.com/iago-carto/api/v1/map/2edba0a73a790c4afb83222183782123:1508164637676/analysis/node/dc2b9be10f88baa5b0156f2530f9b978b47a2a23',
|
||||
'https': 'https://cartocdn-ashbu_d.global.ssl.fastly.net/iago-carto/api/v1/map/2edba0a73a790c4afb83222183782123:1508164637676/analysis/node/dc2b9be10f88baa5b0156f2530f9b978b47a2a23'
|
||||
},
|
||||
'query': 'SELECT * FROM table'
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
'cdn_url': {
|
||||
'templates': {
|
||||
'http': {
|
||||
'url': 'http://{s}.ashbu.cartocdn.com',
|
||||
'subdomains': [
|
||||
'0',
|
||||
'1',
|
||||
'2',
|
||||
'3'
|
||||
]
|
||||
},
|
||||
'https': {
|
||||
'url': 'https://cartocdn-ashbu_{s}.global.ssl.fastly.net',
|
||||
'subdomains': [
|
||||
'a',
|
||||
'b',
|
||||
'c',
|
||||
'd'
|
||||
]
|
||||
}
|
||||
},
|
||||
'http': 'ashbu.cartocdn.com',
|
||||
'https': 'cartocdn-ashbu.global.ssl.fastly.net'
|
||||
},
|
||||
'last_updated': '2017-10-16T14:37:17.676Z'
|
||||
};
|
||||
@@ -0,0 +1,371 @@
|
||||
var Response = require('../../../src/windshaft/response');
|
||||
|
||||
describe('windshaft-response', function () {
|
||||
describe('.getLayerMetadata', function () {
|
||||
it('should return the metadata given an index', function () {
|
||||
var windshaftSettings = {};
|
||||
var serverResponse = {
|
||||
layergroupid: 'layergroupid',
|
||||
metadata: {
|
||||
layers: [
|
||||
{
|
||||
'type': 'mapnik',
|
||||
'meta': 'cartodb-metadata',
|
||||
'widgets': {
|
||||
'dataviewId': {
|
||||
'url': {
|
||||
'http': 'http://example.com',
|
||||
'https': 'https://example.com'
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
'type': 'torque',
|
||||
'meta': 'torque-metadata'
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
var response = new Response(windshaftSettings, serverResponse);
|
||||
|
||||
expect(response.getLayerMetadata(0)).toEqual('cartodb-metadata');
|
||||
expect(response.getLayerMetadata(1)).toEqual('torque-metadata');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.getBaseURL', function () {
|
||||
it("should return Windshaft's url if no CDN info is present", function () {
|
||||
var windshaftSettings = {
|
||||
urlTemplate: 'https://{user}.carto.com',
|
||||
userName: 'cartojs-test'
|
||||
};
|
||||
var serverResponse = {
|
||||
layergroupid: '0123456789',
|
||||
metadata: {}
|
||||
};
|
||||
var response = new Response(windshaftSettings, serverResponse);
|
||||
|
||||
expect(response.getBaseURL()).toEqual('https://cartojs-test.carto.com/api/v1/map/0123456789');
|
||||
});
|
||||
|
||||
it('should return the CDN URL for HTTP when CDN info is present', function () {
|
||||
var windshaftSettings = {
|
||||
urlTemplate: 'http://{user}.carto.com',
|
||||
userName: 'cartojs-test'
|
||||
};
|
||||
var serverResponse = {
|
||||
layergroupid: '0123456789',
|
||||
cdn_url: {
|
||||
http: 'cdn.http.example.com',
|
||||
https: 'cdn.https.example.com'
|
||||
},
|
||||
metadata: {}
|
||||
};
|
||||
var response = new Response(windshaftSettings, serverResponse);
|
||||
|
||||
expect(response.getBaseURL()).toEqual('http://cdn.http.example.com/cartojs-test/api/v1/map/0123456789');
|
||||
});
|
||||
|
||||
it('should return the CDN URL for HTTPS when CDN info is present', function () {
|
||||
var windshaftSettings = {
|
||||
urlTemplate: 'https://{user}.carto.com',
|
||||
userName: 'cartojs-test'
|
||||
};
|
||||
var serverResponse = {
|
||||
layergroupid: '0123456789',
|
||||
cdn_url: {
|
||||
http: 'cdn.http.example.com',
|
||||
https: 'cdn.https.example.com'
|
||||
},
|
||||
metadata: {}
|
||||
};
|
||||
var response = new Response(windshaftSettings, serverResponse);
|
||||
|
||||
expect(response.getBaseURL()).toEqual('https://cdn.https.example.com/cartojs-test/api/v1/map/0123456789');
|
||||
});
|
||||
|
||||
it('should use the CDN template', function () {
|
||||
var windshaftSettings = {
|
||||
urlTemplate: 'https://{user}.carto.com',
|
||||
userName: 'cartojs-test'
|
||||
};
|
||||
var serverResponse = {
|
||||
layergroupid: '0123456789',
|
||||
cdn_url: {
|
||||
http: 'cdn1.http.example.com',
|
||||
https: 'cdn1.https.example.com',
|
||||
templates: {
|
||||
http: {
|
||||
url: 'http://{s}.cdn2.http.example.com',
|
||||
subdomains: ['0', '1', '2']
|
||||
},
|
||||
https: {
|
||||
url: 'https://{s}.cdn2.https.example.com',
|
||||
subdomains: ['0', '1', '2']
|
||||
}
|
||||
}
|
||||
},
|
||||
metadata: {}
|
||||
};
|
||||
var response = new Response(windshaftSettings, serverResponse);
|
||||
|
||||
var expected = 'https://{s}.cdn2.https.example.com/cartojs-test/api/v1/map/0123456789';
|
||||
var actual = response.getBaseURL();
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.getDataviewMetadata', function () {
|
||||
it('should return undefined if dataviews key is not present in the metadata', function () {
|
||||
var windshaftSettings = {
|
||||
urlTemplate: 'https://{user}.carto.com',
|
||||
userName: 'cartojs-test'
|
||||
};
|
||||
var serverResponse = {
|
||||
'layergroupid': '0123456789',
|
||||
'metadata': {
|
||||
'layers': [
|
||||
{
|
||||
'type': 'mapnik',
|
||||
'meta': {}
|
||||
},
|
||||
{
|
||||
'type': 'torque',
|
||||
'meta': {}
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
var response = new Response(windshaftSettings, serverResponse);
|
||||
|
||||
var dataviewMetadata = response.getDataviewMetadata('whatever');
|
||||
expect(dataviewMetadata).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return the URL for the given dataviewId when metadata is under dataview', function () {
|
||||
var windshaftSettings = {
|
||||
urlTemplate: 'https://{user}.carto.com',
|
||||
userName: 'cartojs-test'
|
||||
};
|
||||
var serverResponse = {
|
||||
layergroupid: '0123456789',
|
||||
metadata: {
|
||||
layers: [
|
||||
{
|
||||
type: 'mapnik',
|
||||
meta: {}
|
||||
},
|
||||
{
|
||||
type: 'torque',
|
||||
meta: {}
|
||||
}
|
||||
],
|
||||
dataviews: {
|
||||
dataviewId: {
|
||||
url: {
|
||||
http: 'http://example.com',
|
||||
https: 'https://example.com'
|
||||
}
|
||||
},
|
||||
dataviewId2: {
|
||||
url: {
|
||||
http: 'http://example2.com',
|
||||
https: 'https://example2.com'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
var response = new Response(windshaftSettings, serverResponse);
|
||||
|
||||
var dataviewMetadata = response.getDataviewMetadata('dataviewId');
|
||||
expect(dataviewMetadata).toEqual({
|
||||
'url': {
|
||||
'http': 'http://example.com',
|
||||
'https': 'https://example.com'
|
||||
}
|
||||
});
|
||||
|
||||
dataviewMetadata = response.getDataviewMetadata('dataviewId2');
|
||||
expect(dataviewMetadata).toEqual({
|
||||
'url': {
|
||||
'http': 'http://example2.com',
|
||||
'https': 'https://example2.com'
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should return the URL for the given dataviewId when metadata is under layers', function () {
|
||||
var windshaftSettings = {
|
||||
urlTemplate: 'https://{user}.carto.com',
|
||||
userName: 'cartojs-test'
|
||||
};
|
||||
var serverResponse = {
|
||||
'layergroupid': 'observatory@2168bf86@a71df05e422879e95930bbcb932a9b0a:1460751254653',
|
||||
'metadata': {
|
||||
'layers': [
|
||||
{
|
||||
'type': 'http',
|
||||
'meta': {
|
||||
'stats': [],
|
||||
'cartocss': {}
|
||||
}
|
||||
},
|
||||
{
|
||||
'type': 'mapnik',
|
||||
'meta': {
|
||||
'stats': [],
|
||||
'cartocss': '/** choropleth visualization */\n\n\n@1 : #5C308C;\n@2 : #833599;\n@3 : #A640A2;\n@4 : #C753A8; \n@5 : #e76cac;\n@6 : #ff8db1 ;\n@7 : #ffc2c7 ;\n\n\n#5C308C,#833599,#A640A2,#C753A8,#E76CAC,#FF8DB1,#FFC2C7\n\n\n#segregated_tracts{\n polygon-fill: #FFFFB2;\n polygon-opacity: 0.8;\n}\n#segregated_tracts [ prob_being_same <= 1] {\n polygon-fill: @7;\n line-color: lighten(@7,5);\n}\n#segregated_tracts [ prob_being_same <= 0.900302811812] {\n polygon-fill: @6;\n line-color: lighten(@6,5);\n}\n#segregated_tracts [ prob_being_same <= 0.807340578641] {\n polygon-fill: @5;\n line-color: lighten(@5,5);\n}\n#segregated_tracts [ prob_being_same <= 0.69976803713] {\n polygon-fill: @4;\n line-color: lighten(@4,5);\n}\n#segregated_tracts [ prob_being_same <= 0.588361334051] {\n polygon-fill: @3;\n line-color: lighten(@3,5);\n}\n#segregated_tracts [ prob_being_same <= 0.487706534839] {\n polygon-fill: @2;\n line-color: lighten(@2,5);\n}\n#segregated_tracts [ prob_being_same <= 0.399914994417] {\n polygon-fill: @1;\n line-color: lighten(@1,5);\n}'
|
||||
},
|
||||
'widgets': {
|
||||
'dataviewId': {
|
||||
'url': {
|
||||
'http': 'http://example.com',
|
||||
'https': 'https://example.com'
|
||||
}
|
||||
},
|
||||
'dataviewId2': {
|
||||
'url': {
|
||||
'http': 'http://example2.com',
|
||||
'https': 'https://example2.com'
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
'type': 'mapnik',
|
||||
'meta': {
|
||||
'stats': [],
|
||||
'cartocss': '/** simple visualization */\n\n#us_census_tiger2013_state{\n polygon-fill: #FF6600;\n polygon-opacity: 0;\n line-color: #FFF;\n line-width: 1;\n line-opacity: 1;\n}'
|
||||
}
|
||||
},
|
||||
{
|
||||
'type': 'mapnik',
|
||||
'meta': {
|
||||
'stats': [],
|
||||
'cartocss': '/** simple visualization */\n\n#detailed_water{\n polygon-fill: #CDD2D4;\n polygon-opacity: 1;\n line-color: #CDD2D4;\n line-width: 0.5;\n line-opacity: 1;\n}'
|
||||
}
|
||||
},
|
||||
{
|
||||
'type': 'http',
|
||||
'meta': {
|
||||
'stats': [],
|
||||
'cartocss': {}
|
||||
}
|
||||
}
|
||||
],
|
||||
'dataviews': {},
|
||||
'analyses': []
|
||||
},
|
||||
'cdn_url': {
|
||||
'http': 'ashbu.cartocdn.com',
|
||||
'https': 'cartocdn-ashbu.global.ssl.fastly.net'
|
||||
},
|
||||
'last_updated': '2016-04-15T20:14:14.653Z'
|
||||
};
|
||||
var response = new Response(windshaftSettings, serverResponse);
|
||||
|
||||
var dataviewMetadata = response.getDataviewMetadata('dataviewId');
|
||||
expect(dataviewMetadata).toEqual({
|
||||
'url': {
|
||||
'http': 'http://example.com',
|
||||
'https': 'https://example.com'
|
||||
}
|
||||
});
|
||||
|
||||
dataviewMetadata = response.getDataviewMetadata('dataviewId2');
|
||||
expect(dataviewMetadata).toEqual({
|
||||
'url': {
|
||||
'http': 'http://example2.com',
|
||||
'https': 'https://example2.com'
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('.getAnalysesMedatada', function () {
|
||||
it('should return undefined when there are no analyses', function () {
|
||||
var windshaftSettings = {};
|
||||
var serverResponse = {
|
||||
'metadata': {
|
||||
'layers': [],
|
||||
'dataviews': {},
|
||||
'analyses': []
|
||||
}
|
||||
};
|
||||
|
||||
var response = new Response(windshaftSettings, serverResponse);
|
||||
|
||||
var analysisMetadata = response.getAnalysisNodeMetadata('fakeId');
|
||||
expect(analysisMetadata).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return the analysis metadata when exists', function () {
|
||||
var windshaftSettings = {};
|
||||
var serverResponse = {
|
||||
'metadata': {
|
||||
'layers': [],
|
||||
'dataviews': {},
|
||||
'analyses': [
|
||||
{
|
||||
'nodes': {
|
||||
'a0': {
|
||||
'status': 'ready',
|
||||
'url': {
|
||||
'http': 'http://3.ashbu.cartocdn.com/iago-carto/api/v1/map/2edba0a73a790c4afb83222183782123:1508164637676/analysis/node/dc2b9be10f88baa5b0156f2530f9b978b47a2a23',
|
||||
'https': 'https://cartocdn-ashbu_d.global.ssl.fastly.net/iago-carto/api/v1/map/2edba0a73a790c4afb83222183782123:1508164637676/analysis/node/dc2b9be10f88baa5b0156f2530f9b978b47a2a23'
|
||||
},
|
||||
'query': 'SELECT * FROM table'
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
var response = new Response(windshaftSettings, serverResponse);
|
||||
var analysisMetadata = response.getAnalysisNodeMetadata('a0');
|
||||
|
||||
expect(analysisMetadata).toBeDefined();
|
||||
expect(analysisMetadata.status).toEqual('ready');
|
||||
expect(analysisMetadata.query).toEqual('SELECT * FROM table');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.getSupportedSubdomains', function () {
|
||||
var response;
|
||||
beforeEach(function () {
|
||||
var windshaftSettings = {
|
||||
urlTemplate: 'https://{user}.carto.com',
|
||||
userName: 'cartojs-test'
|
||||
};
|
||||
var serverResponse = {
|
||||
layergroupid: '0123456789',
|
||||
cdn_url: {
|
||||
templates: {
|
||||
http: {
|
||||
url: 'http://cdn.carto.com',
|
||||
subdomains: ['a', 'b']
|
||||
},
|
||||
https: {
|
||||
url: 'http://cdn.carto.com',
|
||||
subdomains: ['c', 'd']
|
||||
}
|
||||
}
|
||||
},
|
||||
metadata: {}
|
||||
};
|
||||
response = new Response(windshaftSettings, serverResponse);
|
||||
});
|
||||
|
||||
it('should return supported subdomains if urlTemplate uses HTTP', function () {
|
||||
response._windshaftSettings.urlTemplate = 'http://{username}.carto.com';
|
||||
expect(response.getSupportedSubdomains()).toEqual(['a', 'b']);
|
||||
});
|
||||
|
||||
it('should return not subdomains if urlTemplate uses HTTPS', function () {
|
||||
response._windshaftSettings.urlTemplate = 'https://{username}.carto.com';
|
||||
expect(response.getSupportedSubdomains()).toEqual(['c', 'd']);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user