add integration tests

This commit is contained in:
Gsk54 2023-01-25 16:01:04 +01:00
parent 4c8084e37a
commit fd11af5ba3
3 changed files with 208 additions and 1 deletions

12
.env.test Normal file
View File

@ -0,0 +1,12 @@
# SERVICE
SERVICE_URL=0.0.0.0
SERVICE_PORT=5003
# PRISMA
DATABASE_URL="postgresql://configuration:configuration@localhost:5603/configuration?schema=public"
# RABBIT MQ
RMQ_URI=amqp://v3-broker:5672
# POSTGRES
POSTGRES_IMAGE=postgres:15.0

View File

@ -1,3 +1,3 @@
export enum Domain { export enum Domain {
user = 'user', user = 'USER',
} }

View File

@ -0,0 +1,195 @@
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 { ConfigurationRepository } from '../../adapters/secondaries/configuration.repository';
import { Domain } from '../../domain/dtos/domain.enum';
import { Configuration } from '../../domain/entities/configuration';
describe('ConfigurationRepository', () => {
let prismaService: PrismaService;
let configurationRepository: ConfigurationRepository;
const createConfigurations = async (nbToCreate = 10) => {
for (let i = 0; i < nbToCreate; i++) {
await prismaService.configuration.create({
data: {
domain: Domain.user,
key: `key-${i}`,
value: `key-${i}`,
},
});
}
};
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [DatabaseModule],
providers: [ConfigurationRepository, PrismaService],
}).compile();
prismaService = module.get<PrismaService>(PrismaService);
configurationRepository = module.get<ConfigurationRepository>(
ConfigurationRepository,
);
});
afterAll(async () => {
await prismaService.$disconnect();
});
beforeEach(async () => {
await prismaService.configuration.deleteMany();
});
describe('findAll', () => {
it('should return an empty data array', async () => {
const res = await configurationRepository.findAll();
expect(res).toEqual({
data: [],
total: 0,
});
});
it('should return a data array with 8 configurations', async () => {
await createConfigurations(8);
const configurations = await configurationRepository.findAll();
expect(configurations.data.length).toBe(8);
expect(configurations.total).toBe(8);
});
it('should return a data array limited to 10 configurations', async () => {
await createConfigurations(20);
const configurations = await configurationRepository.findAll();
expect(configurations.data.length).toBe(10);
expect(configurations.total).toBe(20);
});
});
describe('findOneByUuid', () => {
it('should return a configuration', async () => {
const configurationToFind = await prismaService.configuration.create({
data: {
domain: Domain.user,
key: 'key1',
value: 'value1',
},
});
const configuration = await configurationRepository.findOneByUuid(
configurationToFind.uuid,
);
expect(configuration.uuid).toBe(configurationToFind.uuid);
});
it('should return null', async () => {
const configuration = await configurationRepository.findOneByUuid(
'544572be-11fb-4244-8235-587221fc9104',
);
expect(configuration).toBeNull();
});
});
describe('findOne', () => {
it('should return a configuration according to its domain and key', async () => {
const configurationToFind = await prismaService.configuration.create({
data: {
domain: Domain.user,
key: 'key1',
value: 'value1',
},
});
const configuration = await configurationRepository.findOne({
domain: Domain.user,
key: 'key1',
});
expect(configuration.uuid).toBe(configurationToFind.uuid);
});
it('should return null with unknown domain and key', async () => {
const configuration = await configurationRepository.findOne({
domain: Domain.user,
key: 'key1',
});
expect(configuration).toBeNull();
});
});
describe('create', () => {
it('should create a configuration', async () => {
const beforeCount = await prismaService.configuration.count();
const configurationToCreate: Configuration = new Configuration();
configurationToCreate.domain = Domain.user;
configurationToCreate.key = 'key1';
configurationToCreate.value = 'value1';
const configuration = await configurationRepository.create(
configurationToCreate,
);
const afterCount = await prismaService.configuration.count();
expect(afterCount - beforeCount).toBe(1);
expect(configuration.uuid).toBeDefined();
});
});
describe('update', () => {
it('should update configuration key', async () => {
const configurationToUpdate = await prismaService.configuration.create({
data: {
domain: Domain.user,
key: 'key1',
value: 'value1',
},
});
const toUpdate: Configuration = new Configuration();
toUpdate.key = 'key2';
const updatedConfiguration = await configurationRepository.update(
configurationToUpdate.uuid,
toUpdate,
);
expect(updatedConfiguration.uuid).toBe(configurationToUpdate.uuid);
expect(updatedConfiguration.key).toBe('key2');
});
it('should throw DatabaseException', async () => {
const toUpdate: Configuration = new Configuration();
toUpdate.key = 'updated';
await expect(
configurationRepository.update(
'544572be-11fb-4244-8235-587221fc9104',
toUpdate,
),
).rejects.toBeInstanceOf(DatabaseException);
});
});
describe('delete', () => {
it('should delete a configuration', async () => {
const configurationToRemove = await prismaService.configuration.create({
data: {
domain: Domain.user,
key: 'key1',
value: 'value1',
},
});
await configurationRepository.delete(configurationToRemove.uuid);
const count = await prismaService.configuration.count();
expect(count).toBe(0);
});
it('should throw DatabaseException', async () => {
await expect(
configurationRepository.delete('544572be-11fb-4244-8235-587221fc9104'),
).rejects.toBeInstanceOf(DatabaseException);
});
});
});