Skip to content

Commit

Permalink
Merge 912749f into 16077b4
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellmorten committed Jun 11, 2018
2 parents 16077b4 + 912749f commit ff9111e
Show file tree
Hide file tree
Showing 94 changed files with 4,259 additions and 5,165 deletions.
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
*-test.js
.vscode
.*
tests
346 changes: 173 additions & 173 deletions README.md

Large diffs are not rendered by default.

92 changes: 46 additions & 46 deletions lib/actions/delete-test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import test from 'ava'
import nock from 'nock'
import json from '../adapters/json'
import source from '../source'
import datatype from '../datatype'
import service from '../service'
import schema from '../schema'
import createEndpoint from '../../tests/helpers/createEndpoint'
import createMapping from '../mapping'

import deleteFn from './delete'

// Helpers

const datatypes = {
entry: datatype({
const schemas = {
entry: schema({
id: 'entry',
attributes: {
type: 'string',
title: {default: 'A title'}
}
}),
account: datatype({
account: schema({
id: 'account',
attributes: {
name: 'string'
Expand All @@ -30,22 +30,22 @@ const datatypes = {
const mappings = [
createMapping({
type: 'entry',
source: 'entries',
service: 'entries',
attributes: {id: 'id', title: 'header'}
}, {datatypes}),
}, {schemas}),
createMapping({
type: 'account',
source: 'accounts',
service: 'accounts',
attributes: {id: 'id', name: 'name'}
}, {datatypes})
}, {schemas})
]
test.after.always(() => {
nock.restore()
})

// Tests

test('should delete items from source', async (t) => {
test('should delete items from service', async (t) => {
const scope = nock('http://api1.test')
.post('/database/bulk_delete', {docs: [{id: 'ent1'}, {id: 'ent2'}]})
.reply(200, [{ok: true, id: 'ent1', rev: '2-000001'}, {ok: true, id: 'ent2', rev: '2-000001'}])
Expand All @@ -55,24 +55,24 @@ test('should delete items from source', async (t) => {
path: 'docs[]',
method: 'POST'
})]
const src = source({id: 'entries', adapter: json, endpoints}, {mappings})
const getSource = (type, source) => (source === 'entries') ? src : null
const src = service({id: 'entries', adapter: json, endpoints}, {mappings})
const getService = (type, service) => (service === 'entries') ? src : null
const payload = {
data: [
{id: 'ent1', type: 'entry'},
{id: 'ent2', type: 'entry'}
],
source: 'entries'
service: 'entries'
}
const expected = {status: 'ok'}

const ret = await deleteFn({payload}, {getSource})
const ret = await deleteFn({payload}, {getService})

t.deepEqual(ret, expected)
t.true(scope.isDone())
})

test('should delete one item from source', async (t) => {
test('should delete one item from service', async (t) => {
const scope = nock('http://api1.test')
.delete('/database/ent1')
.reply(200, {ok: true, id: 'ent1', rev: '000001'})
Expand All @@ -82,18 +82,18 @@ test('should delete one item from source', async (t) => {
uri: 'http://api1.test/database/{id}',
method: 'DELETE'
})]
const src = source({id: 'entries', adapter: json, endpoints}, {mappings})
const getSource = (type, source) => (source === 'entries') ? src : null
const payload = {id: 'ent1', type: 'entry', source: 'entries'}
const src = service({id: 'entries', adapter: json, endpoints}, {mappings})
const getService = (type, service) => (service === 'entries') ? src : null
const payload = {id: 'ent1', type: 'entry', service: 'entries'}

const ret = await deleteFn({payload}, {getSource})
const ret = await deleteFn({payload}, {getService})

t.truthy(ret)
t.is(ret.status, 'ok', ret.error)
t.true(scope.isDone())
})

test('should infer source id from type', async (t) => {
test('should infer service id from type', async (t) => {
const scope = nock('http://api2.test')
.post('/database/bulk_delete', {docs: [{id: 'ent1'}, {id: 'ent2'}]})
.reply(200, [{ok: true, id: 'ent1', rev: '2-000001'}, {ok: true, id: 'ent2', rev: '2-000001'}])
Expand All @@ -103,14 +103,14 @@ test('should infer source id from type', async (t) => {
path: 'docs[]',
method: 'POST'
})]
const src = source({id: 'entries', adapter: json, endpoints}, {mappings})
const getSource = (type, source) => (type === 'entry') ? src : null
const src = service({id: 'entries', adapter: json, endpoints}, {mappings})
const getService = (type, service) => (type === 'entry') ? src : null
const payload = {
data: [{id: 'ent1', type: 'entry'}, {id: 'ent2', type: 'entry'}],
type: 'entry'
}

const ret = await deleteFn({payload}, {getSource})
const ret = await deleteFn({payload}, {getService})

t.truthy(ret)
t.is(ret.status, 'ok', ret.error)
Expand All @@ -126,16 +126,16 @@ test('should delete with other endpoint and uri params', async (t) => {
uri: 'http://api3.test/{typefolder}/bulk_delete',
method: 'POST'
})]
const src = source({id: 'entries', adapter: json, endpoints}, {mappings})
const getSource = (type, source) => src
const src = service({id: 'entries', adapter: json, endpoints}, {mappings})
const getService = (type, service) => src
const payload = {
data: [{id: 'ent1', type: 'entry'}, {id: 'ent2', type: 'entry'}],
type: 'entry',
endpoint: 'other',
params: {typefolder: 'entries'}
}

const ret = await deleteFn({payload}, {getSource})
const ret = await deleteFn({payload}, {getService})

t.truthy(ret)
t.is(ret.status, 'ok', ret.error)
Expand All @@ -152,14 +152,14 @@ test('should return error from response', async (t) => {
path: 'docs[]',
method: 'POST'
})]
const src = source({id: 'entries', adapter: json, endpoints}, {mappings})
const getSource = (type, source) => src
const src = service({id: 'entries', adapter: json, endpoints}, {mappings})
const getService = (type, service) => src
const payload = {
data: [{id: 'ent1', type: 'entry'}],
type: 'entry'
}

const ret = await deleteFn({payload}, {getSource})
const ret = await deleteFn({payload}, {getService})

t.truthy(ret)
t.is(ret.status, 'notfound', ret.error)
Expand All @@ -170,28 +170,28 @@ test('should return error from response', async (t) => {

test('should return noaction when nothing to delete', async (t) => {
const endpoints = [createEndpoint({id: 'delete', uri: 'http://api1.test/database/bulk_delete'})]
const src = source({id: 'entries', adapter: json, endpoints}, {mappings})
const getSource = (type, source) => src
const payload = {data: [], source: 'entries'}
const src = service({id: 'entries', adapter: json, endpoints}, {mappings})
const getService = (type, service) => src
const payload = {data: [], service: 'entries'}

const ret = await deleteFn({payload}, {getSource})
const ret = await deleteFn({payload}, {getService})

t.truthy(ret)
t.is(ret.status, 'noaction')
})

test('should skip null values in data array', async (t) => {
const endpoints = [createEndpoint({id: 'delete', uri: 'http://api1.test/database/bulk_delete'})]
const src = source({id: 'entries', adapter: json, endpoints}, {mappings})
const getSource = (type, source) => src
const payload = {data: [null], source: 'entries'}
const src = service({id: 'entries', adapter: json, endpoints}, {mappings})
const getService = (type, service) => src
const payload = {data: [null], service: 'entries'}

const ret = await deleteFn({payload}, {getSource})
const ret = await deleteFn({payload}, {getService})

t.is(ret.status, 'noaction')
})

test('should delete items from source', async (t) => {
test('should delete items from service', async (t) => {
const scope = nock('http://api4.test')
.post('/database/bulk_delete', {docs: [{id: 'johnf'}]})
.reply(200, [{ok: true, id: 'ent1', rev: '2-000001'}, {ok: true, id: 'ent2', rev: '2-000001'}])
Expand All @@ -201,24 +201,24 @@ test('should delete items from source', async (t) => {
path: 'docs[]',
method: 'POST'
})]
const src = source({id: 'accounts', adapter: json, endpoints}, {mappings})
const getSource = (type, source) => (source === 'accounts') ? src : null
const src = service({id: 'accounts', adapter: json, endpoints}, {mappings})
const getService = (type, service) => (service === 'accounts') ? src : null
const payload = {
data: [
{id: 'johnf', type: 'account'},
{id: 'betty', type: 'account'}
],
source: 'accounts'
service: 'accounts'
}
const ident = {id: 'johnf'}

const ret = await deleteFn({payload, ident}, {getSource})
const ret = await deleteFn({payload, ident}, {getService})

t.is(ret.status, 'ok', ret.error)
t.true(scope.isDone())
})

test('should return error if no getSource', async (t) => {
test('should return error if no getService', async (t) => {
const payload = {id: 'ent1', type: 'entry'}

const ret = await deleteFn({payload})
Expand All @@ -228,10 +228,10 @@ test('should return error if no getSource', async (t) => {

test('should return error if no payload', async (t) => {
const payload = null
const src = source({id: 'entries', adapter: json}, {mappings})
const getSource = () => src
const src = service({id: 'entries', adapter: json}, {mappings})
const getService = () => src

const ret = await deleteFn({payload}, {getSource})
const ret = await deleteFn({payload}, {getService})

t.truthy(ret)
t.is(ret.status, 'error')
Expand Down
28 changes: 14 additions & 14 deletions lib/actions/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const prepareData = (payload) => {
}
}

const sendDeleteRequest = async (source, payload, data, ident) => {
const sendDeleteRequest = async (service, payload, data, ident) => {
const {type, id, params, endpoint} = payload
const request = {
action: 'DELETE',
Expand All @@ -24,41 +24,41 @@ const sendDeleteRequest = async (source, payload, data, ident) => {
data,
access: {ident}
}
const {response} = await source.send(request)
const {response} = await service.send(request)

return (response.status === 'ok') ? {status: 'ok'} : response
}

/**
* Delete several items from a source, based on the given payload.
* Delete several items from a service, based on the given payload.
* @param {Object} payload - Payload from action object
* @param {Object} resources - Object with getSource
* @returns {Object} Response object with any data returned from the source
* @param {Object} resources - Object with getService
* @returns {Object} Response object with any data returned from the service
*/
async function deleteFn ({payload, ident}, {getSource} = {}) {
async function deleteFn ({payload, ident}, {getService} = {}) {
debug('Action: DELETE')
if (!payload) {
debug('DELETE: No payload')
return createError('No payload')
}

const {type, id, source: sourceId, endpoint} = payload
const {type, id, service: serviceId, endpoint} = payload

const source = (typeof getSource === 'function') ? getSource(type, sourceId) : null
if (!source) {
debug('DELETE: No source')
return createError('No source')
const service = (typeof getService === 'function') ? getService(type, serviceId) : null
if (!service) {
debug('DELETE: No service')
return createError('No service')
}

const data = prepareData(payload)
if (data.length === 0) {
return createError(`No items to delete from source '${source.id}'`, 'noaction')
return createError(`No items to delete from service '${service.id}'`, 'noaction')
}

const endpointDebug = (endpoint) ? `endpoint '${endpoint}'` : `endpoint matching ${type} and ${id}`
debug('DELETE: Delete from source \'%s\' at %s.', source.id, endpointDebug)
debug('DELETE: Delete from service \'%s\' at %s.', service.id, endpointDebug)

return sendDeleteRequest(source, payload, data, ident)
return sendDeleteRequest(service, payload, data, ident)
}

module.exports = deleteFn
Loading

0 comments on commit ff9111e

Please sign in to comment.