diff --git a/src/modules/database/src/domain/territory-repository.ts b/src/modules/database/src/domain/territory-repository.ts index a53b0b2..1b153dd 100644 --- a/src/modules/database/src/domain/territory-repository.ts +++ b/src/modules/database/src/domain/territory-repository.ts @@ -15,7 +15,7 @@ export class TerritoryRepository extends PrismaRepository { ); } - async createTerritory(territory: Territory): Promise { + async createTerritory(territory: Partial): Promise { try { const affectedRowNumber = await this.createWithFields({ uuid: `'${uuidv4()}'`, @@ -33,7 +33,10 @@ export class TerritoryRepository extends PrismaRepository { } } - async updateTerritory(uuid: string, territory: Territory): Promise { + async updateTerritory( + uuid: string, + territory: Partial, + ): Promise { try { const fields = {}; if (territory.name) fields['name'] = `'${territory.name}'`; diff --git a/src/modules/database/tests/unit/territory-repository.spec.ts b/src/modules/database/tests/unit/territory-repository.spec.ts new file mode 100644 index 0000000..b26fe92 --- /dev/null +++ b/src/modules/database/tests/unit/territory-repository.spec.ts @@ -0,0 +1,127 @@ +import { Injectable } from '@nestjs/common'; +import { Test, TestingModule } from '@nestjs/testing'; +import { PrismaService } from '../../src/adapters/secondaries/prisma-service'; +import { TerritoryRepository } from '../../src/domain/territory-repository'; +import { Territory } from '../../../territories/domain/entities/territory'; +import { Point } from '../../../territories/domain/entities/point'; +import { DatabaseException } from '../../src/exceptions/database.exception'; + +const mockTerritories: Array = [ + { + uuid: 'bb281075-1b98-4456-89d6-c643d3044a91', + name: 'Nancy', + shape: 'nancy-shape', + }, + { + uuid: 'bb281075-1b98-4456-89d6-c643d3044a92', + name: 'Meurthe-et-Moselle', + shape: 'nancy-meurthe-et-moselle', + }, +]; + +const territoryCreated: Territory = { + uuid: 'bb281075-1b98-4456-89d6-c643d3044a91', + name: 'Nancy', + shape: 'nancy-shape', +}; + +const territoryUpdated: Territory = { + uuid: 'bb281075-1b98-4456-89d6-c643d3044a91', + name: 'New Nancy', + shape: 'new-nancy-shape', +}; + +@Injectable() +class TerritoriesRepository extends TerritoryRepository { + protected _model = 'territory'; +} + +const mockPrismaService = { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + $queryRawUnsafe: jest.fn().mockImplementation((query?: string) => { + return Promise.resolve(mockTerritories); + }), + $executeRawUnsafe: jest + .fn() + .mockResolvedValueOnce(1) + .mockResolvedValueOnce(0) + .mockResolvedValueOnce(1) + .mockResolvedValueOnce(0), + territory: { + findFirst: jest + .fn() + .mockResolvedValueOnce(territoryCreated) + .mockResolvedValueOnce(territoryUpdated), + }, +}; + +describe('TerritoryRepository', () => { + let repository: TerritoriesRepository; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + TerritoriesRepository, + { + provide: PrismaService, + useValue: mockPrismaService, + }, + ], + }).compile(); + + repository = module.get(TerritoriesRepository); + }); + + it('should be defined', () => { + expect(repository).toBeDefined(); + }); + + describe('findForPoint', () => { + it('should return an array of entities', async () => { + const entities = await repository.findForPoint(new Point(6.1, 48.2)); + expect(entities).toStrictEqual({ + data: mockTerritories, + total: mockTerritories.length, + }); + }); + }); + + describe('createTerritory', () => { + it('should create a new territory', async () => { + const territory = await repository.createTerritory({ + name: territoryCreated.name, + shape: territoryCreated.shape, + }); + expect(territory.uuid).toBe(territoryCreated.uuid); + }); + it('should throw a DatabaseException for client error', async () => { + await expect( + repository.createTerritory({ + name: territoryCreated.name, + shape: territoryCreated.shape, + }), + ).rejects.toBeInstanceOf(DatabaseException); + }); + }); + + describe('updateTerritory', () => { + it('should update a territory', async () => { + const updatedTerritory = await repository.updateTerritory( + 'bb281075-1b98-4456-89d6-c643d3044a91', + { + name: 'newNancy', + shape: 'new-nancy-shape', + }, + ); + expect(updatedTerritory.name).toBe(territoryUpdated.name); + }); + it('should throw a DatabaseException for client error', async () => { + await expect( + repository.updateTerritory('bb281075-1b98-4456-89d6-c643d3044a91', { + name: 'newNancy', + shape: 'new-nancy-shape', + }), + ).rejects.toBeInstanceOf(DatabaseException); + }); + }); +});