basic match query

This commit is contained in:
sbriat 2023-08-25 10:37:32 +02:00
parent a98e5b3c83
commit effe51b9a2
3 changed files with 38 additions and 9 deletions

View File

@ -116,4 +116,12 @@ export class MatchRequestDto {
@Min(0) @Min(0)
@Max(1) @Max(1)
maxDetourDurationRatio?: number; maxDetourDurationRatio?: number;
@IsOptional()
@IsInt()
page?: number;
@IsOptional()
@IsInt()
perPage?: number;
} }

View File

@ -25,7 +25,7 @@ export class MatchGrpcController {
data: matches, data: matches,
page: 1, page: 1,
perPage: 5, perPage: 5,
total: 1, total: matches.length,
}; };
} catch (e) { } catch (e) {
throw new RpcException({ throw new RpcException({

View File

@ -1,8 +1,10 @@
import { RpcExceptionCode } from '@mobicoop/ddd-library';
import { Frequency } from '@modules/matcher/core/domain/match.types'; import { Frequency } from '@modules/matcher/core/domain/match.types';
import { MatchRequestDto } from '@modules/matcher/interface/grpc-controllers/dtos/match.request.dto'; import { MatchRequestDto } from '@modules/matcher/interface/grpc-controllers/dtos/match.request.dto';
import { WaypointDto } from '@modules/matcher/interface/grpc-controllers/dtos/waypoint.dto'; import { WaypointDto } from '@modules/matcher/interface/grpc-controllers/dtos/waypoint.dto';
import { MatchGrpcController } from '@modules/matcher/interface/grpc-controllers/match.grpc-controller'; import { MatchGrpcController } from '@modules/matcher/interface/grpc-controllers/match.grpc-controller';
import { QueryBus } from '@nestjs/cqrs'; import { QueryBus } from '@nestjs/cqrs';
import { RpcException } from '@nestjs/microservices';
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
const originWaypoint: WaypointDto = { const originWaypoint: WaypointDto = {
@ -38,14 +40,19 @@ const punctualMatchRequestDto: MatchRequestDto = {
}; };
const mockQueryBus = { const mockQueryBus = {
execute: jest.fn().mockImplementation(() => [ execute: jest
{ .fn()
adId: 1, .mockImplementationOnce(() => [
}, {
{ adId: 1,
adId: 2, },
}, {
]), adId: 2,
},
])
.mockImplementationOnce(() => {
throw new Error();
}),
}; };
describe('Match Grpc Controller', () => { describe('Match Grpc Controller', () => {
@ -74,9 +81,23 @@ describe('Match Grpc Controller', () => {
}); });
it('should return matches', async () => { it('should return matches', async () => {
jest.spyOn(mockQueryBus, 'execute');
const matchPaginatedResponseDto = await matchGrpcController.match( const matchPaginatedResponseDto = await matchGrpcController.match(
punctualMatchRequestDto, punctualMatchRequestDto,
); );
expect(matchPaginatedResponseDto.data).toHaveLength(2); 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);
}); });
}); });