From 39511f6a5b7365e1ba4a2c193161e85defb6040e Mon Sep 17 00:00:00 2001 From: iPromKnight Date: Thu, 8 Feb 2024 13:06:54 +0000 Subject: [PATCH] Add cache service tests --- src/node/consumer/test/cache_service.test.ts | 120 +++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 src/node/consumer/test/cache_service.test.ts diff --git a/src/node/consumer/test/cache_service.test.ts b/src/node/consumer/test/cache_service.test.ts new file mode 100644 index 0000000..ebef771 --- /dev/null +++ b/src/node/consumer/test/cache_service.test.ts @@ -0,0 +1,120 @@ +import "reflect-metadata"; // required +import { ILoggingService } from '@interfaces/logging_service'; +import { CacheService, CacheMethod } from '@services/cache_service'; + +process.env.LOG_LEVEL = 'debug'; + +jest.mock('@services/configuration_service', () => { + return { + configurationService: { + cacheConfig: { + MONGODB_HOST: 'localhost', + MONGODB_PORT: '27017', + MONGODB_DB: 'knightcrawler', + MONGO_INITDB_ROOT_USERNAME: 'mongo', + MONGO_INITDB_ROOT_PASSWORD: 'mongo', + NO_CACHE: false, + COLLECTION_NAME: 'knightcrawler_consumer_collection', + }, + } + } +}); + +jest.mock('@services/logging_service', () => { + return { + error: jest.fn(), + info: jest.fn(), + debug: jest.fn(), + } +}) + +jest.mock('cache-manager', () => { + return { + createCache: jest.fn().mockReturnValue({ + wrap: jest.fn().mockImplementation((_, method) => method()), + }), + memoryStore: jest.fn(), + }; +}); + +jest.mock('@tirke/node-cache-manager-mongodb', () => { + return { + mongoDbStore: jest.fn(), + }; +}); + +describe('CacheService', () => { + let cacheService: CacheService, + loggingService: ILoggingService, + cacheMethod: CacheMethod; + + beforeEach(() => { + loggingService = jest.requireMock('@services/logging_service'); + cacheMethod = jest.fn().mockResolvedValue({}); + cacheService = new CacheService(loggingService); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it('should cacheWrapImdbId correctly', async () => { + const result = await cacheService.cacheWrapImdbId('testKey', cacheMethod); + expect(result).toBeDefined(); + expect(loggingService.debug).toHaveBeenCalledTimes(3); + }); + + it('should cacheWrapKitsuId correctly', async () => { + const result = await cacheService.cacheWrapKitsuId('testKey', cacheMethod); + expect(result).toBeDefined(); + expect(loggingService.debug).toHaveBeenCalledTimes(3); + }); + + it('should cacheWrapMetadata correctly', async () => { + const result = await cacheService.cacheWrapMetadata('testId', cacheMethod); + expect(result).toBeDefined(); + expect(loggingService.debug).toHaveBeenCalledTimes(3); + }); + + it('should cacheTrackers correctly', async () => { + const result = await cacheService.cacheTrackers(cacheMethod); + expect(result).toBeDefined(); + expect(loggingService.debug).toHaveBeenCalledTimes(3); + }); + + it('should handle error in cacheMethod for cacheWrapImdbId', async () => { + cacheMethod = jest.fn().mockRejectedValue(new Error('Test error')); + await expect(cacheService.cacheWrapImdbId('testKey', cacheMethod)).rejects.toThrow('Test error'); + }); + + it('should handle error in cacheMethod for cacheWrapKitsuId', async () => { + cacheMethod = jest.fn().mockRejectedValue(new Error('Test error')); + await expect(cacheService.cacheWrapKitsuId('testKey', cacheMethod)).rejects.toThrow('Test error'); + }); + + it('should handle error in cacheMethod for cacheWrapMetadata', async () => { + cacheMethod = jest.fn().mockRejectedValue(new Error('Test error')); + await expect(cacheService.cacheWrapMetadata('testId', cacheMethod)).rejects.toThrow('Test error'); + }); + + it('should handle error in cacheMethod for cacheTrackers', async () => { + cacheMethod = jest.fn().mockRejectedValue(new Error('Test error')); + await expect(cacheService.cacheTrackers(cacheMethod)).rejects.toThrow('Test error'); + }); + + it('should handle when cache is disabled', async () => { + jest.mock('@services/configuration_service', () => { + return { + configurationService: { + cacheConfig: { + NO_CACHE: true, + }, + } + } + }); + + cacheService = new CacheService(loggingService); + const result = await cacheService.cacheWrapImdbId('testKey', cacheMethod); + expect(result).toBeDefined(); + }); +}); \ No newline at end of file