improve test coverage

This commit is contained in:
sbriat 2023-06-23 16:34:32 +02:00
parent b7bb656f10
commit d22de7c448
12 changed files with 238 additions and 12 deletions

View File

@ -112,8 +112,8 @@ The app exposes the following [gRPC](https://grpc.io/) services :
"waypoints": [ "waypoints": [
{ {
"position": 0, "position": 0,
"lon": 48.68944505415954, "lon": 48.689445,
"lat": 6.176510296462267, "lat": 6.17651,
"houseNumber": "5", "houseNumber": "5",
"street": "Avenue Foch", "street": "Avenue Foch",
"locality": "Nancy", "locality": "Nancy",

View File

@ -16,8 +16,8 @@ const originWaypointProps: WaypointProps = {
postalCode: '54000', postalCode: '54000',
country: 'France', country: 'France',
coordinates: { coordinates: {
lon: 48.68944505415954, lon: 48.689445,
lat: 6.176510296462267, lat: 6.17651,
}, },
}, },
}; };
@ -364,8 +364,8 @@ describe('Ad entity create', () => {
postalCode: '54000', postalCode: '54000',
country: 'France', country: 'France',
coordinates: { coordinates: {
lon: 48.68944505415954, lon: 48.689445,
lat: 6.176510296462267, lat: 6.17651,
}, },
}, },
}, },

View File

@ -0,0 +1,24 @@
import { Address } from '@modules/ad/core/value-objects/address.value-object';
describe('Address value object', () => {
it('should create an address value object', () => {
const addressVO = new Address({
houseNumber: '5',
street: 'rue de la monnaie',
locality: 'Nancy',
postalCode: '54000',
country: 'France',
coordinates: {
lon: 48.689445,
lat: 6.17651,
},
});
expect(addressVO.houseNumber).toBe('5');
expect(addressVO.street).toBe('rue de la monnaie');
expect(addressVO.locality).toBe('Nancy');
expect(addressVO.postalCode).toBe('54000');
expect(addressVO.country).toBe('France');
expect(addressVO.coordinates.lon).toBe(48.689445);
expect(addressVO.name).toBeUndefined();
});
});

View File

@ -0,0 +1,12 @@
import { Coordinates } from '@modules/ad/core/value-objects/coordinates.value-object';
describe('Coordinates value object', () => {
it('should create a coordinates value object', () => {
const coordinatesVO = new Coordinates({
lon: 48.689445,
lat: 6.17651,
});
expect(coordinatesVO.lon).toBe(48.689445);
expect(coordinatesVO.lat).toBe(6.17651);
});
});

View File

@ -14,8 +14,8 @@ import { ConflictException } from '@libs/exceptions';
const originWaypoint: WaypointDto = { const originWaypoint: WaypointDto = {
position: 0, position: 0,
lon: 48.68944505415954, lon: 48.689445,
lat: 6.176510296462267, lat: 6.17651,
houseNumber: '5', houseNumber: '5',
street: 'Avenue Foch', street: 'Avenue Foch',
locality: 'Nancy', locality: 'Nancy',

View File

@ -20,8 +20,8 @@ const originWaypointProps: WaypointProps = {
postalCode: '54000', postalCode: '54000',
country: 'France', country: 'France',
coordinates: { coordinates: {
lon: 48.68944505415954, lon: 48.689445,
lat: 6.176510296462267, lat: 6.17651,
}, },
}, },
}; };

View File

@ -0,0 +1,47 @@
import { MarginDurations } from '@modules/ad/core/value-objects/margin-durations.value-object';
describe('Margin durations value object', () => {
it('should create a margin durations value object', () => {
const marginDurationsVO = new MarginDurations({
mon: 600,
tue: 610,
wed: 620,
thu: 630,
fri: 640,
sat: 650,
sun: 660,
});
expect(marginDurationsVO.mon).toBe(600);
expect(marginDurationsVO.tue).toBe(610);
expect(marginDurationsVO.wed).toBe(620);
expect(marginDurationsVO.thu).toBe(630);
expect(marginDurationsVO.fri).toBe(640);
expect(marginDurationsVO.sat).toBe(650);
expect(marginDurationsVO.sun).toBe(660);
});
it('should update margin durations value object values', () => {
const marginDurationsVO = new MarginDurations({
mon: 600,
tue: 610,
wed: 620,
thu: 630,
fri: 640,
sat: 650,
sun: 660,
});
marginDurationsVO.mon = 700;
marginDurationsVO.tue = 710;
marginDurationsVO.wed = 720;
marginDurationsVO.thu = 730;
marginDurationsVO.fri = 740;
marginDurationsVO.sat = 750;
marginDurationsVO.sun = 760;
expect(marginDurationsVO.mon).toBe(700);
expect(marginDurationsVO.tue).toBe(710);
expect(marginDurationsVO.wed).toBe(720);
expect(marginDurationsVO.thu).toBe(730);
expect(marginDurationsVO.fri).toBe(740);
expect(marginDurationsVO.sat).toBe(750);
expect(marginDurationsVO.sun).toBe(760);
});
});

View File

@ -0,0 +1,22 @@
import { Schedule } from '@modules/ad/core/value-objects/schedule.value-object';
describe('Schedule value object', () => {
it('should create a schedule value object', () => {
const scheduleVO = new Schedule({
mon: '07:00',
tue: '07:05',
wed: '07:10',
thu: '07:15',
fri: '07:20',
sat: '07:25',
sun: '07:30',
});
expect(scheduleVO.mon).toBe('07:00');
expect(scheduleVO.tue).toBe('07:05');
expect(scheduleVO.wed).toBe('07:10');
expect(scheduleVO.thu).toBe('07:15');
expect(scheduleVO.fri).toBe('07:20');
expect(scheduleVO.sat).toBe('07:25');
expect(scheduleVO.sun).toBe('07:30');
});
});

View File

@ -0,0 +1,22 @@
import { Waypoint } from '@modules/ad/core/value-objects/waypoint.value-object';
describe('Waypoint value object', () => {
it('should create a waypoint value object', () => {
const waypointVO = new Waypoint({
position: 0,
address: {
houseNumber: '5',
street: 'rue de la monnaie',
locality: 'Nancy',
postalCode: '54000',
country: 'France',
coordinates: {
lon: 48.689445,
lat: 6.17651,
},
},
});
expect(waypointVO.position).toBe(0);
expect(waypointVO.address.country).toBe('France');
});
});

View File

@ -0,0 +1,88 @@
import { PrismaService } from '@libs/db/prisma.service';
import {
PARAMS_PROVIDER,
TIMEZONE_FINDER,
TIME_CONVERTER,
} from '@modules/ad/ad.di-tokens';
import { AdMapper } from '@modules/ad/ad.mapper';
import { DefaultParamsProviderPort } from '@modules/ad/core/ports/default-params-provider.port';
import { TimeConverterPort } from '@modules/ad/core/ports/time-converter.port';
import { TimezoneFinderPort } from '@modules/ad/core/ports/timezone-finder.port';
import { AdRepository } from '@modules/ad/infrastructure/ad.repository';
import { EventEmitter2, EventEmitterModule } from '@nestjs/event-emitter';
import { Test, TestingModule } from '@nestjs/testing';
const mockDefaultParamsProvider: DefaultParamsProviderPort = {
getParams: () => {
return {
MON_MARGIN: 900,
TUE_MARGIN: 900,
WED_MARGIN: 900,
THU_MARGIN: 900,
FRI_MARGIN: 900,
SAT_MARGIN: 900,
SUN_MARGIN: 900,
DRIVER: false,
SEATS_PROPOSED: 3,
PASSENGER: true,
SEATS_REQUESTED: 1,
STRICT: false,
DEFAULT_TIMEZONE: 'Europe/Paris',
};
},
};
const mockTimezoneFinder: TimezoneFinderPort = {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
timezones: jest.fn().mockImplementation((lon: number, lat: number) => {
if (lon < 60) return 'Europe/Paris';
return 'America/New_York';
}),
};
const mockTimeConverter: TimeConverterPort = {
localDateTimeToUtc: jest
.fn()
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.mockImplementation((datetime: Date, timezone: string, dst?: boolean) => {
return datetime;
}),
utcDatetimeToLocalTime: jest.fn(),
};
describe('Ad repository', () => {
let prismaService: PrismaService;
let adMapper: AdMapper;
let eventEmitter: EventEmitter2;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [EventEmitterModule.forRoot()],
providers: [
PrismaService,
AdMapper,
{
provide: PARAMS_PROVIDER,
useValue: mockDefaultParamsProvider,
},
{
provide: TIMEZONE_FINDER,
useValue: mockTimezoneFinder,
},
{
provide: TIME_CONVERTER,
useValue: mockTimeConverter,
},
],
}).compile();
prismaService = module.get<PrismaService>(PrismaService);
adMapper = module.get<AdMapper>(AdMapper);
eventEmitter = module.get<EventEmitter2>(EventEmitter2);
});
it('should be defined', () => {
expect(
new AdRepository(prismaService, adMapper, eventEmitter),
).toBeDefined();
});
});

View File

@ -51,6 +51,17 @@ describe('Time Converter', () => {
); );
expect(utcDatetime).toBeUndefined(); expect(utcDatetime).toBeUndefined();
}); });
it('should return undefined if date is undefined', () => {
const timeConverter: TimeConverter = new TimeConverter();
const parisDate = undefined;
const parisTime = '08:00';
const utcDatetime = timeConverter.localDateTimeToUtc(
parisDate,
parisTime,
'Europe/Paris',
);
expect(utcDatetime).toBeUndefined();
});
}); });
describe('utcDatetimeToLocalTime', () => { describe('utcDatetimeToLocalTime', () => {

View File

@ -3,8 +3,8 @@ import { WaypointDto } from '@modules/ad/interface/grpc-controllers/dtos/waypoin
describe('addresses position validator', () => { describe('addresses position validator', () => {
const mockAddress1: WaypointDto = { const mockAddress1: WaypointDto = {
lon: 48.68944505415954, lon: 48.689445,
lat: 6.176510296462267, lat: 6.17651,
houseNumber: '5', houseNumber: '5',
street: 'Avenue Foch', street: 'Avenue Foch',
locality: 'Nancy', locality: 'Nancy',