From 6d47e0df895f050f780d60c8dcf37ea7c454e623 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Mon, 13 Apr 2020 16:46:28 +0100 Subject: [PATCH] allow deletion of model --- .../server/middleware/controllers/model.js | 6 ++++- .../server/middleware/routes/neo/model.js | 2 +- .../routes/neo/tests/couchTestUtils.js | 7 ++++-- .../middleware/routes/neo/tests/model.spec.js | 25 +++++++++++++++++++ 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/packages/server/middleware/controllers/model.js b/packages/server/middleware/controllers/model.js index 0b0a71d6a6..f9b7f8b968 100644 --- a/packages/server/middleware/controllers/model.js +++ b/packages/server/middleware/controllers/model.js @@ -61,5 +61,9 @@ exports.update = async function(ctx) { exports.destroy = async function(ctx) { const db = couchdb.db.use(ctx.params.instanceId) - ctx.body = await db.destroy(ctx.params.modelId, ctx.params.rev); + const model = await db.destroy(ctx.params.modelId, ctx.params.revId); + ctx.body = { + message: `Model ${model.id} deleted.`, + status: 200 + } } diff --git a/packages/server/middleware/routes/neo/model.js b/packages/server/middleware/routes/neo/model.js index 91839a0a90..7166cd3429 100644 --- a/packages/server/middleware/routes/neo/model.js +++ b/packages/server/middleware/routes/neo/model.js @@ -7,7 +7,7 @@ router .get("/api/:instanceId/models", controller.fetch) .post("/api/:instanceId/models", controller.create) // .patch("/api/:instanceId/models", controller.update) - // .delete("/api/:instanceId/models/:modelId", controller.delete) + .delete("/api/:instanceId/models/:modelId/:revId", controller.destroy); module.exports = router; \ No newline at end of file diff --git a/packages/server/middleware/routes/neo/tests/couchTestUtils.js b/packages/server/middleware/routes/neo/tests/couchTestUtils.js index c7a892297c..e6bcd8d181 100644 --- a/packages/server/middleware/routes/neo/tests/couchTestUtils.js +++ b/packages/server/middleware/routes/neo/tests/couchTestUtils.js @@ -16,8 +16,11 @@ exports.createModel = async instanceId => { } ] } - await couchdb.db.use(instanceId).insert(model); - return model; + const response = await couchdb.db.use(instanceId).insert(model); + return { + ...response, + ...model + }; } exports.createClientDatabase = async () => { diff --git a/packages/server/middleware/routes/neo/tests/model.spec.js b/packages/server/middleware/routes/neo/tests/model.spec.js index dbc7e50876..f296f6a923 100644 --- a/packages/server/middleware/routes/neo/tests/model.spec.js +++ b/packages/server/middleware/routes/neo/tests/model.spec.js @@ -80,4 +80,29 @@ describe("/models", () => { }); }) }); + + describe("destroy", () => { + let testModel; + + beforeEach(async () => { + await createInstanceDatabase(TEST_INSTANCE_ID); + testModel = await createModel(TEST_INSTANCE_ID); + }); + + afterEach(async () => { + await destroyDatabase(TEST_INSTANCE_ID); + }); + + it("returns all the models for that instance in the response body", done => { + request + .delete(`/api/${TEST_INSTANCE_ID}/models/${testModel.id}/${testModel.rev}`) + .set("Accept", "application/json") + .expect('Content-Type', /json/) + .expect(200) + .end(async (_, res) => { + expect(res.body.message).toEqual(`Model ${testModel.id} deleted.`); + done(); + }); + }) + }); });