From 3c792718aba2d518dfda301978c6e845d9b5a07b Mon Sep 17 00:00:00 2001 From: Sergii Mykyteiek Date: Tue, 21 Feb 2023 14:29:14 +0200 Subject: [PATCH 1/2] LT-26: Exclude default ttl value, edit readme --- README.md | 3 +-- src/config/app.ts | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index 59395c7..cdc2ca6 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,6 @@ Clone this repo. * Define .env file in the root of project and variables (below default values which will run automatically): * PORT=3000 * NODE_ENV=dev - * TTL_EXPIRATION=30 * API_SWAGGER_ENABLED=1 * API_SWAGGER_URL=api-docs @@ -34,5 +33,5 @@ You can use all endpoints via Swagger http://localhost:3000/api-docs * TTL data * GET localhost:3000/ttl/:key - *get item by key* * POST localhost:3000/ttl - *add item* - * body { "key": string; "value": any } + * body { "key": string; "value": any, "expirationSeconds": number (optional) } * DELETE localhost:3000/ttl/:key - *remove item by key* diff --git a/src/config/app.ts b/src/config/app.ts index dcfc912..64d7c68 100644 --- a/src/config/app.ts +++ b/src/config/app.ts @@ -1,5 +1,4 @@ export const appConfig = { - TTL_EXPIRATION: 30, API_SWAGGER_ENABLED: 1, API_SWAGGER_URL: 'api-docs', }; -- GitLab From 04c2bec3485bf3115b7c7ac346336cb52d63fc2e Mon Sep 17 00:00:00 2001 From: Sergii Mykyteiek Date: Tue, 21 Feb 2023 14:29:26 +0200 Subject: [PATCH 2/2] LT-26: Add ttl repository tests --- src/modules/ttl/ttl.repository.ts | 7 +-- src/tests/ttl/ttl.repository.spec.ts | 75 ++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 src/tests/ttl/ttl.repository.spec.ts diff --git a/src/modules/ttl/ttl.repository.ts b/src/modules/ttl/ttl.repository.ts index 61b14b1..0ec607d 100644 --- a/src/modules/ttl/ttl.repository.ts +++ b/src/modules/ttl/ttl.repository.ts @@ -14,12 +14,7 @@ export class TtlRepository { } get(key: string): TtlResponse | undefined { - const item = this.store.get(key); - if (item === undefined) { - return item; - } - - return item; + return this.store.get(key); } remove(key: string): number { diff --git a/src/tests/ttl/ttl.repository.spec.ts b/src/tests/ttl/ttl.repository.spec.ts new file mode 100644 index 0000000..19382da --- /dev/null +++ b/src/tests/ttl/ttl.repository.spec.ts @@ -0,0 +1,75 @@ +import 'reflect-metadata'; +import { Container } from 'typedi'; +import { TtlModel } from '../../modules/ttl/ttl.model'; +import { TtlRepository } from '../../modules/ttl/ttl.repository'; +import { TtlDto, TtlResponse } from '../../modules/ttl/ttl.dto'; + +describe('TtlRepository', () => { + let ttlModel: TtlModel; + let ttlRepository: TtlRepository; + + beforeEach(() => { + ttlModel = Container.get(TtlModel); + ttlRepository = Container.get(TtlRepository); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + describe('add', () => { + it('Should call the push method of model', () => { + const dtoMock: TtlDto = { + key: 'one', + value: 1, + expirationSeconds: 30, + }; + + const responseMock: TtlResponse = { + key: dtoMock.key, + value: dtoMock.value, + }; + + jest.spyOn(ttlModel, 'push').mockImplementation(() => {}); + + const result = ttlRepository.add(dtoMock); + + expect(ttlModel.push).toBeCalledWith( + dtoMock.key, + dtoMock.value, + dtoMock.expirationSeconds, + ); + expect(result).toEqual(responseMock); + }); + }); + + describe('get', () => { + it('Should call the get method of the model', () => { + const dataMock: TtlResponse = { + key: 'one', + value: 1, + }; + + jest.spyOn(ttlModel, 'get').mockImplementation(() => dataMock); + + const result = ttlRepository.get(dataMock.key); + + expect(ttlModel.get).toBeCalledWith(dataMock.key); + expect(result).toEqual(dataMock); + }); + }); + + describe('remove', () => { + it('Should call the del method of the model', () => { + const keyMock = 'one'; + const removedItemResp = 1; + + jest.spyOn(ttlModel, 'del').mockImplementation(() => removedItemResp); + + const result = ttlRepository.remove(keyMock); + + expect(ttlModel.del).toBeCalledWith(keyMock); + expect(result).toBe(removedItemResp); + }); + }); +}); -- GitLab