Skip to content

Commit

Permalink
Respond with notfound
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellmorten committed Mar 30, 2018
1 parent 6aa04d4 commit 48bae07
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
53 changes: 53 additions & 0 deletions lib/adapter/send-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions lib/adapter/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <post@kjellmorten.no>",
Expand Down

0 comments on commit 48bae07

Please sign in to comment.