-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add patch functionality to lists (#691)
* add list endpoints to add and delete from list * use patch * add tests * add integration test * remove error throw * 7.9.0-0 * Revert "7.9.0-0" This reverts commit e3c7c5d.
- Loading branch information
Showing
5 changed files
with
178 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
'use strict'; | ||
|
||
const _isEqual = require('lodash/isEqual'); | ||
|
||
let db = require('./db'); | ||
|
||
/** | ||
* Appends an entry to a list and returns the updated list. | ||
* @param {Array<Object>} list | ||
* @param {Array<Object>} data | ||
* @returns {Array<Object>} | ||
*/ | ||
function addToList(list, data) { | ||
return list.concat(data); | ||
} | ||
|
||
/** | ||
* Removes an entry from a list and returns the updated list. | ||
* @param {Array<Object>} list | ||
* @param {Array<Object>} data | ||
* @returns {Array<Object>} | ||
*/ | ||
function removeFromList(list, data) { | ||
for (const deletion of data) { | ||
list = list.filter(entry => !_isEqual(entry, deletion)); | ||
} | ||
|
||
return list; | ||
} | ||
|
||
/** | ||
* Add or Remove an item from a list | ||
* @param {string} uri | ||
* @param {Array<Object>} data | ||
* @returns {Promise} | ||
*/ | ||
function patchList(uri, data) { | ||
if (!Array.isArray(data.add) && !Array.isArray(data.remove)) { | ||
throw new Error('Bad Request. List PATCH requires `add` or `remove` to be an array.'); | ||
} | ||
|
||
return db.get(uri).then(list => { | ||
if (Array.isArray(data.add)) { | ||
list = addToList(list, data.add); | ||
} | ||
|
||
if (Array.isArray(data.remove)) { | ||
list = removeFromList(list, data.remove); | ||
} | ||
|
||
// db.put wraps result in an object `{ _value: list }`, return list only | ||
// hopefully db.put never does anything to the data bc we're just returning list | ||
return db.put(uri, list).then(() => list); | ||
}); | ||
} | ||
|
||
module.exports.patchList = patchList; | ||
|
||
// For testing | ||
module.exports.setDb = mock => db = mock; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'), | ||
filename = __filename.split('/').pop().split('.').shift(), | ||
lib = require('./' + filename), | ||
sinon = require('sinon'), | ||
expect = require('chai').expect, | ||
storage = require('../../test/fixtures/mocks/storage'); | ||
|
||
describe(_.startCase(filename), function () { | ||
let sandbox, db; | ||
|
||
beforeEach(function () { | ||
sandbox = sinon.sandbox.create(); | ||
|
||
db = storage(); | ||
lib.setDb(db); | ||
}); | ||
|
||
afterEach(function () { | ||
sandbox.restore(); | ||
}); | ||
|
||
describe('patchList', function () { | ||
const fn = lib.patchList; | ||
|
||
it('will throw if bad request body', function () { | ||
try { | ||
fn('domain.com/_lists/test', {}); | ||
} catch (e) { | ||
expect(e.message).to.eql('Bad Request. List PATCH requires `add` or `remove` to be an array.'); | ||
} | ||
}); | ||
|
||
it('adds to existing lists if has add property', function () { | ||
db.get.resolves([]); | ||
db.put.callsFake((uri, list) => Promise.resolve({ _value: list })); | ||
|
||
return fn('domain.com/_lists/test', { add: [ 'hello' ] }).then(list => { | ||
expect(list).to.eql([ 'hello' ]); | ||
}); | ||
}); | ||
|
||
it('removes from existing lists if has remove property', function () { | ||
db.get.resolves([ 'hello' ]); | ||
db.put.callsFake((uri, list) => Promise.resolve({ _value: list })); | ||
|
||
return fn('domain.com/_lists/test', { remove: [ 'hello' ] }).then(list => { | ||
expect(list).to.eql([]); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'), | ||
apiAccepts = require('../../fixtures/api-accepts'), | ||
endpointName = _.startCase(__dirname.split('/').pop()), | ||
filename = _.startCase(__filename.split('/').pop().split('.')[0]), | ||
sinon = require('sinon'); | ||
|
||
describe(endpointName, function () { | ||
describe(filename, function () { | ||
let sandbox, | ||
hostname = 'localhost.example.com', | ||
acceptsJsonBody = apiAccepts.acceptsJsonBody(_.camelCase(filename)), | ||
start = ['item1', 'item2'], | ||
data = { | ||
add: ['item3', 'item4'], | ||
remove: ['item1'] | ||
}, | ||
end = ['item2', 'item3', 'item4']; | ||
|
||
beforeEach(function () { | ||
sandbox = sinon.sandbox.create(); | ||
return apiAccepts.beforeEachTest({ sandbox, hostname, pathsAndData: {'/_lists/valid': start} }); | ||
}); | ||
|
||
afterEach(function () { | ||
sandbox.restore(); | ||
}); | ||
|
||
describe('/_lists/:name', function () { | ||
const path = this.title; | ||
|
||
// overrides existing data | ||
acceptsJsonBody(path, {name: 'valid'}, data, 200, end); | ||
acceptsJsonBody(path, {name: 'missing'}, data, 404); | ||
}); | ||
}); | ||
}); |