From effe51b9a201a20480776aa65127aa1a874ec1cb Mon Sep 17 00:00:00 2001 From: sbriat Date: Fri, 25 Aug 2023 10:37:32 +0200 Subject: [PATCH] basic match query --- .../dtos/match.request.dto.ts | 8 ++++ .../grpc-controllers/match.grpc-controller.ts | 2 +- .../interface/match.grpc.controller.spec.ts | 37 +++++++++++++++---- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/modules/matcher/interface/grpc-controllers/dtos/match.request.dto.ts b/src/modules/matcher/interface/grpc-controllers/dtos/match.request.dto.ts index 607d485..5ffca07 100644 --- a/src/modules/matcher/interface/grpc-controllers/dtos/match.request.dto.ts +++ b/src/modules/matcher/interface/grpc-controllers/dtos/match.request.dto.ts @@ -116,4 +116,12 @@ export class MatchRequestDto { @Min(0) @Max(1) maxDetourDurationRatio?: number; + + @IsOptional() + @IsInt() + page?: number; + + @IsOptional() + @IsInt() + perPage?: number; } diff --git a/src/modules/matcher/interface/grpc-controllers/match.grpc-controller.ts b/src/modules/matcher/interface/grpc-controllers/match.grpc-controller.ts index 7fdee64..47cabfb 100644 --- a/src/modules/matcher/interface/grpc-controllers/match.grpc-controller.ts +++ b/src/modules/matcher/interface/grpc-controllers/match.grpc-controller.ts @@ -25,7 +25,7 @@ export class MatchGrpcController { data: matches, page: 1, perPage: 5, - total: 1, + total: matches.length, }; } catch (e) { throw new RpcException({ diff --git a/src/modules/matcher/tests/unit/interface/match.grpc.controller.spec.ts b/src/modules/matcher/tests/unit/interface/match.grpc.controller.spec.ts index 7c1ef68..894bda6 100644 --- a/src/modules/matcher/tests/unit/interface/match.grpc.controller.spec.ts +++ b/src/modules/matcher/tests/unit/interface/match.grpc.controller.spec.ts @@ -1,8 +1,10 @@ +import { RpcExceptionCode } from '@mobicoop/ddd-library'; import { Frequency } from '@modules/matcher/core/domain/match.types'; import { MatchRequestDto } from '@modules/matcher/interface/grpc-controllers/dtos/match.request.dto'; import { WaypointDto } from '@modules/matcher/interface/grpc-controllers/dtos/waypoint.dto'; import { MatchGrpcController } from '@modules/matcher/interface/grpc-controllers/match.grpc-controller'; import { QueryBus } from '@nestjs/cqrs'; +import { RpcException } from '@nestjs/microservices'; import { Test, TestingModule } from '@nestjs/testing'; const originWaypoint: WaypointDto = { @@ -38,14 +40,19 @@ const punctualMatchRequestDto: MatchRequestDto = { }; const mockQueryBus = { - execute: jest.fn().mockImplementation(() => [ - { - adId: 1, - }, - { - adId: 2, - }, - ]), + execute: jest + .fn() + .mockImplementationOnce(() => [ + { + adId: 1, + }, + { + adId: 2, + }, + ]) + .mockImplementationOnce(() => { + throw new Error(); + }), }; describe('Match Grpc Controller', () => { @@ -74,9 +81,23 @@ describe('Match Grpc Controller', () => { }); it('should return matches', async () => { + jest.spyOn(mockQueryBus, 'execute'); const matchPaginatedResponseDto = await matchGrpcController.match( punctualMatchRequestDto, ); expect(matchPaginatedResponseDto.data).toHaveLength(2); + expect(mockQueryBus.execute).toHaveBeenCalledTimes(1); + }); + + it('should throw a generic RpcException', async () => { + jest.spyOn(mockQueryBus, 'execute'); + expect.assertions(3); + try { + await matchGrpcController.match(punctualMatchRequestDto); + } catch (e: any) { + expect(e).toBeInstanceOf(RpcException); + expect(e.error.code).toBe(RpcExceptionCode.UNKNOWN); + } + expect(mockQueryBus.execute).toHaveBeenCalledTimes(1); }); });