2019-02-26 22:37:03 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
require('../helper');
|
|
|
|
|
2019-10-04 00:24:39 +08:00
|
|
|
const server = require('../../lib/server')();
|
2019-02-26 22:37:03 +08:00
|
|
|
const assert = require('../support/assert');
|
|
|
|
const qs = require('querystring');
|
2019-02-26 23:22:39 +08:00
|
|
|
const BatchTestClient = require('../support/batch-test-client');
|
2019-10-04 00:24:39 +08:00
|
|
|
const JobStatus = require('../../lib/batch/job-status');
|
2019-02-26 22:37:03 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
const QUERY = 'SELECT 14 as foo';
|
2019-02-26 22:37:03 +08:00
|
|
|
const API_KEY = 1234;
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
describe('Handle query middleware', function () {
|
|
|
|
describe('regular queries endpoint', function () {
|
2019-02-26 23:22:39 +08:00
|
|
|
['GET', 'POST'].forEach(method => {
|
2019-12-24 01:19:08 +08:00
|
|
|
it(`${method} without query fails`, function (done) {
|
2019-02-26 23:22:39 +08:00
|
|
|
assert.response(server,
|
|
|
|
{
|
|
|
|
method,
|
|
|
|
url: '/api/v1/sql?' + qs.stringify({
|
|
|
|
api_key: API_KEY
|
|
|
|
}),
|
|
|
|
headers: {
|
|
|
|
host: 'vizzuality.cartodb.com'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ statusCode: 400 },
|
2019-12-24 01:19:08 +08:00
|
|
|
function (err, res) {
|
2019-02-26 23:22:39 +08:00
|
|
|
assert.ok(!err);
|
|
|
|
|
|
|
|
const response = JSON.parse(res.body);
|
2019-12-24 01:19:08 +08:00
|
|
|
assert.deepEqual(response, { error: ['You must indicate a sql query'] });
|
2019-02-26 23:22:39 +08:00
|
|
|
|
|
|
|
return done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it(`${method} query`, function (done) {
|
2019-02-26 23:22:39 +08:00
|
|
|
assert.response(server,
|
|
|
|
{
|
|
|
|
method,
|
|
|
|
url: '/api/v1/sql?' + qs.stringify({
|
|
|
|
q: QUERY,
|
|
|
|
api_key: API_KEY
|
|
|
|
}),
|
|
|
|
headers: {
|
|
|
|
host: 'vizzuality.cartodb.com'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ statusCode: 200 },
|
2019-12-24 01:19:08 +08:00
|
|
|
function (err, res) {
|
2019-02-26 23:22:39 +08:00
|
|
|
assert.ok(!err);
|
|
|
|
|
|
|
|
const response = JSON.parse(res.body);
|
|
|
|
assert.equal(response.rows.length, 1);
|
|
|
|
assert.equal(response.rows[0].foo, 14);
|
|
|
|
|
|
|
|
return done();
|
2019-02-26 22:43:14 +08:00
|
|
|
}
|
2019-02-26 23:22:39 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
describe('batch api queries', function () {
|
|
|
|
before(function () {
|
2019-02-26 23:22:39 +08:00
|
|
|
this.batchTestClient = new BatchTestClient();
|
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
after(function (done) {
|
2019-02-26 23:22:39 +08:00
|
|
|
this.batchTestClient.drain(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('one query', function (done) {
|
|
|
|
var payload = { query: QUERY };
|
2019-12-24 01:19:08 +08:00
|
|
|
this.batchTestClient.createJob(payload, function (err, jobResult) {
|
2019-02-26 23:22:39 +08:00
|
|
|
assert.ok(!err);
|
|
|
|
|
|
|
|
jobResult.getStatus(function (err, job) {
|
2019-02-26 22:43:14 +08:00
|
|
|
assert.ok(!err);
|
|
|
|
|
2019-02-26 23:22:39 +08:00
|
|
|
assert.equal(job.status, JobStatus.DONE);
|
|
|
|
assert.equal(job.query, QUERY);
|
2019-02-26 22:43:14 +08:00
|
|
|
|
2019-02-26 23:22:39 +08:00
|
|
|
return done();
|
|
|
|
});
|
|
|
|
});
|
2019-02-26 22:43:14 +08:00
|
|
|
});
|
|
|
|
|
2019-02-26 23:22:39 +08:00
|
|
|
it('multiquery job with two queries', function (done) {
|
|
|
|
var payload = { query: [QUERY, QUERY] };
|
2019-12-24 01:19:08 +08:00
|
|
|
this.batchTestClient.createJob(payload, function (err, jobResult) {
|
2019-02-26 23:22:39 +08:00
|
|
|
assert.ok(!err);
|
|
|
|
|
|
|
|
jobResult.getStatus(function (err, job) {
|
2019-02-26 22:37:03 +08:00
|
|
|
assert.ok(!err);
|
|
|
|
|
2019-02-26 23:22:39 +08:00
|
|
|
assert.equal(job.status, JobStatus.DONE);
|
|
|
|
assert.deepEqual(job.query, [
|
|
|
|
{ query: QUERY, status: JobStatus.DONE },
|
2019-12-24 01:19:08 +08:00
|
|
|
{ query: QUERY, status: JobStatus.DONE }
|
2019-02-26 23:22:39 +08:00
|
|
|
]);
|
2019-02-26 22:37:03 +08:00
|
|
|
|
2019-02-26 23:22:39 +08:00
|
|
|
return done();
|
|
|
|
});
|
|
|
|
});
|
2019-02-26 22:37:03 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|