route tests

This commit is contained in:
sbriat
2023-08-21 17:06:28 +02:00
parent e93d8b0c9d
commit dad964b39c
9 changed files with 290 additions and 21 deletions

View File

@@ -0,0 +1,256 @@
import { GeorouterPort } from '@modules/geography/core/application/ports/georouter.port';
import { Coordinates } from '@modules/geography/core/application/types/coordinates.type';
import { RouteEntity } from '@modules/geography/core/domain/route.entity';
import { RouteNotFoundException } from '@modules/geography/core/domain/route.errors';
import {
CreateRouteProps,
PathType,
Role,
} from '@modules/geography/core/domain/route.types';
const originCoordinates: Coordinates = {
lon: 48.689445,
lat: 6.17651,
};
const destinationCoordinates: Coordinates = {
lon: 48.8566,
lat: 2.3522,
};
const additionalCoordinates: Coordinates = {
lon: 48.7566,
lat: 4.4498,
};
const mockGeorouter: GeorouterPort = {
routes: jest
.fn()
.mockImplementationOnce(() => [
{
type: PathType.DRIVER,
distance: 350101,
duration: 14422,
fwdAzimuth: 273,
backAzimuth: 93,
distanceAzimuth: 336544,
points: [
{
lon: 6.1765102,
lat: 48.689445,
},
{
lon: 4.984578,
lat: 48.725687,
},
{
lon: 2.3522,
lat: 48.8566,
},
],
spacetimePoints: [],
},
])
.mockImplementationOnce(() => [
{
type: PathType.PASSENGER,
distance: 350102,
duration: 14423,
fwdAzimuth: 273,
backAzimuth: 93,
distanceAzimuth: 336545,
points: [
{
lon: 6.1765103,
lat: 48.689446,
},
{
lon: 4.984579,
lat: 48.725688,
},
{
lon: 2.3523,
lat: 48.8567,
},
],
spacetimePoints: [],
},
])
.mockImplementationOnce(() => [
{
type: PathType.GENERIC,
distance: 350100,
duration: 14421,
fwdAzimuth: 273,
backAzimuth: 93,
distanceAzimuth: 336543,
points: [
{
lon: 6.1765101,
lat: 48.689444,
},
{
lon: 4.984577,
lat: 48.725686,
},
{
lon: 2.3521,
lat: 48.8565,
},
],
spacetimePoints: [],
},
])
.mockImplementationOnce(() => [
{
type: PathType.GENERIC,
distance: 350108,
duration: 14428,
fwdAzimuth: 273,
backAzimuth: 93,
distanceAzimuth: 336548,
points: [
{
lon: 6.1765101,
lat: 48.689444,
},
{
lon: 4.984577,
lat: 48.725686,
},
{
lon: 2.3521,
lat: 48.8565,
},
],
spacetimePoints: [],
},
])
.mockImplementationOnce(() => []),
};
const createDriverRouteProps: CreateRouteProps = {
roles: [Role.DRIVER],
waypoints: [
{
position: 0,
...originCoordinates,
},
{
position: 1,
...destinationCoordinates,
},
],
georouter: mockGeorouter,
georouterSettings: {
points: true,
detailedDistance: false,
detailedDuration: false,
},
};
const createPassengerRouteProps: CreateRouteProps = {
roles: [Role.PASSENGER],
waypoints: [
{
position: 0,
...originCoordinates,
},
{
position: 1,
...destinationCoordinates,
},
],
georouter: mockGeorouter,
georouterSettings: {
points: true,
detailedDistance: false,
detailedDuration: false,
},
};
const createSimpleDriverAndPassengerRouteProps: CreateRouteProps = {
roles: [Role.DRIVER, Role.PASSENGER],
waypoints: [
{
position: 0,
...originCoordinates,
},
{
position: 1,
...destinationCoordinates,
},
],
georouter: mockGeorouter,
georouterSettings: {
points: true,
detailedDistance: false,
detailedDuration: false,
},
};
const createComplexDriverAndPassengerRouteProps: CreateRouteProps = {
roles: [Role.DRIVER, Role.PASSENGER],
waypoints: [
{
position: 0,
...originCoordinates,
},
{
position: 1,
...additionalCoordinates,
},
{
position: 2,
...destinationCoordinates,
},
],
georouter: mockGeorouter,
georouterSettings: {
points: true,
detailedDistance: false,
detailedDuration: false,
},
};
describe('Route entity create', () => {
it('should create a new entity for a driver only', async () => {
const route: RouteEntity = await RouteEntity.create(createDriverRouteProps);
expect(route.id.length).toBe(36);
expect(route.getProps().driverDuration).toBe(14422);
expect(route.getProps().passengerDistance).toBeUndefined();
});
it('should create a new entity for a passenger only', async () => {
const route: RouteEntity = await RouteEntity.create(
createPassengerRouteProps,
);
expect(route.id.length).toBe(36);
expect(route.getProps().passengerDuration).toBe(14423);
expect(route.getProps().driverDistance).toBeUndefined();
});
it('should create a new entity for a simple driver and passenger route', async () => {
const route: RouteEntity = await RouteEntity.create(
createSimpleDriverAndPassengerRouteProps,
);
expect(route.id.length).toBe(36);
expect(route.getProps().driverDuration).toBe(14421);
expect(route.getProps().driverDistance).toBe(350100);
expect(route.getProps().passengerDuration).toBe(14421);
expect(route.getProps().passengerDistance).toBe(350100);
});
it('should create a new entity for a complex driver and passenger route', async () => {
const route: RouteEntity = await RouteEntity.create(
createComplexDriverAndPassengerRouteProps,
);
expect(route.id.length).toBe(36);
expect(route.getProps().driverDuration).toBe(14428);
expect(route.getProps().driverDistance).toBe(350108);
expect(route.getProps().passengerDuration).toBe(14428);
expect(route.getProps().passengerDistance).toBe(350108);
});
it('should throw an exception if route is not found', async () => {
try {
await RouteEntity.create(createDriverRouteProps);
} catch (e: any) {
expect(e).toBeInstanceOf(RouteNotFoundException);
}
});
});