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

210 lines
6.5 KiB
JavaScript
Raw Normal View History

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('../../../batch/job_status');
2016-10-19 02:01:11 +08:00
describe('batch happy cases', function() {
2016-10-19 02:01:11 +08:00
before(function() {
this.batchTestClient = new BatchTestClient();
});
2016-10-19 02:01:11 +08:00
after(function(done) {
this.batchTestClient.drain(done);
});
2016-10-19 02:01:11 +08:00
function jobPayload(query) {
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');
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) {
2016-10-19 02:01:11 +08:00
var payload = jobPayload('select * into batch_test_table from (select * from private_table) as job');
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');
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
});
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',
'select * from private_table',
'select * from private_table',
'select * from private_table',
'select * from private_table',
'select * from private_table',
'select * from private_table',
'select * from private_table',
'select * from private_table'
];
2016-10-19 02:01:11 +08:00
var jobsQueue = queue(4);
2015-12-23 06:13:33 +08:00
jobs.forEach(function(job) {
2016-10-19 02:01:11 +08:00
jobsQueue.defer(function(payload, done) {
self.batchTestClient.createJob(payload, function(err, jobResult) {
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);
}
2016-10-19 02:01:11 +08:00
jobsCreated.forEach(function(job) {
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-22 18:37:35 +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',
'select * from unexistent_table',
'select * from unexistent_table',
'select * from unexistent_table',
'select * from unexistent_table',
'select * from unexistent_table',
'select * from unexistent_table',
'select * from unexistent_table'
];
2016-10-19 02:01:11 +08:00
var jobsQueue = queue(4);
jobs.forEach(function(job) {
2016-10-19 02:01:11 +08:00
jobsQueue.defer(function(payload, done) {
self.batchTestClient.createJob(payload, function(err, jobResult) {
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);
}
2016-10-19 02:01:11 +08:00
jobsCreated.forEach(function(job) {
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);
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);
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);
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
});