add integration tests
This commit is contained in:
parent
368ce98174
commit
48e4f456a7
|
@ -0,0 +1,165 @@
|
||||||
|
import { TestingModule, Test } from '@nestjs/testing';
|
||||||
|
import { DatabaseModule } from '../../../database/database.module';
|
||||||
|
import { PrismaService } from '../../../database/src/adapters/secondaries/prisma-service';
|
||||||
|
import { DatabaseException } from '../../../database/src/exceptions/database.exception';
|
||||||
|
import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository';
|
||||||
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
import { Territory } from '../../domain/entities/territory';
|
||||||
|
|
||||||
|
describe('TerritoriesRepository', () => {
|
||||||
|
let prismaService: PrismaService;
|
||||||
|
let territoriesRepository: 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 ] ] ] ]}'))`;
|
||||||
|
await prismaService.$executeRawUnsafe(command);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
|
imports: [DatabaseModule],
|
||||||
|
providers: [TerritoriesRepository, PrismaService],
|
||||||
|
}).compile();
|
||||||
|
|
||||||
|
prismaService = module.get<PrismaService>(PrismaService);
|
||||||
|
territoriesRepository = module.get<TerritoriesRepository>(
|
||||||
|
TerritoriesRepository,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await prismaService.$disconnect();
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
await prismaService.territory.deleteMany();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('findAll', () => {
|
||||||
|
it('should return an empty data array', async () => {
|
||||||
|
const res = await territoriesRepository.findAll();
|
||||||
|
expect(res).toEqual({
|
||||||
|
data: [],
|
||||||
|
total: 0,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return a data array with 8 territories', async () => {
|
||||||
|
await createTerritories(8);
|
||||||
|
const users = await territoriesRepository.findAll();
|
||||||
|
expect(users.data.length).toBe(8);
|
||||||
|
expect(users.total).toBe(8);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return a data array limited to 10 territories', async () => {
|
||||||
|
await createTerritories(20);
|
||||||
|
const users = await territoriesRepository.findAll();
|
||||||
|
expect(users.data.length).toBe(10);
|
||||||
|
expect(users.total).toBe(20);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
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 ] ] ] ]}'))`;
|
||||||
|
await prismaService.$executeRawUnsafe(command);
|
||||||
|
|
||||||
|
const territory = await territoriesRepository.findOneByUuid(
|
||||||
|
'd83a1697-fc82-4f84-b7f4-dd9e2bcae327',
|
||||||
|
);
|
||||||
|
expect(territory.name).toBe('test');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return null', async () => {
|
||||||
|
const territory = await territoriesRepository.findOneByUuid(
|
||||||
|
'544572be-11fb-4244-8235-587221fc9104',
|
||||||
|
);
|
||||||
|
expect(territory).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('findOne', () => {
|
||||||
|
it('should return a territory according to its name', 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 ] ] ] ]}'))`;
|
||||||
|
await prismaService.$executeRawUnsafe(command);
|
||||||
|
|
||||||
|
const territory = await territoriesRepository.findOne({
|
||||||
|
name: 'test',
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(territory.uuid).toBe('d83a1697-fc82-4f84-b7f4-dd9e2bcae327');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return null with unknown name', async () => {
|
||||||
|
const territory = await territoriesRepository.findOne({
|
||||||
|
name: 'wrong name',
|
||||||
|
});
|
||||||
|
expect(territory).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('create', () => {
|
||||||
|
it('should create a territory', async () => {
|
||||||
|
const beforeCount = await prismaService.territory.count();
|
||||||
|
|
||||||
|
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 ] ] ] ]}'))`;
|
||||||
|
await prismaService.$executeRawUnsafe(command);
|
||||||
|
|
||||||
|
const afterCount = await prismaService.territory.count();
|
||||||
|
|
||||||
|
expect(afterCount - beforeCount).toBe(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('update', () => {
|
||||||
|
it('should update territory name', 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 ] ] ] ]}'))`;
|
||||||
|
await prismaService.$executeRawUnsafe(command);
|
||||||
|
|
||||||
|
const toUpdate: Territory = new Territory();
|
||||||
|
toUpdate.name = "'updated'";
|
||||||
|
await territoriesRepository.updateWithFields(
|
||||||
|
'd83a1697-fc82-4f84-b7f4-dd9e2bcae327',
|
||||||
|
toUpdate,
|
||||||
|
);
|
||||||
|
|
||||||
|
const updatedTerritory = await territoriesRepository.findOneByUuid(
|
||||||
|
'd83a1697-fc82-4f84-b7f4-dd9e2bcae327',
|
||||||
|
);
|
||||||
|
expect(updatedTerritory.name).toBe('updated');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return 0 updated territories', async () => {
|
||||||
|
const toUpdate: Territory = new Territory();
|
||||||
|
toUpdate.name = "'updated'";
|
||||||
|
|
||||||
|
const affectedRowNumber = await territoriesRepository.updateWithFields(
|
||||||
|
'544572be-11fb-4244-8235-587221fc9104',
|
||||||
|
toUpdate,
|
||||||
|
);
|
||||||
|
expect(affectedRowNumber).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('delete', () => {
|
||||||
|
it('should delete 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 ] ] ] ]}'))`;
|
||||||
|
await prismaService.$executeRawUnsafe(command);
|
||||||
|
|
||||||
|
await territoriesRepository.delete(
|
||||||
|
'd83a1697-fc82-4f84-b7f4-dd9e2bcae327',
|
||||||
|
);
|
||||||
|
|
||||||
|
const count = await prismaService.territory.count();
|
||||||
|
expect(count).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw DatabaseException', async () => {
|
||||||
|
await expect(
|
||||||
|
territoriesRepository.delete('544572be-11fb-4244-8235-587221fc9104'),
|
||||||
|
).rejects.toBeInstanceOf(DatabaseException);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue