Merge branch 'improveTests' into 'main'
improve tests See merge request v3/services/territory!3
This commit is contained in:
commit
aa69267dac
|
@ -15,7 +15,7 @@ export class TerritoryRepository<T> extends PrismaRepository<T> {
|
|||
);
|
||||
}
|
||||
|
||||
async createTerritory(territory: Territory): Promise<T> {
|
||||
async createTerritory(territory: Partial<Territory>): Promise<T> {
|
||||
try {
|
||||
const affectedRowNumber = await this.createWithFields({
|
||||
uuid: `'${uuidv4()}'`,
|
||||
|
@ -33,7 +33,10 @@ export class TerritoryRepository<T> extends PrismaRepository<T> {
|
|||
}
|
||||
}
|
||||
|
||||
async updateTerritory(uuid: string, territory: Territory): Promise<T> {
|
||||
async updateTerritory(
|
||||
uuid: string,
|
||||
territory: Partial<Territory>,
|
||||
): Promise<T> {
|
||||
try {
|
||||
const fields = {};
|
||||
if (territory.name) fields['name'] = `'${territory.name}'`;
|
||||
|
|
|
@ -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<Territory> = [
|
||||
{
|
||||
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<Territory> {
|
||||
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>(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);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue