Skip to content

Commit b27064e

Browse files
committed
Refactor
1 parent fc1f2a6 commit b27064e

File tree

3 files changed

+51
-44
lines changed

3 files changed

+51
-44
lines changed

tests/cache-debug.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ import CacheMongoose from '../src/plugin'
66

77
import UserSchema from './schemas/UserSchema'
88

9-
describe('CacheMongoose', async () => {
10-
const mongod = await MongoMemoryServer.create()
9+
describe('cache-debug', async () => {
10+
const mongod = await MongoMemoryServer.create({
11+
instance: {
12+
dbName: 'cache-debug',
13+
},
14+
})
1115
const User = model('User', UserSchema)
1216

1317
const cache = CacheMongoose.init(mongoose, {
@@ -25,7 +29,7 @@ describe('CacheMongoose', async () => {
2529
await cache.close()
2630
await mongoose.connection.dropDatabase()
2731
await mongoose.connection.close()
28-
await mongod.stop()
32+
await mongod.stop({ doCleanup: true, force: true })
2933
})
3034

3135
beforeEach(async () => {

tests/cache-memory.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ import CacheMongoose from '../src/plugin'
66

77
import UserSchema from './schemas/UserSchema'
88

9-
describe('CacheMongoose', async () => {
10-
const mongod = await MongoMemoryServer.create()
9+
describe('cache-memory', async () => {
10+
const mongod = await MongoMemoryServer.create({
11+
instance: {
12+
dbName: 'cache-memory',
13+
},
14+
})
1115
const User = model('User', UserSchema)
1216

1317
const cache = CacheMongoose.init(mongoose, {
@@ -24,7 +28,7 @@ describe('CacheMongoose', async () => {
2428
await cache.close()
2529
await mongoose.connection.dropDatabase()
2630
await mongoose.connection.close()
27-
await mongod.stop()
31+
await mongod.stop({ doCleanup: true, force: true })
2832
})
2933

3034
beforeEach(async () => {

tests/cache-redis.test.ts

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ import User from './models/User'
99

1010
import { ObjectId } from 'bson'
1111

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+
})
1418

1519
const cache = plugin.init(mongoose, {
1620
engine: 'redis',
@@ -31,7 +35,7 @@ describe('cache redis', async () => {
3135
await cache.close()
3236
await mongoose.connection.dropDatabase()
3337
await mongoose.connection.close()
34-
await mongod.stop()
38+
await mongod.stop({ doCleanup: true })
3539
})
3640

3741
beforeEach(async () => {
@@ -299,7 +303,7 @@ describe('cache redis', async () => {
299303

300304
const cache7 = await User.distinct('createdAt').cache('30 seconds').exec()
301305

302-
expect(miss).toEqual(hit)
306+
expect(miss.map((id) => id.toString())).toEqual(hit.map((id) => id.toString()))
303307
expect(cache4).toEqual(cache5)
304308
expect(cache6).toEqual(cache7)
305309
})
@@ -365,20 +369,20 @@ describe('cache redis', async () => {
365369
expect(miss?._id.toString()).toBe(hit?._id.toString())
366370
expect(miss?.name).toBe(hit?.name)
367371
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())
370374

371375
expect(miss?.stories?.[0]._id.toString()).toBe(hit?.stories?.[0]._id.toString())
372376
expect(miss?.stories?.[0].title).toBe(hit?.stories?.[0].title)
373377
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())
376380

377381
expect(miss?.stories?.[1]._id.toString()).toBe(hit?.stories?.[1]._id.toString())
378382
expect(miss?.stories?.[1].title).toBe(hit?.stories?.[1].title)
379383
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())
382386
})
383387

384388
it('should not misclassify certain fields as objectIds', async () => {
@@ -418,7 +422,7 @@ describe('cache redis', async () => {
418422
const distinctHit = await User.distinct('_id').cache('30 seconds').lean().exec()
419423
expect(distinctHit).not.toBeNull()
420424
expect(distinctHit?.length).toBe(1)
421-
expect(distinctHit).toEqual([pureLean?._id])
425+
expect(distinctHit.map((id) => id.toString())).toEqual([pureLean?._id.toString()])
422426

423427
const distinctCreatedAtMiss = await User.distinct('createdAt').cache('30 seconds').lean().exec()
424428
expect(distinctCreatedAtMiss).not.toBeNull()
@@ -434,7 +438,11 @@ describe('cache redis', async () => {
434438
expect(distinctCreatedAtMiss?.[0] instanceof Date).toBeTruthy()
435439
expect(distinctCreatedAtHit).toEqual([pureLean?.createdAt])
436440

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())
438446
})
439447

440448
it('should hydrate populated objects from cache', async () => {
@@ -500,31 +508,22 @@ describe('cache redis', async () => {
500508
expect(typeof populatedCache?.stories?.[1].createdAt).toBe('object')
501509
expect(populatedCache?.stories?.[1].createdAt instanceof Date).toBeTruthy()
502510

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())
529528
})
530529
})

0 commit comments

Comments
 (0)