create ad usecase tests

This commit is contained in:
sbriat 2023-05-22 16:38:34 +02:00
parent 17acaa449c
commit 72516037d1
2 changed files with 132 additions and 62 deletions

View File

@ -56,6 +56,36 @@ export class CreateAdUseCase {
withPoints: true, withPoints: true,
withTime: false, withTime: false,
}); });
this.setAdGeography(command);
this.setAdSchedule(command);
return await this.adRepository.createAd(this.ad);
} catch (error) {
throw error;
}
}
private setTimezone = (coordinates: Coordinate[]): void => {
this.timezone = this.defaultParams.DEFAULT_TIMEZONE;
try {
const timezones = this.timezoneFinder.timezones(
coordinates[0].lat,
coordinates[0].lon,
);
if (timezones.length > 0) this.timezone = timezones[0];
} catch (e) {}
};
private setRoles = (createAdRequest: CreateAdRequest): void => {
this.roles = [];
if (createAdRequest.driver) this.roles.push(Role.DRIVER);
if (createAdRequest.passenger) this.roles.push(Role.PASSENGER);
};
private setGeography = (coordinates: Coordinate[]): void => {
this.geography = new Geography(coordinates);
};
private setAdGeography = (command: CreateAdCommand): void => {
this.ad.driverDistance = this.geography.driverRoute?.distance; this.ad.driverDistance = this.geography.driverRoute?.distance;
this.ad.driverDuration = this.geography.driverRoute?.duration; this.ad.driverDuration = this.geography.driverRoute?.duration;
this.ad.passengerDistance = this.geography.passengerRoute?.distance; this.ad.passengerDistance = this.geography.passengerRoute?.distance;
@ -72,6 +102,9 @@ export class CreateAdUseCase {
this.ad.direction = this.geography.driverRoute this.ad.direction = this.geography.driverRoute
? this.directionEncoder.encode(this.geography.driverRoute.points) ? this.directionEncoder.encode(this.geography.driverRoute.points)
: undefined; : undefined;
};
private setAdSchedule = (command: CreateAdCommand): void => {
this.ad.monTime = TimeConverter.toUtcDatetime( this.ad.monTime = TimeConverter.toUtcDatetime(
this.ad.fromDate, this.ad.fromDate,
command.createAdRequest.monTime, command.createAdRequest.monTime,
@ -107,30 +140,5 @@ export class CreateAdUseCase {
command.createAdRequest.sunTime, command.createAdRequest.sunTime,
this.timezone, this.timezone,
); );
return await this.adRepository.createAd(this.ad);
} catch (error) {
throw error;
}
}
private setTimezone = (coordinates: Coordinate[]): void => {
this.timezone = this.defaultParams.DEFAULT_TIMEZONE;
try {
const timezones = this.timezoneFinder.timezones(
coordinates[0].lat,
coordinates[0].lon,
);
if (timezones.length > 0) this.timezone = timezones[0];
} catch (e) {}
};
private setRoles = (createAdRequest: CreateAdRequest): void => {
this.roles = [];
if (createAdRequest.driver) this.roles.push(Role.DRIVER);
if (createAdRequest.passenger) this.roles.push(Role.PASSENGER);
};
private setGeography = (coordinates: Coordinate[]): void => {
this.geography = new Geography(coordinates);
}; };
} }

View File

@ -9,14 +9,48 @@ import { Ad } from '../../../domain/entities/ad';
import { AdProfile } from '../../../mappers/ad.profile'; import { AdProfile } from '../../../mappers/ad.profile';
import { Frequency } from '../../../domain/types/frequency.enum'; import { Frequency } from '../../../domain/types/frequency.enum';
import { RouteKey } from '../../../domain/entities/geography'; import { RouteKey } from '../../../domain/entities/geography';
import { DatabaseException } from '../../../../database/exceptions/database.exception';
import { Route } from '../../../../geography/domain/entities/route';
const mockAdRepository = {}; const mockAdRepository = {
createAd: jest.fn().mockImplementation((ad) => {
if (ad.uuid == '00000000-0000-0000-0000-000000000000')
throw new DatabaseException();
return new Ad();
}),
};
const mockGeorouterCreator = { const mockGeorouterCreator = {
create: jest.fn().mockImplementation(() => ({ create: jest.fn().mockImplementation(() => ({
route: jest.fn().mockImplementation(() => [ route: jest.fn().mockImplementation(() => [
{ {
key: RouteKey.COMMON, key: RouteKey.DRIVER,
route: <Route>{
points: [], points: [],
fwdAzimuth: 0,
backAzimuth: 180,
distance: 20000,
duration: 1800,
},
},
{
key: RouteKey.PASSENGER,
route: <Route>{
points: [],
fwdAzimuth: 0,
backAzimuth: 180,
distance: 20000,
duration: 1800,
},
},
{
key: RouteKey.COMMON,
route: <Route>{
points: [],
fwdAzimuth: 0,
backAzimuth: 180,
distance: 20000,
duration: 1800,
},
}, },
]), ]),
})), })),
@ -31,7 +65,9 @@ const mockParamsProvider = {
const mockTimezoneFinder = { const mockTimezoneFinder = {
timezones: jest.fn().mockImplementation(() => ['Europe/Paris']), timezones: jest.fn().mockImplementation(() => ['Europe/Paris']),
}; };
const mockDirectionEncoder = {}; const mockDirectionEncoder = {
encode: jest.fn(),
};
const createAdRequest: CreateAdRequest = { const createAdRequest: CreateAdRequest = {
uuid: '77c55dfc-c28b-4026-942e-f94e95401fb1', uuid: '77c55dfc-c28b-4026-942e-f94e95401fb1',
@ -64,6 +100,18 @@ const createAdRequest: CreateAdRequest = {
], ],
}; };
const setUuid = async (uuid: string): Promise<void> => {
createAdRequest.uuid = uuid;
};
const setIsDriver = async (isDriver: boolean): Promise<void> => {
createAdRequest.driver = isDriver;
};
const setIsPassenger = async (isPassenger: boolean): Promise<void> => {
createAdRequest.passenger = isPassenger;
};
describe('CreateAdUseCase', () => { describe('CreateAdUseCase', () => {
let createAdUseCase: CreateAdUseCase; let createAdUseCase: CreateAdUseCase;
@ -103,12 +151,26 @@ describe('CreateAdUseCase', () => {
expect(createAdUseCase).toBeDefined(); expect(createAdUseCase).toBeDefined();
}); });
// describe('execute', () => { describe('execute', () => {
// it('should create an ad', async () => { it('should create an ad as driver', async () => {
// const ad = await createAdUseCase.execute( const ad = await createAdUseCase.execute(
// new CreateAdCommand(createAdRequest), new CreateAdCommand(createAdRequest),
// ); );
// expect(ad).toBeInstanceOf(Ad); expect(ad).toBeInstanceOf(Ad);
// }); });
// }); it('should create an ad as passenger', async () => {
await setIsDriver(false);
await setIsPassenger(true);
const ad = await createAdUseCase.execute(
new CreateAdCommand(createAdRequest),
);
expect(ad).toBeInstanceOf(Ad);
});
it('should throw an exception if repository fails', async () => {
await setUuid('00000000-0000-0000-0000-000000000000');
await expect(
createAdUseCase.execute(new CreateAdCommand(createAdRequest)),
).rejects.toBeInstanceOf(DatabaseException);
});
});
}); });