@@ -9,8 +9,12 @@ import User from './models/User'
9
9
10
10
import { ObjectId } from 'bson'
11
11
12
- describe ( 'cache redis' , async ( ) => {
13
- const mongod = await MongoMemoryServer . create ( )
12
+ describe ( 'cache-redis' , async ( ) => {
13
+ const mongod = await MongoMemoryServer . create ( {
14
+ instance : {
15
+ dbName : 'redis-cache' ,
16
+ } ,
17
+ } )
14
18
15
19
const cache = plugin . init ( mongoose , {
16
20
engine : 'redis' ,
@@ -31,7 +35,7 @@ describe('cache redis', async () => {
31
35
await cache . close ( )
32
36
await mongoose . connection . dropDatabase ( )
33
37
await mongoose . connection . close ( )
34
- await mongod . stop ( )
38
+ await mongod . stop ( { doCleanup : true } )
35
39
} )
36
40
37
41
beforeEach ( async ( ) => {
@@ -299,7 +303,7 @@ describe('cache redis', async () => {
299
303
300
304
const cache7 = await User . distinct ( 'createdAt' ) . cache ( '30 seconds' ) . exec ( )
301
305
302
- expect ( miss ) . toEqual ( hit )
306
+ expect ( miss . map ( ( id ) => id . toString ( ) ) ) . toEqual ( hit . map ( ( id ) => id . toString ( ) ) )
303
307
expect ( cache4 ) . toEqual ( cache5 )
304
308
expect ( cache6 ) . toEqual ( cache7 )
305
309
} )
@@ -365,20 +369,20 @@ describe('cache redis', async () => {
365
369
expect ( miss ?. _id . toString ( ) ) . toBe ( hit ?. _id . toString ( ) )
366
370
expect ( miss ?. name ) . toBe ( hit ?. name )
367
371
expect ( miss ?. role ) . toBe ( hit ?. role )
368
- expect ( miss ?. createdAt ) . toBe ( hit ?. createdAt )
369
- expect ( miss ?. updatedAt ) . toBe ( hit ?. updatedAt )
372
+ expect ( miss ?. createdAt ?. toString ( ) ) . toBe ( hit ?. createdAt ?. toString ( ) )
373
+ expect ( miss ?. updatedAt ?. toString ( ) ) . toBe ( hit ?. updatedAt ?. toString ( ) )
370
374
371
375
expect ( miss ?. stories ?. [ 0 ] . _id . toString ( ) ) . toBe ( hit ?. stories ?. [ 0 ] . _id . toString ( ) )
372
376
expect ( miss ?. stories ?. [ 0 ] . title ) . toBe ( hit ?. stories ?. [ 0 ] . title )
373
377
expect ( miss ?. stories ?. [ 0 ] . userId . toString ( ) ) . toBe ( hit ?. stories ?. [ 0 ] . userId . toString ( ) )
374
- expect ( miss ?. stories ?. [ 0 ] . createdAt ) . toBe ( hit ?. stories ?. [ 0 ] . createdAt )
375
- expect ( miss ?. stories ?. [ 0 ] . updatedAt ) . toBe ( hit ?. stories ?. [ 0 ] . updatedAt )
378
+ expect ( miss ?. stories ?. [ 0 ] . createdAt ?. toString ( ) ) . toBe ( hit ?. stories ?. [ 0 ] . createdAt ?. toString ( ) )
379
+ expect ( miss ?. stories ?. [ 0 ] . updatedAt ?. toString ( ) ) . toBe ( hit ?. stories ?. [ 0 ] . updatedAt ?. toString ( ) )
376
380
377
381
expect ( miss ?. stories ?. [ 1 ] . _id . toString ( ) ) . toBe ( hit ?. stories ?. [ 1 ] . _id . toString ( ) )
378
382
expect ( miss ?. stories ?. [ 1 ] . title ) . toBe ( hit ?. stories ?. [ 1 ] . title )
379
383
expect ( miss ?. stories ?. [ 1 ] . userId . toString ( ) ) . toBe ( hit ?. stories ?. [ 1 ] . userId . toString ( ) )
380
- expect ( miss ?. stories ?. [ 1 ] . createdAt ) . toBe ( hit ?. stories ?. [ 1 ] . createdAt )
381
- expect ( miss ?. stories ?. [ 1 ] . updatedAt ) . toBe ( hit ?. stories ?. [ 1 ] . updatedAt )
384
+ expect ( miss ?. stories ?. [ 1 ] . createdAt ?. toString ( ) ) . toBe ( hit ?. stories ?. [ 1 ] . createdAt ?. toString ( ) )
385
+ expect ( miss ?. stories ?. [ 1 ] . updatedAt ?. toString ( ) ) . toBe ( hit ?. stories ?. [ 1 ] . updatedAt ?. toString ( ) )
382
386
} )
383
387
384
388
it ( 'should not misclassify certain fields as objectIds' , async ( ) => {
@@ -418,7 +422,7 @@ describe('cache redis', async () => {
418
422
const distinctHit = await User . distinct ( '_id' ) . cache ( '30 seconds' ) . lean ( ) . exec ( )
419
423
expect ( distinctHit ) . not . toBeNull ( )
420
424
expect ( distinctHit ?. length ) . toBe ( 1 )
421
- expect ( distinctHit ) . toEqual ( [ pureLean ?. _id ] )
425
+ expect ( distinctHit . map ( ( id ) => id . toString ( ) ) ) . toEqual ( [ pureLean ?. _id . toString ( ) ] )
422
426
423
427
const distinctCreatedAtMiss = await User . distinct ( 'createdAt' ) . cache ( '30 seconds' ) . lean ( ) . exec ( )
424
428
expect ( distinctCreatedAtMiss ) . not . toBeNull ( )
@@ -434,7 +438,11 @@ describe('cache redis', async () => {
434
438
expect ( distinctCreatedAtMiss ?. [ 0 ] instanceof Date ) . toBeTruthy ( )
435
439
expect ( distinctCreatedAtHit ) . toEqual ( [ pureLean ?. createdAt ] )
436
440
437
- expect ( miss ) . toEqual ( hit )
441
+ expect ( miss ?. _id . toString ( ) ) . toBe ( hit ?. _id . toString ( ) )
442
+ expect ( miss ?. name ) . toBe ( hit ?. name )
443
+ expect ( miss ?. role ) . toBe ( hit ?. role )
444
+ expect ( miss ?. createdAt ?. toString ( ) ) . toBe ( hit ?. createdAt ?. toString ( ) )
445
+ expect ( miss ?. updatedAt ?. toString ( ) ) . toBe ( hit ?. updatedAt ?. toString ( ) )
438
446
} )
439
447
440
448
it ( 'should hydrate populated objects from cache' , async ( ) => {
@@ -500,31 +508,22 @@ describe('cache redis', async () => {
500
508
expect ( typeof populatedCache ?. stories ?. [ 1 ] . createdAt ) . toBe ( 'object' )
501
509
expect ( populatedCache ?. stories ?. [ 1 ] . createdAt instanceof Date ) . toBeTruthy ( )
502
510
503
- expect ( populatedOriginal ) . toEqual ( populatedCache )
504
-
505
- // Code bellow will fail see: https://github.com/Automattic/mongoose/issues/14503
506
-
507
- // const populatedJson = JSON.stringify(populatedOriginal)
508
- // expect(populatedJson).not.toBeNull()
509
-
510
- // const hydrated = User.hydrate(JSON.parse(populatedJson), undefined, { hydratedPopulatedDocs: true })
511
-
512
- // expect(hydrated).not.toBeNull()
513
- // expect(hydrated?._id instanceof mongoose.Types.ObjectId).toBeTruthy()
514
- // expect(hydrated?.name).toBe('Alex')
515
- // expect(hydrated?.stories).not.toBeNull()
516
- // expect(hydrated?.stories?.length).toBe(2)
517
-
518
- // expect(hydrated?.stories?.[0]._id).toEqual(story1._id)
519
- // expect(typeof hydrated?.stories?.[0]._id).toBe('object')
520
- // expect(hydrated?.stories?.[0]._id instanceof mongoose.Types.ObjectId).toBeTruthy()
521
- // expect(typeof hydrated?.stories?.[0].createdAt).toBe('object')
522
- // expect(hydrated?.stories?.[0].createdAt instanceof Date).toBeTruthy()
523
-
524
- // expect(hydrated?.stories?.[1]._id).toEqual(story2._id)
525
- // expect(typeof hydrated?.stories?.[1]._id).toBe('object')
526
- // expect(hydrated?.stories?.[1]._id instanceof mongoose.Types.ObjectId).toBeTruthy()
527
- // expect(typeof hydrated?.stories?.[1].createdAt).toBe('object')
528
- // expect(hydrated?.stories?.[1].createdAt instanceof Date).toBeTruthy()
511
+ expect ( populatedOriginal ?. _id . toString ( ) ) . toBe ( populatedCache ?. _id . toString ( ) )
512
+ expect ( populatedOriginal ?. name ) . toBe ( populatedCache ?. name )
513
+ expect ( populatedOriginal ?. role ) . toBe ( populatedCache ?. role )
514
+ expect ( populatedOriginal ?. createdAt ?. toString ( ) ) . toBe ( populatedCache ?. createdAt ?. toString ( ) )
515
+ expect ( populatedOriginal ?. updatedAt ?. toString ( ) ) . toBe ( populatedCache ?. updatedAt ?. toString ( ) )
516
+
517
+ expect ( populatedOriginal ?. stories ?. [ 0 ] . _id . toString ( ) ) . toBe ( populatedCache ?. stories ?. [ 0 ] . _id . toString ( ) )
518
+ expect ( populatedOriginal ?. stories ?. [ 0 ] . title ) . toBe ( populatedCache ?. stories ?. [ 0 ] . title )
519
+ expect ( populatedOriginal ?. stories ?. [ 0 ] . userId . toString ( ) ) . toBe ( populatedCache ?. stories ?. [ 0 ] . userId . toString ( ) )
520
+ expect ( populatedOriginal ?. stories ?. [ 0 ] . createdAt ?. toString ( ) ) . toBe ( populatedCache ?. stories ?. [ 0 ] . createdAt ?. toString ( ) )
521
+ expect ( populatedOriginal ?. stories ?. [ 0 ] . updatedAt ?. toString ( ) ) . toBe ( populatedCache ?. stories ?. [ 0 ] . updatedAt ?. toString ( ) )
522
+
523
+ expect ( populatedOriginal ?. stories ?. [ 1 ] . _id . toString ( ) ) . toBe ( populatedCache ?. stories ?. [ 1 ] . _id . toString ( ) )
524
+ expect ( populatedOriginal ?. stories ?. [ 1 ] . title ) . toBe ( populatedCache ?. stories ?. [ 1 ] . title )
525
+ expect ( populatedOriginal ?. stories ?. [ 1 ] . userId . toString ( ) ) . toBe ( populatedCache ?. stories ?. [ 1 ] . userId . toString ( ) )
526
+ expect ( populatedOriginal ?. stories ?. [ 1 ] . createdAt ?. toString ( ) ) . toBe ( populatedCache ?. stories ?. [ 1 ] . createdAt ?. toString ( ) )
527
+ expect ( populatedOriginal ?. stories ?. [ 1 ] . updatedAt ?. toString ( ) ) . toBe ( populatedCache ?. stories ?. [ 1 ] . updatedAt ?. toString ( ) )
529
528
} )
530
529
} )
0 commit comments