From 58a7a66940ec2779d2c1937bbda6cd8037126671 Mon Sep 17 00:00:00 2001 From: sbriat Date: Thu, 9 Feb 2023 15:35:31 +0100 Subject: [PATCH] improve integration tests --- .../territories.repository.spec.ts | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/modules/territories/tests/integration/territories.repository.spec.ts b/src/modules/territories/tests/integration/territories.repository.spec.ts index c712673..63e9655 100644 --- a/src/modules/territories/tests/integration/territories.repository.spec.ts +++ b/src/modules/territories/tests/integration/territories.repository.spec.ts @@ -5,6 +5,7 @@ import { DatabaseException } from '../../../database/src/exceptions/database.exc import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository'; import { v4 as uuidv4 } from 'uuid'; import { Territory } from '../../domain/entities/territory'; +import { Point } from '../../domain/entities/point'; describe('TerritoriesRepository', () => { let prismaService: PrismaService; @@ -12,11 +13,20 @@ describe('TerritoriesRepository', () => { const createTerritories = async (nbToCreate = 10) => { 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); } }; + 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 () => { const module: TestingModule = await Test.createTestingModule({ 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', () => { 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 ] ] ] ]}'))`;