From 48bae078c7643a9410ef85d22519ef8b0abba297 Mon Sep 17 00:00:00 2001 From: Kjell-Morten Date: Fri, 30 Mar 2018 18:22:03 +0200 Subject: [PATCH] Respond with notfound --- lib/adapter/send-test.js | 53 ++++++++++++++++++++++++++++++++++++++++ lib/adapter/send.js | 7 ++++++ package.json | 2 +- 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/lib/adapter/send-test.js b/lib/adapter/send-test.js index e85c11d..44bcbd6 100644 --- a/lib/adapter/send-test.js +++ b/lib/adapter/send-test.js @@ -102,6 +102,59 @@ test('should get with query', async (t) => { t.deepEqual(arg, expected) }) +test('should return empty array when not one item', async (t) => { + const find = sinon.stub().returns({ + toArray: () => [] + }) + const connection = createConnection({find}) + const request = { + action: 'GET', + params: { + type: 'entry', + typePlural: 'entries' + }, + endpoint: { + collection: 'documents', + db: 'database' + } + } + const expected = { + status: 'ok', + data: [] + } + + const response = await send(request, connection) + + t.deepEqual(response, expected) +}) + +test('should return notfound when one item not found', async (t) => { + const find = sinon.stub().returns({ + toArray: () => [] + }) + const connection = createConnection({find}) + const request = { + action: 'GET', + params: { + id: 'ent1', + type: 'entry', + typePlural: 'entries' + }, + endpoint: { + collection: 'documents', + db: 'database' + } + } + const expected = { + status: 'notfound', + error: 'Could not find \'ent1\' of type \'entry\'' + } + + const response = await send(request, connection) + + t.deepEqual(response, expected) +}) + test('should update one item', async (t) => { const updateOne = sinon.stub().returns({ matchedCount: 1, modifiedCount: 1, upsertedCount: 0 diff --git a/lib/adapter/send.js b/lib/adapter/send.js index 94cd83f..126e19f 100644 --- a/lib/adapter/send.js +++ b/lib/adapter/send.js @@ -64,6 +64,13 @@ const getData = async (getCollection, {endpoint, params}) => { const filter = prepareFilter(params, endpoint, params) const data = await collection.find(filter).toArray() + if (data.length === 0 && params.id) { + return { + status: 'notfound', + error: `Could not find '${params.id}' of type '${params.type}'` + } + } + return {status: 'ok', data} } diff --git a/package.json b/package.json index b4c44ab..62debb8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "integreat-adapter-mongodb", - "version": "0.1.2", + "version": "0.1.3", "description": "Integreat adapter for mongodb", "main": "index.js", "author": "Kjell-Morten Bratsberg Thorsen ",