CartoDB-SQL-API/test/acceptance/batch/batch-multiquery-test.js

236 lines
6.6 KiB
JavaScript
Raw Normal View History

2016-10-19 02:18:49 +08:00
'use strict';
require('../../helper');
var BatchTestClient = require('../../support/batch-test-client');
var JobStatus = require('../../../lib/batch/job-status');
2016-10-19 02:18:49 +08:00
var assert = require('../../support/assert');
var queue = require('queue-async');
2019-12-24 01:19:08 +08:00
describe('batch multiquery', function () {
function jobPayload (query) {
2016-10-19 02:18:49 +08:00
return {
query: query
};
}
2019-12-24 01:19:08 +08:00
before(function () {
2016-10-19 02:18:49 +08:00
this.batchTestClient = new BatchTestClient();
});
after(function (done) {
this.batchTestClient.drain(done);
});
it('should perform one multiquery job with two queries', function (done) {
var queries = [
'select pg_sleep(0)',
'select pg_sleep(0)'
];
var payload = jobPayload(queries);
2019-12-24 01:19:08 +08:00
this.batchTestClient.createJob(payload, function (err, jobResult) {
2016-10-19 02:18:49 +08:00
if (err) {
return done(err);
}
jobResult.getStatus(function (err, job) {
if (err) {
return done(err);
}
assert.equal(job.status, JobStatus.DONE);
return done();
});
});
});
it('should perform one multiquery job with two queries and fail on last one', function (done) {
var queries = [
'select pg_sleep(0)',
'select shouldFail()'
];
var payload = jobPayload(queries);
2019-12-24 01:19:08 +08:00
this.batchTestClient.createJob(payload, function (err, jobResult) {
2016-10-19 02:18:49 +08:00
if (err) {
return done(err);
}
jobResult.getStatus(function (err, job) {
if (err) {
return done(err);
}
assert.equal(job.status, JobStatus.FAILED);
return done();
});
});
});
it('should perform one multiquery job with three queries and fail on last one', function (done) {
var queries = [
'select pg_sleep(0)',
'select pg_sleep(0)',
'select shouldFail()'
];
var payload = jobPayload(queries);
2019-12-24 01:19:08 +08:00
this.batchTestClient.createJob(payload, function (err, jobResult) {
2016-10-19 02:18:49 +08:00
if (err) {
return done(err);
}
jobResult.getStatus(function (err, job) {
if (err) {
return done(err);
}
assert.equal(job.status, JobStatus.FAILED);
return done();
});
});
});
it('should perform one multiquery job with three queries and fail on second one', function (done) {
var queries = [
'select pg_sleep(0)',
'select shouldFail()',
'select pg_sleep(0)'
];
var payload = jobPayload(queries);
2019-12-24 01:19:08 +08:00
this.batchTestClient.createJob(payload, function (err, jobResult) {
2016-10-19 02:18:49 +08:00
if (err) {
return done(err);
}
jobResult.getStatus(function (err, job) {
if (err) {
return done(err);
}
assert.equal(job.status, JobStatus.FAILED);
return done();
});
});
});
2016-11-24 23:45:00 +08:00
it('should perform two multiquery job with two queries for each one', function (done) {
2016-10-19 02:18:49 +08:00
var self = this;
var jobs = [
[
'select pg_sleep(0)',
'select pg_sleep(0)'
],
[
'select pg_sleep(0)',
'select pg_sleep(0)'
]
];
2016-11-24 23:53:15 +08:00
var jobsQueue = queue(1);
2016-10-19 02:18:49 +08:00
2019-12-24 01:19:08 +08:00
jobs.forEach(function (job) {
jobsQueue.defer(function (payload, done) {
self.batchTestClient.createJob(payload, function (err, jobResult) {
2016-10-19 02:18:49 +08:00
if (err) {
return done(err);
}
jobResult.getStatus(done);
});
}, jobPayload(job));
});
jobsQueue.awaitAll(function (err, jobsCreated) {
if (err) {
return done(err);
}
2019-12-24 01:19:08 +08:00
jobsCreated.forEach(function (job) {
2016-10-19 02:18:49 +08:00
assert.equal(job.status, JobStatus.DONE);
});
return done();
});
});
2016-11-24 23:45:00 +08:00
it('should perform two multiquery job with two queries for each one and fail the first one', function (done) {
2016-10-19 02:18:49 +08:00
var self = this;
var jobs = [
[
'select pg_sleep(0)',
'select shouldFail()'
],
[
'select pg_sleep(0)',
'select pg_sleep(0)'
]
];
var expectedStatus = [JobStatus.FAILED, JobStatus.DONE];
2016-11-24 23:53:15 +08:00
var jobsQueue = queue(1);
2016-10-19 02:18:49 +08:00
2019-12-24 01:19:08 +08:00
jobs.forEach(function (job) {
jobsQueue.defer(function (payload, done) {
self.batchTestClient.createJob(payload, function (err, jobResult) {
2016-10-19 02:18:49 +08:00
if (err) {
return done(err);
}
jobResult.getStatus(done);
});
}, jobPayload(job));
});
jobsQueue.awaitAll(function (err, jobsCreated) {
if (err) {
return done(err);
}
2019-12-24 01:19:08 +08:00
var statuses = jobsCreated.map(function (job) {
2016-10-19 02:18:49 +08:00
return job.status;
});
assert.deepEqual(statuses, expectedStatus);
return done();
});
});
2016-11-24 23:45:00 +08:00
it('should perform two multiquery job with two queries for each one and fail the second one', function (done) {
2016-10-19 02:18:49 +08:00
var self = this;
var jobs = [
[
'select pg_sleep(0)',
'select pg_sleep(0)'
],
[
'select pg_sleep(0)',
'select shouldFail()'
]
];
var expectedStatus = [JobStatus.DONE, JobStatus.FAILED];
2016-11-24 23:53:15 +08:00
var jobsQueue = queue(1);
2016-10-19 02:18:49 +08:00
2019-12-24 01:19:08 +08:00
jobs.forEach(function (job) {
jobsQueue.defer(function (payload, done) {
self.batchTestClient.createJob(payload, function (err, jobResult) {
2016-10-19 02:18:49 +08:00
if (err) {
return done(err);
}
jobResult.getStatus(done);
});
}, jobPayload(job));
});
jobsQueue.awaitAll(function (err, jobsCreated) {
if (err) {
return done(err);
}
2019-12-24 01:19:08 +08:00
var statuses = jobsCreated.map(function (job) {
2016-10-19 02:18:49 +08:00
return job.status;
});
assert.deepEqual(statuses, expectedStatus);
return done();
});
});
});