166 lines
4.0 KiB
TypeScript
166 lines
4.0 KiB
TypeScript
import { AD_DIRECTION_ENCODER } from '@modules/ad/ad.di-tokens';
|
|
import { AdMapper } from '@modules/ad/ad.mapper';
|
|
import { AdEntity } from '@modules/ad/core/domain/ad.entity';
|
|
import { Frequency } from '@modules/ad/core/domain/ad.types';
|
|
import {
|
|
AdReadModel,
|
|
AdWriteExtraModel,
|
|
AdWriteModel,
|
|
} from '@modules/ad/infrastructure/ad.repository';
|
|
import { DirectionEncoderPort } from '@modules/geography/core/application/ports/direction-encoder.port';
|
|
import { Test } from '@nestjs/testing';
|
|
|
|
const now = new Date('2023-06-21 06:00:00');
|
|
const adEntity: AdEntity = new AdEntity({
|
|
id: 'c160cf8c-f057-4962-841f-3ad68346df44',
|
|
props: {
|
|
driver: true,
|
|
passenger: true,
|
|
frequency: Frequency.PUNCTUAL,
|
|
fromDate: '2023-06-21',
|
|
toDate: '2023-06-21',
|
|
schedule: [
|
|
{
|
|
day: 3,
|
|
time: '07:15',
|
|
margin: 900,
|
|
},
|
|
],
|
|
waypoints: [
|
|
{
|
|
lat: 48.689445,
|
|
lon: 6.1765102,
|
|
},
|
|
{
|
|
lat: 48.8566,
|
|
lon: 2.3522,
|
|
},
|
|
],
|
|
strict: false,
|
|
pause: false,
|
|
seatsProposed: 3,
|
|
seatsRequested: 1,
|
|
driverDistance: 350101,
|
|
driverDuration: 14422,
|
|
passengerDistance: 350101,
|
|
passengerDuration: 14422,
|
|
fwdAzimuth: 273,
|
|
backAzimuth: 93,
|
|
points: [
|
|
{
|
|
lon: 6.1765102,
|
|
lat: 48.689445,
|
|
},
|
|
{
|
|
lon: 4.984578,
|
|
lat: 48.725687,
|
|
},
|
|
{
|
|
lon: 2.3522,
|
|
lat: 48.8566,
|
|
},
|
|
],
|
|
},
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
});
|
|
|
|
const adReadModel: AdReadModel = {
|
|
uuid: 'c160cf8c-f057-4962-841f-3ad68346df44',
|
|
driver: true,
|
|
passenger: true,
|
|
frequency: Frequency.PUNCTUAL,
|
|
fromDate: new Date('2023-06-21'),
|
|
toDate: new Date('2023-06-21'),
|
|
schedule: [
|
|
{
|
|
uuid: '3978f3d6-560f-4a8f-83ba-9bf5aa9a2d27',
|
|
day: 3,
|
|
time: new Date('2023-06-21T07:05:00Z'),
|
|
margin: 900,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
},
|
|
],
|
|
waypoints: "'LINESTRING(6.1765102 48.689445,2.3522 48.8566)'",
|
|
driverDistance: 350000,
|
|
driverDuration: 14400,
|
|
passengerDistance: 350000,
|
|
passengerDuration: 14400,
|
|
fwdAzimuth: 273,
|
|
backAzimuth: 93,
|
|
strict: false,
|
|
pause: false,
|
|
seatsProposed: 3,
|
|
seatsRequested: 1,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
};
|
|
|
|
const mockDirectionEncoder: DirectionEncoderPort = {
|
|
encode: jest
|
|
.fn()
|
|
.mockImplementationOnce(
|
|
() => "'LINESTRING(6.1765102 48.689445,2.3522 48.8566)'",
|
|
)
|
|
.mockImplementationOnce(
|
|
() =>
|
|
"'LINESTRING(6.1765102 48.689445,4.984578 48.725687,2.3522 48.8566)'",
|
|
),
|
|
decode: jest.fn().mockImplementation(() => [
|
|
{
|
|
lon: 6.1765102,
|
|
lat: 48.689445,
|
|
},
|
|
{
|
|
lon: 2.3522,
|
|
lat: 48.8566,
|
|
},
|
|
]),
|
|
};
|
|
|
|
describe('Ad Mapper', () => {
|
|
let adMapper: AdMapper;
|
|
|
|
beforeAll(async () => {
|
|
const module = await Test.createTestingModule({
|
|
providers: [
|
|
AdMapper,
|
|
{
|
|
provide: AD_DIRECTION_ENCODER,
|
|
useValue: mockDirectionEncoder,
|
|
},
|
|
],
|
|
}).compile();
|
|
adMapper = module.get<AdMapper>(AdMapper);
|
|
});
|
|
|
|
it('should be defined', () => {
|
|
expect(adMapper).toBeDefined();
|
|
});
|
|
|
|
it('should map domain entity to persistence data', async () => {
|
|
const mapped: AdWriteModel = adMapper.toPersistence(adEntity);
|
|
expect(mapped.schedule.create.length).toBe(1);
|
|
expect(mapped.driverDuration).toBe(14422);
|
|
expect(mapped.fwdAzimuth).toBe(273);
|
|
});
|
|
|
|
it('should map domain entity to unsupported db persistence data', async () => {
|
|
const mapped: AdWriteExtraModel = adMapper.toPersistenceExtra(adEntity);
|
|
expect(mapped.waypoints).toBe(
|
|
"'LINESTRING(6.1765102 48.689445,2.3522 48.8566)'",
|
|
);
|
|
expect(mapped.direction).toBe(
|
|
"'LINESTRING(6.1765102 48.689445,4.984578 48.725687,2.3522 48.8566)'",
|
|
);
|
|
});
|
|
|
|
it('should map persisted data to domain entity', async () => {
|
|
const mapped: AdEntity = adMapper.toDomain(adReadModel);
|
|
expect(mapped.getProps().schedule.length).toBe(1);
|
|
expect(mapped.getProps().schedule[0].time).toBe('07:05');
|
|
expect(mapped.getProps().waypoints.length).toBe(2);
|
|
});
|
|
});
|