2018-05-04 00:32:39 +08:00
|
|
|
require('../helper');
|
|
|
|
|
|
|
|
const fs = require('fs');
|
2018-05-04 00:46:16 +08:00
|
|
|
const querystring = require('querystring');
|
2018-05-04 00:32:39 +08:00
|
|
|
const assert = require('../support/assert');
|
2018-05-22 17:54:10 +08:00
|
|
|
const os = require('os');
|
2018-05-04 00:32:39 +08:00
|
|
|
|
2018-05-22 17:54:10 +08:00
|
|
|
const StatsClient = require('../../app/stats/client');
|
|
|
|
if (global.settings.statsd) {
|
|
|
|
// Perform keyword substitution in statsd
|
|
|
|
if (global.settings.statsd.prefix) {
|
|
|
|
const hostToken = os.hostname().split('.').reverse().join('.');
|
|
|
|
global.settings.statsd.prefix = global.settings.statsd.prefix.replace(/:host/, hostToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const statsClient = StatsClient.getInstance(global.settings.statsd);
|
|
|
|
const server = require('../../app/server')(statsClient);
|
|
|
|
|
|
|
|
|
2018-05-22 17:57:10 +08:00
|
|
|
describe('copy-endpoints', function() {
|
2018-05-11 16:38:04 +08:00
|
|
|
it('should work with copyfrom endpoint', function(done){
|
2018-05-04 00:32:39 +08:00
|
|
|
assert.response(server, {
|
2018-05-11 03:11:55 +08:00
|
|
|
url: "/api/v1/sql/copyfrom?" + querystring.stringify({
|
2018-05-22 21:42:57 +08:00
|
|
|
q: "COPY copy_endpoints_test (id, name) FROM STDIN WITH (FORMAT CSV, DELIMITER ',', HEADER true)"
|
2018-05-11 16:38:04 +08:00
|
|
|
}),
|
2018-05-11 03:11:55 +08:00
|
|
|
data: fs.createReadStream(__dirname + '/../support/csv/copy_test_table.csv'),
|
2018-05-04 00:32:39 +08:00
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'POST'
|
|
|
|
},{}, function(err, res) {
|
|
|
|
assert.ifError(err);
|
|
|
|
const response = JSON.parse(res.body);
|
|
|
|
assert.equal(!!response.time, true);
|
|
|
|
assert.strictEqual(response.total_rows, 6);
|
2018-05-22 01:13:44 +08:00
|
|
|
|
|
|
|
assert.ok(res.headers['x-sqlapi-profiler']);
|
|
|
|
const headers = JSON.parse(res.headers['x-sqlapi-profiler']);
|
|
|
|
assert.ok(headers.copyFrom);
|
|
|
|
const metrics = headers.copyFrom;
|
|
|
|
assert.equal(metrics.size, 57);
|
|
|
|
assert.equal(metrics.format, 'CSV');
|
|
|
|
assert.equal(metrics.time, response.time);
|
|
|
|
assert.equal(metrics.total_rows, response.total_rows);
|
|
|
|
assert.equal(metrics.gzip, false);
|
|
|
|
|
2018-05-04 00:32:39 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-05-04 00:46:16 +08:00
|
|
|
|
2018-05-04 22:47:02 +08:00
|
|
|
it('should fail with copyfrom endpoint and unexisting table', function(done){
|
|
|
|
assert.response(server, {
|
2018-05-11 03:11:55 +08:00
|
|
|
url: "/api/v1/sql/copyfrom?" + querystring.stringify({
|
2018-05-22 21:42:57 +08:00
|
|
|
q: "COPY unexisting_table (id, name) FROM STDIN WITH (FORMAT CSV, DELIMITER ',', HEADER true)"
|
2018-05-11 16:38:04 +08:00
|
|
|
}),
|
2018-05-11 03:11:55 +08:00
|
|
|
data: fs.createReadStream(__dirname + '/../support/csv/copy_test_table.csv'),
|
2018-05-04 22:47:02 +08:00
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'POST'
|
|
|
|
},{}, function(err, res) {
|
|
|
|
assert.ifError(err);
|
|
|
|
assert.deepEqual(
|
|
|
|
JSON.parse(res.body),
|
|
|
|
{
|
|
|
|
error:['relation \"unexisting_table\" does not exist']
|
|
|
|
}
|
|
|
|
);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail with copyfrom endpoint and without csv', function(done){
|
|
|
|
assert.response(server, {
|
2018-05-11 03:11:55 +08:00
|
|
|
url: "/api/v1/sql/copyfrom?" + querystring.stringify({
|
2018-05-22 21:42:57 +08:00
|
|
|
q: "COPY copy_endpoints_test (id, name) FROM STDIN WITH (FORMAT CSV, DELIMITER ',', HEADER true)"
|
2018-05-11 16:38:04 +08:00
|
|
|
}),
|
2018-05-04 22:47:02 +08:00
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'POST'
|
|
|
|
},{}, function(err, res) {
|
|
|
|
assert.ifError(err);
|
|
|
|
assert.deepEqual(
|
|
|
|
JSON.parse(res.body),
|
|
|
|
{
|
2018-05-11 16:38:04 +08:00
|
|
|
error:['No rows copied']
|
2018-05-04 22:47:02 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail with copyfrom endpoint and without sql', function(done){
|
|
|
|
assert.response(server, {
|
2018-05-11 16:38:04 +08:00
|
|
|
url: "/api/v1/sql/copyfrom",
|
2018-05-11 03:11:55 +08:00
|
|
|
data: fs.createReadStream(__dirname + '/../support/csv/copy_test_table.csv'),
|
2018-05-04 22:47:02 +08:00
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'POST'
|
|
|
|
},{}, function(err, res) {
|
|
|
|
assert.ifError(err);
|
|
|
|
assert.deepEqual(
|
|
|
|
JSON.parse(res.body),
|
|
|
|
{
|
2018-05-22 21:42:57 +08:00
|
|
|
error:["SQL is missing"]
|
2018-05-04 22:47:02 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-11 16:38:04 +08:00
|
|
|
it('should work with copyto endpoint', function(done){
|
2018-05-04 00:46:16 +08:00
|
|
|
assert.response(server, {
|
|
|
|
url: "/api/v1/sql/copyto?" + querystring.stringify({
|
2018-05-22 21:42:57 +08:00
|
|
|
q: 'COPY copy_endpoints_test TO STDOUT',
|
2018-05-04 00:46:16 +08:00
|
|
|
filename: '/tmp/output.dmp'
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{}, function(err, res) {
|
|
|
|
assert.ifError(err);
|
|
|
|
assert.strictEqual(
|
|
|
|
res.body,
|
|
|
|
'11\tPaul\t10\n12\tPeter\t10\n13\tMatthew\t10\n14\t\\N\t10\n15\tJames\t10\n16\tJohn\t10\n'
|
|
|
|
);
|
2018-05-22 17:54:10 +08:00
|
|
|
|
|
|
|
assert.equal(res.headers['content-disposition'], 'attachment; filename=%2Ftmp%2Foutput.dmp');
|
|
|
|
assert.equal(res.headers['content-type'], 'application/octet-stream');
|
|
|
|
|
2018-05-04 00:46:16 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-11 19:33:54 +08:00
|
|
|
it('should work with copyfrom and gzip', function(done){
|
|
|
|
assert.response(server, {
|
|
|
|
url: "/api/v1/sql/copyfrom?" + querystring.stringify({
|
2018-05-22 21:42:57 +08:00
|
|
|
q: "COPY copy_endpoints_test2 (id, name) FROM STDIN WITH (FORMAT CSV, DELIMITER ',', HEADER true)"
|
2018-05-11 19:33:54 +08:00
|
|
|
}),
|
|
|
|
data: fs.createReadStream(__dirname + '/../support/csv/copy_test_table.csv.gz'),
|
|
|
|
headers: {
|
|
|
|
host: 'vizzuality.cartodb.com',
|
|
|
|
'content-encoding': 'gzip'
|
|
|
|
},
|
|
|
|
method: 'POST'
|
|
|
|
},{}, function(err, res) {
|
|
|
|
assert.ifError(err);
|
|
|
|
const response = JSON.parse(res.body);
|
|
|
|
assert.equal(!!response.time, true);
|
|
|
|
assert.strictEqual(response.total_rows, 6);
|
2018-05-22 01:13:44 +08:00
|
|
|
|
|
|
|
assert.ok(res.headers['x-sqlapi-profiler']);
|
|
|
|
const headers = JSON.parse(res.headers['x-sqlapi-profiler']);
|
|
|
|
assert.ok(headers.copyFrom);
|
|
|
|
const metrics = headers.copyFrom;
|
|
|
|
assert.equal(metrics.size, 57);
|
|
|
|
assert.equal(metrics.format, 'CSV');
|
|
|
|
assert.equal(metrics.time, response.time);
|
|
|
|
assert.equal(metrics.total_rows, response.total_rows);
|
|
|
|
assert.equal(metrics.gzip, true);
|
|
|
|
|
2018-05-11 19:33:54 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-04 00:32:39 +08:00
|
|
|
});
|