Skip to content

Commit

Permalink
Add S3CacheService tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBartusek committed Apr 14, 2024
1 parent 5627504 commit 8c65ca7
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions apps/api/src/s3-cache/s3-cache.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Test, TestingModule } from '@nestjs/testing';
import { S3CacheService, S3_CACHE_REDIS_PREFIX } from './s3-cache.service';
import { S3Service } from '../s3/s3.service';
import { RedisService } from '../redis/redis.service';
import { Readable } from 'stream';
import Utils from '../helpers/utils';
import Redis from 'ioredis-mock';

describe('S3CacheService', () => {
let service: S3CacheService;

const s3ServiceMock = {
getObjectBody: jest.fn(() => Readable.from(Buffer.from('object'))),
};

const redisMock = new Redis();
const redisService = {
client: redisMock,
};

const getObjectBodyMock = jest.spyOn(s3ServiceMock, 'getObjectBody');
const redisSetSpy = jest.spyOn(redisService.client, 'set');

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
S3CacheService,
{
provide: S3Service,
useValue: s3ServiceMock,
},
{
provide: RedisService,
useValue: redisService,
},
],
}).compile();
service = module.get<S3CacheService>(S3CacheService);
});

afterAll(() => {
jest.clearAllMocks();
redisService.client.flushall();
});

it('should be defined', () => {
expect(service).toBeDefined();
});

it('should get object if cached', async () => {
const mockObject = Buffer.from('object');
redisService.client.set(generateRedisKey('old-key'), mockObject);

const object = await service.getObject('old-key');

expect(Utils.streamToBuffer(object)).resolves.toEqual(mockObject);
expect(getObjectBodyMock).toHaveBeenCalledTimes(0);
});

it('should get and store object if not cached', async () => {
const object = await service.getObject('new-key');

expect(Utils.streamToBuffer(object)).resolves.toEqual(Buffer.from('object'));
expect(getObjectBodyMock).toHaveBeenCalledWith('new-key');
expect(redisSetSpy).toHaveBeenCalledWith(generateRedisKey('new-key'), Buffer.from('object'));
});

function generateRedisKey(key: string) {
return `${S3_CACHE_REDIS_PREFIX}${key}`;
}
});

0 comments on commit 8c65ca7

Please sign in to comment.