From d22de7c448f119668cd994a75c30a288a939f6bb Mon Sep 17 00:00:00 2001 From: sbriat Date: Fri, 23 Jun 2023 16:34:32 +0200 Subject: [PATCH] improve test coverage --- README.md | 4 +- .../ad/tests/unit/core/ad.entity.spec.ts | 8 +- .../unit/core/address.value-object.spec.ts | 24 +++++ .../core/coordinates.value-object.spec.ts | 12 +++ .../tests/unit/core/create-ad.service.spec.ts | 4 +- .../core/find-ad-by-id.query-handler.spec.ts | 4 +- .../margin-durations.value-object.spec.ts | 47 ++++++++++ .../unit/core/schedule.value-object.spec.ts | 22 +++++ .../unit/core/waypoint.value-object.spec.ts | 22 +++++ .../unit/infrastructure/ad.repository.spec.ts | 88 +++++++++++++++++++ .../infrastructure/time-converter.spec.ts | 11 +++ ...s-valid-position-indexes.validator.spec.ts | 4 +- 12 files changed, 238 insertions(+), 12 deletions(-) create mode 100644 src/modules/ad/tests/unit/core/address.value-object.spec.ts create mode 100644 src/modules/ad/tests/unit/core/coordinates.value-object.spec.ts create mode 100644 src/modules/ad/tests/unit/core/margin-durations.value-object.spec.ts create mode 100644 src/modules/ad/tests/unit/core/schedule.value-object.spec.ts create mode 100644 src/modules/ad/tests/unit/core/waypoint.value-object.spec.ts create mode 100644 src/modules/ad/tests/unit/infrastructure/ad.repository.spec.ts diff --git a/README.md b/README.md index c683b97..5fe1dc6 100644 --- a/README.md +++ b/README.md @@ -112,8 +112,8 @@ The app exposes the following [gRPC](https://grpc.io/) services : "waypoints": [ { "position": 0, - "lon": 48.68944505415954, - "lat": 6.176510296462267, + "lon": 48.689445, + "lat": 6.17651, "houseNumber": "5", "street": "Avenue Foch", "locality": "Nancy", diff --git a/src/modules/ad/tests/unit/core/ad.entity.spec.ts b/src/modules/ad/tests/unit/core/ad.entity.spec.ts index 55d2be6..8d71ac6 100644 --- a/src/modules/ad/tests/unit/core/ad.entity.spec.ts +++ b/src/modules/ad/tests/unit/core/ad.entity.spec.ts @@ -16,8 +16,8 @@ const originWaypointProps: WaypointProps = { postalCode: '54000', country: 'France', coordinates: { - lon: 48.68944505415954, - lat: 6.176510296462267, + lon: 48.689445, + lat: 6.17651, }, }, }; @@ -364,8 +364,8 @@ describe('Ad entity create', () => { postalCode: '54000', country: 'France', coordinates: { - lon: 48.68944505415954, - lat: 6.176510296462267, + lon: 48.689445, + lat: 6.17651, }, }, }, diff --git a/src/modules/ad/tests/unit/core/address.value-object.spec.ts b/src/modules/ad/tests/unit/core/address.value-object.spec.ts new file mode 100644 index 0000000..5475680 --- /dev/null +++ b/src/modules/ad/tests/unit/core/address.value-object.spec.ts @@ -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(); + }); +}); diff --git a/src/modules/ad/tests/unit/core/coordinates.value-object.spec.ts b/src/modules/ad/tests/unit/core/coordinates.value-object.spec.ts new file mode 100644 index 0000000..4718ac5 --- /dev/null +++ b/src/modules/ad/tests/unit/core/coordinates.value-object.spec.ts @@ -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); + }); +}); diff --git a/src/modules/ad/tests/unit/core/create-ad.service.spec.ts b/src/modules/ad/tests/unit/core/create-ad.service.spec.ts index 2080706..e90dea6 100644 --- a/src/modules/ad/tests/unit/core/create-ad.service.spec.ts +++ b/src/modules/ad/tests/unit/core/create-ad.service.spec.ts @@ -14,8 +14,8 @@ import { ConflictException } from '@libs/exceptions'; const originWaypoint: WaypointDto = { position: 0, - lon: 48.68944505415954, - lat: 6.176510296462267, + lon: 48.689445, + lat: 6.17651, houseNumber: '5', street: 'Avenue Foch', locality: 'Nancy', diff --git a/src/modules/ad/tests/unit/core/find-ad-by-id.query-handler.spec.ts b/src/modules/ad/tests/unit/core/find-ad-by-id.query-handler.spec.ts index 37f294c..96bcabf 100644 --- a/src/modules/ad/tests/unit/core/find-ad-by-id.query-handler.spec.ts +++ b/src/modules/ad/tests/unit/core/find-ad-by-id.query-handler.spec.ts @@ -20,8 +20,8 @@ const originWaypointProps: WaypointProps = { postalCode: '54000', country: 'France', coordinates: { - lon: 48.68944505415954, - lat: 6.176510296462267, + lon: 48.689445, + lat: 6.17651, }, }, }; diff --git a/src/modules/ad/tests/unit/core/margin-durations.value-object.spec.ts b/src/modules/ad/tests/unit/core/margin-durations.value-object.spec.ts new file mode 100644 index 0000000..c7e99d4 --- /dev/null +++ b/src/modules/ad/tests/unit/core/margin-durations.value-object.spec.ts @@ -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); + }); +}); diff --git a/src/modules/ad/tests/unit/core/schedule.value-object.spec.ts b/src/modules/ad/tests/unit/core/schedule.value-object.spec.ts new file mode 100644 index 0000000..b4d14bd --- /dev/null +++ b/src/modules/ad/tests/unit/core/schedule.value-object.spec.ts @@ -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'); + }); +}); diff --git a/src/modules/ad/tests/unit/core/waypoint.value-object.spec.ts b/src/modules/ad/tests/unit/core/waypoint.value-object.spec.ts new file mode 100644 index 0000000..ed4b842 --- /dev/null +++ b/src/modules/ad/tests/unit/core/waypoint.value-object.spec.ts @@ -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'); + }); +}); diff --git a/src/modules/ad/tests/unit/infrastructure/ad.repository.spec.ts b/src/modules/ad/tests/unit/infrastructure/ad.repository.spec.ts new file mode 100644 index 0000000..88b9e48 --- /dev/null +++ b/src/modules/ad/tests/unit/infrastructure/ad.repository.spec.ts @@ -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); + adMapper = module.get(AdMapper); + eventEmitter = module.get(EventEmitter2); + }); + it('should be defined', () => { + expect( + new AdRepository(prismaService, adMapper, eventEmitter), + ).toBeDefined(); + }); +}); diff --git a/src/modules/ad/tests/unit/infrastructure/time-converter.spec.ts b/src/modules/ad/tests/unit/infrastructure/time-converter.spec.ts index 136e272..f5634e0 100644 --- a/src/modules/ad/tests/unit/infrastructure/time-converter.spec.ts +++ b/src/modules/ad/tests/unit/infrastructure/time-converter.spec.ts @@ -51,6 +51,17 @@ describe('Time Converter', () => { ); 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', () => { diff --git a/src/modules/ad/tests/unit/interface/has-valid-position-indexes.validator.spec.ts b/src/modules/ad/tests/unit/interface/has-valid-position-indexes.validator.spec.ts index ffc0272..c2a52b0 100644 --- a/src/modules/ad/tests/unit/interface/has-valid-position-indexes.validator.spec.ts +++ b/src/modules/ad/tests/unit/interface/has-valid-position-indexes.validator.spec.ts @@ -3,8 +3,8 @@ import { WaypointDto } from '@modules/ad/interface/grpc-controllers/dtos/waypoin describe('addresses position validator', () => { const mockAddress1: WaypointDto = { - lon: 48.68944505415954, - lat: 6.176510296462267, + lon: 48.689445, + lat: 6.17651, houseNumber: '5', street: 'Avenue Foch', locality: 'Nancy',