CartoDB-SQL-API/test/acceptance/pagination-test.js

184 lines
6.7 KiB
JavaScript
Raw Normal View History

2018-10-24 21:42:33 +08:00
'use strict';
2016-12-09 17:38:35 +08:00
require('../helper');
var server = require('../../lib/server')();
2016-12-09 17:38:35 +08:00
var assert = require('../support/assert');
var querystring = require('querystring');
var step = require('step');
2019-12-24 01:19:08 +08:00
describe('results-pagination', function () {
2016-12-09 17:38:35 +08:00
var RESPONSE_OK = {
statusCode: 200
};
// Test for https://github.com/Vizzuality/CartoDB-SQL-API/issues/85
2019-12-24 01:19:08 +08:00
it("paging doesn't break x-cache-channel", function (done) {
2016-12-09 17:38:35 +08:00
assert.response(server, {
url: '/api/v1/sql?' + querystring.stringify({
// note: select casing intentionally mixed
2019-12-24 01:19:08 +08:00
q: 'selECT cartodb_id*3 FROM untitle_table_4',
api_key: '1234',
rows_per_page: 1,
page: 2
2016-12-09 17:38:35 +08:00
}),
2019-12-24 01:19:08 +08:00
headers: { host: 'vizzuality.cartodb.com' },
2016-12-09 17:38:35 +08:00
method: 'GET'
2019-12-24 01:19:08 +08:00
}, RESPONSE_OK, function (err, res) {
2019-12-26 23:10:41 +08:00
assert.ifError(err);
2019-12-26 21:01:18 +08:00
assert.strictEqual(res.headers['x-cache-channel'], 'cartodb_test_user_1_db:public.untitle_table_4');
2016-12-09 17:38:35 +08:00
var parsed = JSON.parse(res.body);
2019-12-26 21:01:18 +08:00
assert.strictEqual(parsed.rows.length, 1);
2016-12-09 17:38:35 +08:00
done();
});
});
// Test page and rows_per_page params
2019-12-24 01:19:08 +08:00
it('paging', function (done) {
2016-12-09 17:38:35 +08:00
var sql = 'SELECT * FROM (VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9)) t(v)';
2019-12-24 01:19:08 +08:00
var pr = [[2, 3], [0, 4]]; // page and rows
var methods = ['GET', 'POST'];
2016-12-09 17:38:35 +08:00
var authorized = 0;
var testing = 0;
var method = 0;
2019-12-24 01:19:08 +08:00
var testNext = function () {
if (testing >= pr.length) {
if (method + 1 >= methods.length) {
if (authorized) {
done();
return;
} else {
authorized = 1;
method = 0;
testing = 0;
}
} else {
testing = 0;
++method;
}
2016-12-09 17:38:35 +08:00
}
2019-12-24 01:19:08 +08:00
var prcur = pr[testing++];
var page = prcur[0];
var nrows = prcur[1];
2019-12-26 23:10:41 +08:00
var dataObj = {
2016-12-09 17:38:35 +08:00
q: sql,
rows_per_page: nrows,
page: page
};
2019-12-24 01:19:08 +08:00
if (authorized) {
2019-12-26 23:10:41 +08:00
dataObj.api_key = '1234';
2016-12-09 17:38:35 +08:00
}
2019-12-26 23:10:41 +08:00
var data = querystring.stringify(dataObj);
2019-12-24 01:19:08 +08:00
var req = {
url: '/api/v1/sql',
headers: { host: 'vizzuality.cartodb.com' }
};
if (methods[method] === 'GET') {
req.method = 'GET';
req.url += '?' + data;
} else {
req.method = 'POST';
req.headers['Content-Type'] = 'application/x-www-form-urlencoded';
req.data = data;
}
assert.response(server, req, RESPONSE_OK, function (err, res) {
2019-12-26 23:10:41 +08:00
assert.ifError(err);
2019-12-24 01:19:08 +08:00
var parsed = JSON.parse(res.body);
2019-12-26 21:01:18 +08:00
assert.strictEqual(parsed.rows.length, nrows);
2019-12-24 01:19:08 +08:00
for (var i = 0; i < nrows; ++i) {
var obt = parsed.rows[i].v;
var exp = page * nrows + i + 1;
2019-12-26 21:01:18 +08:00
assert.strictEqual(obt, exp, 'Value ' + i + ' in page ' + page + ' is ' + obt + ', expected ' + exp);
2019-12-24 01:19:08 +08:00
}
testNext();
});
2016-12-09 17:38:35 +08:00
};
testNext();
});
// Test paging with WITH queries
2019-12-24 01:19:08 +08:00
it('paging starting with comment', function (done) {
var sql = '-- this is a comment\n' +
'SELECT * FROM (VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9)) t(v)';
2016-12-09 17:38:35 +08:00
var nrows = 3;
var page = 2;
assert.response(server, {
url: '/api/v1/sql?' + querystring.stringify({
2019-12-24 01:19:08 +08:00
q: sql,
rows_per_page: nrows,
page: page
2016-12-09 17:38:35 +08:00
}),
2019-12-24 01:19:08 +08:00
headers: { host: 'vizzuality.cartodb.com' },
2016-12-09 17:38:35 +08:00
method: 'GET'
2019-12-24 01:19:08 +08:00
}, RESPONSE_OK, function (err, res) {
2019-12-26 23:10:41 +08:00
assert.ifError(err);
2016-12-09 17:38:35 +08:00
var parsed = JSON.parse(res.body);
2019-12-26 21:01:18 +08:00
assert.strictEqual(parsed.rows.length, 3);
2019-12-24 01:19:08 +08:00
for (var i = 0; i < nrows; ++i) {
2016-12-09 17:38:35 +08:00
var obt = parsed.rows[i].v;
var exp = page * nrows + i + 1;
2019-12-26 21:01:18 +08:00
assert.strictEqual(obt, exp, 'Value ' + i + ' in page ' + page + ' is ' + obt + ', expected ' + exp);
2016-12-09 17:38:35 +08:00
}
done();
});
});
// See http://github.com/CartoDB/CartoDB-SQL-API/issues/127
2019-12-24 01:19:08 +08:00
it('SELECT INTO with paging', function (done) {
2019-12-26 23:10:41 +08:00
var escTabname = 'test ""select into""'; // escaped ident
2016-12-09 17:38:35 +08:00
step(
2019-12-26 23:10:41 +08:00
function selectInto () {
2019-12-24 01:19:08 +08:00
var next = this;
assert.response(server, {
url: '/api/v1/sql?' + querystring.stringify({
2019-12-26 23:10:41 +08:00
q: 'SELECT generate_series(1,10) InTO "' + escTabname + '"',
2019-12-24 01:19:08 +08:00
rows_per_page: 1,
page: 1,
api_key: 1234
}),
headers: { host: 'vizzuality.cartodb.com' },
method: 'GET'
2019-12-26 23:10:41 +08:00
}, RESPONSE_OK, function (err, res) {
assert.ifError(err);
next(null, res);
});
2019-12-24 01:19:08 +08:00
},
2019-12-26 23:10:41 +08:00
function checkResTestFakeInto1 (err) {
2019-12-24 01:19:08 +08:00
assert.ifError(err);
var next = this;
assert.response(server, {
url: '/api/v1/sql?' + querystring.stringify({
2019-12-26 23:10:41 +08:00
q: 'SELECT \' INTO "c"\' FROM "' + escTabname + '"',
2019-12-24 01:19:08 +08:00
rows_per_page: 1,
page: 1,
api_key: 1234
}),
headers: { host: 'vizzuality.cartodb.com' },
method: 'GET'
2019-12-26 23:10:41 +08:00
}, RESPONSE_OK, function (err, res) {
assert.ifError(err);
next(null, res);
});
2019-12-24 01:19:08 +08:00
},
2019-12-26 23:10:41 +08:00
function checkResDropTable (err, res) {
2019-12-24 01:19:08 +08:00
assert.ifError(err);
var out = JSON.parse(res.body);
2019-12-26 21:01:18 +08:00
assert.strictEqual(out.total_rows, 1); // windowing works
2019-12-24 01:19:08 +08:00
var next = this;
assert.response(server, {
url: '/api/v1/sql?' + querystring.stringify({
2019-12-26 23:10:41 +08:00
q: 'DROP TABLE "' + escTabname + '"',
2019-12-24 01:19:08 +08:00
api_key: 1234
}),
headers: { host: 'vizzuality.cartodb.com' },
method: 'GET'
2019-12-26 23:10:41 +08:00
}, RESPONSE_OK, function (err, res) {
assert.ifError(err);
next(null, res);
});
2019-12-24 01:19:08 +08:00
},
done
2016-12-09 17:38:35 +08:00
);
});
});