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

112 lines
3.5 KiB
JavaScript
Raw Normal View History

'use strict';
require('../helper');
const server = require('../../app/server')();
const assert = require('../support/assert');
const qs = require('querystring');
2019-02-26 23:22:39 +08:00
const BatchTestClient = require('../support/batch-test-client');
const JobStatus = require('../../batch/job_status');
const QUERY = `SELECT 14 as foo`;
const API_KEY = 1234;
2019-02-26 23:29:07 +08:00
describe('Handle query middleware', function() {
2019-02-26 23:22:39 +08:00
describe('regular queries endpoint', function() {
['GET', 'POST'].forEach(method => {
it(`${method} without query fails`, function(done) {
assert.response(server,
{
method,
url: '/api/v1/sql?' + qs.stringify({
api_key: API_KEY
}),
headers: {
host: 'vizzuality.cartodb.com'
}
},
{ statusCode: 400 },
function(err, res) {
assert.ok(!err);
const response = JSON.parse(res.body);
assert.deepEqual(response, { error: ["You must indicate a sql query"] });
return done();
}
);
});
it(`${method} query`, function(done) {
assert.response(server,
{
method,
url: '/api/v1/sql?' + qs.stringify({
q: QUERY,
api_key: API_KEY
}),
headers: {
host: 'vizzuality.cartodb.com'
}
},
{ statusCode: 200 },
function(err, res) {
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
);
});
});
});
describe('batch api queries', function() {
before(function() {
this.batchTestClient = new BatchTestClient();
});
after(function(done) {
this.batchTestClient.drain(done);
});
it('one query', function (done) {
var payload = { query: QUERY };
this.batchTestClient.createJob(payload, function(err, jobResult) {
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] };
this.batchTestClient.createJob(payload, function(err, jobResult) {
assert.ok(!err);
jobResult.getStatus(function (err, job) {
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 },
{ query: QUERY, status: JobStatus.DONE },
]);
2019-02-26 23:22:39 +08:00
return done();
});
});
});
});
});