1 |
|
2 |
|
3 | 'use strict'
|
4 | const chai = require('chai')
|
5 | const chaiSubset = require('chai-subset')
|
6 | chai.use(chaiSubset)
|
7 | const expect = chai.expect
|
8 | const MemoryModel = require('../lib/plugin/components/services/storage/Memory-model')
|
9 | const assert = require('assert')
|
10 |
|
11 | describe('Memory Model promise tests', function () {
|
12 | let planetsModel
|
13 | let personModel
|
14 |
|
15 | it('should get some model instances', function () {
|
16 | planetsModel = new MemoryModel(
|
17 | {
|
18 | id: 'planets',
|
19 | name: 'planets',
|
20 | namespace: 'test',
|
21 | primaryKey: ['name'],
|
22 | properties: {
|
23 | name: 'string'
|
24 | }
|
25 | }
|
26 | )
|
27 | personModel = new MemoryModel(
|
28 | {
|
29 | id: 'people',
|
30 | name: 'people',
|
31 | namespace: 'test',
|
32 | primaryKey: ['employeeNo'],
|
33 | properties: {
|
34 | employeeNo: 'number'
|
35 | }
|
36 | }
|
37 | )
|
38 | })
|
39 |
|
40 | it('should create a new person', function () {
|
41 | return personModel.create(
|
42 | {
|
43 | employeeNo: 1,
|
44 | firstName: 'Homer',
|
45 | lastName: 'Simpson',
|
46 | age: 39
|
47 | },
|
48 | {}
|
49 | ).then(idProperties =>
|
50 | expect(idProperties).to.eql(
|
51 | {
|
52 | idProperties:
|
53 | {
|
54 | employeeNo: 1
|
55 | }
|
56 | }
|
57 | )
|
58 | )
|
59 | })
|
60 |
|
61 | it('should create multiple new people', function () {
|
62 | return personModel.create(
|
63 | [
|
64 | {
|
65 | employeeNo: 2,
|
66 | firstName: 'Maggie',
|
67 | lastName: 'Simpson'
|
68 | },
|
69 | {
|
70 | employeeNo: 3,
|
71 | firstName: 'Lisa',
|
72 | lastName: 'Simpson',
|
73 | age: 8
|
74 | },
|
75 | {
|
76 | employeeNo: 4,
|
77 | firstName: 'Marge',
|
78 | lastName: 'Simpson',
|
79 | age: 36
|
80 | },
|
81 | {
|
82 | employeeNo: 5,
|
83 | firstName: 'Bart',
|
84 | lastName: 'Simpson',
|
85 | age: 10
|
86 | }
|
87 | ]
|
88 | )
|
89 | })
|
90 |
|
91 | it('should fail creating a new person with an already-used primary key', function () {
|
92 | return personModel.create(
|
93 | {
|
94 | employeeNo: 1,
|
95 | firstName: 'Ned',
|
96 | lastName: 'Flanders',
|
97 | age: 60
|
98 | },
|
99 | {}
|
100 | )
|
101 | .then(() => assert(false))
|
102 | .catch(err =>
|
103 | expect(err.name).to.eql('DuplicatePrimaryKey')
|
104 | )
|
105 | })
|
106 |
|
107 | it('should fail creating new people with an already-used primary key', function () {
|
108 | return personModel.create(
|
109 | [
|
110 | {
|
111 | employeeNo: 2,
|
112 | firstName: 'Maude',
|
113 | lastName: 'Flanders'
|
114 | }
|
115 | ],
|
116 | {}
|
117 | )
|
118 | .then(() => assert(false))
|
119 | .catch(err =>
|
120 | expect(err.name).to.eql('DuplicatePrimaryKey')
|
121 | )
|
122 | })
|
123 |
|
124 | it('should find a person via primary key', function () {
|
125 | return personModel.findById(3)
|
126 | .then(doc =>
|
127 | expect(doc).to.containSubset(
|
128 | {
|
129 | employeeNo: 3,
|
130 | firstName: 'Lisa',
|
131 | lastName: 'Simpson',
|
132 | age: 8
|
133 | }
|
134 | )
|
135 | )
|
136 | })
|
137 |
|
138 | it("should fail finding a person that's not there", function () {
|
139 | return personModel.findById(6)
|
140 | .then(doc =>
|
141 | expect(doc).to.equal(undefined)
|
142 | )
|
143 | })
|
144 |
|
145 | it('should find 5 people, youngest first', async function () {
|
146 | const doc = await personModel.find(
|
147 | {
|
148 | orderBy: ['age']
|
149 | }
|
150 | )
|
151 |
|
152 | expect(doc).to.containSubset(
|
153 | [
|
154 | {
|
155 | age: 8,
|
156 | employeeNo: 3,
|
157 | firstName: 'Lisa',
|
158 | lastName: 'Simpson'
|
159 | },
|
160 | {
|
161 | age: 10,
|
162 | employeeNo: 5,
|
163 | firstName: 'Bart',
|
164 | lastName: 'Simpson'
|
165 | },
|
166 | {
|
167 | age: 36,
|
168 | employeeNo: 4,
|
169 | firstName: 'Marge',
|
170 | lastName: 'Simpson'
|
171 | },
|
172 | {
|
173 | age: 39,
|
174 | employeeNo: 1,
|
175 | firstName: 'Homer',
|
176 | lastName: 'Simpson'
|
177 | },
|
178 | {
|
179 | employeeNo: 2,
|
180 | firstName: 'Maggie',
|
181 | lastName: 'Simpson'
|
182 | }
|
183 | ]
|
184 | )
|
185 | })
|
186 |
|
187 | it('should find Bart by name', async () => {
|
188 | const doc = await personModel.find(
|
189 | {
|
190 | where: {
|
191 | firstName: { equals: 'Bart' },
|
192 | lastName: { equals: 'Simpson' }
|
193 | }
|
194 | }
|
195 | )
|
196 |
|
197 | expect(doc).to.have.length(1)
|
198 | expect(doc).to.containSubset(
|
199 | [
|
200 | {
|
201 | age: 10,
|
202 | employeeNo: 5,
|
203 | firstName: 'Bart',
|
204 | lastName: 'Simpson'
|
205 | }
|
206 | ]
|
207 | )
|
208 | })
|
209 |
|
210 | it('should get one Homer by name', function () {
|
211 | return personModel.findOne(
|
212 | {
|
213 | where: {
|
214 | firstName: { equals: 'Homer' },
|
215 | lastName: { equals: 'Simpson' }
|
216 | }
|
217 | }
|
218 | ).then(doc =>
|
219 | expect(doc).to.containSubset(
|
220 | {
|
221 | age: 39,
|
222 | employeeNo: 1,
|
223 | firstName: 'Homer',
|
224 | lastName: 'Simpson'
|
225 | }
|
226 | )
|
227 | )
|
228 | })
|
229 |
|
230 | it('find Bart or Homer by name', async () => {
|
231 | const doc = await personModel.findOne(
|
232 | {
|
233 | where: {
|
234 | firstName: { equals: ['Homer', 'Bart'] },
|
235 | lastName: { equals: 'Simpson' }
|
236 | }
|
237 | }
|
238 | )
|
239 | expect(doc).to.containSubset(
|
240 | {
|
241 | age: 39,
|
242 | employeeNo: 1,
|
243 | firstName: 'Homer',
|
244 | lastName: 'Simpson'
|
245 | },
|
246 | {
|
247 | age: 10,
|
248 | employeeNo: 5,
|
249 | firstName: 'Bart',
|
250 | lastName: 'Simpson'
|
251 | }
|
252 | )
|
253 | })
|
254 |
|
255 | it("shouldn't get one missing person", async () => {
|
256 | const doc = await personModel.findOne(
|
257 | {
|
258 | where: {
|
259 | firstName: { equals: 'Ned' },
|
260 | lastName: { equals: 'Flanders' }
|
261 | }
|
262 | }
|
263 | )
|
264 |
|
265 | expect(doc).to.equal(undefined)
|
266 | })
|
267 |
|
268 | it("should update Maggie's age to 1", () => {
|
269 | return personModel.update(
|
270 | {
|
271 | employeeNo: 2,
|
272 | age: 1,
|
273 | firstName: 'Maggie',
|
274 | lastName: 'Simpson'
|
275 | },
|
276 | {}
|
277 | )
|
278 | })
|
279 |
|
280 | it('should find Maggie has an age now', () => {
|
281 | personModel.findById(2)
|
282 | .then(doc =>
|
283 | expect(doc).to.containSubset(
|
284 | {
|
285 | employeeNo: 2,
|
286 | firstName: 'Maggie',
|
287 | lastName: 'Simpson',
|
288 | age: 1
|
289 | }
|
290 | )
|
291 | )
|
292 | })
|
293 |
|
294 | it('should update Maggie again, but this time without an age', async () => {
|
295 | await personModel.update(
|
296 | {
|
297 | employeeNo: 2,
|
298 | firstName: 'Maggie',
|
299 | lastName: 'Simpson'
|
300 | },
|
301 | {}
|
302 | )
|
303 | })
|
304 |
|
305 | it("should find Maggie's age has gone again", async () => {
|
306 | const doc = await personModel.findById(2)
|
307 |
|
308 | expect(doc).to.containSubset(
|
309 | {
|
310 | employeeNo: 2,
|
311 | firstName: 'Maggie',
|
312 | lastName: 'Simpson'
|
313 | }
|
314 | )
|
315 | })
|
316 |
|
317 | it('should patch Maggie to Margaret', function (done) {
|
318 | personModel.patch(
|
319 | {
|
320 | employeeNo: 2,
|
321 | firstName: 'Margaret'
|
322 | },
|
323 | {}
|
324 | ).then(() => done())
|
325 | })
|
326 |
|
327 | it('should find Maggie is now a Margaret', async () => {
|
328 | const doc = await personModel.findById(2)
|
329 |
|
330 | expect(doc).to.containSubset(
|
331 | {
|
332 | employeeNo: 2,
|
333 | firstName: 'Margaret',
|
334 | lastName: 'Simpson'
|
335 | }
|
336 | )
|
337 | })
|
338 |
|
339 | it('should delete Maggie/Margaret by via her id', function (done) {
|
340 | personModel.destroyById(2).then(() => done())
|
341 | })
|
342 |
|
343 | it('should fail getting a deleted record', async () => {
|
344 | const doc = await personModel.findById(2)
|
345 | expect(doc).to.equal(undefined)
|
346 | })
|
347 |
|
348 | it('should upsert (insert) Grampa', () => {
|
349 | personModel.upsert(
|
350 | {
|
351 | employeeNo: 10,
|
352 | firstName: 'Abe',
|
353 | lastName: 'Simpson',
|
354 | age: 82
|
355 | },
|
356 | {}
|
357 | ).then(idProperties =>
|
358 | expect(idProperties).to.eql(
|
359 | {
|
360 | idProperties: {
|
361 | employeeNo: 10
|
362 | }
|
363 | }
|
364 | )
|
365 | )
|
366 | })
|
367 |
|
368 | it('should find Grampa has been inserted via upsert', async () => {
|
369 | const doc = await personModel.findById(10)
|
370 | expect(doc).to.containSubset(
|
371 | {
|
372 | employeeNo: 10,
|
373 | firstName: 'Abe',
|
374 | lastName: 'Simpson',
|
375 | age: 82
|
376 | }
|
377 | )
|
378 | })
|
379 |
|
380 | it('should upsert (update) Grampa', function (done) {
|
381 | personModel.upsert(
|
382 | {
|
383 | employeeNo: 10,
|
384 | firstName: 'Abraham',
|
385 | lastName: 'Simpson',
|
386 | age: 82
|
387 | },
|
388 | {}
|
389 | ).then(() => done())
|
390 | })
|
391 |
|
392 | it('should find Grampa has now been updates via upsert', async () => {
|
393 | const doc = await personModel.findById(10)
|
394 | expect(doc).to.containSubset(
|
395 | {
|
396 | employeeNo: 10,
|
397 | firstName: 'Abraham',
|
398 | lastName: 'Simpson',
|
399 | age: 82
|
400 | }
|
401 | )
|
402 | })
|
403 |
|
404 | it('should now upsert (update) Grampa, resetting his name', (done) => {
|
405 | personModel.upsert(
|
406 | {
|
407 | employeeNo: 10,
|
408 | firstName: 'Abe',
|
409 | lastName: 'Simpson'
|
410 | },
|
411 | {
|
412 | setMissingPropertiesToNull: false
|
413 | }
|
414 | ).then(() => done())
|
415 | })
|
416 |
|
417 | it('should find Grampa again, with his age preserved and an updated name', async () => {
|
418 | const doc = await personModel.findById(10)
|
419 | expect(doc).to.containSubset(
|
420 | {
|
421 | employeeNo: 10,
|
422 | firstName: 'Abe',
|
423 | lastName: 'Simpson',
|
424 | age: 82
|
425 | }
|
426 | )
|
427 | })
|
428 |
|
429 | it('should upsert (update) Grampa again, but turn age to null', function (done) {
|
430 | personModel.upsert(
|
431 | {
|
432 | employeeNo: 10,
|
433 | firstName: 'Abraham',
|
434 | lastName: 'Simpson'
|
435 | },
|
436 | {}
|
437 | ).then(() => done())
|
438 | })
|
439 |
|
440 | it('should find Grampa again, but now with a null age', async () => {
|
441 | const doc = await personModel.findById(10)
|
442 | expect(doc).to.containSubset(
|
443 | {
|
444 | employeeNo: 10,
|
445 | firstName: 'Abraham',
|
446 | lastName: 'Simpson'
|
447 | }
|
448 | )
|
449 | })
|
450 |
|
451 | it('should create mars, with two moons and a few craters', function (done) {
|
452 | planetsModel.create(
|
453 | {
|
454 | name: 'mars',
|
455 | title: 'Mars',
|
456 | type: 'Terrestrial',
|
457 | diameter: 6700,
|
458 | color: 'red',
|
459 | url: 'http://en.wikipedia.org/wiki/Mars',
|
460 | otherFacts: {
|
461 | radius: 3390,
|
462 | surfacePressure: '0.636 (0.4–0.87) kPa; 0.00628 atm',
|
463 | equatorialRotationVelocity: '868.22 km/h (241.17 m/s)'
|
464 | },
|
465 | moons: [
|
466 | {
|
467 | title: 'Phobos',
|
468 | discoveredBy: 'Asaph Hall',
|
469 | discoveryYear: 1800,
|
470 | craters: [
|
471 | {
|
472 | title: 'Stickney',
|
473 | diameter: 9
|
474 | }
|
475 | ]
|
476 | },
|
477 | {
|
478 | title: 'Deimos',
|
479 | discoveredBy: 'Asaph Hall',
|
480 | discoveryYear: 1800
|
481 | }
|
482 | ]
|
483 | },
|
484 | {},
|
485 | function (err, idProperties) {
|
486 | expect(err).to.equal(null)
|
487 | expect(idProperties).to.eql(
|
488 | {
|
489 | idProperties: {
|
490 | name: 'mars'
|
491 | }
|
492 | }
|
493 | )
|
494 | done()
|
495 | }
|
496 | )
|
497 | })
|
498 |
|
499 | it('should find Mars via primary key', function (done) {
|
500 | planetsModel.findById(
|
501 | 'mars',
|
502 | function (err, doc) {
|
503 | expect(err).to.equal(null)
|
504 | expect(doc).to.containSubset(
|
505 | {
|
506 | name: 'mars',
|
507 | title: 'Mars',
|
508 | type: 'Terrestrial',
|
509 | diameter: 6700,
|
510 | color: 'red',
|
511 | url: 'http://en.wikipedia.org/wiki/Mars',
|
512 | otherFacts: {
|
513 | radius: 3390,
|
514 | surfacePressure: '0.636 (0.4–0.87) kPa; 0.00628 atm',
|
515 | equatorialRotationVelocity: '868.22 km/h (241.17 m/s)'
|
516 | },
|
517 | moons: [
|
518 | {
|
519 | title: 'Phobos',
|
520 | discoveredBy: 'Asaph Hall',
|
521 | discoveryYear: 1800,
|
522 | craters: [
|
523 | {
|
524 | title: 'Stickney',
|
525 | diameter: 9
|
526 | }
|
527 | ]
|
528 | },
|
529 | {
|
530 | title: 'Deimos',
|
531 | discoveredBy: 'Asaph Hall',
|
532 | discoveryYear: 1800
|
533 | }
|
534 | ]
|
535 | }
|
536 | )
|
537 |
|
538 | done()
|
539 | }
|
540 | )
|
541 | })
|
542 |
|
543 | it('should delete Mars', function (done) {
|
544 | planetsModel.destroyById('mars')
|
545 | .then(() => done())
|
546 | })
|
547 |
|
548 | it('should now fail to find Mars', () => {
|
549 | return planetsModel.findById('mars')
|
550 | .then(doc => expect(doc).to.equal(undefined))
|
551 | })
|
552 | })
|