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

Commit

Permalink
Support $select in get and make sure patch many only returns changed …
Browse files Browse the repository at this point in the history
…items (#32)

* Support  in get and make sure patch many only returns changed items

* Add tests for  with 0

* Adding  tests for other methods

* Fix assertion
  • Loading branch information
daffl authored Nov 11, 2016
1 parent 669ffd1 commit 6a6eb15
Showing 1 changed file with 103 additions and 23 deletions.
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)
);

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);

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

0 comments on commit 6a6eb15

Please sign in to comment.