get candidates in ad repository

This commit is contained in:
sbriat
2023-09-04 10:15:02 +02:00
parent f4097e96eb
commit 717d047aa8
8 changed files with 227 additions and 92 deletions

View File

@@ -1,13 +1,18 @@
import {
AD_DIRECTION_ENCODER,
AD_MESSAGE_PUBLISHER,
AD_ROUTE_PROVIDER,
} from '@modules/ad/ad.di-tokens';
import { AdMapper } from '@modules/ad/ad.mapper';
import { RouteProviderPort } from '@modules/ad/core/application/ports/route-provider.port';
import { AdRepository } from '@modules/ad/infrastructure/ad.repository';
import { Frequency } from '@modules/ad/core/domain/ad.types';
import {
AdReadModel,
AdRepository,
} from '@modules/ad/infrastructure/ad.repository';
import { PrismaService } from '@modules/ad/infrastructure/prisma.service';
import { DirectionEncoderPort } from '@modules/geography/core/application/ports/direction-encoder.port';
import { EventEmitter2, EventEmitterModule } from '@nestjs/event-emitter';
import { EventEmitterModule } from '@nestjs/event-emitter';
import { Test, TestingModule } from '@nestjs/testing';
const mockMessagePublisher = {
@@ -23,17 +28,146 @@ const mockRouteProvider: RouteProviderPort = {
getBasic: jest.fn(),
};
const mockPrismaService = {
$queryRawUnsafe: jest
.fn()
.mockImplementationOnce(() => {
return [
{
uuid: 'cc260669-1c6d-441f-80a5-19cd59afb777',
driver: true,
passenger: true,
frequency: Frequency.PUNCTUAL,
fromDate: new Date('2023-06-21'),
toDate: new Date('2023-06-21'),
seatsProposed: 3,
seatsRequested: 1,
strict: false,
ddriverDistance: 350000,
driverDuration: 14400,
passengerDistance: 350000,
passengerDuration: 14400,
fwdAzimuth: 273,
backAzimuth: 93,
createdAt: new Date('2023-06-20T17:05:00Z'),
updatedAt: new Date('2023-06-20T17:05:00Z'),
waypoints: 'LINESTRING(6.1765102 48.689445,2.3522 48.8566)',
day: 3,
time: new Date('2023-06-21T07:05:00Z'),
margin: 900,
},
{
uuid: '84af18ff-8779-4cac-9651-1ed5ab0713c4',
driver: true,
passenger: false,
frequency: Frequency.PUNCTUAL,
fromDate: new Date('2023-06-21'),
toDate: new Date('2023-06-21'),
seatsProposed: 3,
seatsRequested: 1,
strict: false,
ddriverDistance: 349000,
driverDuration: 14300,
passengerDistance: 350000,
passengerDuration: 14400,
fwdAzimuth: 273,
backAzimuth: 93,
createdAt: new Date('2023-06-18T14:16:10Z'),
updatedAt: new Date('2023-06-18T14:16:10Z'),
waypoints: 'LINESTRING(6.1765109 48.689455,2.3598 48.8589)',
day: 3,
time: new Date('2023-06-21T07:14:00Z'),
margin: 900,
},
];
})
.mockImplementationOnce(() => {
return [
{
uuid: 'cc260669-1c6d-441f-80a5-19cd59afb777',
driver: true,
passenger: true,
frequency: Frequency.RECURRENT,
fromDate: new Date('2023-06-21'),
toDate: new Date('2024-06-21'),
seatsProposed: 3,
seatsRequested: 1,
strict: false,
ddriverDistance: 350000,
driverDuration: 14400,
passengerDistance: 350000,
passengerDuration: 14400,
fwdAzimuth: 273,
backAzimuth: 93,
createdAt: new Date('2023-06-20T17:05:00Z'),
updatedAt: new Date('2023-06-20T17:05:00Z'),
waypoints: 'LINESTRING(6.1765102 48.689445,2.3522 48.8566)',
day: 3,
time: new Date('2023-06-21T07:05:00Z'),
margin: 900,
},
{
uuid: 'cc260669-1c6d-441f-80a5-19cd59afb777',
driver: true,
passenger: true,
frequency: Frequency.RECURRENT,
fromDate: new Date('2023-06-21'),
toDate: new Date('2024-06-21'),
seatsProposed: 3,
seatsRequested: 1,
strict: false,
ddriverDistance: 350000,
driverDuration: 14400,
passengerDistance: 350000,
passengerDuration: 14400,
fwdAzimuth: 273,
backAzimuth: 93,
createdAt: new Date('2023-06-20T17:05:00Z'),
updatedAt: new Date('2023-06-20T17:05:00Z'),
waypoints: 'LINESTRING(6.1765102 48.689445,2.3522 48.8566)',
day: 4,
time: new Date('2023-06-21T07:15:00Z'),
margin: 900,
},
{
uuid: 'cc260669-1c6d-441f-80a5-19cd59afb777',
driver: true,
passenger: true,
frequency: Frequency.RECURRENT,
fromDate: new Date('2023-06-21'),
toDate: new Date('2024-06-21'),
seatsProposed: 3,
seatsRequested: 1,
strict: false,
ddriverDistance: 350000,
driverDuration: 14400,
passengerDistance: 350000,
passengerDuration: 14400,
fwdAzimuth: 273,
backAzimuth: 93,
createdAt: new Date('2023-06-20T17:05:00Z'),
updatedAt: new Date('2023-06-20T17:05:00Z'),
waypoints: 'LINESTRING(6.1765102 48.689445,2.3522 48.8566)',
day: 5,
time: new Date('2023-06-21T07:16:00Z'),
margin: 900,
},
];
})
.mockImplementationOnce(() => {
return [];
}),
};
describe('Ad repository', () => {
let prismaService: PrismaService;
let adMapper: AdMapper;
let eventEmitter: EventEmitter2;
let adRepository: AdRepository;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [EventEmitterModule.forRoot()],
providers: [
PrismaService,
AdMapper,
AdRepository,
{
provide: AD_DIRECTION_ENCODER,
useValue: mockDirectionEncoder,
@@ -42,21 +176,42 @@ describe('Ad repository', () => {
provide: AD_ROUTE_PROVIDER,
useValue: mockRouteProvider,
},
{
provide: AD_MESSAGE_PUBLISHER,
useValue: mockMessagePublisher,
},
{
provide: PrismaService,
useValue: mockPrismaService,
},
],
}).compile();
prismaService = module.get<PrismaService>(PrismaService);
adMapper = module.get<AdMapper>(AdMapper);
eventEmitter = module.get<EventEmitter2>(EventEmitter2);
adRepository = module.get<AdRepository>(AdRepository);
});
it('should be defined', () => {
expect(
new AdRepository(
prismaService,
adMapper,
eventEmitter,
mockMessagePublisher,
),
).toBeDefined();
expect(adRepository).toBeDefined();
});
it('should get candidates if query returns punctual Ads', async () => {
const candidates: AdReadModel[] = await adRepository.getCandidates(
'somePunctualQueryString',
);
expect(candidates.length).toBe(2);
});
it('should get candidates if query returns recurrent Ads', async () => {
const candidates: AdReadModel[] = await adRepository.getCandidates(
'someRecurrentQueryString',
);
expect(candidates.length).toBe(1);
expect(candidates[0].schedule.length).toBe(3);
});
it('should return an empty array of candidates if query does not return Ads', async () => {
const candidates: AdReadModel[] = await adRepository.getCandidates(
'someQueryString',
);
expect(candidates.length).toBe(0);
});
});