2023-05-23 12:00:39 +00:00
|
|
|
import { TestingModule, Test } from '@nestjs/testing';
|
|
|
|
import { DatabaseModule } from '../../../database/database.module';
|
2023-05-25 08:20:00 +00:00
|
|
|
import { PrismaService } from '../../../database/adapters/secondaries/prisma.service';
|
2023-05-23 12:00:39 +00:00
|
|
|
import { AdRepository } from '../../adapters/secondaries/ad.repository';
|
|
|
|
import { Ad } from '../../domain/entities/ad';
|
|
|
|
import { Frequency } from '../../domain/types/frequency.enum';
|
|
|
|
|
|
|
|
describe('AdRepository', () => {
|
|
|
|
let prismaService: PrismaService;
|
|
|
|
let adRepository: AdRepository;
|
|
|
|
|
|
|
|
const baseUuid = {
|
|
|
|
uuid: 'be459a29-7a41-4c0b-b371-abe90bfb6f00',
|
|
|
|
};
|
|
|
|
const baseUserUuid = {
|
|
|
|
userUuid: '4e52b54d-a729-4dbd-9283-f84a11bb2200',
|
|
|
|
};
|
|
|
|
const driverAd = {
|
|
|
|
driver: 'true',
|
|
|
|
passenger: 'false',
|
|
|
|
fwdAzimuth: 0,
|
|
|
|
backAzimuth: 180,
|
|
|
|
waypoints: "'LINESTRING(6 47,6.1 47.1,6.2 47.2)'",
|
|
|
|
direction: "'LINESTRING(6 47,6.05 47.05,6.1 47.1,6.15 47.15,6.2 47.2)'",
|
|
|
|
seatsDriver: 3,
|
|
|
|
seatsPassenger: 1,
|
|
|
|
seatsUsed: 0,
|
|
|
|
strict: 'false',
|
|
|
|
};
|
|
|
|
const passengerAd = {
|
|
|
|
driver: 'false',
|
|
|
|
passenger: 'true',
|
|
|
|
fwdAzimuth: 0,
|
|
|
|
backAzimuth: 180,
|
|
|
|
waypoints: "'LINESTRING(6 47,6.2 47.2)'",
|
|
|
|
direction: "'LINESTRING(6 47,6.05 47.05,6.15 47.15,6.2 47.2)'",
|
|
|
|
seatsDriver: 3,
|
|
|
|
seatsPassenger: 1,
|
|
|
|
seatsUsed: 0,
|
|
|
|
strict: 'false',
|
|
|
|
};
|
|
|
|
const driverAndPassengerAd = {
|
|
|
|
driver: 'true',
|
|
|
|
passenger: 'true',
|
|
|
|
fwdAzimuth: 0,
|
|
|
|
backAzimuth: 180,
|
|
|
|
waypoints: "'LINESTRING(6 47,6.1 47.1,6.2 47.2)'",
|
|
|
|
direction: "'LINESTRING(6 47,6.05 47.05,6.1 47.1,6.15 47.15,6.2 47.2)'",
|
|
|
|
seatsDriver: 3,
|
|
|
|
seatsPassenger: 1,
|
|
|
|
seatsUsed: 0,
|
|
|
|
strict: 'false',
|
|
|
|
};
|
|
|
|
const punctualAd = {
|
|
|
|
frequency: `'PUNCTUAL'`,
|
|
|
|
fromDate: `'2023-01-01'`,
|
|
|
|
toDate: `'2023-01-01'`,
|
|
|
|
monTime: 'NULL',
|
|
|
|
tueTime: 'NULL',
|
|
|
|
wedTime: 'NULL',
|
|
|
|
thuTime: 'NULL',
|
|
|
|
friTime: 'NULL',
|
|
|
|
satTime: 'NULL',
|
|
|
|
sunTime: `'2023-01-01T07:00Z'`,
|
|
|
|
monMargin: 900,
|
|
|
|
tueMargin: 900,
|
|
|
|
wedMargin: 900,
|
|
|
|
thuMargin: 900,
|
|
|
|
friMargin: 900,
|
|
|
|
satMargin: 900,
|
|
|
|
sunMargin: 900,
|
|
|
|
};
|
|
|
|
const recurrentAd = {
|
|
|
|
frequency: `'RECURRENT'`,
|
|
|
|
fromDate: `'2023-01-01'`,
|
|
|
|
toDate: `'2023-12-31'`,
|
|
|
|
monTime: `'2023-01-01T07:00Z'`,
|
|
|
|
tueTime: `'2023-01-01T07:00Z'`,
|
|
|
|
wedTime: `'2023-01-01T07:00Z'`,
|
|
|
|
thuTime: `'2023-01-01T07:00Z'`,
|
|
|
|
friTime: `'2023-01-01T07:00Z'`,
|
|
|
|
satTime: 'NULL',
|
|
|
|
sunTime: 'NULL',
|
|
|
|
monMargin: 900,
|
|
|
|
tueMargin: 900,
|
|
|
|
wedMargin: 900,
|
|
|
|
thuMargin: 900,
|
|
|
|
friMargin: 900,
|
|
|
|
satMargin: 900,
|
|
|
|
sunMargin: 900,
|
|
|
|
};
|
|
|
|
|
|
|
|
const createPunctualDriverAds = async (nbToCreate = 10) => {
|
|
|
|
const adToCreate = {
|
|
|
|
...baseUuid,
|
|
|
|
...baseUserUuid,
|
|
|
|
...driverAd,
|
|
|
|
...punctualAd,
|
|
|
|
};
|
|
|
|
for (let i = 0; i < nbToCreate; i++) {
|
|
|
|
adToCreate.uuid = `'${baseUuid.uuid.slice(0, -2)}${i
|
|
|
|
.toString(16)
|
|
|
|
.padStart(2, '0')}'`;
|
|
|
|
adToCreate.userUuid = `'${baseUserUuid.userUuid.slice(0, -2)}${i
|
|
|
|
.toString(16)
|
|
|
|
.padStart(2, '0')}'`;
|
|
|
|
await executeInsertCommand(adToCreate);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const createRecurrentDriverAds = async (nbToCreate = 10) => {
|
|
|
|
const adToCreate = {
|
|
|
|
...baseUuid,
|
|
|
|
...baseUserUuid,
|
|
|
|
...driverAd,
|
|
|
|
...recurrentAd,
|
|
|
|
};
|
|
|
|
for (let i = 0; i < nbToCreate; i++) {
|
|
|
|
adToCreate.uuid = `'${baseUuid.uuid.slice(0, -2)}${i
|
|
|
|
.toString(16)
|
|
|
|
.padStart(2, '0')}'`;
|
|
|
|
adToCreate.userUuid = `'${baseUserUuid.userUuid.slice(0, -2)}${i
|
|
|
|
.toString(16)
|
|
|
|
.padStart(2, '0')}'`;
|
|
|
|
await executeInsertCommand(adToCreate);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const createPunctualPassengerAds = async (nbToCreate = 10) => {
|
|
|
|
const adToCreate = {
|
|
|
|
...baseUuid,
|
|
|
|
...baseUserUuid,
|
|
|
|
...passengerAd,
|
|
|
|
...punctualAd,
|
|
|
|
};
|
|
|
|
for (let i = 0; i < nbToCreate; i++) {
|
|
|
|
adToCreate.uuid = `'${baseUuid.uuid.slice(0, -2)}${i
|
|
|
|
.toString(16)
|
|
|
|
.padStart(2, '0')}'`;
|
|
|
|
adToCreate.userUuid = `'${baseUserUuid.userUuid.slice(0, -2)}${i
|
|
|
|
.toString(16)
|
|
|
|
.padStart(2, '0')}'`;
|
|
|
|
await executeInsertCommand(adToCreate);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const createRecurrentPassengerAds = async (nbToCreate = 10) => {
|
|
|
|
const adToCreate = {
|
|
|
|
...baseUuid,
|
|
|
|
...baseUserUuid,
|
|
|
|
...passengerAd,
|
|
|
|
...recurrentAd,
|
|
|
|
};
|
|
|
|
for (let i = 0; i < nbToCreate; i++) {
|
|
|
|
adToCreate.uuid = `'${baseUuid.uuid.slice(0, -2)}${i
|
|
|
|
.toString(16)
|
|
|
|
.padStart(2, '0')}'`;
|
|
|
|
adToCreate.userUuid = `'${baseUserUuid.userUuid.slice(0, -2)}${i
|
|
|
|
.toString(16)
|
|
|
|
.padStart(2, '0')}'`;
|
|
|
|
await executeInsertCommand(adToCreate);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const createPunctualDriverPassengerAds = async (nbToCreate = 10) => {
|
|
|
|
const adToCreate = {
|
|
|
|
...baseUuid,
|
|
|
|
...baseUserUuid,
|
|
|
|
...driverAndPassengerAd,
|
|
|
|
...punctualAd,
|
|
|
|
};
|
|
|
|
for (let i = 0; i < nbToCreate; i++) {
|
|
|
|
adToCreate.uuid = `'${baseUuid.uuid.slice(0, -2)}${i
|
|
|
|
.toString(16)
|
|
|
|
.padStart(2, '0')}'`;
|
|
|
|
adToCreate.userUuid = `'${baseUserUuid.userUuid.slice(0, -2)}${i
|
|
|
|
.toString(16)
|
|
|
|
.padStart(2, '0')}'`;
|
|
|
|
await executeInsertCommand(adToCreate);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const createRecurrentDriverPassengerAds = async (nbToCreate = 10) => {
|
|
|
|
const adToCreate = {
|
|
|
|
...baseUuid,
|
|
|
|
...baseUserUuid,
|
|
|
|
...driverAndPassengerAd,
|
|
|
|
...recurrentAd,
|
|
|
|
};
|
|
|
|
for (let i = 0; i < nbToCreate; i++) {
|
|
|
|
adToCreate.uuid = `'${baseUuid.uuid.slice(0, -2)}${i
|
|
|
|
.toString(16)
|
|
|
|
.padStart(2, '0')}'`;
|
|
|
|
adToCreate.userUuid = `'${baseUserUuid.userUuid.slice(0, -2)}${i
|
|
|
|
.toString(16)
|
|
|
|
.padStart(2, '0')}'`;
|
|
|
|
await executeInsertCommand(adToCreate);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const executeInsertCommand = async (object: any) => {
|
|
|
|
const command = `INSERT INTO ad ("${Object.keys(object).join(
|
|
|
|
'","',
|
|
|
|
)}") VALUES (${Object.values(object).join(',')})`;
|
|
|
|
await prismaService.$executeRawUnsafe(command);
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
|
|
imports: [DatabaseModule],
|
|
|
|
providers: [AdRepository, PrismaService],
|
|
|
|
}).compile();
|
|
|
|
|
|
|
|
prismaService = module.get<PrismaService>(PrismaService);
|
|
|
|
adRepository = module.get<AdRepository>(AdRepository);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
await prismaService.$disconnect();
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
await prismaService.ad.deleteMany();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('findAll', () => {
|
|
|
|
it('should return an empty data array', async () => {
|
|
|
|
const res = await adRepository.findAll();
|
|
|
|
expect(res).toEqual({
|
|
|
|
data: [],
|
|
|
|
total: 0,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('drivers', () => {
|
|
|
|
it('should return a data array with 8 punctual driver ads', async () => {
|
|
|
|
await createPunctualDriverAds(8);
|
|
|
|
const ads = await adRepository.findAll();
|
|
|
|
expect(ads.data.length).toBe(8);
|
|
|
|
expect(ads.total).toBe(8);
|
|
|
|
expect(ads.data[0].driver).toBeTruthy();
|
|
|
|
expect(ads.data[0].passenger).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return a data array limited to 10 punctual driver ads', async () => {
|
|
|
|
await createPunctualDriverAds(20);
|
|
|
|
const ads = await adRepository.findAll();
|
|
|
|
expect(ads.data.length).toBe(10);
|
|
|
|
expect(ads.total).toBe(20);
|
|
|
|
expect(ads.data[1].driver).toBeTruthy();
|
|
|
|
expect(ads.data[1].passenger).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return a data array with 8 recurrent driver ads', async () => {
|
|
|
|
await createRecurrentDriverAds(8);
|
|
|
|
const ads = await adRepository.findAll();
|
|
|
|
expect(ads.data.length).toBe(8);
|
|
|
|
expect(ads.total).toBe(8);
|
|
|
|
expect(ads.data[2].driver).toBeTruthy();
|
|
|
|
expect(ads.data[2].passenger).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return a data array limited to 10 recurrent driver ads', async () => {
|
|
|
|
await createRecurrentDriverAds(20);
|
|
|
|
const ads = await adRepository.findAll();
|
|
|
|
expect(ads.data.length).toBe(10);
|
|
|
|
expect(ads.total).toBe(20);
|
|
|
|
expect(ads.data[3].driver).toBeTruthy();
|
|
|
|
expect(ads.data[3].passenger).toBeFalsy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('passengers', () => {
|
|
|
|
it('should return a data array with 7 punctual passenger ads', async () => {
|
|
|
|
await createPunctualPassengerAds(7);
|
|
|
|
const ads = await adRepository.findAll();
|
|
|
|
expect(ads.data.length).toBe(7);
|
|
|
|
expect(ads.total).toBe(7);
|
|
|
|
expect(ads.data[0].passenger).toBeTruthy();
|
|
|
|
expect(ads.data[0].driver).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return a data array limited to 10 punctual passenger ads', async () => {
|
|
|
|
await createPunctualPassengerAds(15);
|
|
|
|
const ads = await adRepository.findAll();
|
|
|
|
expect(ads.data.length).toBe(10);
|
|
|
|
expect(ads.total).toBe(15);
|
|
|
|
expect(ads.data[1].passenger).toBeTruthy();
|
|
|
|
expect(ads.data[1].driver).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return a data array with 7 recurrent passenger ads', async () => {
|
|
|
|
await createRecurrentPassengerAds(7);
|
|
|
|
const ads = await adRepository.findAll();
|
|
|
|
expect(ads.data.length).toBe(7);
|
|
|
|
expect(ads.total).toBe(7);
|
|
|
|
expect(ads.data[2].passenger).toBeTruthy();
|
|
|
|
expect(ads.data[2].driver).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return a data array limited to 10 recurrent passenger ads', async () => {
|
|
|
|
await createRecurrentPassengerAds(15);
|
|
|
|
const ads = await adRepository.findAll();
|
|
|
|
expect(ads.data.length).toBe(10);
|
|
|
|
expect(ads.total).toBe(15);
|
|
|
|
expect(ads.data[3].passenger).toBeTruthy();
|
|
|
|
expect(ads.data[3].driver).toBeFalsy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('drivers and passengers', () => {
|
|
|
|
it('should return a data array with 6 punctual driver and passenger ads', async () => {
|
|
|
|
await createPunctualDriverPassengerAds(6);
|
|
|
|
const ads = await adRepository.findAll();
|
|
|
|
expect(ads.data.length).toBe(6);
|
|
|
|
expect(ads.total).toBe(6);
|
|
|
|
expect(ads.data[0].passenger).toBeTruthy();
|
|
|
|
expect(ads.data[0].driver).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return a data array limited to 10 punctual driver and passenger ads', async () => {
|
|
|
|
await createPunctualDriverPassengerAds(16);
|
|
|
|
const ads = await adRepository.findAll();
|
|
|
|
expect(ads.data.length).toBe(10);
|
|
|
|
expect(ads.total).toBe(16);
|
|
|
|
expect(ads.data[1].passenger).toBeTruthy();
|
|
|
|
expect(ads.data[1].driver).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return a data array with 6 recurrent driver and passenger ads', async () => {
|
|
|
|
await createRecurrentDriverPassengerAds(6);
|
|
|
|
const ads = await adRepository.findAll();
|
|
|
|
expect(ads.data.length).toBe(6);
|
|
|
|
expect(ads.total).toBe(6);
|
|
|
|
expect(ads.data[2].passenger).toBeTruthy();
|
|
|
|
expect(ads.data[2].driver).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return a data array limited to 10 recurrent driver and passenger ads', async () => {
|
|
|
|
await createRecurrentDriverPassengerAds(16);
|
|
|
|
const ads = await adRepository.findAll();
|
|
|
|
expect(ads.data.length).toBe(10);
|
|
|
|
expect(ads.total).toBe(16);
|
|
|
|
expect(ads.data[3].passenger).toBeTruthy();
|
|
|
|
expect(ads.data[3].driver).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('findOneByUuid', () => {
|
|
|
|
it('should return an ad', async () => {
|
|
|
|
await createPunctualDriverAds(1);
|
|
|
|
const ad = await adRepository.findOneByUuid(baseUuid.uuid);
|
|
|
|
expect(ad.uuid).toBe(baseUuid.uuid);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return null', async () => {
|
|
|
|
const ad = await adRepository.findOneByUuid(
|
|
|
|
'544572be-11fb-4244-8235-587221fc9104',
|
|
|
|
);
|
|
|
|
expect(ad).toBeNull();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('create', () => {
|
|
|
|
it('should create an ad', async () => {
|
|
|
|
const beforeCount = await prismaService.ad.count();
|
|
|
|
|
|
|
|
const adToCreate: Ad = new Ad();
|
|
|
|
adToCreate.uuid = 'be459a29-7a41-4c0b-b371-abe90bfb6f00';
|
|
|
|
adToCreate.userUuid = '4e52b54d-a729-4dbd-9283-f84a11bb2200';
|
|
|
|
adToCreate.driver = true;
|
|
|
|
adToCreate.passenger = false;
|
|
|
|
adToCreate.fwdAzimuth = 0;
|
|
|
|
adToCreate.backAzimuth = 180;
|
|
|
|
adToCreate.waypoints = "'LINESTRING(6 47,6.1 47.1,6.2 47.2)'";
|
|
|
|
adToCreate.direction =
|
|
|
|
"'LINESTRING(6 47,6.05 47.05,6.1 47.1,6.15 47.15,6.2 47.2)'";
|
|
|
|
adToCreate.seatsDriver = 3;
|
|
|
|
adToCreate.seatsPassenger = 1;
|
|
|
|
adToCreate.seatsUsed = 0;
|
|
|
|
adToCreate.strict = false;
|
|
|
|
adToCreate.frequency = Frequency.PUNCTUAL;
|
|
|
|
adToCreate.fromDate = new Date(2023, 0, 1);
|
|
|
|
adToCreate.toDate = new Date(2023, 0, 1);
|
|
|
|
adToCreate.sunTime = new Date(2023, 0, 1, 6, 0, 0);
|
|
|
|
adToCreate.monMargin = 900;
|
|
|
|
adToCreate.tueMargin = 900;
|
|
|
|
adToCreate.wedMargin = 900;
|
|
|
|
adToCreate.thuMargin = 900;
|
|
|
|
adToCreate.friMargin = 900;
|
|
|
|
adToCreate.satMargin = 900;
|
|
|
|
adToCreate.sunMargin = 900;
|
|
|
|
const ad = await adRepository.createAd(adToCreate);
|
|
|
|
|
|
|
|
const afterCount = await prismaService.ad.count();
|
|
|
|
|
|
|
|
expect(afterCount - beforeCount).toBe(1);
|
|
|
|
expect(ad.uuid).toBe('be459a29-7a41-4c0b-b371-abe90bfb6f00');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|