This commit is contained in:
zhongjin
2020-06-15 12:07:54 +08:00
parent 610ed21a90
commit a96ef233c9
444 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,573 @@
describe('api.layers', function() {
describe('loadLayer leaflet', function() {
loadLayerSpecs(function() {
return L.map($('<div>')[0]).setView([0, 0], 3);
});
});
describe('loadLayer gmaps', function() {
loadLayerSpecs(function() {
return new google.maps.Map($('<div>')[0],{
zoom: 3,
center: new google.maps.LatLng(0, 0),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
});
});
describe('loadLayer unknow', function() {
it("should return an error for unknow map types", function(done) {
var map = {};
var err = false;
cartodb.createLayer(map, { kind: 'plain', options: {} }, function(l) {
layer = l;
}).error(function() {
err = true;
});
setTimeout(function() {
expect(err).toEqual(true);
done();
}, 1000);
})
});
//
// shared specs for each map
//
function loadLayerSpecs(mapFn) {
describe("(shared)", function() {
var map;
beforeEach(function() {
map = mapFn();
cartodb.torque = torque;
});
afterEach(function() {
delete cartodb.torque;
});
it("should fecth layer when user and pass are specified", function() {
spyOn(cdb.core.Loader, 'get');
cartodb.createLayer(map, {
user: 'development',
table: 'clubbing',
host: 'localhost.lan:3000',
protocol: 'http'
});
expect(cdb.core.Loader.get).toHaveBeenCalled();
});
it("should fecth layer when a url is specified", function() {
spyOn(cdb.core.Loader, 'get');
cartodb.createLayer(map, 'http://test.com/layer.json');
expect(cdb.core.Loader.get).toHaveBeenCalled();
});
it("should not fecth layer when kind and options are specified", function() {
spyOn(cdb.core.Loader, 'get');
cartodb.createLayer(map, { kind: 'plain', options: {} });
expect(cdb.core.Loader.get).not.toHaveBeenCalled();
});
it("should create a layer", function(done) {
var layer;
cartodb.createLayer(map, { kind: 'plain', options: {} }, function(l) {
layer = l;
});
setTimeout(function() {
expect(layer).not.toEqual(undefined);
expect(layer.type).toEqual('plain');
done();
}, 100);
});
it("should create a layer with type", function(done) {
var layer;
cartodb.createLayer(map, { kind: 'cartodb', options: { tile_style: 'test', table_name: 'table', user_name: 'test'} }, function(l) {
layer = l;
});
setTimeout(function() {
expect(layer.type).toEqual('cartodb');
done();
}, 100);
});
it("should create a layer with options", function(done) {
var layer;
cartodb.createLayer(map, { kind: 'cartodb', options: {tile_style: 'test', table_name: 'table', user_name: 'test'} }, {query: 'select test'}, function(l) {
layer = l;
});
setTimeout(function() {
expect(layer.options.query).toEqual('select test');
done();
}, 100);
});
it("should use https when https == true", function(done) {
var layer;
cartodb.createLayer(map, { kind: 'cartodb', options: {tile_style: 'test', table_name: 'table', user_name: 'test'} }, {https: true}, function(l) {
layer = l;
});
setTimeout(function() {
expect(layer._host().indexOf('https')).toEqual(0);
done();
}, 100);
});
it("should not use https when https == false", function(done) {
var layer;
cartodb.createLayer(map, { kind: 'cartodb', options: {tile_style: 'test', table_name: 'table', user_name: 'test'} }, {https: false}, function(l) {
layer = l;
});
setTimeout(function() {
expect(layer._host().indexOf('https')).toEqual(-1);
done();
}, 100);
});
it("should not substitute mapnik tokens", function(done) {
var layer;
cartodb.createLayer(map, { kind: 'cartodb', options: {tile_style: 'test', table_name: 'table', user_name: 'test'} }, {query: 'select !bbox!'}, function(l) {
layer = l
})
setTimeout(function() {
expect(layer.getQuery()).toEqual('select !bbox!');
done();
}, 100);
});
it("should manage errors", function(done) {
var s = sinon.spy();
cartodb.createLayer(map, { options: {} }).on('error', s);
setTimeout(function() {
expect(s.called).toEqual(true);
done();
}, 10);
});
it("should call callback if the last argument is a function", function(done) {
var layer;
var s = sinon.spy();
var s2 = sinon.spy();
cartodb.createLayer(map, { kind: 'plain', options: {} }, s);
cartodb.createLayer(map, layer={ kind: 'plain', options: {} }, { rambo: 'thebest'} ,s2);
setTimeout(function() {
expect(s.called).toEqual(true);
expect(layer.options.rambo).toEqual('thebest');
expect(s2.called).toEqual(true);
done();
}, 10);
});
it("should load vis.json", function(done) {
var layer;
var s = sinon.spy();
cartodb.createLayer(map, {
updated_at: 'jaja',
layers: [
{ type: 'tiled', options: {} },
{
type: 'layergroup',
options: {
layer_definition: {
layers: []
},
extra_params: { cache_buster: 'cb' }
}
}
]
}, s).done(function(lyr) {
layer = lyr;
});
setTimeout(function() {
expect(s.called).toEqual(true);
expect(layer.model.attributes.extra_params.cache_buster).toEqual('cb');
done();
}, 10);
});
it("should load specified layer", function(done) {
var layer;
var s = sinon.spy();
cartodb.createLayer(map, {
updated_at: 'jaja',
layers: [
null,
{kind: 'cartodb', options: { user_name: 'test', table_name: 'test', tile_style: 'test'}, infowindow: null },
{kind: 'torque', options: { user_name: 'test', table_name: 'test', tile_style: 'Map{ -torque-frame-count: 10; }#test { marker-width: 10; }'}, infowindow: null }
]
}, { layerIndex: 2 }, s).done(function(lyr) {
layer = lyr;
});
setTimeout(function() {
expect(s.called).toEqual(true);
// check it's a torque layer and not a cartodb one
expect(layer.model.get('type')).toEqual('torque');
done();
}, 500);
});
it("should load the `namedmap` layer by default", function(done) {
var layer;
cartodb.createLayer(map, {
updated_at: 'jaja',
layers: [
{ type: 'tiled', options: {} },
{ type: 'tiled', options: {} },
{
type: 'namedmap',
user_name: 'dev',
options: {
named_map: {
name: 'testing',
params: {
color: 'red'
}
}
}
}
]
}).done(function(lyr) {
layer = lyr;
});
setTimeout(function() {
expect(layer).toBeDefined();
expect(layer.options.type).toEqual('namedmap');
done();
}, 0);
});
it("should load the `layergroup` layer by default", function(done) {
var layer;
cartodb.createLayer(map, {
updated_at: 'jaja',
layers: [
{ type: 'tiled', options: {} },
{ type: 'tiled', options: {} },
{
type: 'layergroup',
options: {
layer_definition: {
layers: []
}
}
}
]
}).done(function(lyr) {
layer = lyr;
});
setTimeout(function() {
expect(layer).toBeDefined();
expect(layer.options.type).toEqual('layergroup');
done();
}, 0);
});
it("should load the `torque` layer by default", function(done) {
var layer;
cartodb.createLayer(map, {
updated_at: 'jaja',
layers: [
{ type: 'tiled', options: {} },
{ type: 'tiled', options: {} },
{
type: 'torque',
options: {
'torque-steps': 3
}
}
]
}).done(function(lyr) {
layer = lyr;
});
setTimeout(function() {
expect(layer).toBeDefined();
expect(layer.type).toEqual('torque');
done();
}, 0);
});
it("should add a torque layer", function(done) {
var layer;
var s = sinon.spy();
cartodb.createLayer(map, {
updated_at: 'jaja',
layers: [
null,
{kind: 'cartodb', options: { user_name: 'test', table_name: 'test', tile_style: 'test'}, infowindow: null },
{kind: 'torque', options: { user_name: 'test', table_name: 'test', tile_style: 'Map { -torque-frame-count: 10;} #test { marker-width: 10; }'}, infowindow: null }
]
}, { layerIndex: 2 }, s).done(function(lyr) {
layer = lyr;
}).addTo(map)
var wait = 500;
if (!map.getContainer) wait = 2500;
setTimeout(function() {
if (map.getContainer) expect($(map.getContainer()).find('.cartodb-timeslider').length).toBe(1)
if (map.getDiv) expect($(map.getDiv()).find('.cartodb-timeslider').length).toBe(1)
done()
}, wait);
});
it("should ask for https data when https is on at torque layer", function(done) {
var layer;
var s = sinon.spy();
cartodb.createLayer(map, {
updated_at: 'jaja',
layers: [
null,
{kind: 'cartodb', options: { user_name: 'test', table_name: 'test', tile_style: 'test'}, infowindow: null },
{kind: 'torque', options: { user_name: 'test', table_name: 'test', tile_style: 'Map { -torque-frame-count: 10;} #test { marker-width: 10; }'}, infowindow: null }
]
}, { layerIndex: 2, https: true }, s).done(function(lyr) {
layer = lyr;
}).addTo(map)
var wait = 500;
if (!map.getContainer) wait = 2500;
setTimeout(function() {
expect(layer.provider.options.tiler_protocol).toBe("https");
done()
}, wait);
});
it("should not add a torque layer timeslider if steps are not greater than 1", function(done) {
var layer;
var s = sinon.spy();
cartodb.createLayer(map, {
updated_at: 'jaja',
layers: [
null,
{kind: 'cartodb', options: { user_name: 'test', table_name: 'test', tile_style: 'test'}, infowindow: null },
{kind: 'torque', options: { user_name: 'test', table_name: 'test', tile_style: 'Map { -torque-frame-count: 1;} #test { marker-width: 10; }'}, infowindow: null }
]
}, { layerIndex: 2 }, s).done(function(lyr) {
layer = lyr;
}).addTo(map)
var wait = 500;
if (!map.getContainer) wait = 2500;
setTimeout(function() {
if (map.getContainer) expect($(map.getContainer()).find('.cartodb-timeslider').length).toBe(0)
if (map.getDiv) expect($(map.getDiv()).find('.cartodb-timeslider').length).toBe(0)
done()
}, wait);
});
it("should add cartodb logo with torque layer although it is not defined", function(done) {
var layer;
var s = sinon.spy();
cartodb.createLayer(map, {
updated_at: 'jaja',
layers: [
null,
{kind: 'cartodb', options: { user_name: 'test', table_name: 'test', tile_style: 'test'}, infowindow: null },
{kind: 'torque', options: { user_name: 'test', table_name: 'test', tile_style: 'Map{ -torque-frame-count: 10;}#test { marker-width: 10; }'}, infowindow: null }
]
}, { layerIndex: 2 }, s).done(function(lyr) {
layer = lyr;
}).addTo(map)
var wait = 500;
if (!map.getContainer) wait = 2500;
setTimeout(function() {
expect(layer.options.cartodb_logo).toEqual(undefined);
if (map.getContainer) expect($(map.getContainer()).find('.cartodb-logo').length).toBe(1)
if (map.getDiv) expect($(map.getDiv()).find('.cartodb-logo').length).toBe(1)
done();
}, wait);
});
it("should create a named map", function(done) {
var layer;
cartodb.createLayer(map, {
type: 'namedmap',
user_name: 'dev',
options: {
named_map: {
name: 'testing',
params: {
color: 'red'
}
}
}
}).done(function(lyr) {
layer = lyr;
});
setTimeout(function() {
expect(layer).not.toEqual(undefined);
expect(layer.toJSON()).toEqual({ color: 'red' });
done();
}, 100);
});
it("should use access_token", function(done) {
cartodb.createLayer(map, {
type: 'namedmap',
user_name: 'dev',
options: {
named_map: {
name: 'testing',
params: {
color: 'red'
}
}
}
}, { https: true, auth_token: 'at_rambo' }).done(function(layer) {
spyOn(layer, 'createMap').and.returnValue({
layergroupid: 'test',
metadata: {
layers: []
}
})
layer.getTiles(function(tiles) {
expect(tiles.tiles[0].indexOf("auth_token=at_rambo")).not.toEqual(-1);
});
done();
});
});
it("should create a layer from the list of sublayers", function(done) {
var layer;
cartodb.createLayer(map, {
type: 'cartodb',
sublayers: [{
sql: 'select * from table',
cartocss: 'test',
interactivity: 'testi'
}]
}).done(function(lyr) {
layer = lyr;
});
setTimeout(function() {
expect(layer).not.toEqual(undefined);
expect(layer.toJSON()).toEqual({
version: '1.3.0',
stat_tag: 'API',
layers: [{
type: 'cartodb',
options: {
sql: 'select * from table',
cartocss: 'test',
cartocss_version: '2.1.0',
interactivity: ['testi']
}
}]
});
done();
}, 100);
});
it("should return a promise that responds to addTo", function(done) {
var layer;
cartodb.createLayer(map, {
type: 'cartodb',
sublayers: [{
sql: 'select * from table',
cartocss: 'test',
interactivity: 'testi'
}]
})
.addTo(map)
.done(function(lyr) {
layer = lyr;
});
setTimeout(function() {
expect(layer).not.toEqual(undefined);
if(map.overlayMapTypes) {
expect(layer).toBe(map.overlayMapTypes.getAt(0));
} else {
expect(layer).toBe(map._layers[L.stamp(layer)]);
}
done();
}, 100);
});
it("should have several 'addTo' with zIndex set", function(done) {
var layer0, layer1;
cartodb.createLayer(map, {
type: 'cartodb',
sublayers: [{
sql: 'select * from table',
cartocss: 'test',
interactivity: 'testi'
}]
})
.addTo(map,0)
.done(function(lyr) {
layer0 = lyr;
});
cartodb.createLayer(map, {
type: 'cartodb',
sublayers: [{
sql: 'select * from table2',
cartocss: 'test2',
interactivity: 'testii'
}]
})
.addTo(map,1)
.done(function(lyr) {
layer1 = lyr;
});
setTimeout(function() {
//Test only for Leaflet
if(map.overlayMapTypes === undefined) {
expect(layer0).not.toEqual(undefined);
expect(layer0.options.zIndex).toEqual(0);
expect(layer1).not.toEqual(undefined);
expect(layer1.options.zIndex).toEqual(1);
}
done();
}, 100);
});
});
}
});

View File

@@ -0,0 +1,230 @@
describe('api.layers.cartodb', function() {
describe('loadLayer leaflet', function() {
loadLayerSpecs(function() {
return L.map($('<div>')[0]).setView([0, 0], 3);
}, function(map, layer) {
map.addLayer(layer);
});
});
describe('loadLayer gmaps', function() {
loadLayerSpecs(function() {
return new google.maps.Map($('<div>')[0],{
zoom: 3,
center: new google.maps.LatLng(0, 0),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
},
function(map, layer) {
map.overlayMapTypes.setAt(0, layer);
});
});
//
// shared specs for each map
//
function loadLayerSpecs(mapFn, addFn) {
var layer;
var map;
beforeEach(function() {
map = mapFn();
});
it("has all the needed methods", function(done) {
var methods = [
'show',
'hide',
'setInteraction',
'setQuery',
'setCartoCSS',
'isVisible',
'setInteractivity',
'setOpacity',
'setOptions'
];
cartodb.createLayer(map, { kind: 'cartodb', options: { table_name:'test', tile_style: 'test', user_name: 'test'} }, function(l) {
layer = l;
});
setTimeout(function() {
_.each(methods, function(m) {
expect(layer[m]).not.toEqual(undefined);
})
done();
}, 100);
});
function get_url_options(u) {
var o = u.split('?')[1].split('&');
var opts = {};
for(var i in o) {
var tk = o[i].split('=');
opts[tk[0]] = decodeURIComponent(tk[1]);
}
return opts;
}
it("should add a infowindow", function(done) {
//cdb.templates.add(new cdb.core.Template({
//name: 'test',
//}));
cartodb.createLayer(map, {
kind: 'cartodb',
options: {
table_name: 'test',
user_name: 'test',
tile_style: 'tesst'
},
infowindow: {
template: '<div></div>',
fields: [{name: 'test', title: true, order: 0}]
}
}, function(l) {
addFn(map, l);
layer = l;
});
setTimeout(function() {
expect(layer.infowindow).not.toEqual(undefined);
expect(layer.infowindow.get('fields').length).toEqual(1);
expect(layer.infowindow.get('fields')[0].name).toEqual('test');
expect(layer.options.interactivity).toEqual('cartodb_id');
done();
}, 100);
});
it("should expose the legend", function(done) {
var legend = {
type: "custom",
show_title: true,
title: "wadus",
template: "",
items: [
{
name: "item1",
visible: true,
value: "#FFCC00",
sync: true
},
{
name: "item2",
visible: true,
value: "#3B007F",
sync: true
}
]
};
cartodb.createLayer(map, {
kind: 'cartodb',
options: {
table_name: 'test',
user_name: 'test',
tile_style: 'tesst'
},
infowindow: {
template: '<div></div>',
fields: [{name: 'test', title: true, order: 0}]
},
legend: legend,
visible: true
}, function(l) {
addFn(map, l);
layer = l;
});
setTimeout(function() {
expect(layer.legend instanceof cdb.geo.ui.LegendModel).toBeTruthy();
expect(layer.legend.get('visible')).toBeTruthy();
expect(layer.legend.get('items')).toEqual(legend.items);
done();
}, 100);
});
it("should add interactivity if there is infowindow", function(done) {
cartodb.createLayer(map, {
kind: 'cartodb',
options: {
table_name: 'test',
user_name: 'test',
tile_style: 'test'
},
infowindow: {
template: '<div></div>',
fields: [{name: 'test', title: true, order: 0}]
}
}, {
interactivity: 'myname,jaja'
}, function(l) {
addFn(map, l);
layer = l;
});
setTimeout(function() {
expect(layer.infowindow).not.toEqual(undefined);
expect(layer.options.interactivity).toEqual('myname,jaja,cartodb_id');
done();
}, 100);
});
it("should not add interactivity when interaction is false", function(done) {
cartodb.createLayer(map, {
kind: 'cartodb',
options: {
table_name: 'test',
user_name: 'test',
tile_style: 'test'
},
infowindow: {
template: '<div></div>',
fields: [{name: 'test', title: true, order: 0}]
}
}, {
interactivity: 'myname,jaja',
interaction: false
}, function(l) {
addFn(map, l);
layer = l;
});
setTimeout(function() {
expect(layer.infowindow).not.toEqual(undefined);
expect(layer.options.interactivity).toEqual('myname,jaja,cartodb_id');
expect(layer.options.interaction).toEqual(false);
done();
}, 100);
});
it("should add to the map when done", function() {
cartodb.createLayer(map, {
kind: 'cartodb',
options: {
table_name: 'test',
user_name: 'test',
tile_style: 'test'
},
infowindow: {
template: '<div></div>',
fields: [{name: 'test', title: true, order: 0}]
}
}, {
interactivity: 'myname,jaja',
interaction: false
}, function(l) {
addFn(map, l);
layer = l;
});
});
};
});

View File

@@ -0,0 +1,429 @@
describe('SQL api client', function() {
var USER = 'rambo';
var TEST_DATA = { test: 'good' };
var sql;
var ajaxParams;
var throwError = false;
var jquery_ajax;
var ajax;
beforeEach(function() {
ajaxParams = null;
ajax = function(params) {
ajaxParams = params;
_.defer(function() {
if(!throwError && params.success) params.success(TEST_DATA, 200);
throwError && params.error && params.error({
responseText: JSON.stringify({
error: ['jaja']
})
});
});
}
sql = new cartodb.SQL({
user: USER,
protocol: 'https',
ajax: ajax
})
jquery_ajax = $.ajax;
});
afterEach(function() {
$.ajax = jquery_ajax;
});
it("should compile the url if not completeDomain passed", function() {
expect(sql._host()).toEqual('https://rambo.carto.com/api/v2/sql');
});
it("should compile the url if completeDomain passed", function() {
var sqlBis = new cartodb.SQL({
user: USER,
protocol: 'https',
completeDomain: 'http://troloroloro.com'
})
expect(sqlBis._host()).toEqual('http://troloroloro.com/api/v2/sql');
});
it("should execute a query", function() {
sql.execute('select * from table');
expect(ajaxParams.url).toEqual(
'https://' + USER + '.carto.com/api/v2/sql?q=' + encodeURIComponent('select * from table')
)
expect(ajaxParams.type).toEqual('get');
expect(ajaxParams.dataType).toEqual('json');
expect(ajaxParams.crossDomain).toEqual(true);
});
it("should parse template", function() {
sql.execute('select * from {{table}}', {
table: 'rambo'
})
expect(ajaxParams.url).toEqual(
'https://' + USER + '.carto.com/api/v2/sql?q=' + encodeURIComponent('select * from rambo')
)
});
it("should execute a long query", function() {
//Generating a giant query
var long_sql = []
var i = 2000;
while (--i) long_sql.push("10000");
var long_query = 'SELECT * ' + long_sql;
sql.execute(long_query);
expect(ajaxParams.url).toEqual(
'https://' + USER + '.carto.com/api/v2/sql'
)
expect(ajaxParams.data.q).toEqual(long_query);
expect(ajaxParams.type).toEqual('post');
expect(ajaxParams.dataType).toEqual('json');
expect(ajaxParams.crossDomain).toEqual(true);
});
it("should execute a long query with params", function() {
s = new cartodb.SQL({
user: 'rambo',
format: 'geojson',
protocol: 'http',
host: 'charlies.com',
api_key: 'testkey',
rambo: 'test',
ajax: ajax
})
//Generating a giant query
var long_sql = []
var i = 2000;
while (--i) long_sql.push("10000");
var long_query = 'SELECT * ' + long_sql;
s.execute(long_query, null, {
dp: 2
})
expect(ajaxParams.url.indexOf('http://')).not.toEqual(-1);
expect(ajaxParams.url.indexOf('rambo.charlies.com')).not.toEqual(-1);
//Check that we don't have params in the URI
expect(ajaxParams.url.indexOf('&format=geojson')).toEqual(-1);
expect(ajaxParams.url.indexOf('&api_key=testkey')).toEqual(-1);
expect(ajaxParams.url.indexOf('&dp=2')).toEqual(-1);
expect(ajaxParams.url.indexOf('&rambo')).toEqual(-1);
//Check that we have the params in the body
expect(ajaxParams.data.q).toEqual(long_query);
expect(ajaxParams.data.format).toEqual('geojson');
expect(ajaxParams.data.api_key).toEqual('testkey');
expect(ajaxParams.data.dp).toEqual(2);
expect(ajaxParams.rambo).toEqual('test');
});
it("should substitute mapnik tokens", function() {
sql.execute('select !pixel_width! as w, !pixel_height! as h, !bbox! as b from {{table}}', {
table: 't'
})
var earth_circumference = 40075017;
var tile_size = 256;
var srid = 3857;
var full_resolution = earth_circumference/tile_size;
var shift = earth_circumference / 2.0;
var pw = full_resolution;
var ph = pw;
var bbox = 'ST_MakeEnvelope(' + (-shift) + ',' + (-shift) + ','
+ shift + ',' + shift + ',' + srid + ')';
expect(ajaxParams.url).toEqual(
'https://' + USER + '.carto.com/api/v2/sql?q=' + encodeURIComponent(
'select ' + pw + ' as w, ' + ph + ' as h, '
+ bbox + ' as b from t')
)
});
it("should call promise", function(done) {
var data;
var data_callback;
sql.execute('select * from bla', function(data) { data_callback = data }).done(function(d) {
data = d;
});
setTimeout(function() {
expect(data).toEqual(TEST_DATA);
expect(data_callback).toEqual(TEST_DATA);
done()
}, 500); //Fix cartodb.js issue #336
});
it("should call promise on error", function(done) {
throwError = true;
var err = false;
sql.execute('select * from bla').error(function(d) {
err = true;
});
setTimeout(function() {
expect(err).toEqual(true);
done();
},10);
});
it("should include url params", function() {
s = new cartodb.SQL({
user: 'rambo',
format: 'geojson',
protocol: 'http',
host: 'charlies.com',
api_key: 'testkey',
rambo: 'test',
ajax: ajax
})
s.execute('select * from rambo', null, {
dp: 2
})
expect(ajaxParams.url.indexOf('http://')).not.toEqual(-1);
expect(ajaxParams.url.indexOf('rambo.charlies.com')).not.toEqual(-1);
expect(ajaxParams.url.indexOf('&format=geojson')).not.toEqual(-1);
expect(ajaxParams.url.indexOf('&api_key=testkey')).not.toEqual(-1);
expect(ajaxParams.url.indexOf('&dp=2')).not.toEqual(-1);
expect(ajaxParams.url.indexOf('&rambo')).toEqual(-1);
});
it("should include extra url params", function() {
s = new cartodb.SQL({
user: 'rambo',
format: 'geojson',
protocol: 'http',
host: 'charlies.com',
api_key: 'testkey',
rambo: 'test',
ajax: ajax,
extra_params: ['rambo']
})
s.execute('select * from rambo', null, {
dp: 2
})
expect(ajaxParams.url.indexOf('http://')).not.toEqual(-1);
expect(ajaxParams.url.indexOf('rambo.charlies.com')).not.toEqual(-1);
expect(ajaxParams.url.indexOf('&format=geojson')).not.toEqual(-1);
expect(ajaxParams.url.indexOf('&api_key=testkey')).not.toEqual(-1);
expect(ajaxParams.url.indexOf('&dp=2')).not.toEqual(-1);
expect(ajaxParams.url.indexOf('&rambo=test')).not.toEqual(-1);
s.execute('select * from rambo', null, {
dp: 2,
rambo: 'test2'
})
expect(ajaxParams.url.indexOf('&rambo=test2')).not.toEqual(-1);
});
it("should use jsonp if browser does not support cors", function() {
$.support.cors = false;
s = new cartodb.SQL({ user: 'jaja', ajax: ajax });
expect(s.options.jsonp).toEqual(true);
s.execute('select * from rambo', null, {
dp: 2,
jsonpCallback: 'test_callback',
cache: false
})
expect(ajaxParams.dataType).toEqual('jsonp');
expect(ajaxParams.crossDomain).toEqual(undefined);
expect(ajaxParams.jsonp).toEqual(undefined);
expect(ajaxParams.jsonpCallback).toEqual('test_callback');
expect(ajaxParams.cache).toEqual(false);
$.support.cors = true;
});
it("should get bounds for query", function() {
var sql = 'SELECT ST_XMin(ST_Extent(the_geom)) as minx,' +
' ST_YMin(ST_Extent(the_geom)) as miny,'+
' ST_XMax(ST_Extent(the_geom)) as maxx,' +
' ST_YMax(ST_Extent(the_geom)) as maxy' +
' from (select * from rambo where id=2) as subq';
s = new cartodb.SQL({ user: 'jaja', ajax: ajax });
s.getBounds('select * from rambo where id={{id}}', {id: 2});
expect(ajaxParams.url.indexOf(encodeURIComponent(sql))).not.toEqual(-1);
});
it("should get bounds for query with appostrophes", function() {
s = new cartodb.SQL({ user: 'jaja', ajax: ajax });
s.getBounds("select * from country where name={{ name }}", { name: "'Spain'"});
expect(ajaxParams.url.indexOf("%26amp%3B%2339%3B")).toEqual(-1);
});
});
describe('sql.table', function() {
var USER = 'rambo';
var sql;
beforeEach(function() {
ajaxParams = null;
sql = new cartodb.SQL({
user: USER,
protocol: 'https'
})
});
it("sql", function() {
var s = sql.table('test');
expect(s.sql()).toEqual('select * from test');
s.columns(['age', 'jeta'])
expect(s.sql()).toEqual('select age,jeta from test');
s.filter('age < 10')
expect(s.sql()).toEqual('select age,jeta from test where age < 10');
s.limit(15)
expect(s.sql()).toEqual('select age,jeta from test where age < 10 limit 15');
s.order_by('age')
expect(s.sql()).toEqual('select age,jeta from test where age < 10 limit 15 order by age');
})
});
describe("column descriptions", function(){
var USER = 'manolo';
var sql;
beforeAll(function(){
this.colDate = new Backbone.Model(JSON.parse('{"name":"object_postedtime","type":"date","geometry_type":"point","bbox":[[-28.92163128242129,-201.09375],[75.84516854027044,196.875]],"analyzed":true,"success":true,"stats":{"type":"date","start_time":"2015-02-19T15:13:16.000Z","end_time":"2015-02-22T04:34:05.000Z","range":220849000,"steps":1024,"null_ratio":0,"column":"object_postedtime"}}'));
this.colFloat = new Backbone.Model(JSON.parse('{"name":"asdfd","type":"number","geometry_type":"point"}'));
this.colString = new Backbone.Model(JSON.parse('{"name":"asdfd","type":"string","geometry_type":"point"}'));
this.colGeom = new Backbone.Model(JSON.parse('{"name":"asdfd","type":"geometry","geometry_type":"point"}'));
this.colBoolean = new Backbone.Model(JSON.parse('{"name":"asdfd","type":"boolean","geometry_type":"point"}'));
this.query = "SELECT * FROM whatevs";
sql = new cartodb.SQL({
user: USER,
protocol: 'https'
});
sql.execute = function(sql, callback){
callback({});
}
});
it("should deduct correct describe method", function(){
spyOn(sql, "describeDate");
sql.describe(this.query, this.colDate, {type: this.colDate.get("type")}, function(){});
expect(sql.describeDate).toHaveBeenCalled;
spyOn(sql, "describeFloat");
sql.describe(this.query, this.colFloat, {type: this.colFloat.get("type")}, function(){});
expect(sql.describeFloat).toHaveBeenCalled();
spyOn(sql, "describeString");
sql.describe(this.query, this.colString, {type: this.colString.get("type")}, function(){});
expect(sql.describeString).toHaveBeenCalled();
spyOn(sql, "describeGeom");
sql.describe(this.query, this.colGeom, {type: this.colGeom.get("type")}, function(){});
expect(sql.describeGeom).toHaveBeenCalled();
spyOn(sql, "describeBoolean");
sql.describe(this.query, this.colBoolean, {type: this.colBoolean.get("type")}, function(){});
expect(sql.describeBoolean).toHaveBeenCalled();
});
describe("string describer", function(){
var description;
beforeAll(function(done){
sql.execute = function(sql, callback){
var data = JSON.parse('{"rows":[{"uniq":462,"cnt":487,"null_count":1,"null_ratio":0.002053388090349076,"skew":0.043121149897330596,"array_agg":""}],"time":0.01,"fields":{"uniq":{"type":"number"},"cnt":{"type":"number"},"null_count":{"type":"number"},"null_ratio":{"type":"number"},"skew":{"type":"number"},"array_agg":{"type":"unknown(2287)"}},"total_rows":1}');
callback(data);
}
var callback = function(stuff){
description = stuff;
done();
}
sql.describeString(sql, this.colString, callback); // THE COLS DON'T MATCH!!!
});
it("should return correct properties", function(){
expect(description.hist.constructor).toEqual(Array); // Right now it's an empty array because JSON.parse doesn't like our way of notating histograms
expect(description.type).toEqual("string");
expect(typeof description.null_count).toEqual("number");
expect(typeof description.distinct).toEqual("number");
expect(typeof description.null_ratio).toEqual("number");
expect(typeof description.skew).toEqual("number");
expect(typeof description.weight).toEqual("number");
});
});
describe("geometry describer", function(){
var description;
beforeAll(function(done){
sql.execute = function(sql, callback){
var data = {"rows":[{"bbox": '{"type":"Polygon","coordinates":[[[-179.9284,-65.2446],[-179.9284,81.8962],[179.9698,81.8962],[179.9698,-65.2446],[-179.9284,-65.2446]]]}',"geometry_type":"ST_Point","clusterrate":0.20359746623640493,"density":0.105333307745705}],"time":0.035,"fields":{"bbox":{"type":"string"},"geometry_type":{"type":"string"},"clusterrate":{"type":"number"},"density":{"type":"number"}},"total_rows":1};
callback(data);
}
var callback = function(stuff){
description = stuff;
done();
}
sql.describeGeom(sql, this.colGeom, callback);
});
it("should return correct properties", function(){
expect(description.type).toEqual("geom");
expect(["ST_Point", "ST_Line", "ST_Polygon"].indexOf(description.geometry_type) > -1).toBe(true);
expect(description.bbox.constructor).toEqual(Array);
expect(typeof description.density).toEqual("number");
expect(typeof description.cluster_rate).toEqual("number");
})
});
describe("number describer", function(){
var description;
beforeAll(function(done){
sql.execute = function(sql, callback){
var data = JSON.parse('{"rows":[{"hist":"{\\"(1,empty,69368)\\",\\"(25,empty,11063)\\"}","min":0,"max":4,"avg":0.3745819397993311,"cnt":89401,"uniq":5,"null_ratio":0,"stddev":0.000009057366328792043,"stddevmean":2.1617223091836313,"dist_type":"U","quantiles":[0,1,2,2,3,4,4],"equalint":[0,0,0,0,0,0,0],"jenks":[0,1,2,3,4],"headtails":[0,1,2,3,4],"cat_hist":"{\\"(1,empty,69368)\\",\\"(25,empty,11063)\\"}"}],"time":1.442,"fields":{"hist":{"type":"unknown(2287)"},"min":{"type":"number"},"max":{"type":"number"},"avg":{"type":"number"},"cnt":{"type":"number"},"uniq":{"type":"number"},"null_ratio":{"type":"number"},"stddev":{"type":"number"},"stddevmean":{"type":"number"},"dist_type":{"type":"string"},"quantiles":{"type":"number[]"},"equalint":{"type":"number[]"},"jenks":{"type":"number[]"},"headtails":{"type":"number[]"},"cat_hist":{"type":"unknown(2287)"}},"total_rows":1}');
callback(data);
}
var callback = function(stuff){
description = stuff;
done();
}
sql.describeFloat(sql, this.colGeom, callback);
});
it("should return correct properties", function(){
expect(description.type).toEqual("number");
expect(["A", "U", "F", "J"].indexOf(description.dist_type) > -1).toBe(true);
var numTypes = ["avg", "max", "min", "stddevmean", "weight", "stddev", "null_ratio", "count"];
for(var i = 0; i < numTypes.length; i++){
expect(typeof description[numTypes[i]]).toEqual("number");
}
var arrayTypes = ["quantiles", "equalint", "jenks", "headtails", "cat_hist", "hist"];
for(var i = 0; i < arrayTypes.length; i++){
expect(description[arrayTypes[i]].constructor).toEqual(Array);
}
})
});
describe("boolean describer", function(){
var description;
beforeAll(function(done){
sql.execute = function(sql, callback){
var data = {"rows":[
{"true_ratio":0.3377926421404682,"null_ratio":0,"uniq":2,"cnt":89401}
],
"time":0.251,
"fields":{"true_ratio":{"type":"number"},"null_ratio":{"type":"number"},"uniq":{"type":"number"},"cnt":{"type":"number"}},
"total_rows":1
};
callback(data);
}
var callback = function(stuff){
description = stuff;
done();
}
sql.describeBoolean(sql, this.colGeom, callback);
});
it("should return correct properties", function(){
expect(description.type).toEqual("boolean");
expect(typeof description.true_ratio).toEqual("number");
expect(typeof description.distinct).toEqual("number");
expect(typeof description.count).toEqual("number");
expect(typeof description.null_ratio).toEqual("number");
})
});
});