From e407e915fa0a614aeb5694d0e0d424eb45b2536e Mon Sep 17 00:00:00 2001 From: sbriat Date: Tue, 27 Jun 2023 12:18:22 +0200 Subject: [PATCH] fixed queryRaw for healthcheck, added health grpc controller test --- src/libs/db/prisma-repository.base.ts | 2 +- .../health.grpc.controller.ts | 6 +- .../tests/unit/health.grpc.controller.spec.ts | 72 +++++++++++++++++++ 3 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 src/modules/health/tests/unit/health.grpc.controller.spec.ts diff --git a/src/libs/db/prisma-repository.base.ts b/src/libs/db/prisma-repository.base.ts index 9f8983d..897834a 100644 --- a/src/libs/db/prisma-repository.base.ts +++ b/src/libs/db/prisma-repository.base.ts @@ -53,7 +53,7 @@ export abstract class PrismaRepositoryBase< async healthCheck(): Promise { try { - await this.prisma.$queryRaw`SELECT 1`; + await this.prismaRaw.$queryRaw`SELECT 1`; return true; } catch (e) { if (e instanceof Prisma.PrismaClientKnownRequestError) { diff --git a/src/modules/health/interface/grpc-controllers/health.grpc.controller.ts b/src/modules/health/interface/grpc-controllers/health.grpc.controller.ts index 5aa260e..f939dd2 100644 --- a/src/modules/health/interface/grpc-controllers/health.grpc.controller.ts +++ b/src/modules/health/interface/grpc-controllers/health.grpc.controller.ts @@ -2,7 +2,7 @@ import { Controller } from '@nestjs/common'; import { GrpcMethod } from '@nestjs/microservices'; import { RepositoriesHealthIndicatorUseCase } from '../../core/usecases/repositories.health-indicator.usecase'; -enum ServingStatus { +export enum ServingStatus { UNKNOWN = 0, SERVING = 1, NOT_SERVING = 2, @@ -25,9 +25,9 @@ export class HealthGrpcController { @GrpcMethod('Health', 'Check') async check( // eslint-disable-next-line @typescript-eslint/no-unused-vars - data: HealthCheckRequest, + data?: HealthCheckRequest, // eslint-disable-next-line @typescript-eslint/no-unused-vars - metadata: any, + metadata?: any, ): Promise { const healthCheck = await this.repositoriesHealthIndicatorUseCase.isHealthy( 'repositories', diff --git a/src/modules/health/tests/unit/health.grpc.controller.spec.ts b/src/modules/health/tests/unit/health.grpc.controller.spec.ts new file mode 100644 index 0000000..0157218 --- /dev/null +++ b/src/modules/health/tests/unit/health.grpc.controller.spec.ts @@ -0,0 +1,72 @@ +import { RepositoriesHealthIndicatorUseCase } from '@modules/health/core/usecases/repositories.health-indicator.usecase'; +import { + HealthGrpcController, + ServingStatus, +} from '@modules/health/interface/grpc-controllers/health.grpc.controller'; +import { Test, TestingModule } from '@nestjs/testing'; + +const mockRepositoriesHealthIndicatorUseCase = { + isHealthy: jest + .fn() + .mockImplementationOnce(() => ({ + repositories: { + status: 'up', + }, + })) + .mockImplementationOnce(() => ({ + repositories: { + status: 'down', + }, + })), +}; + +describe('Health Grpc Controller', () => { + let healthGrpcController: HealthGrpcController; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + { + provide: RepositoriesHealthIndicatorUseCase, + useValue: mockRepositoriesHealthIndicatorUseCase, + }, + HealthGrpcController, + ], + }).compile(); + + healthGrpcController = + module.get(HealthGrpcController); + }); + + afterEach(async () => { + jest.clearAllMocks(); + }); + + it('should be defined', () => { + expect(healthGrpcController).toBeDefined(); + }); + + it('should return a Serving status ', async () => { + jest.spyOn(mockRepositoriesHealthIndicatorUseCase, 'isHealthy'); + const servingStatus: { status: ServingStatus } = + await healthGrpcController.check(); + expect(servingStatus).toEqual({ + status: ServingStatus.SERVING, + }); + expect( + mockRepositoriesHealthIndicatorUseCase.isHealthy, + ).toHaveBeenCalledTimes(1); + }); + + it('should return a Not Serving status ', async () => { + jest.spyOn(mockRepositoriesHealthIndicatorUseCase, 'isHealthy'); + const servingStatus: { status: ServingStatus } = + await healthGrpcController.check(); + expect(servingStatus).toEqual({ + status: ServingStatus.NOT_SERVING, + }); + expect( + mockRepositoriesHealthIndicatorUseCase.isHealthy, + ).toHaveBeenCalledTimes(1); + }); +});