Skip to content

Commit

Permalink
EW-617: Add tests for personenkontext creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkreuzkam-cap committed Oct 6, 2023
1 parent 64c88fa commit 371a764
Show file tree
Hide file tree
Showing 13 changed files with 419 additions and 3 deletions.
53 changes: 52 additions & 1 deletion src/modules/person/api/person-api.mapper.profile.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Mapper } from '@automapper/core';
import { getMapperToken } from '@automapper/nestjs';
import { Test, TestingModule } from '@nestjs/testing';
import { MapperTestModule } from '../../../../test/utils/index.js';
import { DoFactory, MapperTestModule } from '../../../../test/utils/index.js';
import { MappingError } from '../../../shared/error/index.js';
import { CreatePersonBodyParams } from './create-person.body.params.js';
import { CreatePersonDto } from '../domain/create-person.dto.js';
Expand All @@ -16,6 +16,12 @@ import { faker } from '@faker-js/faker';
import { Gender, TrustLevel } from '../domain/person.enums.js';
import { PersonGender, PersonTrustLevel } from './person.enums.js';
import { SichtfreigabeType } from './personen-query.param.js';
import { CreatePersonenkontextBodyParams } from './create-personenkontext.body.params.js';
import { CreatePersonenkontextDto } from './create-personenkontext.dto.js';
import { Jahrgangsstufe, Personenstatus, Rolle } from '../domain/personenkontext.enums.js';
import { PersonenkontextDo } from '../domain/personenkontext.do.js';
import { CreatedPersonenkontextDto } from './created-personenkontext.dto.js';
import { PersonenkontextResponse } from './personenkontext.response.js';

describe('PersonApiMapperProfile', () => {
let module: TestingModule;
Expand Down Expand Up @@ -135,5 +141,50 @@ describe('PersonApiMapperProfile', () => {
};
expect(() => sut.map(dto, CreatePersonDto, PersonDo)).not.toThrowError(MappingError);
});

it('should map CreatePersonenkontextBodyParams to CreatePersonenkontextDto', () => {
const body: CreatePersonenkontextBodyParams = {
rolle: Rolle.Lehrender,
jahrgangsstufe: Jahrgangsstufe.Jahrgangsstufe1,
personenstatus: Personenstatus.Aktiv,
referrer: 'referrer',
};
expect(() => sut.map(body, CreatePersonenkontextBodyParams, CreatePersonenkontextDto)).not.toThrowError(
MappingError,
);
});
it('should map CreatePersonenkontextDto to PersonenkontextDo', () => {
const dto: CreatePersonenkontextDto = {
personId: faker.string.uuid(),
rolle: Rolle.Lehrender,
jahrgangsstufe: Jahrgangsstufe.Jahrgangsstufe1,
personenstatus: Personenstatus.Aktiv,
referrer: 'referrer',
};
expect(() => sut.map(dto, CreatePersonenkontextDto, PersonenkontextDo)).not.toThrowError(MappingError);
});
it('should map PersonenkontextDo to CreatedPersonenkontextDto', () => {
const personDo: PersonenkontextDo<true> = DoFactory.createPersonenkontext(true);
expect(() => sut.map(personDo, PersonenkontextDo, CreatedPersonenkontextDto)).not.toThrowError(
MappingError,
);
});
it('should map CreatedPersonenkontextDto to PersonenkontextResponse', () => {
const dto: CreatedPersonenkontextDto = {
id: faker.string.uuid(),
mandant: faker.string.uuid(),
organisation: {
id: faker.string.uuid(),
},
revision: '1',
rolle: Rolle.Lehrender,
jahrgangsstufe: Jahrgangsstufe.Jahrgangsstufe1,
personenstatus: Personenstatus.Aktiv,
referrer: 'referrer',
};
expect(() => sut.map(dto, CreatedPersonenkontextDto, PersonenkontextResponse)).not.toThrowError(
MappingError,
);
});
});
});
3 changes: 3 additions & 0 deletions src/modules/person/api/person-api.mapper.profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { CreatePersonenkontextDto } from './create-personenkontext.dto.js';
import { PersonenkontextDo } from '../domain/personenkontext.do.js';
import { CreatedPersonenkontextDto } from './created-personenkontext.dto.js';
import { PersonenkontextResponse } from './personenkontext.response.js';
import { OrganisationDo } from '../../organisation/domain/organisation.do.js';
import { CreatedPersonenkontextDtoOrganisation } from './created-personenkontext-org.dto.js';

export const personGenderToGenderConverter: Converter<PersonGender, Gender> = {
convert(source: PersonGender): Gender {
Expand Down Expand Up @@ -299,6 +301,7 @@ export class PersonApiMapperProfile extends AutomapperProfile {
mapFrom((src: PersonenkontextDo<true>) => src.id),
),
);
createMap(mapper, OrganisationDo, CreatedPersonenkontextDtoOrganisation);
createMap(mapper, CreatedPersonenkontextDto, PersonenkontextResponse);
};
}
Expand Down
40 changes: 40 additions & 0 deletions src/modules/person/api/person.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ import { PersonenQueryParam } from './personen-query.param.js';
import { PersonBirthParams } from './person-birth.params.js';
import { TrustLevel } from '../domain/person.enums.js';
import { PersonenDatensatz } from './personendatensatz.js';
import { PersonenkontextUc } from './personenkontext.uc.js';
import { CreatePersonenkontextBodyParams } from './create-personenkontext.body.params.js';
import { CreatedPersonenkontextDto } from './created-personenkontext.dto.js';
import { Jahrgangsstufe, Personenstatus, Rolle } from '../domain/personenkontext.enums.js';

describe('PersonController', () => {
let module: TestingModule;
let personController: PersonController;
let personUcMock: DeepMocked<PersonUc>;
let personenkontextUcMock: DeepMocked<PersonenkontextUc>;
const mockBirthParams: PersonBirthParams = {
datum: faker.date.anytime(),
geburtsort: faker.string.alpha(),
Expand All @@ -33,10 +38,15 @@ describe('PersonController', () => {
provide: PersonUc,
useValue: createMock<PersonUc>(),
},
{
provide: PersonenkontextUc,
useValue: createMock<PersonenkontextUc>(),
},
],
}).compile();
personController = module.get(PersonController);
personUcMock = module.get(PersonUc);
personenkontextUcMock = module.get(PersonenkontextUc);
});

afterAll(async () => {
Expand Down Expand Up @@ -174,4 +184,34 @@ describe('PersonController', () => {
expect(result).toEqual(mockPersondatensatz);
});
});

describe('when creating a personenkontext', () => {
it('should not throw', async () => {
const pathParams: PersonByIdParams = {
personId: faker.string.uuid(),
};
const body: CreatePersonenkontextBodyParams = {
rolle: Rolle.Lehrender,
jahrgangsstufe: Jahrgangsstufe.Jahrgangsstufe1,
personenstatus: Personenstatus.Aktiv,
referrer: 'referrer',
};
const ucResult: CreatedPersonenkontextDto = {
id: faker.string.uuid(),
mandant: faker.string.uuid(),
organisation: {
id: faker.string.uuid(),
},
revision: '1',
rolle: Rolle.Lehrender,
jahrgangsstufe: Jahrgangsstufe.Jahrgangsstufe1,
personenstatus: Personenstatus.Aktiv,
referrer: 'referrer',
};
personenkontextUcMock.createPersonenkontext.mockResolvedValue(ucResult);

await expect(personController.createPersonenkontext(pathParams, body)).resolves.not.toThrow();
expect(personenkontextUcMock.createPersonenkontext).toHaveBeenCalledTimes(1);
});
});
});
77 changes: 77 additions & 0 deletions src/modules/person/api/personenkontext.uc.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { DeepMocked, createMock } from '@golevelup/ts-jest';
import { Test, TestingModule } from '@nestjs/testing';
import { DoFactory, MapperTestModule } from '../../../../test/utils/index.js';
import { PersonenkontextDo } from '../domain/personenkontext.do.js';
import { PersonenkontextService } from '../domain/personenkontext.service.js';
import { CreatePersonenkontextDto } from './create-personenkontext.dto.js';
import { PersonApiMapperProfile } from './person-api.mapper.profile.js';
import { PersonenkontextUc } from './personenkontext.uc.js';
import { EntityCouldNotBeCreated } from '../../../shared/error/entity-could-not-be-created.error.js';
import { CreatedPersonenkontextDto } from './created-personenkontext.dto.js';

describe('PersonenkontextUc', () => {
let module: TestingModule;
let personenkontextUc: PersonenkontextUc;
let personenkontextServiceMock: DeepMocked<PersonenkontextService>;

beforeAll(async () => {
module = await Test.createTestingModule({
imports: [MapperTestModule],
providers: [
PersonenkontextUc,
PersonApiMapperProfile,
{
provide: PersonenkontextService,
useValue: createMock<PersonenkontextService>(),
},
],
}).compile();
personenkontextUc = module.get(PersonenkontextUc);
personenkontextServiceMock = module.get(PersonenkontextService);
});

afterAll(async () => {
await module.close();
});

beforeEach(() => {
jest.resetAllMocks();
});

it('should be defined', () => {
expect(personenkontextUc).toBeDefined();
});

describe('createPersonenkontext', () => {
describe('when creation of personenkontext is successful', () => {
it('should not throw', async () => {
const personenkontextDo: PersonenkontextDo<true> = DoFactory.createPersonenkontext(true);
personenkontextServiceMock.createPersonenkontext.mockResolvedValue({
ok: true,
value: personenkontextDo,
});

const createPersonPromise: Promise<CreatedPersonenkontextDto> = personenkontextUc.createPersonenkontext(
{} as CreatePersonenkontextDto,
);

await expect(createPersonPromise).resolves.not.toThrow();
});
});
describe('when creation of personenkontext is not successful', () => {
it('should throw Error', async () => {
const error: EntityCouldNotBeCreated = new EntityCouldNotBeCreated('Personenkontext');
personenkontextServiceMock.createPersonenkontext.mockResolvedValue({
ok: false,
error: error,
});

const createPersonPromise: Promise<CreatedPersonenkontextDto> = personenkontextUc.createPersonenkontext(
{} as CreatePersonenkontextDto,
);

await expect(createPersonPromise).rejects.toThrow(error);
});
});
});
});
2 changes: 1 addition & 1 deletion src/modules/person/domain/personenkontext.do.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class PersonenkontextDo<WasPersisted extends boolean> implements DoBase<W
public mandant!: string;

@AutoMap()
public organisation!: OrganisationDo<WasPersisted>;
public organisation!: OrganisationDo<true>;

@AutoMap()
public rolle!: Rolle;
Expand Down
79 changes: 79 additions & 0 deletions src/modules/person/domain/personenkontext.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { Dictionary, Mapper } from '@automapper/core';
import { getMapperToken } from '@automapper/nestjs';
import { faker } from '@faker-js/faker';
import { DeepMocked, createMock } from '@golevelup/ts-jest';
import { Test, TestingModule } from '@nestjs/testing';
import { DoFactory } from '../../../../test/utils/do-factory.js';
import { EntityCouldNotBeCreated } from '../../../shared/error/entity-could-not-be-created.error.js';
import { PersonenkontextRepo } from '../persistence/personenkontext.repo.js';
import { PersonenkontextDo } from './personenkontext.do.js';
import { PersonenkontextService } from './personenkontext.service.js';

describe('PersonenkontextService', () => {
let module: TestingModule;
let personenkontextService: PersonenkontextService;
let personenkontextRepoMock: DeepMocked<PersonenkontextRepo>;
let mapperMock: DeepMocked<Mapper>;

beforeAll(async () => {
module = await Test.createTestingModule({
providers: [
PersonenkontextService,
{
provide: PersonenkontextRepo,
useValue: createMock<PersonenkontextRepo>(),
},
{
provide: getMapperToken(),
useValue: createMock<Mapper>(),
},
],
}).compile();
personenkontextService = module.get(PersonenkontextService);
personenkontextRepoMock = module.get(PersonenkontextRepo);
mapperMock = module.get(getMapperToken());
});

afterAll(async () => {
await module.close();
});

beforeEach(() => {
jest.resetAllMocks();
});

it('should be defined', () => {
expect(personenkontextService).toBeDefined();
});

describe('createPersonenkontext', () => {
describe('when personenkontext is saved successfully', () => {
it('should create an Personenkontext', async () => {
const personenkontextDo: PersonenkontextDo<false> = DoFactory.createPersonenkontext(false);
personenkontextRepoMock.save.mockResolvedValue(personenkontextDo as unknown as PersonenkontextDo<true>);
mapperMock.map.mockReturnValue(personenkontextDo as unknown as Dictionary<unknown>);
const result: Result<PersonenkontextDo<true>> = await personenkontextService.createPersonenkontext(
personenkontextDo,
);
expect(result).toEqual<Result<PersonenkontextDo<true>>>({
ok: true,
value: personenkontextDo as unknown as PersonenkontextDo<true>,
});
});
});

describe('when personenkontext is not saved successfully', () => {
it('should return a domain error', async () => {
const personenkontextDo: PersonenkontextDo<false> = DoFactory.createPersonenkontext(false);
personenkontextDo.id = faker.string.uuid();
const result: Result<PersonenkontextDo<true>> = await personenkontextService.createPersonenkontext(
personenkontextDo,
);
expect(result).toEqual<Result<PersonenkontextDo<true>>>({
ok: false,
error: new EntityCouldNotBeCreated(`Personenkontext`),
});
});
});
});
});
2 changes: 1 addition & 1 deletion src/modules/person/domain/personenkontext.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export class PersonenkontextService {
if (personenkontext) {
return { ok: true, value: personenkontext };
}
return { ok: false, error: new EntityCouldNotBeCreated(`Personenkontext could not be created`) };
return { ok: false, error: new EntityCouldNotBeCreated(`Personenkontext`) };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { MappingError } from '../../../shared/error/index.js';
import { PersonDo } from '../domain/person.do.js';
import { PersonEntity } from '../persistence/person.entity.js';
import { PersonPersistenceMapperProfile } from './person-persistence.mapper.profile.js';
import { PersonenkontextDo } from '../domain/personenkontext.do.js';
import { PersonenkontextEntity } from './personenkontext.entity.js';

describe('PersonPersistenceMapperProfile', () => {
let module: TestingModule;
Expand Down Expand Up @@ -37,5 +39,20 @@ describe('PersonPersistenceMapperProfile', () => {
const person: PersonDo<true> = new PersonEntity();
expect(() => sut.map(person, PersonEntity, PersonDo)).not.toThrowError(MappingError);
});

it('should map PersonenkontextDo to PersonenkontextEntity', () => {
const personenkontext: PersonenkontextDo<false> = DoFactory.createPersonenkontext(false);
expect(() => sut.map(personenkontext, PersonenkontextDo, PersonenkontextEntity)).not.toThrowError(
MappingError,
);
});

it('should map PersonenkontextEntity to PersonenkontextDo', () => {
const personenkontext: PersonenkontextEntity = new PersonenkontextEntity();
personenkontext.person = new PersonEntity();
expect(() => sut.map(personenkontext, PersonenkontextEntity, PersonenkontextDo<true>)).not.toThrowError(
MappingError,
);
});
});
});
Loading

0 comments on commit 371a764

Please sign in to comment.