2016-05-16 19:49:56 +08:00
|
|
|
require('../helper');
|
|
|
|
|
2016-05-20 20:42:53 +08:00
|
|
|
var assert = require('../support/assert');
|
2016-05-16 19:49:56 +08:00
|
|
|
var app = require(global.settings.app_root + '/app/app')();
|
2016-05-20 20:42:53 +08:00
|
|
|
var querystring = require('qs');
|
2016-05-16 19:49:56 +08:00
|
|
|
var metadataBackend = require('cartodb-redis')({
|
|
|
|
host: global.settings.redis_host,
|
|
|
|
port: global.settings.redis_port,
|
|
|
|
max: global.settings.redisPool,
|
|
|
|
idleTimeoutMillis: global.settings.redisIdleTimeoutMillis,
|
|
|
|
reapIntervalMillis: global.settings.redisReapIntervalMillis
|
|
|
|
});
|
2016-05-17 07:00:27 +08:00
|
|
|
var batchFactory = require('../../batch');
|
|
|
|
var jobStatus = require('../../batch/job_status');
|
2016-05-16 19:49:56 +08:00
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
describe('Batch API fallback job', function () {
|
2016-05-16 19:49:56 +08:00
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
var batch = batchFactory(metadataBackend);
|
2016-05-16 19:49:56 +08:00
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
before(function () {
|
|
|
|
batch.start();
|
2016-05-16 19:49:56 +08:00
|
|
|
});
|
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
after(function (done) {
|
|
|
|
batch.stop();
|
|
|
|
batch.drain(function () {
|
|
|
|
metadataBackend.redisCmd(5, 'DEL', [ 'batch:queues:localhost' ], done);
|
2016-05-16 19:49:56 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
describe('"onsuccess" on first query should be triggered', function () {
|
|
|
|
var fallbackJob = {};
|
2016-05-16 19:49:56 +08:00
|
|
|
|
2016-05-20 20:42:53 +08:00
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
it('should create a job', function (done) {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job?api_key=1234',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
data: querystring.stringify({
|
2016-05-17 07:00:27 +08:00
|
|
|
query: {
|
|
|
|
query: [{
|
|
|
|
query: "SELECT * FROM untitle_table_4",
|
|
|
|
onsuccess: "SELECT * FROM untitle_table_4 limit 1"
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
})
|
2016-05-20 20:42:53 +08:00
|
|
|
}, {
|
|
|
|
status: 201
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
fallbackJob = JSON.parse(res.body);
|
|
|
|
done();
|
|
|
|
});
|
2016-05-16 19:49:56 +08:00
|
|
|
});
|
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
it('job should be done', function (done) {
|
|
|
|
var expectedQuery = {
|
|
|
|
"query": [{
|
|
|
|
"query": "SELECT * FROM untitle_table_4",
|
|
|
|
"onsuccess": "SELECT * FROM untitle_table_4 limit 1",
|
2016-05-23 21:05:14 +08:00
|
|
|
"status": "done",
|
|
|
|
"fallback_status": "done"
|
2016-05-17 07:00:27 +08:00
|
|
|
}]
|
|
|
|
};
|
2016-05-16 19:49:56 +08:00
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
var interval = setInterval(function () {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job/' + fallbackJob.job_id + '?api_key=1234&',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
}, {
|
|
|
|
status: 200
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
var job = JSON.parse(res.body);
|
|
|
|
if (job.status === jobStatus.DONE) {
|
|
|
|
clearInterval(interval);
|
|
|
|
assert.deepEqual(job.query, expectedQuery);
|
|
|
|
done();
|
|
|
|
} else if (job.status === jobStatus.FAILED || job.status === jobStatus.CANCELLED) {
|
|
|
|
clearInterval(interval);
|
|
|
|
done(new Error('Job ' + job.job_id + ' is ' + job.status + ', expected to be running'));
|
|
|
|
}
|
|
|
|
});
|
2016-05-17 07:00:27 +08:00
|
|
|
}, 50);
|
2016-05-16 19:49:56 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
describe('"onerror" on first query should not be triggered', function () {
|
|
|
|
var fallbackJob = {};
|
|
|
|
|
|
|
|
it('should create a job', function (done) {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job?api_key=1234',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
data: querystring.stringify({
|
2016-05-17 07:00:27 +08:00
|
|
|
query: {
|
|
|
|
query: [{
|
|
|
|
query: "SELECT * FROM untitle_table_4",
|
|
|
|
onerror: "SELECT * FROM untitle_table_4 limit 1"
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
})
|
2016-05-20 20:42:53 +08:00
|
|
|
}, {
|
|
|
|
status: 201
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
fallbackJob = JSON.parse(res.body);
|
|
|
|
done();
|
|
|
|
});
|
2016-05-16 19:49:56 +08:00
|
|
|
});
|
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
it('job should be done', function (done){
|
|
|
|
var expectedQuery = {
|
|
|
|
"query": [{
|
|
|
|
"query": "SELECT * FROM untitle_table_4",
|
|
|
|
"onerror": "SELECT * FROM untitle_table_4 limit 1",
|
2016-05-23 21:05:14 +08:00
|
|
|
"status": "done",
|
|
|
|
"fallback_status": "pending"
|
2016-05-17 07:00:27 +08:00
|
|
|
}]
|
|
|
|
};
|
|
|
|
var interval = setInterval(function () {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job/' + fallbackJob.job_id + '?api_key=1234&',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
}, {
|
|
|
|
status: 200
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
var job = JSON.parse(res.body);
|
|
|
|
if (job.status === jobStatus.DONE) {
|
|
|
|
clearInterval(interval);
|
|
|
|
assert.deepEqual(job.query, expectedQuery);
|
|
|
|
done();
|
|
|
|
} else if (job.status === jobStatus.FAILED || job.status === jobStatus.CANCELLED) {
|
|
|
|
clearInterval(interval);
|
|
|
|
done(new Error('Job ' + job.job_id + ' is ' + job.status + ', expected to be done'));
|
|
|
|
}
|
|
|
|
});
|
2016-05-17 07:00:27 +08:00
|
|
|
}, 50);
|
2016-05-16 19:49:56 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
describe('"onerror" on first query should be triggered', function () {
|
|
|
|
var fallbackJob = {};
|
|
|
|
|
|
|
|
it('should create a job', function (done) {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job?api_key=1234',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
data: querystring.stringify({
|
2016-05-17 07:00:27 +08:00
|
|
|
query: {
|
|
|
|
query: [{
|
2016-05-19 22:58:14 +08:00
|
|
|
query: "SELECT * FROM nonexistent_table /* query should fail */",
|
2016-05-17 07:00:27 +08:00
|
|
|
onerror: "SELECT * FROM untitle_table_4 limit 1"
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
})
|
2016-05-20 20:42:53 +08:00
|
|
|
}, {
|
|
|
|
status: 201
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
fallbackJob = JSON.parse(res.body);
|
|
|
|
done();
|
|
|
|
});
|
2016-05-16 19:49:56 +08:00
|
|
|
});
|
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
it('job should be done', function (done){
|
|
|
|
var expectedQuery = {
|
2016-05-19 22:58:14 +08:00
|
|
|
query: [{
|
|
|
|
query: 'SELECT * FROM nonexistent_table /* query should fail */',
|
|
|
|
onerror: 'SELECT * FROM untitle_table_4 limit 1',
|
2016-05-23 21:05:14 +08:00
|
|
|
status: 'failed',
|
|
|
|
fallback_status: 'done',
|
2016-05-19 22:58:14 +08:00
|
|
|
failed_reason: 'relation "nonexistent_table" does not exist'
|
2016-05-17 07:00:27 +08:00
|
|
|
}]
|
|
|
|
};
|
|
|
|
var interval = setInterval(function () {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job/' + fallbackJob.job_id + '?api_key=1234&',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
}, {
|
|
|
|
status: 200
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
var job = JSON.parse(res.body);
|
|
|
|
if (job.status === jobStatus.FAILED) {
|
|
|
|
clearInterval(interval);
|
|
|
|
assert.deepEqual(job.query, expectedQuery);
|
|
|
|
done();
|
|
|
|
} else if (job.status === jobStatus.DONE || job.status === jobStatus.CANCELLED) {
|
|
|
|
clearInterval(interval);
|
|
|
|
done(new Error('Job ' + job.job_id + ' is ' + job.status + ', expected to be done'));
|
|
|
|
}
|
|
|
|
});
|
2016-05-17 07:00:27 +08:00
|
|
|
}, 50);
|
2016-05-16 19:49:56 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
describe('"onsuccess" on first query should not be triggered', function () {
|
|
|
|
var fallbackJob = {};
|
|
|
|
|
|
|
|
it('should create a job', function (done) {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job?api_key=1234',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
data: querystring.stringify({
|
2016-05-17 07:00:27 +08:00
|
|
|
query: {
|
|
|
|
query: [{
|
2016-05-19 22:58:14 +08:00
|
|
|
query: "SELECT * FROM nonexistent_table /* query should fail */",
|
2016-05-17 07:00:27 +08:00
|
|
|
onsuccess: "SELECT * FROM untitle_table_4 limit 1"
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
})
|
2016-05-20 20:42:53 +08:00
|
|
|
}, {
|
|
|
|
status: 201
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
fallbackJob = JSON.parse(res.body);
|
|
|
|
done();
|
|
|
|
});
|
2016-05-16 19:49:56 +08:00
|
|
|
});
|
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
it('job should be failed', function (done){
|
|
|
|
var expectedQuery = {
|
2016-05-19 22:58:14 +08:00
|
|
|
query: [{
|
|
|
|
query: 'SELECT * FROM nonexistent_table /* query should fail */',
|
|
|
|
onsuccess: 'SELECT * FROM untitle_table_4 limit 1',
|
2016-05-23 21:05:14 +08:00
|
|
|
status: 'failed',
|
|
|
|
fallback_status: 'pending',
|
2016-05-19 22:58:14 +08:00
|
|
|
failed_reason: 'relation "nonexistent_table" does not exist'
|
2016-05-17 07:00:27 +08:00
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
|
|
|
var interval = setInterval(function () {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job/' + fallbackJob.job_id + '?api_key=1234&',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
}, {
|
|
|
|
status: 200
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
var job = JSON.parse(res.body);
|
|
|
|
if (job.status === jobStatus.FAILED) {
|
|
|
|
clearInterval(interval);
|
|
|
|
assert.deepEqual(job.query, expectedQuery);
|
|
|
|
done();
|
|
|
|
} else if (job.status === jobStatus.DONE || job.status === jobStatus.CANCELLED) {
|
|
|
|
clearInterval(interval);
|
|
|
|
done(new Error('Job ' + job.job_id + ' is ' + job.status + ', expected to be failed'));
|
|
|
|
}
|
|
|
|
});
|
2016-05-17 07:00:27 +08:00
|
|
|
}, 50);
|
2016-05-16 19:49:56 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
|
|
|
|
describe('"onsuccess" should be triggered', function () {
|
|
|
|
var fallbackJob = {};
|
|
|
|
|
|
|
|
it('should create a job', function (done) {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job?api_key=1234',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
data: querystring.stringify({
|
2016-05-17 07:00:27 +08:00
|
|
|
query: {
|
|
|
|
query: [{
|
|
|
|
query: "SELECT * FROM untitle_table_4",
|
|
|
|
}],
|
|
|
|
onsuccess: "SELECT * FROM untitle_table_4 limit 1"
|
|
|
|
}
|
|
|
|
})
|
2016-05-20 20:42:53 +08:00
|
|
|
}, {
|
|
|
|
status: 201
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
fallbackJob = JSON.parse(res.body);
|
|
|
|
done();
|
|
|
|
});
|
2016-05-16 19:49:56 +08:00
|
|
|
});
|
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
it('job should be done', function (done) {
|
|
|
|
var expectedQuery = {
|
|
|
|
"query": [{
|
|
|
|
"query": "SELECT * FROM untitle_table_4",
|
|
|
|
"status": "done"
|
|
|
|
}],
|
|
|
|
"onsuccess": "SELECT * FROM untitle_table_4 limit 1"
|
|
|
|
};
|
|
|
|
|
|
|
|
var interval = setInterval(function () {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job/' + fallbackJob.job_id + '?api_key=1234&',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
}, {
|
|
|
|
status: 200
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
var job = JSON.parse(res.body);
|
2016-05-23 21:05:14 +08:00
|
|
|
if (job.status === jobStatus.DONE && job.fallback_status === jobStatus.DONE) {
|
2016-05-20 20:42:53 +08:00
|
|
|
clearInterval(interval);
|
|
|
|
assert.deepEqual(job.query, expectedQuery);
|
|
|
|
done();
|
|
|
|
} else if (job.status === jobStatus.FAILED || job.status === jobStatus.CANCELLED) {
|
|
|
|
clearInterval(interval);
|
|
|
|
done(new Error('Job ' + job.job_id + ' is ' + job.status + ', expected to be done'));
|
|
|
|
}
|
|
|
|
});
|
2016-05-17 07:00:27 +08:00
|
|
|
}, 50);
|
2016-05-16 19:49:56 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
describe('"onsuccess" should not be triggered', function () {
|
|
|
|
var fallbackJob = {};
|
|
|
|
|
|
|
|
it('should create a job', function (done) {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job?api_key=1234',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
data: querystring.stringify({
|
2016-05-17 07:00:27 +08:00
|
|
|
query: {
|
|
|
|
query: [{
|
2016-05-19 22:58:14 +08:00
|
|
|
query: "SELECT * FROM nonexistent_table /* query should fail */",
|
2016-05-17 07:00:27 +08:00
|
|
|
}],
|
|
|
|
onsuccess: "SELECT * FROM untitle_table_4 limit 1"
|
|
|
|
}
|
|
|
|
})
|
2016-05-20 20:42:53 +08:00
|
|
|
}, {
|
|
|
|
status: 201
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
fallbackJob = JSON.parse(res.body);
|
|
|
|
done();
|
|
|
|
});
|
2016-05-16 19:49:56 +08:00
|
|
|
});
|
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
it('job should be done', function (done) {
|
|
|
|
var expectedQuery = {
|
2016-05-19 22:58:14 +08:00
|
|
|
query: [{
|
|
|
|
query: 'SELECT * FROM nonexistent_table /* query should fail */',
|
|
|
|
status: 'failed',
|
|
|
|
failed_reason: 'relation "nonexistent_table" does not exist'
|
2016-05-17 07:00:27 +08:00
|
|
|
}],
|
2016-05-19 22:58:14 +08:00
|
|
|
onsuccess: 'SELECT * FROM untitle_table_4 limit 1'
|
2016-05-17 07:00:27 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
var interval = setInterval(function () {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job/' + fallbackJob.job_id + '?api_key=1234&',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
}, {
|
|
|
|
status: 200
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
var job = JSON.parse(res.body);
|
2016-05-23 21:05:14 +08:00
|
|
|
if (job.status === jobStatus.FAILED && job.fallback_status === jobStatus.PENDING) {
|
2016-05-20 20:42:53 +08:00
|
|
|
clearInterval(interval);
|
|
|
|
assert.deepEqual(job.query, expectedQuery);
|
|
|
|
done();
|
|
|
|
} else if (job.status === jobStatus.FAILED || job.status === jobStatus.CANCELLED) {
|
|
|
|
clearInterval(interval);
|
|
|
|
done(new Error('Job ' + job.job_id + ' is ' + job.status + ', expected to be pending'));
|
|
|
|
}
|
|
|
|
});
|
2016-05-17 07:00:27 +08:00
|
|
|
}, 50);
|
2016-05-16 19:49:56 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
|
|
|
|
describe('"onerror" should be triggered', function () {
|
|
|
|
var fallbackJob = {};
|
|
|
|
|
|
|
|
it('should create a job', function (done) {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job?api_key=1234',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
data: querystring.stringify({
|
2016-05-17 07:00:27 +08:00
|
|
|
query: {
|
|
|
|
query: [{
|
2016-05-19 22:58:14 +08:00
|
|
|
query: "SELECT * FROM nonexistent_table /* query should fail */"
|
2016-05-17 07:00:27 +08:00
|
|
|
}],
|
|
|
|
onerror: "SELECT * FROM untitle_table_4 limit 1"
|
|
|
|
}
|
|
|
|
})
|
2016-05-20 20:42:53 +08:00
|
|
|
}, {
|
|
|
|
status: 201
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
fallbackJob = JSON.parse(res.body);
|
|
|
|
done();
|
|
|
|
});
|
2016-05-16 19:49:56 +08:00
|
|
|
});
|
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
it('job should be done', function (done) {
|
|
|
|
var expectedQuery = {
|
|
|
|
"query": [{
|
2016-05-19 22:58:14 +08:00
|
|
|
"query": "SELECT * FROM nonexistent_table /* query should fail */",
|
|
|
|
"status": "failed",
|
|
|
|
"failed_reason": 'relation "nonexistent_table" does not exist'
|
2016-05-17 07:00:27 +08:00
|
|
|
}],
|
|
|
|
"onerror": "SELECT * FROM untitle_table_4 limit 1"
|
|
|
|
};
|
|
|
|
|
|
|
|
var interval = setInterval(function () {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job/' + fallbackJob.job_id + '?api_key=1234&',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
}, {
|
|
|
|
status: 200
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
var job = JSON.parse(res.body);
|
2016-05-23 21:05:14 +08:00
|
|
|
if (job.status === jobStatus.FAILED && job.fallback_status === jobStatus.DONE) {
|
2016-05-20 20:42:53 +08:00
|
|
|
clearInterval(interval);
|
|
|
|
assert.deepEqual(job.query, expectedQuery);
|
|
|
|
done();
|
2016-05-23 21:05:14 +08:00
|
|
|
} else if (job.status === jobStatus.DONE || job.status === jobStatus.CANCELLED) {
|
2016-05-20 20:42:53 +08:00
|
|
|
clearInterval(interval);
|
2016-05-23 21:05:14 +08:00
|
|
|
done(new Error('Job ' + job.job_id + ' is ' + job.status + ', expected to be failed'));
|
2016-05-20 20:42:53 +08:00
|
|
|
}
|
|
|
|
});
|
2016-05-17 07:00:27 +08:00
|
|
|
}, 50);
|
2016-05-16 19:49:56 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
describe('"onerror" should not be triggered', function () {
|
|
|
|
var fallbackJob = {};
|
|
|
|
|
|
|
|
it('should create a job', function (done) {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job?api_key=1234',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
data: querystring.stringify({
|
2016-05-17 07:00:27 +08:00
|
|
|
query: {
|
|
|
|
query: [{
|
|
|
|
query: "SELECT * FROM untitle_table_4",
|
|
|
|
}],
|
|
|
|
onerror: "SELECT * FROM untitle_table_4 limit 1"
|
|
|
|
}
|
|
|
|
})
|
2016-05-20 20:42:53 +08:00
|
|
|
}, {
|
|
|
|
status: 201
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
fallbackJob = JSON.parse(res.body);
|
|
|
|
done();
|
|
|
|
});
|
2016-05-16 19:49:56 +08:00
|
|
|
});
|
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
it('job should be done', function (done) {
|
|
|
|
var expectedQuery = {
|
|
|
|
"query": [{
|
|
|
|
"query": "SELECT * FROM untitle_table_4",
|
|
|
|
"status": "done"
|
|
|
|
}],
|
|
|
|
"onerror": "SELECT * FROM untitle_table_4 limit 1"
|
|
|
|
};
|
|
|
|
|
|
|
|
var interval = setInterval(function () {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job/' + fallbackJob.job_id + '?api_key=1234&',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
}, {
|
|
|
|
status: 200
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
var job = JSON.parse(res.body);
|
2016-05-23 21:05:14 +08:00
|
|
|
if (job.status === jobStatus.DONE && job.fallback_status === jobStatus.PENDING) {
|
2016-05-20 20:42:53 +08:00
|
|
|
clearInterval(interval);
|
|
|
|
assert.deepEqual(job.query, expectedQuery);
|
|
|
|
done();
|
|
|
|
} else if (job.status === jobStatus.FAILED || job.status === jobStatus.CANCELLED) {
|
|
|
|
clearInterval(interval);
|
2016-05-23 21:05:14 +08:00
|
|
|
done(new Error('Job ' + job.job_id + ' is ' + job.status + ', expected to be done'));
|
2016-05-20 20:42:53 +08:00
|
|
|
}
|
|
|
|
});
|
2016-05-17 07:00:27 +08:00
|
|
|
}, 50);
|
2016-05-16 19:49:56 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
|
2016-05-18 00:51:02 +08:00
|
|
|
describe('"onsuccess" & "onsuccess" on query should be triggered', function () {
|
2016-05-17 07:00:27 +08:00
|
|
|
var fallbackJob = {};
|
|
|
|
|
|
|
|
it('should create a job', function (done) {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job?api_key=1234',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
data: querystring.stringify({
|
2016-05-17 07:00:27 +08:00
|
|
|
query: {
|
|
|
|
query: [{
|
|
|
|
query: "SELECT * FROM untitle_table_4",
|
|
|
|
onsuccess: "SELECT * FROM untitle_table_4 limit 1"
|
|
|
|
}],
|
|
|
|
onsuccess: "SELECT * FROM untitle_table_4 limit 2"
|
|
|
|
}
|
|
|
|
})
|
2016-05-20 20:42:53 +08:00
|
|
|
}, {
|
|
|
|
status: 201
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
fallbackJob = JSON.parse(res.body);
|
|
|
|
done();
|
|
|
|
});
|
2016-05-16 19:49:56 +08:00
|
|
|
});
|
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
it('job should be done', function (done) {
|
|
|
|
var expectedQuery = {
|
|
|
|
"query": [{
|
|
|
|
"query": "SELECT * FROM untitle_table_4",
|
|
|
|
"onsuccess": "SELECT * FROM untitle_table_4 limit 1",
|
2016-05-23 21:05:14 +08:00
|
|
|
"status": "done",
|
|
|
|
"fallback_status": "done"
|
2016-05-17 07:00:27 +08:00
|
|
|
}],
|
|
|
|
"onsuccess": "SELECT * FROM untitle_table_4 limit 2"
|
|
|
|
};
|
|
|
|
|
|
|
|
var interval = setInterval(function () {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job/' + fallbackJob.job_id + '?api_key=1234&',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
}, {
|
|
|
|
status: 200
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
var job = JSON.parse(res.body);
|
2016-05-23 21:05:14 +08:00
|
|
|
if (job.status === jobStatus.DONE && job.fallback_status === jobStatus.DONE) {
|
2016-05-20 20:42:53 +08:00
|
|
|
clearInterval(interval);
|
|
|
|
assert.deepEqual(job.query, expectedQuery);
|
|
|
|
done();
|
|
|
|
} else if (job.status === jobStatus.FAILED || job.status === jobStatus.CANCELLED) {
|
|
|
|
clearInterval(interval);
|
|
|
|
done(new Error('Job ' + job.job_id + ' is ' + job.status + ', expected to be done'));
|
|
|
|
}
|
|
|
|
});
|
2016-05-17 07:00:27 +08:00
|
|
|
}, 50);
|
2016-05-16 19:49:56 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
describe('"onsuccess" for each query should be triggered', function () {
|
|
|
|
var fallbackJob = {};
|
|
|
|
|
|
|
|
it('should create a job', function (done) {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job?api_key=1234',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
data: querystring.stringify({
|
2016-05-17 07:00:27 +08:00
|
|
|
query: {
|
|
|
|
query: [{
|
|
|
|
query: "SELECT * FROM untitle_table_4",
|
|
|
|
onsuccess: "SELECT * FROM untitle_table_4 limit 1"
|
|
|
|
}, {
|
|
|
|
query: "SELECT * FROM untitle_table_4 limit 2",
|
|
|
|
onsuccess: "SELECT * FROM untitle_table_4 limit 3"
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
})
|
2016-05-20 20:42:53 +08:00
|
|
|
}, {
|
|
|
|
status: 201
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
fallbackJob = JSON.parse(res.body);
|
|
|
|
done();
|
|
|
|
});
|
2016-05-16 19:49:56 +08:00
|
|
|
});
|
|
|
|
|
2016-05-17 07:00:27 +08:00
|
|
|
it('job should be done', function (done) {
|
|
|
|
var expectedQuery = {
|
|
|
|
"query": [{
|
|
|
|
"query": "SELECT * FROM untitle_table_4",
|
|
|
|
"onsuccess": "SELECT * FROM untitle_table_4 limit 1",
|
2016-05-23 21:05:14 +08:00
|
|
|
"status": "done",
|
|
|
|
"fallback_status": "done"
|
2016-05-17 07:00:27 +08:00
|
|
|
}, {
|
|
|
|
"query": "SELECT * FROM untitle_table_4 limit 2",
|
|
|
|
"onsuccess": "SELECT * FROM untitle_table_4 limit 3",
|
2016-05-23 21:05:14 +08:00
|
|
|
"status": "done",
|
|
|
|
"fallback_status": "done"
|
2016-05-17 07:00:27 +08:00
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
|
|
|
var interval = setInterval(function () {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job/' + fallbackJob.job_id + '?api_key=1234&',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
}, {
|
|
|
|
status: 200
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
var job = JSON.parse(res.body);
|
|
|
|
if (job.status === jobStatus.DONE) {
|
|
|
|
clearInterval(interval);
|
|
|
|
assert.deepEqual(job.query, expectedQuery);
|
|
|
|
done();
|
|
|
|
} else if (job.status === jobStatus.FAILED || job.status === jobStatus.CANCELLED) {
|
|
|
|
clearInterval(interval);
|
|
|
|
done(new Error('Job ' + job.job_id + ' is ' + job.status + ', expected to be done'));
|
|
|
|
}
|
|
|
|
});
|
2016-05-18 00:51:02 +08:00
|
|
|
}, 50);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('"onsuccess" for each query should not be triggered', function () {
|
|
|
|
var fallbackJob = {};
|
|
|
|
|
|
|
|
it('should create a job', function (done) {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job?api_key=1234',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
data: querystring.stringify({
|
2016-05-18 00:51:02 +08:00
|
|
|
query: {
|
|
|
|
query: [{
|
2016-05-19 22:58:14 +08:00
|
|
|
query: "SELECT * FROM nonexistent_table /* should fail */",
|
2016-05-18 00:51:02 +08:00
|
|
|
onsuccess: "SELECT * FROM untitle_table_4 limit 1"
|
|
|
|
}, {
|
|
|
|
query: "SELECT * FROM untitle_table_4 limit 2",
|
|
|
|
onsuccess: "SELECT * FROM untitle_table_4 limit 3"
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
})
|
2016-05-20 20:42:53 +08:00
|
|
|
}, {
|
|
|
|
status: 201
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
fallbackJob = JSON.parse(res.body);
|
|
|
|
done();
|
|
|
|
});
|
2016-05-18 00:51:02 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('job should be failed', function (done) {
|
|
|
|
var expectedQuery = {
|
|
|
|
"query": [{
|
2016-05-19 22:58:14 +08:00
|
|
|
"query": "SELECT * FROM nonexistent_table /* should fail */",
|
2016-05-18 00:51:02 +08:00
|
|
|
"onsuccess": "SELECT * FROM untitle_table_4 limit 1",
|
2016-05-23 21:05:14 +08:00
|
|
|
"status": "failed",
|
|
|
|
"fallback_status": "pending",
|
2016-05-19 22:58:14 +08:00
|
|
|
"failed_reason": 'relation "nonexistent_table" does not exist'
|
2016-05-18 00:51:02 +08:00
|
|
|
}, {
|
|
|
|
"query": "SELECT * FROM untitle_table_4 limit 2",
|
|
|
|
"onsuccess": "SELECT * FROM untitle_table_4 limit 3",
|
2016-05-23 21:05:14 +08:00
|
|
|
"status": "pending",
|
|
|
|
"fallback_status": "pending"
|
2016-05-18 00:51:02 +08:00
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
|
|
|
var interval = setInterval(function () {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job/' + fallbackJob.job_id + '?api_key=1234&',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
}, {
|
|
|
|
status: 200
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
var job = JSON.parse(res.body);
|
|
|
|
if (job.status === jobStatus.FAILED) {
|
|
|
|
clearInterval(interval);
|
|
|
|
assert.deepEqual(job.query, expectedQuery);
|
|
|
|
done();
|
|
|
|
} else if (job.status === jobStatus.DONE || job.status === jobStatus.CANCELLED) {
|
|
|
|
clearInterval(interval);
|
|
|
|
done(new Error('Job ' + job.job_id + ' is ' + job.status + ', expected to be failed'));
|
|
|
|
}
|
|
|
|
});
|
2016-05-18 00:51:02 +08:00
|
|
|
}, 50);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('"onsuccess" for second query should not be triggered', function () {
|
|
|
|
var fallbackJob = {};
|
|
|
|
|
|
|
|
it('should create a job', function (done) {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job?api_key=1234',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
data: querystring.stringify({
|
2016-05-18 00:51:02 +08:00
|
|
|
query: {
|
|
|
|
query: [{
|
|
|
|
query: "SELECT * FROM untitle_table_4 limit 2",
|
|
|
|
onsuccess: "SELECT * FROM untitle_table_4 limit 1"
|
|
|
|
}, {
|
2016-05-19 22:58:14 +08:00
|
|
|
query: "SELECT * FROM nonexistent_table /* should fail */",
|
2016-05-18 00:51:02 +08:00
|
|
|
onsuccess: "SELECT * FROM untitle_table_4 limit 3"
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
})
|
2016-05-20 20:42:53 +08:00
|
|
|
}, {
|
|
|
|
status: 201
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
fallbackJob = JSON.parse(res.body);
|
|
|
|
done();
|
|
|
|
});
|
2016-05-18 00:51:02 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('job should be failed', function (done) {
|
|
|
|
var expectedQuery = {
|
|
|
|
"query": [{
|
|
|
|
"query": "SELECT * FROM untitle_table_4 limit 2",
|
|
|
|
"onsuccess": "SELECT * FROM untitle_table_4 limit 1",
|
2016-05-23 21:05:14 +08:00
|
|
|
"status": "done",
|
|
|
|
"fallback_status": "done"
|
2016-05-18 00:51:02 +08:00
|
|
|
}, {
|
2016-05-19 22:58:14 +08:00
|
|
|
"query": "SELECT * FROM nonexistent_table /* should fail */",
|
2016-05-18 00:51:02 +08:00
|
|
|
"onsuccess": "SELECT * FROM untitle_table_4 limit 3",
|
2016-05-23 21:05:14 +08:00
|
|
|
"status": "failed",
|
|
|
|
"fallback_status": "pending",
|
2016-05-19 22:58:14 +08:00
|
|
|
"failed_reason": 'relation "nonexistent_table" does not exist'
|
2016-05-18 00:51:02 +08:00
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
|
|
|
var interval = setInterval(function () {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job/' + fallbackJob.job_id + '?api_key=1234&',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
}, {
|
|
|
|
status: 200
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
var job = JSON.parse(res.body);
|
|
|
|
if (job.status === jobStatus.FAILED) {
|
|
|
|
clearInterval(interval);
|
|
|
|
assert.deepEqual(job.query, expectedQuery);
|
|
|
|
done();
|
|
|
|
} else if (job.status === jobStatus.DONE || job.status === jobStatus.CANCELLED) {
|
|
|
|
clearInterval(interval);
|
|
|
|
done(new Error('Job ' + job.job_id + ' is ' + job.status + ', expected to be failed'));
|
|
|
|
}
|
|
|
|
});
|
2016-05-17 07:00:27 +08:00
|
|
|
}, 50);
|
2016-05-16 19:49:56 +08:00
|
|
|
});
|
|
|
|
});
|
2016-05-18 00:51:02 +08:00
|
|
|
|
2016-05-25 23:00:27 +08:00
|
|
|
describe('"onerror" should not be triggered for any query', function () {
|
|
|
|
var fallbackJob = {};
|
|
|
|
|
|
|
|
it('should create a job', function (done) {
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job?api_key=1234',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
data: querystring.stringify({
|
|
|
|
query: {
|
|
|
|
query: [{
|
|
|
|
query: "SELECT * FROM untitle_table_4 limit 1",
|
|
|
|
onerror: "SELECT * FROM untitle_table_4 limit 2"
|
|
|
|
}, {
|
|
|
|
query: "SELECT * FROM untitle_table_4 limit 3",
|
|
|
|
onerror: "SELECT * FROM untitle_table_4 limit 4"
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}, {
|
|
|
|
status: 201
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
fallbackJob = JSON.parse(res.body);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('job should be failed', function (done) {
|
|
|
|
var expectedQuery = {
|
|
|
|
query: [{
|
|
|
|
query: 'SELECT * FROM untitle_table_4 limit 1',
|
|
|
|
onerror: 'SELECT * FROM untitle_table_4 limit 2',
|
|
|
|
status: 'done',
|
|
|
|
fallback_status: 'pending'
|
|
|
|
}, {
|
|
|
|
query: 'SELECT * FROM untitle_table_4 limit 3',
|
|
|
|
onerror: 'SELECT * FROM untitle_table_4 limit 4',
|
|
|
|
status: 'done',
|
|
|
|
fallback_status: 'pending'
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
|
|
|
var interval = setInterval(function () {
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job/' + fallbackJob.job_id + '?api_key=1234&',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
}, {
|
|
|
|
status: 200
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
var job = JSON.parse(res.body);
|
|
|
|
if (job.status === jobStatus.DONE) {
|
|
|
|
clearInterval(interval);
|
|
|
|
assert.deepEqual(job.query, expectedQuery);
|
|
|
|
done();
|
|
|
|
} else if (job.status === jobStatus.FAILED || job.status === jobStatus.CANCELLED) {
|
|
|
|
clearInterval(interval);
|
|
|
|
done(new Error('Job ' + job.job_id + ' is ' + job.status + ', expected to be failed'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 50);
|
|
|
|
});
|
|
|
|
});
|
2016-05-18 00:51:02 +08:00
|
|
|
|
|
|
|
describe('"onsuccess" for first query should fail', function () {
|
|
|
|
var fallbackJob = {};
|
|
|
|
|
|
|
|
it('should create a job', function (done) {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job?api_key=1234',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
data: querystring.stringify({
|
2016-05-18 00:51:02 +08:00
|
|
|
query: {
|
|
|
|
query: [{
|
|
|
|
query: "SELECT * FROM untitle_table_4 limit 1",
|
2016-05-19 22:58:14 +08:00
|
|
|
onsuccess: "SELECT * FROM nonexistent_table /* should fail */"
|
2016-05-18 00:51:02 +08:00
|
|
|
}, {
|
|
|
|
query: "SELECT * FROM untitle_table_4 limit 2",
|
|
|
|
onsuccess: "SELECT * FROM untitle_table_4 limit 3"
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
})
|
2016-05-20 20:42:53 +08:00
|
|
|
}, {
|
|
|
|
status: 201
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
fallbackJob = JSON.parse(res.body);
|
|
|
|
done();
|
|
|
|
});
|
2016-05-18 00:51:02 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('job should be done', function (done) {
|
|
|
|
var expectedQuery = {
|
|
|
|
"query": [{
|
|
|
|
"query": "SELECT * FROM untitle_table_4 limit 1",
|
2016-05-19 22:58:14 +08:00
|
|
|
"onsuccess": "SELECT * FROM nonexistent_table /* should fail */",
|
2016-05-23 21:05:14 +08:00
|
|
|
"status": "done",
|
|
|
|
"fallback_status": "failed",
|
2016-05-19 22:58:14 +08:00
|
|
|
"failed_reason": 'relation "nonexistent_table" does not exist'
|
2016-05-18 00:51:02 +08:00
|
|
|
}, {
|
|
|
|
"query": "SELECT * FROM untitle_table_4 limit 2",
|
|
|
|
"onsuccess": "SELECT * FROM untitle_table_4 limit 3",
|
2016-05-23 21:05:14 +08:00
|
|
|
"status": "done",
|
|
|
|
"fallback_status": "done"
|
2016-05-18 00:51:02 +08:00
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
|
|
|
var interval = setInterval(function () {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job/' + fallbackJob.job_id + '?api_key=1234&',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
}, {
|
|
|
|
status: 200
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
var job = JSON.parse(res.body);
|
|
|
|
if (job.status === jobStatus.DONE) {
|
|
|
|
clearInterval(interval);
|
|
|
|
assert.deepEqual(job.query, expectedQuery);
|
|
|
|
done();
|
|
|
|
} else if (job.status === jobStatus.FAILED || job.status === jobStatus.CANCELLED) {
|
|
|
|
clearInterval(interval);
|
|
|
|
done(new Error('Job ' + job.job_id + ' is ' + job.status + ', expected to be done'));
|
|
|
|
}
|
|
|
|
});
|
2016-05-18 00:51:02 +08:00
|
|
|
}, 50);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('"onsuccess" for second query should fail', function () {
|
|
|
|
var fallbackJob = {};
|
|
|
|
|
|
|
|
it('should create a job', function (done) {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job?api_key=1234',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
data: querystring.stringify({
|
2016-05-18 00:51:02 +08:00
|
|
|
query: {
|
|
|
|
query: [{
|
|
|
|
query: "SELECT * FROM untitle_table_4 limit 1",
|
|
|
|
onsuccess: "SELECT * FROM untitle_table_4 limit 2"
|
|
|
|
}, {
|
|
|
|
query: "SELECT * FROM untitle_table_4 limit 3",
|
2016-05-19 22:58:14 +08:00
|
|
|
onsuccess: "SELECT * FROM nonexistent_table /* should fail */"
|
2016-05-18 00:51:02 +08:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
})
|
2016-05-20 20:42:53 +08:00
|
|
|
}, {
|
|
|
|
status: 201
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
fallbackJob = JSON.parse(res.body);
|
|
|
|
done();
|
|
|
|
});
|
2016-05-18 00:51:02 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('job should be done', function (done) {
|
|
|
|
var expectedQuery = {
|
|
|
|
"query": [{
|
|
|
|
"query": "SELECT * FROM untitle_table_4 limit 1",
|
|
|
|
"onsuccess": "SELECT * FROM untitle_table_4 limit 2",
|
2016-05-23 21:05:14 +08:00
|
|
|
"status": "done",
|
|
|
|
"fallback_status": "done"
|
2016-05-18 00:51:02 +08:00
|
|
|
}, {
|
|
|
|
"query": "SELECT * FROM untitle_table_4 limit 3",
|
2016-05-19 22:58:14 +08:00
|
|
|
"onsuccess": "SELECT * FROM nonexistent_table /* should fail */",
|
2016-05-23 21:05:14 +08:00
|
|
|
"status": "done",
|
|
|
|
"fallback_status": "failed",
|
2016-05-19 22:58:14 +08:00
|
|
|
"failed_reason": 'relation "nonexistent_table" does not exist'
|
2016-05-18 00:51:02 +08:00
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
|
|
|
var interval = setInterval(function () {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job/' + fallbackJob.job_id + '?api_key=1234&',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
}, {
|
|
|
|
status: 200
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
var job = JSON.parse(res.body);
|
|
|
|
if (job.status === jobStatus.DONE) {
|
|
|
|
clearInterval(interval);
|
|
|
|
assert.deepEqual(job.query, expectedQuery);
|
|
|
|
done();
|
|
|
|
} else if (job.status === jobStatus.FAILED || job.status === jobStatus.CANCELLED) {
|
|
|
|
clearInterval(interval);
|
|
|
|
done(new Error('Job ' + job.job_id + ' is ' + job.status + ', expected to be done'));
|
|
|
|
}
|
|
|
|
});
|
2016-05-18 00:51:02 +08:00
|
|
|
}, 50);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('"onsuccess" for job & "onsuccess" for each query should be triggered', function () {
|
|
|
|
var fallbackJob = {};
|
|
|
|
|
|
|
|
it('should create a job', function (done) {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job?api_key=1234',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
data: querystring.stringify({
|
2016-05-18 00:51:02 +08:00
|
|
|
query: {
|
|
|
|
query: [{
|
|
|
|
query: "SELECT * FROM untitle_table_4 limit 1",
|
|
|
|
onsuccess: "SELECT * FROM untitle_table_4 limit 2"
|
|
|
|
}, {
|
|
|
|
query: "SELECT * FROM untitle_table_4 limit 3",
|
|
|
|
onsuccess: "SELECT * FROM untitle_table_4 limit 4"
|
|
|
|
}],
|
|
|
|
onsuccess: "SELECT * FROM untitle_table_4 limit 5"
|
|
|
|
}
|
|
|
|
})
|
2016-05-20 20:42:53 +08:00
|
|
|
}, {
|
|
|
|
status: 201
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
fallbackJob = JSON.parse(res.body);
|
|
|
|
done();
|
|
|
|
});
|
2016-05-18 00:51:02 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('job should be done', function (done) {
|
|
|
|
var expectedQuery = {
|
|
|
|
"query": [{
|
|
|
|
"query": "SELECT * FROM untitle_table_4 limit 1",
|
|
|
|
"onsuccess": "SELECT * FROM untitle_table_4 limit 2",
|
2016-05-23 21:05:14 +08:00
|
|
|
"status": "done",
|
|
|
|
"fallback_status": "done"
|
2016-05-18 00:51:02 +08:00
|
|
|
}, {
|
|
|
|
"query": "SELECT * FROM untitle_table_4 limit 3",
|
|
|
|
"onsuccess": "SELECT * FROM untitle_table_4 limit 4",
|
2016-05-23 21:05:14 +08:00
|
|
|
"status": "done",
|
|
|
|
"fallback_status": "done"
|
2016-05-18 00:51:02 +08:00
|
|
|
}],
|
|
|
|
onsuccess: "SELECT * FROM untitle_table_4 limit 5"
|
|
|
|
};
|
|
|
|
|
|
|
|
var interval = setInterval(function () {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job/' + fallbackJob.job_id + '?api_key=1234&',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
}, {
|
|
|
|
status: 200
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
var job = JSON.parse(res.body);
|
2016-05-23 21:05:14 +08:00
|
|
|
if (job.status === jobStatus.DONE && job.fallback_status === jobStatus.DONE) {
|
2016-05-20 20:42:53 +08:00
|
|
|
clearInterval(interval);
|
|
|
|
assert.deepEqual(job.query, expectedQuery);
|
|
|
|
done();
|
2016-05-23 21:05:14 +08:00
|
|
|
} else if (job.status === jobStatus.FAILED || job.status === jobStatus.CANCELLED) {
|
2016-05-20 20:42:53 +08:00
|
|
|
clearInterval(interval);
|
|
|
|
done(new Error('Job ' + job.job_id + ' is ' + job.status + ', expected to be done'));
|
|
|
|
}
|
|
|
|
});
|
2016-05-18 00:51:02 +08:00
|
|
|
}, 50);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('"onsuccess" for job & "onsuccess" for each query should be triggered ' +
|
|
|
|
'(even second "onsuccess" fails job should be done)', function () {
|
|
|
|
var fallbackJob = {};
|
|
|
|
|
|
|
|
it('should create a job', function (done) {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job?api_key=1234',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
data: querystring.stringify({
|
2016-05-18 00:51:02 +08:00
|
|
|
query: {
|
|
|
|
query: [{
|
|
|
|
query: "SELECT * FROM untitle_table_4 limit 1",
|
|
|
|
onsuccess: "SELECT * FROM untitle_table_4 limit 2"
|
|
|
|
}, {
|
|
|
|
query: "SELECT * FROM untitle_table_4 limit 3",
|
2016-05-19 22:58:14 +08:00
|
|
|
onsuccess: "SELECT * FROM nonexistent_table /* should fail */"
|
2016-05-18 00:51:02 +08:00
|
|
|
}],
|
|
|
|
onsuccess: "SELECT * FROM untitle_table_4 limit 5"
|
|
|
|
}
|
|
|
|
})
|
2016-05-20 20:42:53 +08:00
|
|
|
}, {
|
|
|
|
status: 201
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
fallbackJob = JSON.parse(res.body);
|
|
|
|
done();
|
|
|
|
});
|
2016-05-18 00:51:02 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('job should be done', function (done) {
|
|
|
|
var expectedQuery = {
|
|
|
|
"query": [{
|
|
|
|
"query": "SELECT * FROM untitle_table_4 limit 1",
|
|
|
|
"onsuccess": "SELECT * FROM untitle_table_4 limit 2",
|
2016-05-23 21:05:14 +08:00
|
|
|
"status": "done",
|
|
|
|
"fallback_status": "done"
|
2016-05-18 00:51:02 +08:00
|
|
|
}, {
|
|
|
|
"query": "SELECT * FROM untitle_table_4 limit 3",
|
2016-05-19 22:58:14 +08:00
|
|
|
"onsuccess": "SELECT * FROM nonexistent_table /* should fail */",
|
2016-05-23 21:05:14 +08:00
|
|
|
"status": "done",
|
|
|
|
"fallback_status": "failed",
|
2016-05-19 22:58:14 +08:00
|
|
|
"failed_reason": 'relation "nonexistent_table" does not exist'
|
2016-05-18 00:51:02 +08:00
|
|
|
}],
|
|
|
|
"onsuccess": "SELECT * FROM untitle_table_4 limit 5"
|
|
|
|
};
|
|
|
|
|
|
|
|
var interval = setInterval(function () {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job/' + fallbackJob.job_id + '?api_key=1234&',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
}, {
|
|
|
|
status: 200
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
var job = JSON.parse(res.body);
|
2016-05-23 21:05:14 +08:00
|
|
|
if (job.status === jobStatus.DONE && job.fallback_status === jobStatus.DONE) {
|
2016-05-20 20:42:53 +08:00
|
|
|
clearInterval(interval);
|
|
|
|
assert.deepEqual(job.query, expectedQuery);
|
|
|
|
done();
|
2016-05-23 21:05:14 +08:00
|
|
|
} else if (job.status === jobStatus.FAILED || job.status === jobStatus.CANCELLED) {
|
2016-05-20 20:42:53 +08:00
|
|
|
clearInterval(interval);
|
|
|
|
done(new Error('Job ' + job.job_id + ' is ' + job.status + ', expected to be done'));
|
|
|
|
}
|
|
|
|
});
|
2016-05-18 00:51:02 +08:00
|
|
|
}, 50);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-18 19:25:08 +08:00
|
|
|
describe('"onsuccess" for job & "onsuccess" for each query should not be triggered ' +
|
|
|
|
' because it has been cancelled', function () {
|
|
|
|
var fallbackJob = {};
|
|
|
|
|
|
|
|
it('should create a job', function (done) {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job?api_key=1234',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
data: querystring.stringify({
|
2016-05-18 19:25:08 +08:00
|
|
|
query: {
|
|
|
|
query: [{
|
|
|
|
query: "SELECT pg_sleep(3)",
|
|
|
|
onsuccess: "SELECT pg_sleep(0)"
|
|
|
|
}],
|
|
|
|
onsuccess: "SELECT pg_sleep(0)"
|
|
|
|
}
|
|
|
|
})
|
2016-05-20 20:42:53 +08:00
|
|
|
}, {
|
|
|
|
status: 201
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
fallbackJob = JSON.parse(res.body);
|
|
|
|
done();
|
|
|
|
});
|
2016-05-18 19:25:08 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('job should be running', function (done) {
|
|
|
|
var expectedQuery = {
|
|
|
|
"query": [{
|
|
|
|
"query": "SELECT pg_sleep(3)",
|
|
|
|
"onsuccess": "SELECT pg_sleep(0)",
|
2016-05-23 21:05:14 +08:00
|
|
|
"status": "running",
|
|
|
|
"fallback_status": "pending"
|
2016-05-18 19:25:08 +08:00
|
|
|
}],
|
|
|
|
"onsuccess": "SELECT pg_sleep(0)"
|
|
|
|
};
|
|
|
|
|
|
|
|
var interval = setInterval(function () {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job/' + fallbackJob.job_id + '?api_key=1234&',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
}, {
|
|
|
|
status: 200
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
var job = JSON.parse(res.body);
|
2016-05-23 21:05:14 +08:00
|
|
|
if (job.status === jobStatus.RUNNING && job.fallback_status === jobStatus.PENDING) {
|
2016-05-20 20:42:53 +08:00
|
|
|
clearInterval(interval);
|
|
|
|
assert.deepEqual(job.query, expectedQuery);
|
|
|
|
done();
|
2016-05-23 21:05:14 +08:00
|
|
|
} else if (job.status === jobStatus.DONE ||
|
|
|
|
job.status === jobStatus.FAILED ||
|
|
|
|
job.status === jobStatus.CANCELLED) {
|
2016-05-20 20:42:53 +08:00
|
|
|
clearInterval(interval);
|
|
|
|
done(new Error('Job ' + job.job_id + ' is ' + job.status + ', expected to be done'));
|
|
|
|
}
|
|
|
|
});
|
2016-05-18 19:25:08 +08:00
|
|
|
}, 50);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('job should be cancelled', function (done) {
|
|
|
|
var expectedQuery = {
|
|
|
|
"query": [{
|
|
|
|
"query": "SELECT pg_sleep(3)",
|
|
|
|
"onsuccess": "SELECT pg_sleep(0)",
|
2016-05-23 21:05:14 +08:00
|
|
|
"status": "cancelled",
|
|
|
|
"fallback_status": "pending"
|
2016-05-18 19:25:08 +08:00
|
|
|
}],
|
|
|
|
"onsuccess": "SELECT pg_sleep(0)"
|
|
|
|
};
|
|
|
|
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job/' + fallbackJob.job_id + '?api_key=1234&',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'DELETE'
|
|
|
|
}, {
|
|
|
|
status: 200
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
var job = JSON.parse(res.body);
|
2016-05-23 21:05:14 +08:00
|
|
|
if (job.status === jobStatus.CANCELLED && job.fallback_status === jobStatus.PENDING) {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.deepEqual(job.query, expectedQuery);
|
|
|
|
done();
|
2016-05-23 21:05:14 +08:00
|
|
|
} else if (job.status === jobStatus.DONE || job.status === jobStatus.FAILED) {
|
2016-05-20 20:42:53 +08:00
|
|
|
done(new Error('Job ' + job.job_id + ' is ' + job.status + ', expected to be cancelled'));
|
|
|
|
}
|
|
|
|
});
|
2016-05-18 19:25:08 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('first "onsuccess" should be triggered and it will be cancelled', function () {
|
|
|
|
var fallbackJob = {};
|
|
|
|
|
|
|
|
it('should create a job', function (done) {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job?api_key=1234',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
data: querystring.stringify({
|
2016-05-18 19:25:08 +08:00
|
|
|
query: {
|
|
|
|
query: [{
|
|
|
|
query: "SELECT pg_sleep(0)",
|
|
|
|
onsuccess: "SELECT pg_sleep(3)"
|
|
|
|
}],
|
|
|
|
onsuccess: "SELECT pg_sleep(0)"
|
|
|
|
}
|
|
|
|
})
|
2016-05-20 20:42:53 +08:00
|
|
|
}, {
|
|
|
|
status: 201
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
fallbackJob = JSON.parse(res.body);
|
|
|
|
done();
|
|
|
|
});
|
2016-05-18 19:25:08 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('job "onsuccess" should be running', function (done) {
|
|
|
|
var expectedQuery = {
|
|
|
|
"query": [{
|
|
|
|
"query": "SELECT pg_sleep(0)",
|
|
|
|
"onsuccess": "SELECT pg_sleep(3)",
|
2016-05-23 21:05:14 +08:00
|
|
|
"status": "done",
|
|
|
|
"fallback_status": "running"
|
2016-05-18 19:25:08 +08:00
|
|
|
}],
|
|
|
|
"onsuccess": "SELECT pg_sleep(0)"
|
|
|
|
};
|
|
|
|
|
|
|
|
var interval = setInterval(function () {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job/' + fallbackJob.job_id + '?api_key=1234&',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
}, {
|
|
|
|
status: 200
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
var job = JSON.parse(res.body);
|
2016-05-23 21:05:14 +08:00
|
|
|
if (job.query.query[0].status === jobStatus.DONE &&
|
|
|
|
job.query.query[0].fallback_status === jobStatus.RUNNING) {
|
2016-05-20 20:42:53 +08:00
|
|
|
clearInterval(interval);
|
|
|
|
assert.deepEqual(job.query, expectedQuery);
|
|
|
|
done();
|
2016-05-23 21:05:14 +08:00
|
|
|
} else if (job.query.query[0].status === jobStatus.DONE ||
|
|
|
|
job.query.query[0].status === jobStatus.FAILED ||
|
|
|
|
job.query.query[0].status === jobStatus.CANCELLED) {
|
2016-05-20 20:42:53 +08:00
|
|
|
clearInterval(interval);
|
|
|
|
done(new Error('Job ' + job.job_id + ' is ' +
|
|
|
|
job.query.query[0].status +
|
|
|
|
', expected to be running'));
|
|
|
|
}
|
|
|
|
});
|
2016-05-18 19:25:08 +08:00
|
|
|
}, 50);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('job should be cancelled', function (done) {
|
|
|
|
var expectedQuery = {
|
|
|
|
"query": [{
|
|
|
|
"query": "SELECT pg_sleep(0)",
|
|
|
|
"onsuccess": "SELECT pg_sleep(3)",
|
2016-05-23 21:05:14 +08:00
|
|
|
"status": "done",
|
|
|
|
"fallback_status": "cancelled"
|
2016-05-18 19:25:08 +08:00
|
|
|
}],
|
|
|
|
"onsuccess": "SELECT pg_sleep(0)"
|
|
|
|
};
|
|
|
|
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v2/sql/job/' + fallbackJob.job_id + '?api_key=1234&',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'host': 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'DELETE'
|
|
|
|
}, {
|
|
|
|
status: 200
|
|
|
|
}, function (res, err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
var job = JSON.parse(res.body);
|
2016-05-23 21:05:14 +08:00
|
|
|
if (job.status === jobStatus.CANCELLED && job.fallback_status === jobStatus.PENDING) {
|
2016-05-20 20:42:53 +08:00
|
|
|
assert.deepEqual(job.query, expectedQuery);
|
|
|
|
done();
|
2016-05-23 21:05:14 +08:00
|
|
|
} else if (job.status === jobStatus.DONE || job.status === jobStatus.FAILED) {
|
2016-05-20 20:42:53 +08:00
|
|
|
done(new Error('Job ' + job.job_id + ' is ' + job.status + ', expected to be cancelled'));
|
|
|
|
}
|
|
|
|
});
|
2016-05-18 19:25:08 +08:00
|
|
|
});
|
|
|
|
});
|
2016-05-16 19:49:56 +08:00
|
|
|
});
|