Skip to content

Commit b136ffc

Browse files
greenkeeperio-botdaffl
authored andcommitted
Compatibility with latest service tests (#75)
* chore(package): update feathers-service-tests to version 0.8.0 * Finalize service test compatibility
1 parent b6a334c commit b136ffc

3 files changed

Lines changed: 80 additions & 84 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"chai": "^3.4.1",
6363
"feathers": "^2.0.0-pre.4",
6464
"feathers-rest": "^1.3.0",
65-
"feathers-service-tests": "^0.6.0",
65+
"feathers-service-tests": "^0.8.0",
6666
"jshint": "^2.8.0",
6767
"knex": "^0.12.0",
6868
"mocha": "^3.0.0",

src/index.js

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Service {
3737
this.id = options.id || 'id';
3838
this.paginate = options.paginate || {};
3939
this.table = options.name;
40+
this.events = options.events || [];
4041
}
4142

4243
// NOTE (EK): We need this method so that we return a new query
@@ -50,26 +51,29 @@ class Service {
5051
}
5152

5253
knexify(query, params, parentKey) {
53-
Object.keys(params || {}).forEach((key) => {
54+
Object.keys(params || {}).forEach(key => {
5455
const value = params[key];
5556

5657
if (isPlainObject(value)) {
5758
return this.knexify(query, value, key);
5859
}
5960

61+
// const self = this;
6062
const column = parentKey || key;
6163
const method = METHODS[key];
6264
const operator = OPERATORS[key] || '=';
6365

64-
// TODO (EK): Handle $or queries with nested specials.
65-
// Right now they won't work and we'd need to start diving
66-
// into nested where conditions.
6766
if (method) {
6867
if (key === '$or') {
69-
return query.where(function() {
70-
value.forEach(condition => this[method].call(this, condition));
71-
});
68+
const self = this;
69+
70+
return value.forEach(condition => {
71+
query[method](function() {
72+
self.knexify(this, condition);
73+
});
74+
});
7275
}
76+
7377
return query[method].call(query, column, value);
7478
}
7579

@@ -78,13 +82,12 @@ class Service {
7882
}
7983

8084
_find(params, count, getFilter = filter) {
85+
const { filters, query } = getFilter(params.query || {});
8186
let q = this.db().select(['*']);
82-
let { filters, query } = getFilter(params.query || {});
8387

8488
// $select uses a specific find syntax, so it has to come first.
8589
if (filters.$select) {
86-
let fields = filters.$select;
87-
q = this.db().select(... fields);
90+
q = this.db().select(... filters.$select);
8891
}
8992

9093
// build up the knex query out of the query params
@@ -176,21 +179,32 @@ class Service {
176179
return this._create(data, params);
177180
}
178181

179-
patch(id, data, params) {
180-
params.query = params.query || {};
181-
data = Object.assign({}, data);
182+
patch(id, raw, params) {
183+
const query = Object.assign({}, params.query);
184+
const data = Object.assign({}, raw);
185+
const patchQuery = {};
182186

183187
if(id !== null) {
184-
params.query[this.id] = id;
188+
query[this.id] = id;
185189
}
186190

187-
let query = this.db();
188-
this.knexify(query, params.query);
191+
// Account for potentially modified data
192+
Object.keys(query).forEach(key => {
193+
if(query[key] !== undefined && data[key] !== undefined &&
194+
typeof data[key] !== 'object') {
195+
patchQuery[key] = data[key];
196+
} else {
197+
patchQuery[key] = query[key];
198+
}
199+
});
200+
201+
let q = this.db();
202+
this.knexify(q, query);
189203

190204
delete data[this.id];
191205

192-
return query.update(data).then(() => {
193-
return this._find(params).then(page => {
206+
return q.update(data).then(() => {
207+
return this._find({ query: patchQuery }).then(page => {
194208
const items = page.data;
195209

196210
if(id !== null) {

test/index.test.js

Lines changed: 47 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { expect } from 'chai';
44
import assert from 'assert';
55
import feathers from 'feathers';
66
import knex from 'knex';
7-
import { base, orm, example } from 'feathers-service-tests';
7+
import { base, example } from 'feathers-service-tests';
88
import { errors } from 'feathers-errors';
99
import service from '../src';
1010
import server from '../example/app';
@@ -16,92 +16,74 @@ const db = knex({
1616
}
1717
});
1818

19-
const app = feathers().use('/people', service({
20-
Model: db,
21-
name: 'people'
22-
}));
23-
24-
let _ids = {};
25-
let people = app.service('people');
26-
27-
function clean(done) {
28-
db.schema.dropTableIfExists('people').then(() => {
29-
db.schema.createTable('people', table => {
30-
table.increments('id');
31-
table.string('name');
32-
table.integer('age');
33-
table.integer('time');
34-
table.boolean('created');
35-
})
36-
.then(() => {
37-
done();
38-
});
39-
});
19+
function clean() {
20+
return db.schema.dropTableIfExists('people')
21+
.then(() => db.schema.dropTableIfExists('people-customid'))
22+
.then(() =>
23+
db.schema.createTable('people', table => {
24+
table.increments('id');
25+
table.string('name');
26+
table.integer('age');
27+
table.integer('time');
28+
table.boolean('created');
29+
}).then(() => db.schema.createTable('people-customid', table => {
30+
table.increments('customid');
31+
table.string('name');
32+
table.integer('age');
33+
table.integer('time');
34+
table.boolean('created');
35+
}))
36+
);
4037
}
4138

4239
describe('Feathers Knex Service', () => {
40+
const app = feathers().use('/people', service({
41+
Model: db,
42+
name: 'people',
43+
events: [ 'testing' ]
44+
})).use('/people-customid', service({
45+
Model: db,
46+
id: 'customid',
47+
name: 'people-customid',
48+
events: [ 'testing' ]
49+
}));
50+
4351
before(clean);
4452
after(clean);
4553

4654
describe('Initialization', () => {
4755
describe('when missing options', () => {
48-
it('throws an error', () => {
49-
expect(service.bind(null)).to.throw('Knex options have to be provided');
50-
});
56+
it('throws an error', () =>
57+
expect(service.bind(null))
58+
.to.throw('Knex options have to be provided')
59+
);
5160
});
5261

5362
describe('when missing a Model', () => {
54-
it('throws an error', () => {
55-
expect(service.bind(null, {})).to.throw(/You must provide a Model/);
56-
});
63+
it('throws an error', () =>
64+
expect(service.bind(null, {}))
65+
.to.throw(/You must provide a Model/)
66+
);
5767
});
5868

5969
describe('when missing a table name', () => {
60-
it('throws an error', () => {
61-
expect(service.bind(null, { Model: {} })).to.throw('No table name specified.');
62-
});
63-
});
64-
65-
describe('when missing the id option', () => {
66-
it('sets the default to be id', () => {
67-
expect(people.id).to.equal('id');
68-
});
69-
});
70-
71-
describe('when missing the paginate option', () => {
72-
it('sets the default to be {}', () => {
73-
expect(people.paginate).to.deep.equal({});
74-
});
70+
it('throws an error', () =>
71+
expect(service.bind(null, { Model: {} }))
72+
.to.throw('No table name specified.')
73+
);
7574
});
7675
});
7776

7877
describe('Common functionality', () => {
79-
beforeEach(done => {
80-
people.create({
81-
name: 'Doug',
82-
age: 32
83-
}).then(data => {
84-
_ids.Doug = data.id;
85-
done();
86-
}, done);
87-
});
88-
89-
afterEach(done => {
90-
people.remove(_ids.Doug, {}).then(() => done(), () => done());
91-
});
78+
it('is CommonJS compatible', () =>
79+
assert.equal(typeof require('../lib'), 'function')
80+
);
9281

93-
it('is CommonJS compatible', () => {
94-
assert.equal(typeof require('../lib'), 'function');
95-
});
96-
97-
base(people, _ids, errors);
82+
base(app, errors, 'people');
83+
base(app, errors, 'people-customid', 'customid');
9884
});
9985
});
10086

101-
describe.skip('Knex service ORM errors', () => {
102-
orm(people, _ids, errors);
103-
});
104-
10587
describe('Knex service example test', () => {
10688
after(done => server.close(() => done()));
10789

0 commit comments

Comments
 (0)