CartoDB-SQL-API/test/acceptance/query-returning-test.js

163 lines
6.4 KiB
JavaScript
Raw Normal View History

2018-10-24 21:42:33 +08:00
'use strict';
2016-12-09 18:26:45 +08:00
require('../helper');
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
};
var expected_rw_cache_control = 'no-cache,max-age=0,must-revalidate,public';
// 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) {
2016-12-09 18:26:45 +08:00
var out = JSON.parse(res.body);
assert.ok(out.hasOwnProperty('time'));
assert.equal(out.total_rows, 2);
assert.equal(out.rows.length, 0);
// Check cache headers
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/43
assert.ok(!res.hasOwnProperty('x-cache-channel'));
assert.equal(res.headers['cache-control'], expected_rw_cache_control);
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) {
2016-12-09 18:26:45 +08:00
var out = JSON.parse(res.body);
assert.ok(out.hasOwnProperty('time'));
assert.equal(out.total_rows, 2);
assert.equal(out.rows.length, 0);
// Check cache headers
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/43
assert.ok(!res.hasOwnProperty('x-cache-channel'));
assert.equal(res.headers['cache-control'], expected_rw_cache_control);
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) {
2016-12-09 18:26:45 +08:00
var out = JSON.parse(res.body);
assert.ok(out.hasOwnProperty('time'));
assert.equal(out.total_rows, 2);
assert.equal(out.rows.length, 0);
// Check cache headers
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/43
assert.ok(!res.hasOwnProperty('x-cache-channel'));
assert.equal(res.headers['cache-control'], expected_rw_cache_control);
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) {
2016-12-09 18:26:45 +08:00
var out = JSON.parse(res.body);
assert.ok(out.hasOwnProperty('time'));
assert.equal(out.total_rows, 1);
assert.equal(out.rows.length, 1);
assert.equal(_.keys(out.rows[0]).length, 2);
assert.equal(out.rows[0].upper, 'TEST');
assert.equal(out.rows[0].reverse, 'tset');
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) {
2016-12-09 18:26:45 +08:00
var out = JSON.parse(res.body);
assert.ok(out.hasOwnProperty('time'));
assert.equal(out.total_rows, 1);
assert.equal(out.rows.length, 1);
assert.equal(_.keys(out.rows[0]).length, 2);
assert.equal(out.rows[0].upper, 'TOST');
assert.equal(out.rows[0].reverse, 'tsot');
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) {
2016-12-09 18:26:45 +08:00
var out = JSON.parse(res.body);
assert.ok(out.hasOwnProperty('time'));
assert.equal(out.total_rows, 1);
assert.equal(out.rows.length, 1);
assert.equal(_.keys(out.rows[0]).length, 1);
assert.equal(out.rows[0].name, 'tost');
done();
});
});
});