format response

This commit is contained in:
sbriat
2023-09-26 14:03:34 +02:00
parent 528ecfb3f9
commit d0285e265e
30 changed files with 903 additions and 79 deletions

View File

@@ -10,6 +10,7 @@ import { JourneyItem } from '@modules/ad/core/domain/value-objects/journey-item.
import { MatchRequestDto } from '@modules/ad/interface/grpc-controllers/dtos/match.request.dto';
import { WaypointDto } from '@modules/ad/interface/grpc-controllers/dtos/waypoint.dto';
import { MatchGrpcController } from '@modules/ad/interface/grpc-controllers/match.grpc-controller';
import { MatchMapper } from '@modules/ad/match.mapper';
import { QueryBus } from '@nestjs/cqrs';
import { RpcException } from '@nestjs/microservices';
import { Test, TestingModule } from '@nestjs/testing';
@@ -33,16 +34,16 @@ const destinationWaypoint: WaypointDto = {
country: 'France',
};
const punctualMatchRequestDto: MatchRequestDto = {
const recurrentMatchRequestDto: MatchRequestDto = {
driver: false,
passenger: true,
frequency: Frequency.PUNCTUAL,
frequency: Frequency.RECURRENT,
fromDate: '2023-08-15',
toDate: '2023-08-15',
toDate: '2024-09-30',
schedule: [
{
time: '07:00',
day: 2,
day: 5,
margin: 900,
},
],
@@ -58,8 +59,11 @@ const mockQueryBus = {
MatchEntity.create({
adId: '53a0bf71-4132-4f7b-a4cc-88c796b6bdf1',
role: Role.DRIVER,
frequency: Frequency.RECURRENT,
distance: 356041,
duration: 12647,
initialDistance: 349251,
initialDuration: 12103,
journeys: [
{
firstDate: new Date('2023-09-01'),
@@ -172,6 +176,116 @@ const mockRouteProvider: RouteProviderPort = {
getDetailed: jest.fn(),
};
const mockMatchMapper = {
toResponse: jest.fn().mockImplementation(() => ({
adId: '53a0bf71-4132-4f7b-a4cc-88c796b6bdf1',
role: 'DRIVER',
frequency: 'RECURRENT',
distance: 356041,
duration: 12647,
journeys: [
{
firstDate: '2023-09-01',
lastDate: '2024-08-30',
journeyItems: [
{
lat: 48.689445,
lon: 6.17651,
duration: 0,
distance: 0,
actorTimes: [
{
role: 'DRIVER',
target: 'START',
firstDatetime: '2023-09-01 07:00',
firstMinDatetime: '2023-09-01 06:45',
firstMaxDatetime: '2023-09-01 07:15',
lastDatetime: '2024-08-30 07:00',
lastMinDatetime: '2024-08-30 06:45',
lastMaxDatetime: '2024-08-30 07:15',
},
],
},
{
lat: 48.369445,
lon: 6.67487,
duration: 2100,
distance: 56878,
actorTimes: [
{
role: 'DRIVER',
target: 'NEUTRAL',
firstDatetime: '2023-09-01 07:35',
firstMinDatetime: '2023-09-01 07:20',
firstMaxDatetime: '2023-09-01 07:50',
lastDatetime: '2024-08-30 07:35',
lastMinDatetime: '2024-08-30 07:20',
lastMaxDatetime: '2024-08-30 07:50',
},
{
role: 'PASSENGER',
target: 'START',
firstDatetime: '2023-09-01 07:32',
firstMinDatetime: '2023-09-01 07:17',
firstMaxDatetime: '2023-09-01 07:47',
lastDatetime: '2024-08-30 07:32',
lastMinDatetime: '2024-08-30 07:17',
lastMaxDatetime: '2024-08-30 07:47',
},
],
},
{
lat: 47.98487,
lon: 6.9427,
duration: 3840,
distance: 76491,
actorTimes: [
{
role: 'DRIVER',
target: 'NEUTRAL',
firstDatetime: '2023-09-01 08:04',
firstMinDatetime: '2023-09-01 07:51',
firstMaxDatetime: '2023-09-01 08:19',
lastDatetime: '2024-08-30 08:04',
lastMinDatetime: '2024-08-30 07:51',
lastMaxDatetime: '2024-08-30 08:19',
},
{
role: 'PASSENGER',
target: 'FINISH',
firstDatetime: '2023-09-01 08:01',
firstMinDatetime: '2023-09-01 07:46',
firstMaxDatetime: '2023-09-01 08:16',
lastDatetime: '2024-08-30 08:01',
lastMinDatetime: '2024-08-30 07:46',
lastMaxDatetime: '2024-08-30 08:16',
},
],
},
{
lat: 47.365987,
lon: 7.02154,
duration: 4980,
distance: 96475,
actorTimes: [
{
role: 'DRIVER',
target: 'FINISH',
firstDatetime: '2023-09-01 08:23',
firstMinDatetime: '2023-09-01 08:08',
firstMaxDatetime: '2023-09-01 08:38',
lastDatetime: '2024-08-30 08:23',
lastMinDatetime: '2024-08-30 08:08',
lastMaxDatetime: '2024-08-30 08:38',
},
],
},
],
},
],
})),
};
describe('Match Grpc Controller', () => {
let matchGrpcController: MatchGrpcController;
@@ -187,6 +301,10 @@ describe('Match Grpc Controller', () => {
provide: AD_ROUTE_PROVIDER,
useValue: mockRouteProvider,
},
{
provide: MatchMapper,
useValue: mockMatchMapper,
},
],
}).compile();
@@ -204,7 +322,7 @@ describe('Match Grpc Controller', () => {
it('should return matches', async () => {
jest.spyOn(mockQueryBus, 'execute');
const matchPaginatedResponseDto = await matchGrpcController.match(
punctualMatchRequestDto,
recurrentMatchRequestDto,
);
expect(matchPaginatedResponseDto.data).toHaveLength(1);
expect(mockQueryBus.execute).toHaveBeenCalledTimes(1);
@@ -214,7 +332,7 @@ describe('Match Grpc Controller', () => {
jest.spyOn(mockQueryBus, 'execute');
expect.assertions(3);
try {
await matchGrpcController.match(punctualMatchRequestDto);
await matchGrpcController.match(recurrentMatchRequestDto);
} catch (e: any) {
expect(e).toBeInstanceOf(RpcException);
expect(e.error.code).toBe(RpcExceptionCode.UNKNOWN);