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

199 lines
5.9 KiB
JavaScript
Raw Normal View History

2018-10-24 21:42:33 +08:00
'use strict';
require('../../helper');
2016-10-19 02:01:11 +08:00
var assert = require('../../support/assert');
2015-12-23 06:13:33 +08:00
var queue = require('queue-async');
2016-10-19 02:01:11 +08:00
var BatchTestClient = require('../../support/batch-test-client');
var JobStatus = require('../../../lib/batch/job-status');
2019-12-24 01:19:08 +08:00
describe('batch happy cases', function () {
before(function () {
2016-10-19 02:01:11 +08:00
this.batchTestClient = new BatchTestClient();
});
2019-12-24 01:19:08 +08:00
after(function (done) {
2016-10-19 02:01:11 +08:00
this.batchTestClient.drain(done);
});
2019-12-24 01:19:08 +08:00
function jobPayload (query) {
2016-10-19 02:01:11 +08:00
return {
query: query
2016-05-16 07:22:47 +08:00
};
}
it('should perform job with select', function (done) {
2016-10-19 02:01:11 +08:00
var payload = jobPayload('select * from private_table');
2019-12-24 01:19:08 +08:00
this.batchTestClient.createJob(payload, function (err, jobResult) {
if (err) {
return done(err);
}
2016-10-19 02:01:11 +08:00
jobResult.getStatus(function (err, job) {
if (err) {
return done(err);
}
2016-10-19 02:01:11 +08:00
assert.equal(job.status, JobStatus.DONE);
return done();
});
});
});
it('should perform job with select into', function (done) {
var payload = jobPayload(`
DROP TABLE IF EXISTS batch_test_table;
SELECT * INTO batch_test_table FROM (SELECT * from private_table) AS job`);
2019-12-24 01:19:08 +08:00
this.batchTestClient.createJob(payload, function (err, jobResult) {
if (err) {
return done(err);
}
2016-10-19 02:01:11 +08:00
jobResult.getStatus(function (err, job) {
if (err) {
return done(err);
}
2016-10-19 02:01:11 +08:00
assert.equal(job.status, JobStatus.DONE);
return done();
});
});
});
2016-10-19 02:01:11 +08:00
it('should perform job with select from result table', function (done) {
var payload = jobPayload('select * from batch_test_table');
2019-12-24 01:19:08 +08:00
this.batchTestClient.createJob(payload, function (err, jobResult) {
if (err) {
return done(err);
}
2016-10-19 02:01:11 +08:00
jobResult.getStatus(function (err, job) {
if (err) {
return done(err);
}
2016-10-19 02:01:11 +08:00
assert.equal(job.status, JobStatus.DONE);
return done();
});
});
2015-12-09 07:02:08 +08:00
});
2016-11-24 23:45:00 +08:00
it('should perform all enqueued jobs', function (done) {
2016-10-19 02:01:11 +08:00
var self = this;
2015-12-23 06:13:33 +08:00
var jobs = [
'select * from private_table',
'select * from private_table',
2019-12-24 01:19:08 +08:00
'select * from private_table'
2015-12-23 06:13:33 +08:00
];
2016-11-24 23:53:15 +08:00
var jobsQueue = queue(1);
2015-12-23 06:13:33 +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:01:11 +08:00
if (err) {
return done(err);
}
jobResult.getStatus(done);
});
}, jobPayload(job));
2015-12-23 06:13:33 +08:00
});
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:01:11 +08:00
assert.equal(job.status, JobStatus.DONE);
2015-12-23 06:13:33 +08:00
});
2016-10-19 02:01:11 +08:00
return done();
2015-12-23 06:13:33 +08:00
});
});
2016-11-24 23:45:00 +08:00
it('should set all job as failed', function (done) {
2016-10-19 02:01:11 +08:00
var self = this;
var jobs = [
'select * from unexistent_table',
'select * from unexistent_table',
'select * from unexistent_table'
];
2016-11-24 23:53:15 +08:00
var jobsQueue = queue(1);
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:01:11 +08:00
if (err) {
return done(err);
}
2016-10-19 02:01:11 +08:00
jobResult.getStatus(done);
});
2016-10-19 02:01:11 +08:00
}, jobPayload(job));
});
2016-10-19 02:01:11 +08:00
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:01:11 +08:00
assert.equal(job.status, JobStatus.FAILED);
});
2016-10-19 02:01:11 +08:00
return done();
});
});
it('should perform job with array of select', function (done) {
2016-05-16 07:22:47 +08:00
var queries = ['select * from private_table limit 1', 'select * from private_table'];
2016-10-19 02:01:11 +08:00
var payload = jobPayload(queries);
2019-12-24 01:19:08 +08:00
this.batchTestClient.createJob(payload, function (err, jobResult) {
if (err) {
return done(err);
}
2016-10-19 02:01:11 +08:00
jobResult.getStatus(function (err, job) {
if (err) {
return done(err);
}
2016-10-19 02:01:11 +08:00
assert.equal(job.status, JobStatus.DONE);
return done();
});
});
});
it('should set job as failed if last query fails', function (done) {
var queries = ['select * from private_table', 'select * from undefined_table'];
2016-10-19 02:01:11 +08:00
var payload = jobPayload(queries);
2019-12-24 01:19:08 +08:00
this.batchTestClient.createJob(payload, function (err, jobResult) {
if (err) {
return done(err);
}
2016-10-19 02:01:11 +08:00
jobResult.getStatus(function (err, job) {
if (err) {
return done(err);
}
2016-10-19 02:01:11 +08:00
assert.equal(job.status, JobStatus.FAILED);
return done();
});
});
});
it('should set job as failed if first query fails', function (done) {
var queries = ['select * from undefined_table', 'select * from private_table'];
2016-10-19 02:01:11 +08:00
var payload = jobPayload(queries);
2019-12-24 01:19:08 +08:00
this.batchTestClient.createJob(payload, function (err, jobResult) {
if (err) {
return done(err);
}
2016-10-19 02:01:11 +08:00
jobResult.getStatus(function (err, job) {
if (err) {
return done(err);
}
2016-10-19 02:01:11 +08:00
assert.equal(job.status, JobStatus.FAILED);
return done();
});
});
});
2015-12-09 07:02:08 +08:00
});