2016-10-06 17:53:56 +08:00
|
|
|
require('../../helper');
|
2016-06-29 20:22:23 +08:00
|
|
|
|
2016-10-06 17:53:56 +08:00
|
|
|
var assert = require('../../support/assert');
|
2016-11-24 20:10:29 +08:00
|
|
|
var TestClient = require('../../support/test-client');
|
|
|
|
var JobStatus = require('../../../batch/job_status');
|
|
|
|
var BatchTestClient = require('../../support/batch-test-client');
|
2016-06-29 20:22:23 +08:00
|
|
|
|
2016-06-30 23:41:02 +08:00
|
|
|
describe('Batch API callback templates', function () {
|
2016-11-24 20:10:29 +08:00
|
|
|
before(function () {
|
|
|
|
this.batchTestClient = new BatchTestClient();
|
|
|
|
this.testClient = new TestClient();
|
|
|
|
});
|
2016-06-29 20:22:23 +08:00
|
|
|
|
2016-11-24 20:10:29 +08:00
|
|
|
after(function (done) {
|
|
|
|
this.batchTestClient.drain(done);
|
|
|
|
});
|
2016-06-29 20:22:23 +08:00
|
|
|
|
2016-11-24 20:10:29 +08:00
|
|
|
it('should use templates for error_message and job_id onerror callback' +
|
|
|
|
' and keep the original templated query but use the error message', function (done) {
|
|
|
|
var self = this;
|
|
|
|
var payload = {
|
|
|
|
"query": {
|
|
|
|
"query": [
|
|
|
|
{
|
|
|
|
"query": "SELECT * FROM invalid_table",
|
|
|
|
"onerror": "INSERT INTO test_batch_errors " +
|
2016-11-24 20:25:14 +08:00
|
|
|
"values ('<%= job_id %>', '<%= error_message %>')"
|
2016-11-24 20:10:29 +08:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var expectedQuery = {
|
|
|
|
query: [
|
|
|
|
{
|
|
|
|
"query": "SELECT * FROM invalid_table",
|
|
|
|
"onerror": "INSERT INTO test_batch_errors values ('<%= job_id %>', '<%= error_message %>')",
|
|
|
|
status: 'failed',
|
|
|
|
fallback_status: 'done'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
2016-06-29 20:22:23 +08:00
|
|
|
|
2016-11-24 20:10:29 +08:00
|
|
|
self.testClient.getResult(
|
|
|
|
'create table test_batch_errors (job_id text, error_message text)', function (err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2016-06-29 20:22:23 +08:00
|
|
|
|
2016-11-24 20:10:29 +08:00
|
|
|
self.batchTestClient.createJob(payload, function(err, jobResult) {
|
2016-07-01 01:40:36 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
2016-06-29 20:22:23 +08:00
|
|
|
}
|
|
|
|
|
2016-11-24 20:10:29 +08:00
|
|
|
jobResult.getStatus(JobStatus.FAILED, function (err, job) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
2016-07-01 01:12:45 +08:00
|
|
|
}
|
2016-11-24 22:40:09 +08:00
|
|
|
jobResult.validateExpectedResponse(expectedQuery);
|
2016-11-24 20:10:29 +08:00
|
|
|
self.testClient.getResult('select * from test_batch_errors', function(err, rows) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
2016-06-29 20:22:23 +08:00
|
|
|
}
|
2016-11-24 20:10:29 +08:00
|
|
|
assert.equal(rows[0].job_id, job.job_id);
|
|
|
|
assert.equal(rows[0].error_message, 'relation "invalid_table" does not exist');
|
|
|
|
self.testClient.getResult('drop table test_batch_errors', done);
|
|
|
|
});
|
|
|
|
});
|
2016-06-29 20:22:23 +08:00
|
|
|
});
|
|
|
|
});
|
2016-11-24 20:10:29 +08:00
|
|
|
});
|
2016-06-29 20:22:23 +08:00
|
|
|
|
2016-11-24 20:10:29 +08:00
|
|
|
it('should use template for job_id onsuccess callback ' +
|
|
|
|
'and keep the original templated query but use the job_id', function (done) {
|
|
|
|
var self = this;
|
|
|
|
var payload = {
|
|
|
|
"query": {
|
|
|
|
"query": [
|
2016-06-29 20:22:23 +08:00
|
|
|
{
|
2016-11-24 20:10:29 +08:00
|
|
|
query: "create table batch_jobs (job_id text)"
|
2016-06-29 20:22:23 +08:00
|
|
|
},
|
|
|
|
{
|
2016-11-24 20:10:29 +08:00
|
|
|
"query": "SELECT 1",
|
|
|
|
"onsuccess": "INSERT INTO batch_jobs values ('<%= job_id %>')"
|
2016-06-29 20:22:23 +08:00
|
|
|
}
|
|
|
|
]
|
2016-11-24 20:10:29 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
var expectedQuery = {
|
|
|
|
query: [
|
|
|
|
{
|
|
|
|
query: "create table batch_jobs (job_id text)",
|
|
|
|
status: 'done'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
query: "SELECT 1",
|
|
|
|
onsuccess: "INSERT INTO batch_jobs values ('<%= job_id %>')",
|
|
|
|
status: 'done',
|
|
|
|
fallback_status: 'done'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
self.batchTestClient.createJob(payload, function(err, jobResult) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
jobResult.getStatus(function (err, job) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
2016-11-24 22:40:09 +08:00
|
|
|
jobResult.validateExpectedResponse(expectedQuery);
|
2016-11-24 20:10:29 +08:00
|
|
|
self.testClient.getResult('select * from batch_jobs', function(err, rows) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
2016-06-29 20:22:23 +08:00
|
|
|
}
|
2016-11-24 20:10:29 +08:00
|
|
|
assert.equal(rows[0].job_id, job.job_id);
|
|
|
|
|
|
|
|
self.testClient.getResult('drop table batch_jobs', done);
|
2016-06-29 20:22:23 +08:00
|
|
|
});
|
2016-11-24 20:10:29 +08:00
|
|
|
});
|
2016-06-29 20:22:23 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|