CartoDB-SQL-API/test/integration/batch/job_backend.test.js

152 lines
4.3 KiB
JavaScript
Raw Normal View History

2016-07-18 20:49:57 +08:00
'use strict';
require('../../helper');
2016-07-18 20:49:57 +08:00
var BATCH_SOURCE = '../../../batch/';
2016-07-18 20:49:57 +08:00
var assert = require('../../support/assert');
var redisUtils = require('../../support/redis_utils');
2016-07-18 20:49:57 +08:00
var JobQueue = require(BATCH_SOURCE + 'job_queue');
var JobBackend = require(BATCH_SOURCE + 'job_backend');
2016-10-12 00:28:46 +08:00
var JobPublisher = require(BATCH_SOURCE + 'pubsub/job-publisher');
2016-07-18 20:49:57 +08:00
var JobFactory = require(BATCH_SOURCE + 'models/job_factory');
var jobStatus = require(BATCH_SOURCE + 'job_status');
2016-10-17 21:02:34 +08:00
var metadataBackend = require('cartodb-redis')({ pool: redisUtils.getPool() });
var jobPublisher = new JobPublisher(redisUtils.getPool());
2016-07-18 20:49:57 +08:00
var jobQueue = new JobQueue(metadataBackend, jobPublisher);
2016-10-28 02:42:49 +08:00
var queue = require('queue-async');
2016-07-18 20:49:57 +08:00
var USER = 'vizzuality';
var QUERY = 'select pg_sleep(0)';
var HOST = 'localhost';
var JOB = {
user: USER,
query: QUERY,
host: HOST
};
2016-07-19 19:08:52 +08:00
function createWadusJob() {
return JobFactory.create(JSON.parse(JSON.stringify(JOB)));
}
2016-07-18 20:49:57 +08:00
describe('job backend', function() {
var jobBackend = new JobBackend(metadataBackend, jobQueue);
2016-07-18 20:49:57 +08:00
after(function (done) {
redisUtils.clean('batch:*', done);
});
2016-07-18 20:49:57 +08:00
it('.create() should persist a job', function (done) {
2016-07-19 19:08:52 +08:00
var job = createWadusJob();
2016-07-18 20:49:57 +08:00
jobBackend.create(job.data, function (err, jobCreated) {
if (err) {
return done(err);
}
assert.ok(jobCreated.job_id);
assert.equal(jobCreated.status, jobStatus.PENDING);
done();
});
});
2016-07-19 19:08:52 +08:00
it('.create() should return error', function (done) {
var job = createWadusJob();
2016-07-18 20:49:57 +08:00
delete job.data.job_id;
jobBackend.create(job, function (err) {
assert.ok(err);
assert.equal(err.name, 'NotFoundError');
assert.equal(err.message, 'Job with id undefined not found');
done();
});
});
2016-07-19 19:08:52 +08:00
it('.update() should update an existent job', function (done) {
var job = createWadusJob();
jobBackend.create(job.data, function (err, jobCreated) {
if (err) {
return done(err);
}
jobCreated.query = 'select pg_sleep(1)';
var job = JobFactory.create(jobCreated);
jobBackend.update(job.data, function (err, jobUpdated) {
if (err) {
return done(err);
}
assert.equal(jobUpdated.query, 'select pg_sleep(1)');
done();
});
});
});
it('.update() should return error when updates a nonexistent job', function (done) {
var job = createWadusJob();
jobBackend.update(job.data, function (err) {
assert.ok(err, err);
assert.equal(err.name, 'NotFoundError');
assert.equal(err.message, 'Job with id ' + job.data.job_id + ' not found');
done();
});
});
2016-10-28 00:24:39 +08:00
it('.addWorkInProgressJob() should add current job to user and host lists', function (done) {
var job = createWadusJob();
jobBackend.addWorkInProgressJob(job.data.user, job.data.job_id, function (err) {
if (err) {
return done(err);
}
done();
});
});
it('.listWorkInProgressJobByUser() should retrieve WIP jobs of given user', function (done) {
jobBackend.listWorkInProgressJobByUser('vizzuality', function (err, jobs) {
if (err) {
return done(err);
}
assert.ok(jobs.length);
done();
});
});
2016-10-28 02:42:49 +08:00
it('.listWorkInProgressJob() should retrieve WIP users', function (done) {
var jobs = [{ user: 'userA', id: 'jobId1' }, { user: 'userA', id: 'jobId2' }, { user: 'userB', id: 'jobId3' }];
var testQueue = queue();
testQueue.defer(redisUtils.clean, 'batch:*');
jobs.forEach(function (job) {
testQueue.defer(jobBackend.addWorkInProgressJob.bind(jobBackend), job.user, job.id);
});
testQueue.awaitAll(function (err) {
if (err) {
done(err);
}
jobBackend.listWorkInProgressJob(function (err, users) {
if (err) {
return done(err);
}
assert.deepEqual(users, { userA: [ 'jobId1', 'jobId2' ], userB: [ 'jobId3' ] });
done();
});
});
});
2016-07-18 20:49:57 +08:00
});