Merge branch 'findForPoints' into 'main'

improve integration tests

See merge request v3/services/territory!5
This commit is contained in:
Sylvain Briat 2023-02-09 14:40:26 +00:00
commit 45c6fd1fc5
1 changed files with 40 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import { DatabaseException } from '../../../database/src/exceptions/database.exc
import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository'; import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import { Territory } from '../../domain/entities/territory'; import { Territory } from '../../domain/entities/territory';
import { Point } from '../../domain/entities/point';
describe('TerritoriesRepository', () => { describe('TerritoriesRepository', () => {
let prismaService: PrismaService; let prismaService: PrismaService;
@ -12,11 +13,20 @@ describe('TerritoriesRepository', () => {
const createTerritories = async (nbToCreate = 10) => { const createTerritories = async (nbToCreate = 10) => {
for (let i = 0; i < nbToCreate; i++) { for (let i = 0; i < nbToCreate; i++) {
const command = `INSERT INTO territory (uuid, name, shape) VALUES ('${uuidv4()}','name-${i}',ST_GeomFromGeoJSON('{"type":"MultiPolygon","coordinates":[ [ [ [ 4.7953936, 46.2200671 ], [ 4.793836, 46.2155098 ], [ 4.7931368, 46.2124124 ], [ 4.7927677, 46.207594 ], [ 4.793866, 46.2042467 ] ] ] ]}'))`; const command = `INSERT INTO territory (uuid, name, shape) VALUES ('${uuidv4()}','name-${i}',ST_GeomFromGeoJSON('{"type":"MultiPolygon","coordinates":[ [ [ [ 0, 0 ], [ 0, ${
(i + 1) * 2
} ], [ ${(i + 1) * 2}, ${(i + 1) * 2} ], [ ${
(i + 1) * 2
}, 0 ], [ 0, 0 ] ] ] ]}'))`;
await prismaService.$executeRawUnsafe(command); await prismaService.$executeRawUnsafe(command);
} }
}; };
const createExtraTerritory = async () => {
const command = `INSERT INTO territory (uuid, name, shape) VALUES ('${uuidv4()}','name-extra',ST_GeomFromGeoJSON('{"type":"MultiPolygon","coordinates":[ [ [ [ 100, 100 ], [ 100, 110 ], [ 110, 110 ], [ 110, 100 ], [ 100, 100 ] ] ] ]}'))`;
await prismaService.$executeRawUnsafe(command);
};
beforeAll(async () => { beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({ const module: TestingModule = await Test.createTestingModule({
imports: [DatabaseModule], imports: [DatabaseModule],
@ -61,6 +71,35 @@ describe('TerritoriesRepository', () => {
}); });
}); });
describe('findAllForPoint', () => {
it('should return a data array with 3 territories', async () => {
await createTerritories(10);
const users = await territoriesRepository.findForPoint(new Point(15, 15));
expect(users.data.length).toBe(3);
expect(users.total).toBe(3);
});
it('should return an empty data array', async () => {
await createTerritories(10);
const users = await territoriesRepository.findForPoint(new Point(50, 50));
expect(users.data.length).toBe(0);
expect(users.total).toBe(0);
});
});
describe('findAllForPoints', () => {
it('should return a data array with 4 territories', async () => {
await createTerritories(10);
await createExtraTerritory();
const users = await territoriesRepository.findForPoints([
new Point(15, 15),
new Point(105, 105),
]);
expect(users.data.length).toBe(4);
expect(users.total).toBe(4);
});
});
describe('findOneByUuid', () => { describe('findOneByUuid', () => {
it('should return a territory', async () => { it('should return a territory', async () => {
const command = `INSERT INTO territory (uuid, name, shape) VALUES ('d83a1697-fc82-4f84-b7f4-dd9e2bcae327','test',ST_GeomFromGeoJSON('{"type":"MultiPolygon","coordinates":[ [ [ [ 4.7953936, 46.2200671 ], [ 4.793836, 46.2155098 ], [ 4.7931368, 46.2124124 ], [ 4.7927677, 46.207594 ], [ 4.793866, 46.2042467 ] ] ] ]}'))`; const command = `INSERT INTO territory (uuid, name, shape) VALUES ('d83a1697-fc82-4f84-b7f4-dd9e2bcae327','test',ST_GeomFromGeoJSON('{"type":"MultiPolygon","coordinates":[ [ [ [ 4.7953936, 46.2200671 ], [ 4.793836, 46.2155098 ], [ 4.7931368, 46.2124124 ], [ 4.7927677, 46.207594 ], [ 4.793866, 46.2042467 ] ] ] ]}'))`;