forked from feathersjs-ecosystem/feathers-mongoose
/
index.test.js
348 lines (300 loc) · 9.7 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import { expect } from 'chai';
import { base, example } from 'feathers-service-tests';
import errors from 'feathers-errors';
import feathers from 'feathers';
import service, { hooks, Service } from '../src';
import server from './test-app';
import { User, Pet, Peeps, CustomPeeps } from './models';
const _ids = {};
const _petIds = {};
const app = feathers()
.use('/peeps', service({ Model: Peeps, events: [ 'testing' ] }))
.use('/peeps-customid', service({
id: 'customid',
Model: CustomPeeps,
events: [ 'testing' ]
}))
.use('/people', service({ Model: User }))
.use('/pets', service({ Model: Pet }))
.use('/people2', service({ Model: User, lean: true }))
.use('/pets2', service({ Model: Pet, lean: true }));
const people = app.service('people');
const pets = app.service('pets');
const leanPeople = app.service('people2');
const leanPets = app.service('pets2');
let testApp;
describe('Feathers Mongoose Service', () => {
describe('Requiring', () => {
const lib = require('../lib');
it('exposes the service as a default module', () => {
expect(typeof lib).to.equal('function');
});
it('exposes the Service Constructor', () => {
expect(typeof lib.Service).to.equal('function');
});
it('exposes hooks', () => {
expect(typeof lib.hooks).to.equal('object');
});
});
describe('Importing', () => {
it('exposes the service as a default module', () => {
expect(typeof service).to.equal('function');
});
it('exposes the Service constructor', () => {
// Check by calling the Service constructor without
// any params. It should return an error.
let newService;
try {
newService = new Service();
} catch (e) {
expect(e).to.not.be.undefined;
expect(newService).to.be.undefined;
}
});
it('exposes hooks', () => {
expect(typeof hooks).to.equal('object');
});
});
describe('Initialization', () => {
describe('when missing options', () => {
it('throws an error', () => {
expect(service.bind(null)).to.throw('Mongoose options have to be provided');
});
});
describe('when missing a Model', () => {
it('throws an error', () => {
expect(service.bind(null, { name: 'Test' })).to.throw(/You must provide a Mongoose Model/);
});
});
describe('when missing the id option', () => {
it('sets the default to be _id', () => {
expect(people.id).to.equal('_id');
});
});
describe('when missing the paginate option', () => {
it('sets the default to be {}', () => {
expect(people.paginate).to.deep.equal({});
});
});
describe('when missing the overwrite option', () => {
it('sets the default to be true', () => {
expect(people.overwrite).to.be.true;
});
});
describe('when missing the lean option', () => {
it('sets the default to be false', () => {
expect(people.lean).to.be.false;
});
});
});
describe('Common functionality', () => {
beforeEach(() => {
// FIXME (EK): This is shit. We should be loading fixtures
// using the raw driver not our system under test
return pets.create({type: 'dog', name: 'Rufus'}).then(pet => {
_petIds.Rufus = pet._id;
return people.create({
name: 'Doug',
age: 32,
pets: [pet._id]
}).then(user => {
_ids.Doug = user._id;
});
});
});
afterEach(() => {
return pets.remove(null, { query: {} }).then(() =>
people.remove(null, { query: {} })
);
});
it('can $populate with find', function (done) {
var params = {
query: {
name: 'Doug',
$populate: ['pets']
}
};
people.find(params).then(data => {
expect(data[0].pets[0].name).to.equal('Rufus');
done();
});
});
it('can $populate with get', function (done) {
var params = {
query: {
$populate: ['pets']
}
};
people.get(_ids.Doug, params).then(data => {
expect(data.pets[0].name).to.equal('Rufus');
done();
}).catch(done);
});
it('can patch a mongoose model', function (done) {
people.get(_ids.Doug).then(dougModel => {
people.patch(_ids.Doug, dougModel).then(data => {
expect(data.name).to.equal('Doug');
done();
}).catch(done);
}).catch(done);
});
it('can patch a mongoose model', function (done) {
people.get(_ids.Doug).then(dougModel => {
people.update(_ids.Doug, dougModel).then(data => {
expect(data.name).to.equal('Doug');
done();
}).catch(done);
}).catch(done);
});
it('can $populate with update', function (done) {
var params = {
query: {
$populate: ['pets']
}
};
people.get(_ids.Doug).then(doug => {
var newDoug = doug.toObject();
newDoug.name = 'Bob';
people.update(_ids.Doug, newDoug, params).then(data => {
expect(data.name).to.equal('Bob');
expect(data.pets[0].name).to.equal('Rufus');
done();
}).catch(done);
}).catch(done);
});
it('can $populate with patch', function (done) {
var params = {
query: {
$populate: ['pets']
}
};
people.patch(_ids.Doug, { name: 'Bob' }, params).then(data => {
expect(data.name).to.equal('Bob');
expect(data.pets[0].name).to.equal('Rufus');
done();
}).catch(done);
});
it('can $push an item onto an array with update', function (done) {
pets.create({ type: 'cat', name: 'Margeaux' }).then(margeaux => {
people.update(_ids.Doug, { $push: { pets: margeaux } })
.then(() => {
var params = {
query: {
$populate: ['pets']
}
};
people.get(_ids.Doug, params).then(data => {
expect(data.pets[1].name).to.equal('Margeaux');
done();
}).catch(done);
}).catch(done);
}).catch(done);
});
it('can $push an item onto an array with patch', function (done) {
pets.create({ type: 'cat', name: 'Margeaux' }).then(margeaux => {
people.patch(_ids.Doug, { $push: { pets: margeaux } })
.then(() => {
var params = {
query: {
$populate: ['pets']
}
};
people.get(_ids.Doug, params).then(data => {
expect(data.pets[1].name).to.equal('Margeaux');
done();
}).catch(done);
}).catch(done);
}).catch(done);
});
it('runs validators on update', function (done) {
people.create({ name: 'David', age: 33 })
.then(person => people.update(person._id, { name: 'Dada', age: 'wrong' }))
.then(() => done(new Error('Update should not be successful')))
.catch(error => {
expect(error.name).to.equal('BadRequest');
expect(error.message).to.equal('Cast to number failed for value "wrong" at path "age"');
done();
});
});
it('runs validators on patch', function (done) {
people.create({ name: 'David', age: 33 })
.then(person => people.patch(person._id, { name: 'Dada', age: 'wrong' }))
.then(() => done(new Error('Update should not be successful')))
.catch(error => {
expect(error.name).to.equal('BadRequest');
expect(error.message).to.equal('Cast to number failed for value "wrong" at path "age"');
done();
});
});
it('returns a Conflict when unique index is violated', function (done) {
pets.create({ type: 'cat', name: 'Bob' })
.then(() => pets.create({ type: 'cat', name: 'Bob' }))
.then(() => done(new Error('Should not be successful')))
.catch(error => {
expect(error.name).to.equal('Conflict');
done();
});
});
});
describe('Lean Services', () => {
beforeEach((done) => {
// FIXME (EK): This is shit. We should be loading fixtures
// using the raw driver not our system under test
leanPets.create({type: 'dog', name: 'Rufus'}).then(pet => {
_petIds.Rufus = pet._id;
return leanPeople.create({ name: 'Doug', age: 32, pets: [pet._id] }).then(user => {
_ids.Doug = user._id;
done();
});
});
});
afterEach(done => {
leanPets.remove(null, { query: {} }).then(() => {
return leanPeople.remove(null, { query: {} }).then(() => {
return done();
});
});
});
it('can $populate with find', function (done) {
var params = {
query: {
name: 'Doug',
$populate: ['pets']
}
};
leanPeople.find(params).then(data => {
expect(data[0].pets[0].name).to.equal('Rufus');
done();
});
});
it('can $populate with get', function (done) {
var params = {
query: {
$populate: ['pets']
}
};
leanPeople.get(_ids.Doug, params).then(data => {
expect(data.pets[0].name).to.equal('Rufus');
done();
}).catch(done);
});
});
describe('Common tests', () => {
before(() => Promise.all([
app.service('peeps').remove(null),
app.service('peeps-customid').remove(null)
]));
base(app, errors, 'peeps', '_id');
base(app, errors, 'peeps-customid', 'customid');
});
describe('Mongoose service example test', () => {
before(done => {
server.service('todos').remove(null, {}).then(() => {
testApp = server.listen(3030);
return done();
});
});
after(done => testApp.close(() => done()));
example('_id');
});
});