CartoDB-SQL-API/test/acceptance/surrogate-key-test.js

112 lines
4.4 KiB
JavaScript
Raw Normal View History

2018-10-24 21:42:33 +08:00
'use strict';
2016-02-24 18:44:27 +08:00
require('../helper');
var server = require('../../lib/server')();
2016-02-24 18:44:27 +08:00
var assert = require('../support/assert');
var querystring = require('querystring');
var QueryTables = require('cartodb-query-tables').queryTables;
2016-02-24 18:44:27 +08:00
var _ = require('underscore');
2019-12-24 01:19:08 +08:00
describe('Surrogate-Key header', function () {
function createGetRequest (sqlQuery) {
2016-02-24 18:44:27 +08:00
var query = querystring.stringify({
q: sqlQuery,
api_key: 1234
});
return {
url: '/api/v1/sql?' + query,
headers: {
host: 'vizzuality.cartodb.com'
},
method: 'GET'
};
}
var RESPONSE_OK = {
statusCode: 200
};
2019-12-24 01:19:08 +08:00
function surrogateKeyHasTables (surrogateKey, expectedTables) {
var surrogateKeys = surrogateKey.split(' ');
2016-02-24 18:44:27 +08:00
var expectedSurrogateKeys = new QueryTables.QueryMetadata(expectedTables).key();
2016-02-24 18:44:27 +08:00
2019-12-26 21:01:18 +08:00
assert.strictEqual(surrogateKeys.length, expectedSurrogateKeys.length);
2016-02-24 18:44:27 +08:00
var tablesDiff = _.difference(surrogateKeys, expectedSurrogateKeys);
2019-12-26 21:01:18 +08:00
assert.strictEqual(tablesDiff.length, 0, 'Surrogate-Key missing tables: ' + tablesDiff.join(','));
2016-02-24 18:44:27 +08:00
}
2019-12-24 01:19:08 +08:00
function tableNamesInSurrogateKeyHeader (expectedTableNames, done) {
return function (err, res) {
2019-12-26 21:51:09 +08:00
assert.ifError(err);
2016-02-24 18:44:27 +08:00
surrogateKeyHasTables(res.headers['surrogate-key'], expectedTableNames);
done();
};
}
2019-12-24 01:19:08 +08:00
it('supports joins', function (done) {
var sql = 'SELECT a.name as an, b.name as bn FROM untitle_table_4 a ' +
'left join private_table b ON (a.cartodb_id = b.cartodb_id)';
2016-02-24 18:44:27 +08:00
assert.response(server, createGetRequest(sql), RESPONSE_OK, tableNamesInSurrogateKeyHeader([
2019-12-24 01:19:08 +08:00
{ dbname: 'cartodb_test_user_1_db', schema_name: 'public', table_name: 'private_table' },
{ dbname: 'cartodb_test_user_1_db', schema_name: 'public', table_name: 'untitle_table_4' }
2016-02-24 18:44:27 +08:00
], done));
});
2019-12-24 01:19:08 +08:00
it('supports multistatements', function (done) {
var sql = 'SELECT * FROM untitle_table_4; SELECT * FROM private_table';
2016-02-24 18:44:27 +08:00
assert.response(server, createGetRequest(sql), RESPONSE_OK, tableNamesInSurrogateKeyHeader([
2019-12-24 01:19:08 +08:00
{ dbname: 'cartodb_test_user_1_db', schema_name: 'public', table_name: 'private_table' },
{ dbname: 'cartodb_test_user_1_db', schema_name: 'public', table_name: 'untitle_table_4' }
2016-02-24 18:44:27 +08:00
], done));
});
2018-10-24 21:42:33 +08:00
2019-12-24 01:19:08 +08:00
it('supports explicit transactions', function (done) {
var sql = 'BEGIN; SELECT * FROM untitle_table_4; COMMIT; BEGIN; SELECT * FROM private_table; COMMIT;';
2016-02-24 18:44:27 +08:00
assert.response(server, createGetRequest(sql), RESPONSE_OK, tableNamesInSurrogateKeyHeader([
2019-12-24 01:19:08 +08:00
{ dbname: 'cartodb_test_user_1_db', schema_name: 'public', table_name: 'private_table' },
{ dbname: 'cartodb_test_user_1_db', schema_name: 'public', table_name: 'untitle_table_4' }
2016-02-24 18:44:27 +08:00
], done));
});
2019-12-24 01:19:08 +08:00
it('survives partial transactions', function (done) {
var sql = 'BEGIN; SELECT * FROM untitle_table_4';
2016-02-24 18:44:27 +08:00
assert.response(server, createGetRequest(sql), RESPONSE_OK, tableNamesInSurrogateKeyHeader([
2019-12-24 01:19:08 +08:00
{ dbname: 'cartodb_test_user_1_db', schema_name: 'public', table_name: 'untitle_table_4' }
2016-02-24 18:44:27 +08:00
], done));
});
2019-12-24 01:19:08 +08:00
it('should not add header for functions', function (done) {
2016-02-24 18:44:27 +08:00
var sql = "SELECT format('%s', 'wadus')";
2019-12-24 01:19:08 +08:00
assert.response(server, createGetRequest(sql), RESPONSE_OK, function (err, res) {
2019-12-26 21:51:09 +08:00
assert.ifError(err);
assert.ok(!Object.prototype.hasOwnProperty.call(res.headers, 'surrogate-key'), res.headers['surrogate-key']);
2016-02-24 18:44:27 +08:00
done();
});
});
2019-12-24 01:19:08 +08:00
it('should not add header for CDB_QueryTables', function (done) {
2016-02-24 18:44:27 +08:00
var sql = "SELECT CDB_QueryTablesText('select * from untitle_table_4')";
2019-12-24 01:19:08 +08:00
assert.response(server, createGetRequest(sql), RESPONSE_OK, function (err, res) {
2019-12-26 21:51:09 +08:00
assert.ifError(err);
assert.ok(!Object.prototype.hasOwnProperty.call(res, 'surrogate-key'), res.headers['surrogate-key']);
2016-02-24 18:44:27 +08:00
done();
});
});
2019-12-24 01:19:08 +08:00
it('should not add header for non table results', function (done) {
2016-02-24 18:44:27 +08:00
var sql = "SELECT 'wadus'::text";
2019-12-24 01:19:08 +08:00
assert.response(server, createGetRequest(sql), RESPONSE_OK, function (err, res) {
2019-12-26 21:51:09 +08:00
assert.ifError(err);
assert.ok(!Object.prototype.hasOwnProperty.call(res.headers, 'surrogate-key'), res.headers['surrogate-key']);
2016-02-24 18:44:27 +08:00
done();
});
});
});