From f79f5bc2a02e885fc4b8dfed6e0e52a5ab2cf670 Mon Sep 17 00:00:00 2001 From: sbriat Date: Thu, 24 Aug 2023 15:09:12 +0200 Subject: [PATCH] add integration tests --check --- ci/.env.ci | 30 +-- .../tests/integration/ad.repository.spec.ts | 222 ++++++++++++++++++ 2 files changed, 234 insertions(+), 18 deletions(-) create mode 100644 src/modules/ad/tests/integration/ad.repository.spec.ts diff --git a/ci/.env.ci b/ci/.env.ci index fce2397..c216f49 100644 --- a/ci/.env.ci +++ b/ci/.env.ci @@ -2,12 +2,22 @@ SERVICE_URL=0.0.0.0 SERVICE_PORT=5005 +# PRISMA +DATABASE_URL="postgresql://mobicoop:mobicoop@v3-db:5432/mobicoop?schema=public" + +# MESSAGE BROKER +MESSAGE_BROKER_URI=amqp://v3-broker:5672 +MESSAGE_BROKER_EXCHANGE=mobicoop + +# REDIS +REDIS_HOST=v3-redis +REDIS_PASSWORD=redis +REDIS_PORT=6379 + # DEFAULT CONFIGURATION # default identifier used for match requests DEFAULT_UUID=00000000-0000-0000-0000-000000000000 -# default timezone -DEFAULT_TIMEZONE=Europe/Paris # default number of seats proposed as driver DEFAULT_SEATS=3 # algorithm type @@ -41,20 +51,4 @@ GEOROUTER_TYPE=graphhopper GEOROUTER_URL=http://localhost:8989 -# PRISMA -DATABASE_URL="postgresql://mobicoop:mobicoop@v3-db:5432/mobicoop?schema=public" -# MESSAGE BROKER -MESSAGE_BROKER_URI=amqp://v3-broker:5672 -MESSAGE_BROKER_EXCHANGE=mobicoop - -# REDIS -REDIS_IMAGE=redis:7.0-alpine -REDIS_HOST=v3-redis -REDIS_PASSWORD=redis - -# MESSAGE BROKER -BROKER_IMAGE=rabbitmq:3-alpine - -# POSTGRES -POSTGRES_IMAGE=postgis/postgis:15-3.3 diff --git a/src/modules/ad/tests/integration/ad.repository.spec.ts b/src/modules/ad/tests/integration/ad.repository.spec.ts new file mode 100644 index 0000000..c74557c --- /dev/null +++ b/src/modules/ad/tests/integration/ad.repository.spec.ts @@ -0,0 +1,222 @@ +import { + AD_DIRECTION_ENCODER, + AD_MESSAGE_PUBLISHER, + AD_REPOSITORY, +} from '@modules/ad/ad.di-tokens'; +import { AdMapper } from '@modules/ad/ad.mapper'; +import { AdEntity } from '@modules/ad/core/domain/ad.entity'; +import { CreateAdProps, Frequency } from '@modules/ad/core/domain/ad.types'; +import { AdRepository } from '@modules/ad/infrastructure/ad.repository'; +import { PrismaService } from '@modules/ad/infrastructure/prisma.service'; +import { PostgresDirectionEncoder } from '@modules/geography/infrastructure/postgres-direction-encoder'; +import { ConfigModule } from '@nestjs/config'; +import { EventEmitterModule } from '@nestjs/event-emitter'; +import { Test } from '@nestjs/testing'; + +describe('Ad Repository', () => { + let prismaService: PrismaService; + let adRepository: AdRepository; + + const mockMessagePublisher = { + publish: jest.fn().mockImplementation(), + }; + + const mockLogger = { + log: jest.fn(), + warn: jest.fn(), + error: jest.fn(), + }; + + beforeAll(async () => { + const module = await Test.createTestingModule({ + imports: [ + EventEmitterModule.forRoot(), + ConfigModule.forRoot({ isGlobal: true }), + ], + providers: [ + PrismaService, + AdMapper, + { + provide: AD_REPOSITORY, + useClass: AdRepository, + }, + { + provide: AD_MESSAGE_PUBLISHER, + useValue: mockMessagePublisher, + }, + { + provide: AD_DIRECTION_ENCODER, + useClass: PostgresDirectionEncoder, + }, + ], + }) + // disable logging + .setLogger(mockLogger) + .compile(); + + prismaService = module.get(PrismaService); + adRepository = module.get(AD_REPOSITORY); + }); + + afterAll(async () => { + await prismaService.$disconnect(); + }); + + beforeEach(async () => { + await prismaService.ad.deleteMany(); + }); + + describe('create', () => { + it('should create a punctual ad', async () => { + const beforeCount = await prismaService.ad.count(); + + const createAdProps: CreateAdProps = { + id: 'b4b56444-f8d3-4110-917c-e37bba77f383', + driver: true, + passenger: false, + frequency: Frequency.PUNCTUAL, + fromDate: '2023-02-01', + toDate: '2023-02-01', + schedule: [ + { + day: 3, + time: '12:05', + margin: 900, + }, + ], + seatsProposed: 3, + seatsRequested: 1, + strict: false, + waypoints: [ + { + position: 0, + lon: 43.7102, + lat: 7.262, + }, + { + position: 1, + lon: 43.2965, + lat: 5.3698, + }, + ], + points: [ + { + lon: 7.262, + lat: 43.7102, + }, + { + lon: 6.797838, + lat: 43.547031, + }, + { + lon: 6.18535, + lat: 43.407517, + }, + { + lon: 5.3698, + lat: 43.2965, + }, + ], + driverDuration: 7668, + driverDistance: 199000, + passengerDuration: 7668, + passengerDistance: 199000, + fwdAzimuth: 273, + backAzimuth: 93, + }; + + const adToCreate: AdEntity = AdEntity.create(createAdProps); + await adRepository.insertWithUnsupportedFields(adToCreate, 'ad'); + + const afterCount = await prismaService.ad.count(); + + expect(afterCount - beforeCount).toBe(1); + }); + + it('should create a recurrent ad', async () => { + const beforeCount = await prismaService.ad.count(); + + const createAdProps: CreateAdProps = { + id: 'b4b56444-f8d3-4110-917c-e37bba77f383', + driver: true, + passenger: false, + frequency: Frequency.RECURRENT, + fromDate: '2023-02-01', + toDate: '2024-01-31', + schedule: [ + { + day: 1, + time: '08:00', + margin: 900, + }, + { + day: 2, + time: '08:00', + margin: 900, + }, + { + day: 3, + time: '09:00', + margin: 900, + }, + { + day: 4, + time: '08:00', + margin: 900, + }, + { + day: 5, + time: '08:00', + margin: 900, + }, + ], + seatsProposed: 3, + seatsRequested: 1, + strict: false, + waypoints: [ + { + position: 0, + lon: 43.7102, + lat: 7.262, + }, + { + position: 1, + lon: 43.2965, + lat: 5.3698, + }, + ], + points: [ + { + lon: 7.262, + lat: 43.7102, + }, + { + lon: 6.797838, + lat: 43.547031, + }, + { + lon: 6.18535, + lat: 43.407517, + }, + { + lon: 5.3698, + lat: 43.2965, + }, + ], + driverDuration: 7668, + driverDistance: 199000, + passengerDuration: 7668, + passengerDistance: 199000, + fwdAzimuth: 273, + backAzimuth: 93, + }; + + const adToCreate: AdEntity = AdEntity.create(createAdProps); + await adRepository.insertWithUnsupportedFields(adToCreate, 'ad'); + + const afterCount = await prismaService.ad.count(); + + expect(afterCount - beforeCount).toBe(1); + }); + }); +});