adding integration test; fix frequency enum converting ; fix param provider int converting issue

This commit is contained in:
Grégoire Chevalier 2023-05-25 11:39:07 +02:00
parent d0949910f9
commit c0c486f34a
13 changed files with 72 additions and 71 deletions

View File

@ -1,6 +1,7 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { AdRepository } from '../../../database/domain/ad-repository'; import { AdRepository } from '../../../database/domain/ad-repository';
import { Ad } from '../../domain/entities/ad'; import { Ad } from '../../domain/entities/ad';
//TODO : properly implement mutate operation to prisma
@Injectable() @Injectable()
export class AdsRepository extends AdRepository<Ad> { export class AdsRepository extends AdRepository<Ad> {
protected _model = 'ad'; protected _model = 'ad';

View File

@ -8,17 +8,17 @@ export class DefaultParamsProvider implements IProvideParams {
constructor(private readonly configService: ConfigService) {} constructor(private readonly configService: ConfigService) {}
getParams = (): DefaultParams => { getParams = (): DefaultParams => {
return { return {
MON_MARGIN: this.configService.get('DEPARTURE_MARGIN'), MON_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')),
TUE_MARGIN: this.configService.get('DEPARTURE_MARGIN'), TUE_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')),
WED_MARGIN: this.configService.get('DEPARTURE_MARGIN'), WED_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')),
THU_MARGIN: this.configService.get('DEPARTURE_MARGIN'), THU_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')),
FRI_MARGIN: this.configService.get('DEPARTURE_MARGIN'), FRI_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')),
SAT_MARGIN: this.configService.get('DEPARTURE_MARGIN'), SAT_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')),
SUN_MARGIN: this.configService.get('DEPARTURE_MARGIN'), SUN_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')),
DRIVER: this.configService.get('ROLE') == 'driver' ? true : false, DRIVER: this.configService.get('ROLE') == 'driver' ? true : false,
SEATS_PROVIDED: this.configService.get('SEATS_PROVIDED'), SEATS_PROVIDED: parseInt(this.configService.get('SEATS_PROVIDED')),
PASSENGER: this.configService.get('ROLE') == 'passenger' ? true : false, PASSENGER: this.configService.get('ROLE') == 'passenger' ? true : false,
SEATS_REQUESTED: this.configService.get('SEATS_REQUESTED'), SEATS_REQUESTED: parseInt(this.configService.get('SEATS_REQUESTED')),
STRICT: false, STRICT: false,
}; };
}; };

View File

@ -10,8 +10,13 @@ import {
IsUUID, IsUUID,
} from 'class-validator'; } from 'class-validator';
import { Frequency } from '../types/frequency.enum'; import { Frequency } from '../types/frequency.enum';
import { AddressCreation } from './address.creation'; import { Address } from '../entities/address';
export class AdCreation { export class AdCreation {
@IsUUID(4)
@AutoMap()
uuid: string;
@IsUUID(4) @IsUUID(4)
@AutoMap() @AutoMap()
userUuid: string; userUuid: string;
@ -121,5 +126,5 @@ export class AdCreation {
@ValidateNested({ each: true }) @ValidateNested({ each: true })
@AutoMap() @AutoMap()
addresses: { create: AddressCreation[] }; addresses: { create: Address[] };
} }

View File

@ -1,32 +0,0 @@
import { AutoMap } from '@automapper/classes';
import { IsInt } from 'class-validator';
export class AddressCreation {
@IsInt()
@AutoMap()
position: number;
@AutoMap()
lon: number;
@AutoMap()
lat: number;
@AutoMap()
name?: string;
@AutoMap()
houseNumber?: string;
@AutoMap()
street?: string;
@AutoMap()
locality: string;
@AutoMap()
postalCode: string;
@AutoMap()
country: string;
}

View File

@ -1,4 +1,6 @@
import { Frequency } from '../../types/frequency.enum'; import { Frequency } from '../../types/frequency.enum';
export const mappingKeyToFrequency = (index: number): string => { export const mappingKeyToFrequency = (index: number): Frequency => {
return Frequency[index - 1]; if (index == 1) return Frequency.PUNCTUAL;
if (index == 2) return Frequency.RECURRENT;
return undefined;
}; };

View File

@ -1,4 +1,4 @@
export enum Frequency { export enum Frequency {
PUNCTUAL, PUNCTUAL = 'PUNCTUAL',
RECURRENT, RECURRENT = 'RECURRENT',
} }

View File

@ -27,6 +27,7 @@ export class CreateAdUseCase {
} }
async execute(command: CreateAdCommand): Promise<Ad> { async execute(command: CreateAdCommand): Promise<Ad> {
console.log('usecase');
this.ad = this._mapper.map( this.ad = this._mapper.map(
command.createAdRequest, command.createAdRequest,
CreateAdRequest, CreateAdRequest,
@ -38,6 +39,7 @@ export class CreateAdUseCase {
this.setDefaultDistanceMargin(); this.setDefaultDistanceMargin();
try { try {
console.log(this.ad);
const adCreated: Ad = await this._repository.create(this.ad); const adCreated: Ad = await this._repository.create(this.ad);
this._messager.publish('ad.create', JSON.stringify(adCreated)); this._messager.publish('ad.create', JSON.stringify(adCreated));
this._messager.publish( this._messager.publish(

View File

@ -1,17 +0,0 @@
import { Mapper, createMap } from '@automapper/core';
import { AutomapperProfile, InjectMapper } from '@automapper/nestjs';
import { Injectable } from '@nestjs/common';
import { AddressRequestDTO } from '../domain/dtos/create.address.request';
import { AddressCreation } from '../domain/dtos/address.creation';
@Injectable()
export class AdProfile extends AutomapperProfile {
constructor(@InjectMapper() mapper: Mapper) {
super(mapper);
}
override get profile() {
return (mapper) => {
createMap(mapper, AddressRequestDTO, AddressCreation);
};
}
}

View File

@ -3,6 +3,10 @@ import { PrismaService } from '../../../database/adapters/secondaries/prisma-ser
import { AdsRepository } from '../../adapters/secondaries/ads.repository'; import { AdsRepository } from '../../adapters/secondaries/ads.repository';
import { DatabaseModule } from '../../../database/database.module'; import { DatabaseModule } from '../../../database/database.module';
import { Frequency } from '../../domain/types/frequency.enum';
import { AdCreation } from '../../domain/dtos/ad.creation';
import { Address } from '../../domain/entities/address';
describe('Ad Repository', () => { describe('Ad Repository', () => {
let prismaService: PrismaService; let prismaService: PrismaService;
let adsRepository: AdsRepository; let adsRepository: AdsRepository;
@ -383,6 +387,7 @@ describe('Ad Repository', () => {
it('should return an ad', async () => { it('should return an ad', async () => {
await createPunctualDriverAds(1); await createPunctualDriverAds(1);
const ad = await adsRepository.findOneByUuid(baseUuid.uuid); const ad = await adsRepository.findOneByUuid(baseUuid.uuid);
expect(ad.uuid).toBe(baseUuid.uuid); expect(ad.uuid).toBe(baseUuid.uuid);
}); });
@ -396,4 +401,38 @@ describe('Ad Repository', () => {
describe('Create Ad', () => { describe('Create Ad', () => {
it('should Create an ad ', async () => {}); it('should Create an ad ', async () => {});
}); });
describe('create', () => {
it('should create an ad', async () => {
const beforeCount = await prismaService.ad.count();
const adToCreate: AdCreation = new AdCreation();
adToCreate.uuid = 'be459a29-7a41-4c0b-b371-abe90bfb6f00';
adToCreate.userUuid = '4e52b54d-a729-4dbd-9283-f84a11bb2200';
adToCreate.driver = true;
adToCreate.passenger = false;
adToCreate.frequency = Frequency.PUNCTUAL;
adToCreate.fromDate = new Date('05-22-2023 09:36');
adToCreate.toDate = new Date('05-22-2023 09:36');
adToCreate.monTime = '09:36';
adToCreate.monMargin = 900;
adToCreate.tueMargin = 900;
adToCreate.wedMargin = 900;
adToCreate.thuMargin = 900;
adToCreate.friMargin = 900;
adToCreate.satMargin = 900;
adToCreate.sunMargin = 900;
adToCreate.seatsDriver = 3;
adToCreate.seatsPassenger = 0;
adToCreate.strict = false;
adToCreate.addresses = {
create: [address0 as Address, address1 as Address],
};
const ad = await adsRepository.create(adToCreate);
const afterCount = await prismaService.ad.count();
expect(afterCount - beforeCount).toBe(1);
expect(ad.uuid).toBe('be459a29-7a41-4c0b-b371-abe90bfb6f00');
});
});
}); });

View File

@ -6,7 +6,7 @@ import { DefaultParams } from '../../../../domain/types/default-params.type';
const mockConfigService = { const mockConfigService = {
get: jest.fn().mockImplementation(() => 'some_default_value'), get: jest.fn().mockImplementation(() => 'some_default_value'),
}; };
//TODO complete coverage
describe('DefaultParamsProvider', () => { describe('DefaultParamsProvider', () => {
let defaultParamsProvider: DefaultParamsProvider; let defaultParamsProvider: DefaultParamsProvider;
@ -33,7 +33,7 @@ describe('DefaultParamsProvider', () => {
it('should provide default params', async () => { it('should provide default params', async () => {
const params: DefaultParams = defaultParamsProvider.getParams(); const params: DefaultParams = defaultParamsProvider.getParams();
expect(params.SUN_MARGIN).toBe('some_default_value'); expect(params.SUN_MARGIN).toBeNaN();
expect(params.PASSENGER).toBe(false); expect(params.PASSENGER).toBe(false);
expect(params.DRIVER).toBe(false); expect(params.DRIVER).toBe(false);
}); });

View File

@ -11,7 +11,7 @@ import { Ad } from '../../../domain/entities/ad';
import { AdProfile } from '../../../mappers/ad.profile'; import { AdProfile } from '../../../mappers/ad.profile';
import { AddressRequestDTO } from '../../../domain/dtos/create.address.request'; import { AddressRequestDTO } from '../../../domain/dtos/create.address.request';
import { AdCreation } from '../../../domain/dtos/ad.creation'; import { AdCreation } from '../../../domain/dtos/ad.creation';
import { AddressCreation } from '../../../domain/dtos/address.creation'; import { Address } from 'src/modules/ad/domain/entities/address';
const mockAddress1: AddressRequestDTO = { const mockAddress1: AddressRequestDTO = {
position: 0, position: 0,
@ -192,7 +192,7 @@ describe('CreateAdUseCase', () => {
seatsPassenger: mockDefaultParamsProvider.getParams().SEATS_REQUESTED, seatsPassenger: mockDefaultParamsProvider.getParams().SEATS_REQUESTED,
strict: mockDefaultParamsProvider.getParams().STRICT, strict: mockDefaultParamsProvider.getParams().STRICT,
addresses: { addresses: {
create: minimalReccurentAdREquest.addresses as AddressCreation[], create: minimalReccurentAdREquest.addresses as Address[],
}, },
createdAt: undefined, createdAt: undefined,
} as AdCreation; } as AdCreation;
@ -246,7 +246,7 @@ describe('CreateAdUseCase', () => {
seatsPassenger: newPunctualPassengerAdRequest.seatsPassenger, seatsPassenger: newPunctualPassengerAdRequest.seatsPassenger,
strict: mockDefaultParamsProvider.getParams().STRICT, strict: mockDefaultParamsProvider.getParams().STRICT,
addresses: { addresses: {
create: newPunctualPassengerAdRequest.addresses as AddressCreation[], create: newPunctualPassengerAdRequest.addresses as Address[],
}, },
createdAt: undefined, createdAt: undefined,
} as AdCreation; } as AdCreation;

View File

@ -3,10 +3,10 @@ import { Frequency } from '../../../domain/types/frequency.enum';
describe('frequency mapping function ', () => { describe('frequency mapping function ', () => {
it('should return punctual', () => { it('should return punctual', () => {
expect(mappingKeyToFrequency(1)).toBe(Frequency[0]); expect(mappingKeyToFrequency(1)).toBe(Frequency.PUNCTUAL);
}); });
it('should return recurent', () => { it('should return recurent', () => {
expect(mappingKeyToFrequency(2)).toBe(Frequency[1]); expect(mappingKeyToFrequency(2)).toBe(Frequency.RECURRENT);
}); });
it('should return undefined', () => { it('should return undefined', () => {
expect(mappingKeyToFrequency(0)).toBeUndefined(); expect(mappingKeyToFrequency(0)).toBeUndefined();

View File

@ -87,6 +87,7 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
}); });
return res; return res;
} catch (e) { } catch (e) {
console.log(e);
if (e instanceof Prisma.PrismaClientKnownRequestError) { if (e instanceof Prisma.PrismaClientKnownRequestError) {
throw new DatabaseException( throw new DatabaseException(
Prisma.PrismaClientKnownRequestError.name, Prisma.PrismaClientKnownRequestError.name,