integration tests
This commit is contained in:
parent
367307b02e
commit
4ba1e91cee
|
@ -31,8 +31,8 @@ export class ConfigurationEntity extends AggregateRoot<ConfigurationProps> {
|
|||
this.addEvent(
|
||||
new ConfigurationSetDomainEvent({
|
||||
aggregateId: this._id,
|
||||
domain: props.domain,
|
||||
key: props.key,
|
||||
domain: this.props.domain,
|
||||
key: this.props.key,
|
||||
value: props.value,
|
||||
}),
|
||||
);
|
||||
|
|
|
@ -13,8 +13,6 @@ export interface CreateConfigurationProps {
|
|||
}
|
||||
|
||||
export interface UpdateConfigurationProps {
|
||||
domain: string;
|
||||
key: string;
|
||||
value: string;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,174 @@
|
|||
import {
|
||||
CONFIGURATION_MESSAGE_PUBLISHER,
|
||||
CONFIGURATION_REPOSITORY,
|
||||
} from '@modules/configuration/configuration.di-tokens';
|
||||
import { ConfigurationMapper } from '@modules/configuration/configuration.mapper';
|
||||
import { ConfigurationEntity } from '@modules/configuration/core/domain/configuration.entity';
|
||||
import {
|
||||
CreateConfigurationProps,
|
||||
Domain,
|
||||
} from '@modules/configuration/core/domain/configuration.types';
|
||||
import { ConfigurationRepository } from '@modules/configuration/infrastructure/configuration.repository';
|
||||
import { PrismaService } from '@modules/configuration/infrastructure/prisma.service';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { EventEmitterModule } from '@nestjs/event-emitter';
|
||||
import { Test } from '@nestjs/testing';
|
||||
|
||||
describe('Configuration Repository', () => {
|
||||
let prismaService: PrismaService;
|
||||
let configurationRepository: ConfigurationRepository;
|
||||
|
||||
const executeInsertCommand = async (table: string, object: any) => {
|
||||
const command = `INSERT INTO "${table}" ("${Object.keys(object).join(
|
||||
'","',
|
||||
)}") VALUES ('${Object.values(object).join("','")}')`;
|
||||
await prismaService.$executeRawUnsafe(command);
|
||||
};
|
||||
const getSeed = (index: number, uuid: string): string => {
|
||||
return `${uuid.slice(0, -2)}${index.toString(16).padStart(2, '0')}`;
|
||||
};
|
||||
|
||||
const baseUuid = {
|
||||
uuid: 'be459a29-7a41-4c0b-b371-abe90bfb6f00',
|
||||
};
|
||||
|
||||
const createConfigurations = async (nbToCreate = 10) => {
|
||||
for (let i = 0; i < nbToCreate; i++) {
|
||||
const configurationToCreate = {
|
||||
uuid: getSeed(i, baseUuid.uuid),
|
||||
domain: Domain.AD,
|
||||
key: `key${i}`,
|
||||
value: `value${i}`,
|
||||
createdAt: '2023-07-24 13:07:05.000',
|
||||
updatedAt: '2023-07-24 13:07:05.000',
|
||||
};
|
||||
configurationToCreate.uuid = getSeed(i, baseUuid.uuid);
|
||||
await executeInsertCommand('configuration', configurationToCreate);
|
||||
}
|
||||
};
|
||||
|
||||
const mockMessagePublisher = {
|
||||
publish: jest.fn().mockImplementation(),
|
||||
};
|
||||
|
||||
const mockLogger = {
|
||||
log: jest.fn(),
|
||||
warn: jest.fn(),
|
||||
error: jest.fn(),
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
const module = await Test.createTestingModule({
|
||||
imports: [
|
||||
EventEmitterModule.forRoot(),
|
||||
ConfigModule.forRoot({ isGlobal: true }),
|
||||
],
|
||||
providers: [
|
||||
PrismaService,
|
||||
ConfigurationMapper,
|
||||
{
|
||||
provide: CONFIGURATION_REPOSITORY,
|
||||
useClass: ConfigurationRepository,
|
||||
},
|
||||
{
|
||||
provide: CONFIGURATION_MESSAGE_PUBLISHER,
|
||||
useValue: mockMessagePublisher,
|
||||
},
|
||||
],
|
||||
})
|
||||
// disable logging
|
||||
.setLogger(mockLogger)
|
||||
.compile();
|
||||
|
||||
prismaService = module.get<PrismaService>(PrismaService);
|
||||
configurationRepository = module.get<ConfigurationRepository>(
|
||||
CONFIGURATION_REPOSITORY,
|
||||
);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await prismaService.$disconnect();
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
await prismaService.configuration.deleteMany();
|
||||
});
|
||||
|
||||
describe('findOne', () => {
|
||||
it('should return a configuration', async () => {
|
||||
await createConfigurations(1);
|
||||
const result = await configurationRepository.findOne({
|
||||
domain: Domain.AD,
|
||||
key: 'key0',
|
||||
});
|
||||
expect(result.getProps().value).toBe('value0');
|
||||
});
|
||||
});
|
||||
|
||||
describe('findAll', () => {
|
||||
it('should return all configurations', async () => {
|
||||
await createConfigurations(10);
|
||||
const configurations: ConfigurationEntity[] =
|
||||
await configurationRepository.findAll({});
|
||||
expect(configurations).toHaveLength(10);
|
||||
});
|
||||
});
|
||||
|
||||
describe('create', () => {
|
||||
it('should create a configuration', async () => {
|
||||
const beforeCount = await prismaService.configuration.count();
|
||||
|
||||
const createConfigurationProps: CreateConfigurationProps = {
|
||||
domain: Domain.AD,
|
||||
key: 'seatsProposed',
|
||||
value: '3',
|
||||
};
|
||||
|
||||
const configurationToCreate: ConfigurationEntity =
|
||||
ConfigurationEntity.create(createConfigurationProps);
|
||||
await configurationRepository.insert(configurationToCreate);
|
||||
|
||||
const afterCount = await prismaService.configuration.count();
|
||||
|
||||
expect(afterCount - beforeCount).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('update', () => {
|
||||
it('should update a configuration', async () => {
|
||||
await createConfigurations(1);
|
||||
const configurationToUpdate: ConfigurationEntity =
|
||||
await configurationRepository.findOne({
|
||||
domain: Domain.AD,
|
||||
key: 'key0',
|
||||
});
|
||||
configurationToUpdate.update({ value: 'newValue' });
|
||||
await configurationRepository.update(
|
||||
configurationToUpdate.id,
|
||||
configurationToUpdate,
|
||||
);
|
||||
const result: ConfigurationEntity = await configurationRepository.findOne(
|
||||
{
|
||||
domain: Domain.AD,
|
||||
key: 'key0',
|
||||
},
|
||||
);
|
||||
expect(result.getProps().value).toBe('newValue');
|
||||
});
|
||||
});
|
||||
|
||||
describe('delete', () => {
|
||||
it('should delete a configuration', async () => {
|
||||
await createConfigurations(10);
|
||||
const beforeCount = await prismaService.configuration.count();
|
||||
const configurationToDelete: ConfigurationEntity =
|
||||
await configurationRepository.findOne({
|
||||
domain: Domain.AD,
|
||||
key: 'key4',
|
||||
});
|
||||
await configurationRepository.delete(configurationToDelete);
|
||||
const afterCount = await prismaService.configuration.count();
|
||||
expect(afterCount - beforeCount).toBe(-1);
|
||||
});
|
||||
});
|
||||
});
|
|
@ -14,8 +14,6 @@ const createConfigurationProps: CreateConfigurationProps = {
|
|||
};
|
||||
|
||||
const updateConfigurationProps: UpdateConfigurationProps = {
|
||||
domain: Domain.AD,
|
||||
key: 'seatsProposed',
|
||||
value: '2',
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue