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 ",