Skip to content
This repository has been archived by the owner on Dec 17, 2018. It is now read-only.

Support $select in get and make sure patch many only returns changed items #32

Merged
merged 4 commits into from
Nov 11, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 103 additions & 23 deletions src/common-tests.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { expect } from 'chai';

function common (app, errors, serviceName = 'people', idProp = 'id') {
describe(`Common tests, ${serviceName} service with ${idProp} id property`, () => {
describe(`Common tests, ${serviceName} service with` +
` '${idProp}' id property`, () => {

const _ids = {};

beforeEach(() =>
Expand All @@ -20,7 +22,8 @@ function common (app, errors, serviceName = 'people', idProp = 'id') {
);

it('sets `events` property from options', () =>
expect(app.service(serviceName).events).to.deep.equal([ 'testing' ])
expect(app.service(serviceName).events.indexOf('testing'))
.to.not.equal(-1)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's checking for Array index in case we already have existing events (the case e.g. in RethinkDB)

);

describe('extend', () => {
Expand All @@ -44,22 +47,44 @@ function common (app, errors, serviceName = 'people', idProp = 'id') {
return app.service(serviceName).get(_ids.Doug).then(data => {
expect(data[idProp].toString()).to.equal(_ids.Doug.toString());
expect(data.name).to.equal('Doug');
expect(data.age).to.equal(32);
});
});

it('returns NotFound error for non-existing id', () => {
return app.service(serviceName).get('568225fbfe21222432e836ff').catch(error => {
expect(error instanceof errors.NotFound).to.be.ok;
expect(error.message).to.equal('No record found for id \'568225fbfe21222432e836ff\'');
it('supports $select', () => {
return app.service(serviceName).get(_ids.Doug, {
query: { $select: [ 'name' ] }
}).then(data => {
expect(data[idProp].toString()).to.equal(_ids.Doug.toString());
expect(data.name).to.equal('Doug');
expect(data.age).to.not.exist;
});
});

it('returns NotFound error for non-existing id', () => {
return app.service(serviceName).get('568225fbfe21222432e836ff')
.catch(error => {
expect(error instanceof errors.NotFound).to.be.ok;
expect(error.message).to.equal('No record found for id \'568225fbfe21222432e836ff\'');
});
});
});

describe('remove', () => {
it('deletes an existing instance and returns the deleted instance', () => {
app.service(serviceName).remove(_ids.Doug).then(data => {
return app.service(serviceName).remove(_ids.Doug).then(data => {
expect(data).to.be.ok;
expect(data.name).to.equal('Doug');
});
});

it('deletes an existing instance supports $select', () => {
return app.service(serviceName).remove(_ids.Doug, {
query: { $select: [ 'name' ] }
}).then(data => {
expect(data).to.be.ok;
expect(data.name).to.equal('Doug');
expect(data.age).to.not.exist;
});
});

Expand Down Expand Up @@ -170,10 +195,21 @@ function common (app, errors, serviceName = 'people', idProp = 'id') {
}
};

app.service(serviceName).find(params)
return app.service(serviceName).find(params)
.then(data => expect(data.length).to.equal(2));
});

it('can $limit 0', () => {
const params = {
query: {
$limit: 0
}
};

return app.service(serviceName).find(params)
.then(data => expect(data.length).to.equal(0));
});

it('can $skip', () => {
const params = {
query: {
Expand Down Expand Up @@ -435,12 +471,28 @@ function common (app, errors, serviceName = 'people', idProp = 'id') {
const originalData = { [idProp]: _ids.Doug, name: 'Dougler' };
const originalCopy = Object.assign({}, originalData);

return app.service(serviceName).update(_ids.Doug, originalData).then(data => {
expect(originalData).to.deep.equal(originalCopy);
expect(data[idProp].toString()).to.equal(_ids.Doug.toString());
expect(data.name).to.equal('Dougler');
expect(!data.age).to.be.ok;
});
return app.service(serviceName).update(_ids.Doug, originalData)
.then(data => {
expect(originalData).to.deep.equal(originalCopy);
expect(data[idProp].toString()).to.equal(_ids.Doug.toString());
expect(data.name).to.equal('Dougler');
expect(!data.age).to.be.ok;
});
});

it('replaces an existing instance, supports $select', () => {
const originalData = {
[idProp]: _ids.Doug,
name: 'Dougler',
age: 10
};

return app.service(serviceName).update(_ids.Doug, originalData, {
query: { $select: [ 'name' ] }
}).then(data => {
expect(data.name).to.equal('Dougler');
expect(data.age).to.not.exist;
});
});

it('returns NotFound error for non-existing id', () => {
Expand Down Expand Up @@ -468,6 +520,17 @@ function common (app, errors, serviceName = 'people', idProp = 'id') {
});
});

it('updates an existing instance, supports $select', () => {
const originalData = { [idProp]: _ids.Doug, name: 'PatchDoug' };

return app.service(serviceName).patch(_ids.Doug, originalData, {
query: { $select: [ 'name' ] }
}).then(data => {
expect(data.name).to.equal('PatchDoug');
expect(data.age).to.not.exist;
});
});

it('patches multiple instances', () => {
const service = app.service(serviceName);
const params = {
Expand Down Expand Up @@ -522,20 +585,23 @@ function common (app, errors, serviceName = 'people', idProp = 'id') {
}).then(() => service.remove(null, params));
});

it('patches multiple even if query changed', () => {
it('patches multiple, returns correct items', () => {
const service = app.service(serviceName);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current (buggy) implementations make just another find with the changed query which is not correct. What they really need to do is find everything before making the database update and then do another find with the list of all ids from the previous find. This tests covers that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!


return service.create({
name: 'Dave',
age: 2,
created: true
}).then(() =>
service.create({
return service.create([{
name: 'Dave',
age: 2,
created: true
}, {
name: 'David',
age: 2,
created: true
})
).then(() =>
}, {
name: 'D',
age: 8,
created: true
}
]).then(() =>
service.patch(null, {
age: 8
}, { query: { age: 2 } }
Expand Down Expand Up @@ -576,6 +642,20 @@ function common (app, errors, serviceName = 'people', idProp = 'id') {
});
});

it('creates a single new instance, supports $select', () => {
const originalData = {
name: 'William',
age: 23
};

return app.service(serviceName).create(originalData, {
query: { $select: [ 'name' ] }
}).then(data => {
expect(data.name).to.equal('William');
expect(data.age).to.not.exist;
});
});

it('creates multiple new instances', () => {
const items = [
{
Expand Down