1 |
|
2 |
|
3 | 'use strict'
|
4 |
|
5 | const chai = require('chai')
|
6 | const chaiSubset = require('chai-subset')
|
7 | chai.use(chaiSubset)
|
8 | const expect = chai.expect
|
9 | const MemoryModel = require('../lib/plugin/components/services/storage/Memory-model')
|
10 |
|
11 | describe('Memory Model tests', function () {
|
12 | let planetsModel
|
13 | let personModel
|
14 | let phobosId
|
15 | let stickneyId
|
16 |
|
17 | it('should get some model instances', function () {
|
18 | planetsModel = new MemoryModel(
|
19 | {
|
20 | id: 'planets',
|
21 | name: 'planets',
|
22 | namespace: 'test',
|
23 | primaryKey: ['name'],
|
24 | properties: {
|
25 | name: 'string'
|
26 | }
|
27 | }
|
28 | )
|
29 | personModel = new MemoryModel(
|
30 | {
|
31 | id: 'people',
|
32 | name: 'people',
|
33 | namespace: 'test',
|
34 | primaryKey: ['employeeNo'],
|
35 | properties: {
|
36 | employeeNo: 'number'
|
37 | }
|
38 | }
|
39 | )
|
40 | })
|
41 |
|
42 | it('should create a new person', function (done) {
|
43 | personModel.create(
|
44 | {
|
45 | employeeNo: 1,
|
46 | firstName: 'Homer',
|
47 | lastName: 'Simpson',
|
48 | age: 39
|
49 | },
|
50 | {},
|
51 | function (err, idProperties) {
|
52 | expect(err).to.equal(null)
|
53 | expect(idProperties).to.eql(
|
54 | {
|
55 | idProperties:
|
56 | {
|
57 | employeeNo: 1
|
58 | }
|
59 | }
|
60 | )
|
61 | done()
|
62 | }
|
63 | )
|
64 | })
|
65 |
|
66 | it('should create multiple new people', function (done) {
|
67 | personModel.create(
|
68 | [
|
69 | {
|
70 | employeeNo: 2,
|
71 | firstName: 'Maggie',
|
72 | lastName: 'Simpson'
|
73 | },
|
74 | {
|
75 | employeeNo: 3,
|
76 | firstName: 'Lisa',
|
77 | lastName: 'Simpson',
|
78 | age: 8
|
79 | },
|
80 | {
|
81 | employeeNo: 4,
|
82 | firstName: 'Marge',
|
83 | lastName: 'Simpson',
|
84 | age: 36
|
85 | },
|
86 | {
|
87 | employeeNo: 5,
|
88 | firstName: 'Bart',
|
89 | lastName: 'Simpson',
|
90 | age: 10
|
91 | }
|
92 |
|
93 | ],
|
94 | {},
|
95 | function (err) {
|
96 | expect(err).to.equal(null)
|
97 | done()
|
98 | }
|
99 | )
|
100 | })
|
101 |
|
102 | it('should fail creating a new person with an already-used primary key', function (done) {
|
103 | personModel.create(
|
104 | {
|
105 | employeeNo: 1,
|
106 | firstName: 'Ned',
|
107 | lastName: 'Flanders',
|
108 | age: 60
|
109 | },
|
110 | {},
|
111 | function (err, doc) {
|
112 | expect(err).to.containSubset(
|
113 | {
|
114 | name: 'DuplicatePrimaryKey'
|
115 | }
|
116 | )
|
117 | done()
|
118 | }
|
119 | )
|
120 | })
|
121 |
|
122 | it('should fail creating new people with an already-used primary key', function (done) {
|
123 | personModel.create(
|
124 | [
|
125 | {
|
126 | employeeNo: 2,
|
127 | firstName: 'Maude',
|
128 | lastName: 'Flanders'
|
129 | }
|
130 | ],
|
131 | {},
|
132 | function (err, doc) {
|
133 | expect(err).to.containSubset(
|
134 | {
|
135 | name: 'DuplicatePrimaryKey'
|
136 | }
|
137 | )
|
138 | done()
|
139 | }
|
140 | )
|
141 | })
|
142 |
|
143 | it('should find a person via primary key', function (done) {
|
144 | personModel.findById(
|
145 | 3,
|
146 | function (err, doc) {
|
147 | expect(err).to.equal(null)
|
148 | expect(doc).to.containSubset(
|
149 | {
|
150 | employeeNo: 3,
|
151 | firstName: 'Lisa',
|
152 | lastName: 'Simpson',
|
153 | age: 8
|
154 | }
|
155 | )
|
156 | done()
|
157 | }
|
158 | )
|
159 | })
|
160 |
|
161 | it('should fail finding a person that\'s not there', function (done) {
|
162 | personModel.findById(
|
163 | 6,
|
164 | function (err, doc) {
|
165 | expect(err).to.equal(null)
|
166 | expect(doc).to.equal(undefined)
|
167 | done()
|
168 | }
|
169 | )
|
170 | })
|
171 |
|
172 | it('should find 5 people', function (done) {
|
173 | personModel.find(
|
174 | {},
|
175 | function (err, doc) {
|
176 | expect(err).to.equal(null)
|
177 |
|
178 | expect(doc).to.containSubset(
|
179 | [
|
180 | {
|
181 | age: 8,
|
182 | employeeNo: 3,
|
183 | firstName: 'Lisa',
|
184 | lastName: 'Simpson'
|
185 | },
|
186 | {
|
187 | age: 10,
|
188 | employeeNo: 5,
|
189 | firstName: 'Bart',
|
190 | lastName: 'Simpson'
|
191 | },
|
192 | {
|
193 | age: 36,
|
194 | employeeNo: 4,
|
195 | firstName: 'Marge',
|
196 | lastName: 'Simpson'
|
197 | },
|
198 | {
|
199 | age: 39,
|
200 | employeeNo: 1,
|
201 | firstName: 'Homer',
|
202 | lastName: 'Simpson'
|
203 | },
|
204 | {
|
205 | employeeNo: 2,
|
206 | firstName: 'Maggie',
|
207 | lastName: 'Simpson'
|
208 | }
|
209 | ]
|
210 | )
|
211 | done()
|
212 | }
|
213 | )
|
214 | })
|
215 |
|
216 | it('should find Bart by name', function (done) {
|
217 | personModel.find(
|
218 | {
|
219 | where: {
|
220 | firstName: { equals: 'Bart' },
|
221 | lastName: { equals: 'Simpson' }
|
222 | }
|
223 | },
|
224 | function (err, doc) {
|
225 | expect(err).to.equal(null)
|
226 | expect(doc).to.have.length(1)
|
227 | expect(doc).to.containSubset(
|
228 | [
|
229 | {
|
230 | age: 10,
|
231 | employeeNo: 5,
|
232 | firstName: 'Bart',
|
233 | lastName: 'Simpson'
|
234 | }
|
235 | ]
|
236 | )
|
237 |
|
238 | done()
|
239 | }
|
240 | )
|
241 | })
|
242 |
|
243 | it('should find Bart by part of his name', function (done) {
|
244 | personModel.find(
|
245 | {
|
246 | where: {
|
247 | firstName: { like: 'art' }
|
248 | }
|
249 | },
|
250 | function (err, doc) {
|
251 | expect(err).to.equal(null)
|
252 | expect(doc).to.have.length(1)
|
253 | expect(doc).to.containSubset(
|
254 | [
|
255 | {
|
256 | age: 10,
|
257 | employeeNo: 5,
|
258 | firstName: 'Bart',
|
259 | lastName: 'Simpson'
|
260 | }
|
261 | ]
|
262 | )
|
263 |
|
264 | done()
|
265 | }
|
266 | )
|
267 | })
|
268 |
|
269 | it('should get one Homer by name', function (done) {
|
270 | personModel.findOne(
|
271 | {
|
272 | where: {
|
273 | firstName: { equals: 'Homer' },
|
274 | lastName: { equals: 'Simpson' }
|
275 | }
|
276 | },
|
277 | function (err, doc) {
|
278 | expect(err).to.equal(null)
|
279 | expect(doc).to.containSubset(
|
280 | {
|
281 | age: 39,
|
282 | employeeNo: 1,
|
283 | firstName: 'Homer',
|
284 | lastName: 'Simpson'
|
285 | }
|
286 | )
|
287 |
|
288 | done()
|
289 | }
|
290 | )
|
291 | })
|
292 |
|
293 | it('shouldn\'t get one missing person', function (done) {
|
294 | personModel.findOne(
|
295 | {
|
296 | where: {
|
297 | firstName: { equals: 'Ned' },
|
298 | lastName: { equals: 'Flanders' }
|
299 | }
|
300 | },
|
301 | function (err, doc) {
|
302 | expect(err).to.equal(null)
|
303 | expect(doc).to.equal(undefined)
|
304 | done()
|
305 | }
|
306 | )
|
307 | })
|
308 |
|
309 | it('should update Maggie\'s age to 1', (done) => {
|
310 | personModel.update(
|
311 | {
|
312 | employeeNo: 2,
|
313 | age: 1,
|
314 | firstName: 'Maggie',
|
315 | lastName: 'Simpson'
|
316 | },
|
317 | {},
|
318 | function (err) {
|
319 | expect(err).to.equal(null)
|
320 | done()
|
321 | }
|
322 | )
|
323 | })
|
324 |
|
325 | it('should find Maggie has an age now', function (done) {
|
326 | personModel.findById(
|
327 | 2,
|
328 | function (err, doc) {
|
329 | expect(err).to.equal(null)
|
330 | expect(doc).to.containSubset(
|
331 | {
|
332 | employeeNo: 2,
|
333 | firstName: 'Maggie',
|
334 | lastName: 'Simpson',
|
335 | age: 1
|
336 | }
|
337 | )
|
338 | done()
|
339 | }
|
340 | )
|
341 | })
|
342 |
|
343 | it('should update Maggie again, but this time without an age', function (done) {
|
344 | personModel.update(
|
345 | {
|
346 | employeeNo: 2,
|
347 | firstName: 'Maggie',
|
348 | lastName: 'Simpson'
|
349 | },
|
350 | {},
|
351 | function (err, doc) {
|
352 | expect(err).to.equal(null)
|
353 | done()
|
354 | }
|
355 | )
|
356 | })
|
357 |
|
358 | it('should find Maggie\'s age has gone again', function (done) {
|
359 | personModel.findById(
|
360 | 2,
|
361 | function (err, doc) {
|
362 | expect(err).to.equal(null)
|
363 | expect(doc).to.containSubset(
|
364 | {
|
365 | employeeNo: 2,
|
366 | firstName: 'Maggie',
|
367 | lastName: 'Simpson'
|
368 | }
|
369 | )
|
370 | done()
|
371 | }
|
372 | )
|
373 | })
|
374 |
|
375 | it('should patch Maggie to Margaret', function (done) {
|
376 | personModel.patch(
|
377 | {
|
378 | employeeNo: 2,
|
379 | firstName: 'Margaret'
|
380 | },
|
381 | {},
|
382 | function (err, doc) {
|
383 | expect(err).to.equal(null)
|
384 | done()
|
385 | }
|
386 | )
|
387 | })
|
388 |
|
389 | it('should find Maggie is now a Margaret', function (done) {
|
390 | personModel.findById(
|
391 | 2,
|
392 | function (err, doc) {
|
393 | expect(err).to.equal(null)
|
394 | expect(doc).to.containSubset(
|
395 | {
|
396 | employeeNo: 2,
|
397 | firstName: 'Margaret',
|
398 | lastName: 'Simpson'
|
399 | }
|
400 | )
|
401 | done()
|
402 | }
|
403 | )
|
404 | })
|
405 |
|
406 | it('should delete Maggie/Margaret by via her id', function (done) {
|
407 | personModel.destroyById(
|
408 | 2,
|
409 | function (err, doc) {
|
410 | expect(err).to.equal(null)
|
411 | done()
|
412 | }
|
413 | )
|
414 | })
|
415 |
|
416 | it('should fail getting a deleted record', function (done) {
|
417 | personModel.findById(
|
418 | 2,
|
419 | function (err, doc) {
|
420 | expect(err).to.equal(null)
|
421 | expect(doc).to.equal(undefined)
|
422 | done()
|
423 | }
|
424 | )
|
425 | })
|
426 |
|
427 | it('should upsert (insert) Grampa', function (done) {
|
428 | personModel.upsert(
|
429 | {
|
430 | employeeNo: 10,
|
431 | firstName: 'Abe',
|
432 | lastName: 'Simpson',
|
433 | age: 82
|
434 | },
|
435 | {},
|
436 | function (err, idProperties) {
|
437 | expect(idProperties).to.eql(
|
438 | {
|
439 | idProperties: {
|
440 | employeeNo: 10
|
441 | }
|
442 | }
|
443 | )
|
444 | expect(err).to.equal(null)
|
445 | done()
|
446 | }
|
447 | )
|
448 | })
|
449 |
|
450 | it('should find Grampa has been inserted via upsert', function (done) {
|
451 | personModel.findById(
|
452 | 10,
|
453 | function (err, doc) {
|
454 | expect(err).to.equal(null)
|
455 | expect(doc).to.containSubset(
|
456 | {
|
457 | employeeNo: 10,
|
458 | firstName: 'Abe',
|
459 | lastName: 'Simpson',
|
460 | age: 82
|
461 | }
|
462 | )
|
463 | done()
|
464 | }
|
465 | )
|
466 | })
|
467 |
|
468 | it('should upsert (update) Grampa', function (done) {
|
469 | personModel.upsert(
|
470 | {
|
471 | employeeNo: 10,
|
472 | firstName: 'Abraham',
|
473 | lastName: 'Simpson',
|
474 | age: 83
|
475 | },
|
476 | {},
|
477 | function (err, doc) {
|
478 | expect(err).to.equal(null)
|
479 | done()
|
480 | }
|
481 | )
|
482 | })
|
483 |
|
484 | it('should find Grampa has now been updates via upsert', function (done) {
|
485 | personModel.findById(
|
486 | 10,
|
487 | function (err, doc) {
|
488 | expect(err).to.equal(null)
|
489 | expect(doc).to.containSubset(
|
490 | {
|
491 | employeeNo: 10,
|
492 | firstName: 'Abraham',
|
493 | lastName: 'Simpson',
|
494 | age: 83
|
495 | }
|
496 | )
|
497 | done()
|
498 | }
|
499 | )
|
500 | })
|
501 |
|
502 | it('should now upsert (update) Grampa, resetting his name', function (done) {
|
503 | personModel.upsert(
|
504 | {
|
505 | employeeNo: 10,
|
506 | firstName: 'Abe'
|
507 | },
|
508 | {
|
509 | setMissingPropertiesToNull: false
|
510 | },
|
511 | function (err, doc) {
|
512 | expect(err).to.equal(null)
|
513 | done()
|
514 | }
|
515 | )
|
516 | })
|
517 |
|
518 | it('should find Grampa again, with his age preserved and an updated name', function (done) {
|
519 | personModel.findById(
|
520 | 10,
|
521 | function (err, doc) {
|
522 | expect(err).to.equal(null)
|
523 | expect(doc).to.containSubset(
|
524 | {
|
525 | employeeNo: 10,
|
526 | firstName: 'Abe',
|
527 | lastName: 'Simpson',
|
528 | age: 83
|
529 | }
|
530 | )
|
531 | done()
|
532 | }
|
533 | )
|
534 | })
|
535 |
|
536 | it('should upsert (update) Grampa again, but turn age to null', function (done) {
|
537 | personModel.upsert(
|
538 | {
|
539 | employeeNo: 10,
|
540 | firstName: 'Abraham',
|
541 | lastName: 'Simpson'
|
542 | },
|
543 | {},
|
544 | function (err, doc) {
|
545 | expect(err).to.equal(null)
|
546 | done()
|
547 | }
|
548 | )
|
549 | })
|
550 |
|
551 | it('should find Grampa again, but now with a null age', function (done) {
|
552 | personModel.findById(
|
553 | 10,
|
554 | function (err, doc) {
|
555 | expect(err).to.equal(null)
|
556 | expect(doc).to.containSubset(
|
557 | {
|
558 | employeeNo: 10,
|
559 | firstName: 'Abraham',
|
560 | lastName: 'Simpson'
|
561 | }
|
562 | )
|
563 | done()
|
564 | }
|
565 | )
|
566 | })
|
567 |
|
568 | it('should create mars, with two moons and a few craters', function (done) {
|
569 | planetsModel.create(
|
570 | {
|
571 | name: 'mars',
|
572 | title: 'Mars',
|
573 | type: 'Terrestrial',
|
574 | diameter: 6700,
|
575 | color: 'red',
|
576 | url: 'http://en.wikipedia.org/wiki/Mars',
|
577 | otherFacts: {
|
578 | radius: 3390,
|
579 | surfacePressure: '0.636 (0.4–0.87) kPa; 0.00628 atm',
|
580 | equatorialRotationVelocity: '868.22 km/h (241.17 m/s)'
|
581 | },
|
582 | moons: [
|
583 | {
|
584 | title: 'Phobos',
|
585 | discoveredBy: 'Asaph Hall',
|
586 | discoveryYear: 1800,
|
587 | craters: [
|
588 | {
|
589 | title: 'Stickney',
|
590 | diameter: 9
|
591 | }
|
592 | ]
|
593 | },
|
594 | {
|
595 | title: 'Deimos',
|
596 | discoveredBy: 'Asaph Hall',
|
597 | discoveryYear: 1800
|
598 | }
|
599 | ]
|
600 | },
|
601 | {},
|
602 | function (err, idProperties) {
|
603 | expect(err).to.equal(null)
|
604 | expect(idProperties).to.eql(
|
605 | {
|
606 | idProperties: {
|
607 | name: 'mars'
|
608 | }
|
609 | }
|
610 | )
|
611 | done()
|
612 | }
|
613 | )
|
614 | })
|
615 |
|
616 | it('should find Mars via primary key', function (done) {
|
617 | planetsModel.findById(
|
618 | 'mars',
|
619 | function (err, doc) {
|
620 | expect(err).to.equal(null)
|
621 | expect(doc).to.containSubset(
|
622 | {
|
623 | name: 'mars',
|
624 | title: 'Mars',
|
625 | type: 'Terrestrial',
|
626 | diameter: 6700,
|
627 | color: 'red',
|
628 | url: 'http://en.wikipedia.org/wiki/Mars',
|
629 | otherFacts: {
|
630 | radius: 3390,
|
631 | surfacePressure: '0.636 (0.4–0.87) kPa; 0.00628 atm',
|
632 | equatorialRotationVelocity: '868.22 km/h (241.17 m/s)'
|
633 | },
|
634 | moons: [
|
635 | {
|
636 | title: 'Phobos',
|
637 | discoveredBy: 'Asaph Hall',
|
638 | discoveryYear: 1800,
|
639 | craters: [
|
640 | {
|
641 | title: 'Stickney',
|
642 | diameter: 9
|
643 | }
|
644 | ]
|
645 | },
|
646 | {
|
647 | title: 'Deimos',
|
648 | discoveredBy: 'Asaph Hall',
|
649 | discoveryYear: 1800
|
650 | }
|
651 | ]
|
652 | }
|
653 | )
|
654 |
|
655 | const moons = {}
|
656 | moons[doc.moons[0].title] = doc.moons[0]
|
657 | moons[doc.moons[1].title] = doc.moons[1]
|
658 | phobosId = moons.Phobos.id
|
659 | stickneyId = moons.Phobos.craters[0].id
|
660 | done()
|
661 | }
|
662 | )
|
663 | })
|
664 |
|
665 | it('should update Mars with more accurate info', function (done) {
|
666 | planetsModel.update(
|
667 | {
|
668 | name: 'mars',
|
669 | title: 'Mars',
|
670 | type: 'Terrestrial',
|
671 | diameter: 6779,
|
672 | color: 'red',
|
673 | url: 'http://en.wikipedia.org/wiki/Mars',
|
674 | otherFacts: {
|
675 | radius: 3390,
|
676 | surfacePressure: '0.636 (0.4–0.87) kPa; 0.00628 atm',
|
677 | equatorialRotationVelocity: '868.22 km/h (241.17 m/s)',
|
678 | lengthOfDay: '1d 0h 40m'
|
679 | },
|
680 | moons: [
|
681 | {
|
682 | id: phobosId,
|
683 | title: 'Phobos',
|
684 | discoveredBy: 'Asaph Hall',
|
685 | discoveryYear: 1875,
|
686 | craters: [
|
687 | {
|
688 | id: stickneyId,
|
689 | title: 'Stickney',
|
690 | diameter: 10
|
691 | }
|
692 | ]
|
693 | }
|
694 | ]
|
695 | },
|
696 | {},
|
697 | function (err, doc) {
|
698 | expect(err).to.equal(null)
|
699 | done()
|
700 | }
|
701 | )
|
702 | })
|
703 |
|
704 | it('should find updated Mars via primary key', function (done) {
|
705 | planetsModel.findById(
|
706 | 'mars',
|
707 | function (err, doc) {
|
708 | expect(err).to.equal(null)
|
709 | expect(doc.moons).to.have.length(1)
|
710 | expect(doc).to.containSubset(
|
711 | {
|
712 | name: 'mars',
|
713 | title: 'Mars',
|
714 | type: 'Terrestrial',
|
715 | diameter: 6779,
|
716 | color: 'red',
|
717 | url: 'http://en.wikipedia.org/wiki/Mars',
|
718 | otherFacts: {
|
719 | radius: 3390,
|
720 | surfacePressure: '0.636 (0.4–0.87) kPa; 0.00628 atm',
|
721 | equatorialRotationVelocity: '868.22 km/h (241.17 m/s)',
|
722 | lengthOfDay: '1d 0h 40m'
|
723 | },
|
724 | moons: [
|
725 | {
|
726 | id: phobosId,
|
727 | title: 'Phobos',
|
728 | discoveredBy: 'Asaph Hall',
|
729 | discoveryYear: 1875,
|
730 | craters: [
|
731 | {
|
732 | id: stickneyId,
|
733 | title: 'Stickney',
|
734 | diameter: 10
|
735 | }
|
736 | ]
|
737 | }
|
738 | ]
|
739 | }
|
740 | )
|
741 | done()
|
742 | }
|
743 | )
|
744 | })
|
745 |
|
746 | it('should delete Mars', function (done) {
|
747 | planetsModel.destroyById(
|
748 | 'mars',
|
749 | function (err, doc) {
|
750 | expect(err).to.equal(null)
|
751 | done()
|
752 | }
|
753 | )
|
754 | })
|
755 |
|
756 | it('should now fail to find Mars', function (done) {
|
757 | planetsModel.findById('mars',
|
758 | function (err, doc) {
|
759 | expect(err).to.equal(null)
|
760 | expect(doc).to.equal(undefined)
|
761 | done()
|
762 | }
|
763 | )
|
764 | })
|
765 | })
|