Skip to content

Commit

Permalink
feat(custom-js): implement DELETE /processors/{processor_id}
Browse files Browse the repository at this point in the history
  • Loading branch information
syncush committed Nov 9, 2019
1 parent 648b0c8 commit 7bbd22b
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/processors/controllers/processorController.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,13 @@ module.exports.getProcessor = async function (req, res, next) {
return next(err);
}
};

module.exports.deleteProcessor = async function (req, res, next) {
let { params: { processor_id: processorId } } = req;
try {
await processorManager.deleteProcessor(processorId);
return res.status(204).json();
} catch (err) {
return next(err);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ let client;
const INSERT_PROCESSOR = 'INSERT INTO processors(id, name, description, type, file_url, javascript, created_at, updated_at) values(?,?,?,?,?,?,?,?)';
const GET_ALL_PROCESSORS = 'SELECT * FROM processors';
const GET_PROCESSOR = 'SELECT * FROM processors where id=?';
const DELETE_PROCESSOR = 'DELETE FROM processors WHERE id=?';

module.exports = {
init,
insertProcessor,
getAllProcessors,
getProcessor
getProcessor,
deleteProcessor
};

let queryOptions = {
Expand All @@ -33,6 +35,11 @@ async function getProcessor(processorId) {
return processor[0];
}

function deleteProcessor(processorId) {
let params = [processorId];
return executeQuery(DELETE_PROCESSOR, params, queryOptions);
}

async function insertProcessor(processorId, processorInfo) {
let params = [processorId, processorInfo.name, processorInfo.description, processorInfo.type, processorInfo.file_url, processorInfo.javascript, Date.now(), Date.now()];
const processor = await executeQuery(INSERT_PROCESSOR, params, queryOptions);
Expand Down
5 changes: 5 additions & 0 deletions src/processors/models/database/databaseConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
getAllProcessors,
insertProcessor,
getProcessor,
deleteProcessor,
closeConnection
};

Expand All @@ -29,3 +30,7 @@ async function getAllProcessors(from, limit) {
async function getProcessor(processorId) {
return databaseConnector.getProcessor(processorId);
}

async function deleteProcessor(processorId) {
return databaseConnector.deleteProcessor(processorId);
}
12 changes: 11 additions & 1 deletion src/processors/models/database/sequelize/sequelizeConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ module.exports = {
init,
getAllProcessors,
insertProcessor,
getProcessor
getProcessor,
deleteProcessor
};

async function init(sequelizeClient) {
Expand Down Expand Up @@ -44,6 +45,15 @@ async function getProcessor(processorId) {
return processors[0];
}

async function deleteProcessor(processorId) {
const processorsModel = client.model('processor');
return processorsModel.destroy({
where: {
id: processorId
}
});
}

async function initSchemas() {
const processorsFiles = client.define('processor', {
id: {
Expand Down
4 changes: 4 additions & 0 deletions src/processors/models/processorsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ module.exports.getProcessor = async function(processorId) {
throw error;
}
};

module.exports.deleteProcessor = async function (processorId) {
return databaseConnector.deleteProcessor(processorId);
};
3 changes: 2 additions & 1 deletion src/processors/routes/processorsRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ let processors = require('../controllers/processorController');
router.get('/', swaggerValidator.validate, processors.getAllProcessors);
router.post('/', swaggerValidator.validate, processors.createProcessor);
router.get('/:processor_id', swaggerValidator.validate, processors.getProcessor);
router.delete('/:processor_id', swaggerValidator.validate, processors.deleteProcessor);

module.exports = router;
module.exports = router;
10 changes: 9 additions & 1 deletion tests/integration-tests/processors/helpers/requestCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ module.exports = {
init,
createProcessor,
getProcessors,
getProcessor
getProcessor,
deleteProcessor
};

async function init() {
Expand Down Expand Up @@ -37,6 +38,13 @@ function getProcessors(from, limit) {
return res;
});
}
function deleteProcessor(processorId) {
return request(app).delete(`/v1/processors/${processorId}`)
.set({ 'Content-Type': 'application/json' })
.expect(function (res) {
return res;
});
}

function getProcessor(processorId) {
return request(app).get(`/v1/processors/${processorId}`)
Expand Down
23 changes: 22 additions & 1 deletion tests/integration-tests/processors/processors-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,28 @@ describe('Processors api', function() {
should(processors.length).greaterThanOrEqual(101);
});
after(async function() {
// TODO: when DELETE /processors is implemented, use processorsInserted to empty the table.
const processorIds = processorsInserted.map(processor => processor.body.id);
processorIds.forEach(async (processorId) => {
await requestSender.deleteProcessor(processorId);
});
});
});
describe('DELETE /v1/processors/{processor_id}', () => {
it('insert a feature and then delete it', async () => {
const processor = generateRawJSProcessor('some_id');
const insertResponse = await requestSender.createProcessor(processor, validHeaders);
should(insertResponse.statusCode).equal(201);
const processorId = insertResponse.body.id;
const deleteResponse = await requestSender.deleteProcessor(processorId);
should(deleteResponse.statusCode).equal(204);
});

it('delete a processor that doesn\'t exist - expect status code 204', async () => {
const processorId = uuid();
// making sure that there is no processor with the generated uuid (not taking any chances :) )
await requestSender.deleteProcessor(processorId);
const deleteResponse = await requestSender.deleteProcessor(processorId);
should(deleteResponse.statusCode).equal(204);
});
});
describe('GET /v1/processors/{processor_id}', function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,12 @@ describe('Sequelize client tests', function () {
should(paramsArg).has.properties(['created_at', 'updated_at']);
});
});

describe('Delete processor', () => {
it('validate query', async () => {
const processorId = 'A-B-C';
await sequelizeConnector.deleteProcessor(processorId);
should(sequelizeDeleteStub.args[0][0]).deepEqual({ where: { id: processorId } });
});
});
});

0 comments on commit 7bbd22b

Please sign in to comment.