Skip to content

Avoids to create a non-valid job #347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions batch/models/job_multiple.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,52 @@ util.inherits(JobMultiple, JobBase);

module.exports = JobMultiple;

JobMultiple.is = function (query) {
if (!Array.isArray(query)) {
return false;
function isJobParsed(query) {
// From backend: [{ query: 'select * from ...', status: 'pending' },
// { query: 'select * from ...', status: 'pending' } ]

for (var i = 0; i < query.length; i++) {
if (typeof query[i] !== 'object') {
return false;
}

if (typeof query[i].query !== 'string') {
return false;
}

if (typeof query[i].status !== 'string') {
return false;
}
}

// 1. From user: ['select * from ...', 'select * from ...']
// 2. From redis: [ { query: 'select * from ...', status: 'pending' },
// { query: 'select * from ...', status: 'pending' } ]
return true;
}

function isJobRaw(query) {
// From user: ['select * from ...', 'select * from ...']
for (var i = 0; i < query.length; i++) {
if (typeof query[i] !== 'string') {
if (typeof query[i].query !== 'string') {
return false;
}
return false;
}

if (!query[i].length) {
return false;
}
}

return true;
}

JobMultiple.is = function (query) {

if (!Array.isArray(query)) {
return false;
}

if (!isJobRaw(query) && !isJobParsed(query)) {
return false;
}

return true;
};

Expand Down
36 changes: 36 additions & 0 deletions test/integration/batch/job_factory.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

require('../../helper');

var BATCH_SOURCE = '../../../batch/';
var assert = require('../../support/assert');
var JobFactory = require(BATCH_SOURCE + 'models/job_factory');

describe('job factory', function() {

it('should throw error with invalid fallback query', function () {
var jobRaw = {
query: [{
query: "select * from wadus_table",
onerror: "select * from wadus_table"
}]
};

assert.throws(function () {
JobFactory.create(jobRaw);
});
});

it('should throw error with void queries', function () {
var jobRaw = {
query: [
'',
''
]
};

assert.throws(function () {
JobFactory.create(jobRaw);
});
});
});