2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-12-09 18:26:45 +08:00
|
|
|
require('../helper');
|
|
|
|
|
2019-10-04 00:24:39 +08:00
|
|
|
var server = require('../../lib/server')();
|
2016-12-09 18:26:45 +08:00
|
|
|
var assert = require('../support/assert');
|
|
|
|
var querystring = require('querystring');
|
|
|
|
var _ = require('underscore');
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
describe('query-returning', function () {
|
2016-12-09 18:26:45 +08:00
|
|
|
var RESPONSE_OK = {
|
|
|
|
statusCode: 200
|
|
|
|
};
|
|
|
|
|
2019-12-26 21:38:53 +08:00
|
|
|
var expectedRwCacheControl = 'no-cache,max-age=0,must-revalidate,public';
|
2016-12-09 18:26:45 +08:00
|
|
|
|
|
|
|
// Check results from INSERT
|
|
|
|
//
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/13
|
2019-12-24 01:19:08 +08:00
|
|
|
it('INSERT returns affected rows', function (done) {
|
2016-12-09 18:26:45 +08:00
|
|
|
assert.response(server, {
|
|
|
|
// view prepare_db.sh to see where to set api_key
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql?api_key=1234&' + querystring.stringify({
|
|
|
|
q:
|
2016-12-09 18:26:45 +08:00
|
|
|
"INSERT INTO private_table(name) VALUES('noret1') UNION VALUES('noret2')"
|
|
|
|
}),
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.localhost.lan:8080' },
|
2016-12-09 18:26:45 +08:00
|
|
|
method: 'GET'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, RESPONSE_OK, function (err, res) {
|
2019-12-26 21:38:53 +08:00
|
|
|
assert.ifError(err);
|
2016-12-09 18:26:45 +08:00
|
|
|
var out = JSON.parse(res.body);
|
2019-12-26 21:38:53 +08:00
|
|
|
assert.ok(Object.prototype.hasOwnProperty.call(out, 'time'));
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(out.total_rows, 2);
|
|
|
|
assert.strictEqual(out.rows.length, 0);
|
2016-12-09 18:26:45 +08:00
|
|
|
// Check cache headers
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/43
|
2019-12-26 21:38:53 +08:00
|
|
|
assert.ok(!Object.prototype.hasOwnProperty.call(res, 'x-cache-channel'));
|
|
|
|
assert.strictEqual(res.headers['cache-control'], expectedRwCacheControl);
|
2016-12-09 18:26:45 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check results from UPDATE
|
|
|
|
//
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/13
|
2019-12-24 01:19:08 +08:00
|
|
|
it('UPDATE returns affected rows', function (done) {
|
2016-12-09 18:26:45 +08:00
|
|
|
assert.response(server, {
|
|
|
|
// view prepare_db.sh to see where to set api_key
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql?api_key=1234&' + querystring.stringify({
|
|
|
|
q:
|
2016-12-09 18:26:45 +08:00
|
|
|
"UPDATE private_table SET name = upper(name) WHERE name in ('noret1', 'noret2')"
|
|
|
|
}),
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.localhost.lan:8080' },
|
2016-12-09 18:26:45 +08:00
|
|
|
method: 'GET'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, RESPONSE_OK, function (err, res) {
|
2019-12-26 21:38:53 +08:00
|
|
|
assert.ifError(err);
|
2016-12-09 18:26:45 +08:00
|
|
|
var out = JSON.parse(res.body);
|
2019-12-26 21:38:53 +08:00
|
|
|
assert.ok(Object.prototype.hasOwnProperty.call(out, 'time'));
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(out.total_rows, 2);
|
|
|
|
assert.strictEqual(out.rows.length, 0);
|
2016-12-09 18:26:45 +08:00
|
|
|
// Check cache headers
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/43
|
2019-12-26 21:38:53 +08:00
|
|
|
assert.ok(!Object.prototype.hasOwnProperty.call(res, 'x-cache-channel'));
|
|
|
|
assert.strictEqual(res.headers['cache-control'], expectedRwCacheControl);
|
2016-12-09 18:26:45 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check results from DELETE
|
|
|
|
//
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/13
|
2019-12-24 01:19:08 +08:00
|
|
|
it('DELETE returns affected rows', function (done) {
|
2016-12-09 18:26:45 +08:00
|
|
|
assert.response(server, {
|
|
|
|
// view prepare_db.sh to see where to set api_key
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql?api_key=1234&' + querystring.stringify({
|
|
|
|
q:
|
2016-12-09 18:26:45 +08:00
|
|
|
"DELETE FROM private_table WHERE name in ('NORET1', 'NORET2')"
|
|
|
|
}),
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.localhost.lan:8080' },
|
2016-12-09 18:26:45 +08:00
|
|
|
method: 'GET'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, RESPONSE_OK, function (err, res) {
|
2019-12-26 21:38:53 +08:00
|
|
|
assert.ifError(err);
|
2016-12-09 18:26:45 +08:00
|
|
|
var out = JSON.parse(res.body);
|
2019-12-26 21:38:53 +08:00
|
|
|
assert.ok(Object.prototype.hasOwnProperty.call(out, 'time'));
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(out.total_rows, 2);
|
|
|
|
assert.strictEqual(out.rows.length, 0);
|
2016-12-09 18:26:45 +08:00
|
|
|
// Check cache headers
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/43
|
2019-12-26 21:38:53 +08:00
|
|
|
assert.ok(!Object.prototype.hasOwnProperty.call(res, 'x-cache-channel'));
|
|
|
|
assert.strictEqual(res.headers['cache-control'], expectedRwCacheControl);
|
2016-12-09 18:26:45 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check results from INSERT .. RETURNING
|
|
|
|
//
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/50
|
2019-12-24 01:19:08 +08:00
|
|
|
it('INSERT with RETURNING returns all results', function (done) {
|
2016-12-09 18:26:45 +08:00
|
|
|
assert.response(server, {
|
|
|
|
// view prepare_db.sh to see where to set api_key
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql?api_key=1234&' + querystring.stringify({
|
|
|
|
q:
|
2016-12-09 18:26:45 +08:00
|
|
|
"INSERT INTO private_table(name) VALUES('test') RETURNING upper(name), reverse(name)"
|
|
|
|
}),
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.localhost.lan:8080' },
|
2016-12-09 18:26:45 +08:00
|
|
|
method: 'GET'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, RESPONSE_OK, function (err, res) {
|
2019-12-26 21:38:53 +08:00
|
|
|
assert.ifError(err);
|
2016-12-09 18:26:45 +08:00
|
|
|
var out = JSON.parse(res.body);
|
2019-12-26 21:38:53 +08:00
|
|
|
assert.ok(Object.prototype.hasOwnProperty.call(out, 'time'));
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(out.total_rows, 1);
|
|
|
|
assert.strictEqual(out.rows.length, 1);
|
|
|
|
assert.strictEqual(_.keys(out.rows[0]).length, 2);
|
|
|
|
assert.strictEqual(out.rows[0].upper, 'TEST');
|
|
|
|
assert.strictEqual(out.rows[0].reverse, 'tset');
|
2016-12-09 18:26:45 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check results from UPDATE .. RETURNING
|
|
|
|
//
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/50
|
2019-12-24 01:19:08 +08:00
|
|
|
it('UPDATE with RETURNING returns all results', function (done) {
|
2016-12-09 18:26:45 +08:00
|
|
|
assert.response(server, {
|
|
|
|
// view prepare_db.sh to see where to set api_key
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql?api_key=1234&' + querystring.stringify({
|
|
|
|
q:
|
2016-12-09 18:26:45 +08:00
|
|
|
"UPDATE private_table SET name = 'tost' WHERE name = 'test' RETURNING upper(name), reverse(name)"
|
|
|
|
}),
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.localhost.lan:8080' },
|
2016-12-09 18:26:45 +08:00
|
|
|
method: 'GET'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, RESPONSE_OK, function (err, res) {
|
2019-12-26 21:38:53 +08:00
|
|
|
assert.ifError(err);
|
2016-12-09 18:26:45 +08:00
|
|
|
var out = JSON.parse(res.body);
|
2019-12-26 21:38:53 +08:00
|
|
|
assert.ok(Object.prototype.hasOwnProperty.call(out, 'time'));
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(out.total_rows, 1);
|
|
|
|
assert.strictEqual(out.rows.length, 1);
|
|
|
|
assert.strictEqual(_.keys(out.rows[0]).length, 2);
|
|
|
|
assert.strictEqual(out.rows[0].upper, 'TOST');
|
|
|
|
assert.strictEqual(out.rows[0].reverse, 'tsot');
|
2016-12-09 18:26:45 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check results from DELETE .. RETURNING
|
|
|
|
//
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/50
|
2019-12-24 01:19:08 +08:00
|
|
|
it('DELETE with RETURNING returns all results', function (done) {
|
2016-12-09 18:26:45 +08:00
|
|
|
assert.response(server, {
|
|
|
|
// view prepare_db.sh to see where to set api_key
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql?api_key=1234&' + querystring.stringify({
|
|
|
|
q:
|
2016-12-09 18:26:45 +08:00
|
|
|
"DELETE FROM private_table WHERE name = 'tost' RETURNING name"
|
|
|
|
}),
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.localhost.lan:8080' },
|
2016-12-09 18:26:45 +08:00
|
|
|
method: 'GET'
|
2019-12-24 01:19:08 +08:00
|
|
|
}, RESPONSE_OK, function (err, res) {
|
2019-12-26 21:38:53 +08:00
|
|
|
assert.ifError(err);
|
2016-12-09 18:26:45 +08:00
|
|
|
var out = JSON.parse(res.body);
|
|
|
|
assert.ok(out.hasOwnProperty('time'));
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(out.total_rows, 1);
|
|
|
|
assert.strictEqual(out.rows.length, 1);
|
|
|
|
assert.strictEqual(_.keys(out.rows[0]).length, 1);
|
|
|
|
assert.strictEqual(out.rows[0].name, 'tost');
|
2016-12-09 18:26:45 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|