Skip to content

Commit

Permalink
create test for service-prodiver.repo
Browse files Browse the repository at this point in the history
  • Loading branch information
DPDS93CT committed Oct 16, 2023
1 parent 2b210b5 commit c20d2e3
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 1 deletion.
57 changes: 57 additions & 0 deletions src/modules/rolle/api/provider.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {Test, TestingModule} from "@nestjs/testing";

Check warning on line 1 in src/modules/rolle/api/provider.controller.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint / Nest Lint

Replace `Test,·TestingModule}·from·"@nestjs/testing"` with `·Test,·TestingModule·}·from·'@nestjs/testing'`
import { ProviderController } from "./provider.controller";

Check warning on line 2 in src/modules/rolle/api/provider.controller.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint / Nest Lint

Replace `"./provider.controller"` with `'./provider.controller'`

Check failure on line 2 in src/modules/rolle/api/provider.controller.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint / Nest Lint

Missing file extension "ts" for "./provider.controller"
import {createMock, DeepMocked} from "@golevelup/ts-jest";

Check warning on line 3 in src/modules/rolle/api/provider.controller.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint / Nest Lint

Replace `createMock,·DeepMocked}·from·"@golevelup/ts-jest"` with `·createMock,·DeepMocked·}·from·'@golevelup/ts-jest'`
import {RolleService} from "../domain/rolle.service";

Check warning on line 4 in src/modules/rolle/api/provider.controller.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint / Nest Lint

Replace `RolleService}·from·"../domain/rolle.service"` with `·RolleService·}·from·'../domain/rolle.service'`

Check failure on line 4 in src/modules/rolle/api/provider.controller.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint / Nest Lint

Missing file extension "ts" for "../domain/rolle.service"
import {ServiceProviderByPersonIdBodyParams} from "./service-provider-by-person-id.body.params";

Check warning on line 5 in src/modules/rolle/api/provider.controller.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint / Nest Lint

Replace `ServiceProviderByPersonIdBodyParams}·from·"./service-provider-by-person-id.body.params"` with `·ServiceProviderByPersonIdBodyParams·}·from·'./service-provider-by-person-id.body.params'`

Check failure on line 5 in src/modules/rolle/api/provider.controller.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint / Nest Lint

Missing file extension "ts" for "./service-provider-by-person-id.body.params"
import {faker} from "@faker-js/faker";

Check warning on line 6 in src/modules/rolle/api/provider.controller.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint / Nest Lint

Replace `faker}·from·"@faker-js/faker"` with `·faker·}·from·'@faker-js/faker'`
import {ServiceProviderDo} from "../domain/service-provider.do";

Check warning on line 7 in src/modules/rolle/api/provider.controller.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint / Nest Lint

Replace `ServiceProviderDo}·from·"../domain/service-provider.do"` with `·ServiceProviderDo·}·from·'../domain/service-provider.do'`

Check failure on line 7 in src/modules/rolle/api/provider.controller.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint / Nest Lint

Missing file extension "ts" for "../domain/service-provider.do"

describe('ProviderController', () => {
let module: TestingModule;
let providerController: ProviderController;
let rolleServiceMock: DeepMocked<RolleService>;
let serviceProviderDo: DeepMocked<ServiceProviderDo<true>[]>;

beforeAll(async () => {
module = await Test.createTestingModule({
imports: [],
providers: [
ProviderController,
{
provide: RolleService,
useValue: createMock<RolleService>(),
},
{
provide: ServiceProviderDo,
useValue: createMock<ServiceProviderDo<true>[]>(),
},
],
}).compile();
providerController = module.get(ProviderController);
rolleServiceMock = module.get(RolleService);
serviceProviderDo = module.get(ServiceProviderDo);
});

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

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

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

describe('when getting result from service', () => {
it('should not throw', async () => {
const persondId: ServiceProviderByPersonIdBodyParams = {
personId: faker.string.alpha(),
};
rolleServiceMock.getAvailableServiceProviders.mockResolvedValue(serviceProviderDo);
await expect(providerController.getServiceProvidersByPersonId(persondId)).resolves.not.toThrow();
expect(rolleServiceMock.getAvailableServiceProviders).toHaveBeenCalledTimes(1);
});
});
});
3 changes: 3 additions & 0 deletions src/modules/rolle/domain/service-provider.do.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ export class ServiceProviderDo<WasPersisted extends boolean> implements DoBase<W

@AutoMap()
public url!: string;

@AutoMap()
public providedOnSchulstrukturknoten!: string;
}
87 changes: 87 additions & 0 deletions src/modules/rolle/persistence/service-provider.repo.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import {Test, TestingModule} from "@nestjs/testing";

Check warning on line 1 in src/modules/rolle/persistence/service-provider.repo.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint / Nest Lint

Replace `Test,·TestingModule}·from·"@nestjs/testing"` with `·Test,·TestingModule·}·from·'@nestjs/testing'`
import {EntityManager, MikroORM} from "@mikro-orm/core";
import {Mapper} from "@automapper/core";
import {ConfigTestModule, DatabaseTestModule, DoFactory, MapperTestModule} from "../../../../test/utils";

Check failure on line 4 in src/modules/rolle/persistence/service-provider.repo.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint / Nest Lint

Missing file extension "ts" for "../../../../test/utils"
import {getMapperToken} from "@automapper/nestjs";
import {ServiceProviderRepo} from "./service-provider.repo";

Check failure on line 6 in src/modules/rolle/persistence/service-provider.repo.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint / Nest Lint

Missing file extension "ts" for "./service-provider.repo"
import {ServiceProviderMapperProfile} from "./service-provider.mapper.profile";

Check failure on line 7 in src/modules/rolle/persistence/service-provider.repo.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint / Nest Lint

Missing file extension "ts" for "./service-provider.mapper.profile"
import {ServiceProviderZugriffDo} from "../domain/service-provider-zugriff.do";

Check failure on line 8 in src/modules/rolle/persistence/service-provider.repo.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint / Nest Lint

Missing file extension "ts" for "../domain/service-provider-zugriff.do"
import {ServiceProviderDo} from "../domain/service-provider.do";

Check failure on line 9 in src/modules/rolle/persistence/service-provider.repo.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint / Nest Lint

Missing file extension "ts" for "../domain/service-provider.do"
import {ServiceProviderEntity} from "./service-provider.entity";

Check failure on line 10 in src/modules/rolle/persistence/service-provider.repo.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint / Nest Lint

Missing file extension "ts" for "./service-provider.entity"
import {ServiceProviderZugriffEntity} from "./service-provider-zugriff.entity";
import {ServiceProviderZugriffMapperProfile} from "./service-provider-zugriff.mapper.profile";

describe('ServiceProviderRepo', () => {
let module: TestingModule;
let sut: ServiceProviderRepo;
let orm: MikroORM;
let em: EntityManager;
let mapper: Mapper;

beforeAll(async () => {
module = await Test.createTestingModule({
imports: [ConfigTestModule, DatabaseTestModule.forRoot({isDatabaseRequired: true}), MapperTestModule],
providers: [ServiceProviderMapperProfile, ServiceProviderZugriffMapperProfile, ServiceProviderRepo],
}).compile();
sut = module.get(ServiceProviderRepo);
orm = module.get(MikroORM);
em = module.get(EntityManager);
mapper = module.get(getMapperToken());
await DatabaseTestModule.setupDatabase(orm);
}, 30 * 1_000);

afterAll(async () => {
await module.close();
}, 30 * 1_000);

beforeEach(async () => {
await DatabaseTestModule.clearDatabase(orm);
});

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

describe('findById provided by ServiceProviderZugriffDo', () => {
describe('when found by id', () => {
it('should return found serviceProvider', async () => {
const props: Partial<ServiceProviderDo<false>> = {
id: '1'
};
const serviceProviderDo: ServiceProviderDo<false> = DoFactory.createServiceProvider(false, props);
const serviceProviderZugriffDo: ServiceProviderZugriffDo<false> = DoFactory.createServiceProviderZugriff(false, {serviceProvider: '123'});
await em.persistAndFlush(mapper.map(serviceProviderDo, ServiceProviderDo, ServiceProviderEntity));
await em.persistAndFlush(mapper.map(serviceProviderZugriffDo, ServiceProviderZugriffDo, ServiceProviderZugriffEntity));

const serviceProvider: ServiceProviderEntity[] = await em.find(ServiceProviderEntity, {});
expect(serviceProvider).not.toBeNull();
expect(serviceProvider).toHaveLength(1);
console.log(serviceProvider[0]);

const serviceProviderZugriff: ServiceProviderZugriffEntity[] = await em.find(ServiceProviderZugriffEntity, {});
expect(serviceProviderZugriff).not.toBeNull();
expect(serviceProviderZugriff).toHaveLength(1);
console.log(serviceProviderZugriff[0]);

const foundServiceProvider: ServiceProviderDo<true>[] = await sut.findAll(serviceProviderZugriffDo);
expect(foundServiceProvider).not.toBeNull();
expect(foundServiceProvider).toHaveLength(1);

/* const personDo: PersonDo<false> = DoFactory.createPerson(false, {referrer: faker.string.uuid()});
await em.persistAndFlush(mapper.map(personDo, PersonDo, PersonEntity));
const [person]: PersonEntity[] = await em.find(PersonEntity, {});
expect(person).toBeInstanceOf(PersonEntity);
const foundPerson: Option<PersonDo<true>> = await sut.findAll((person as PersonEntity).id);
expect(foundPerson).toBeInstanceOf(PersonDo);*/
});
});

describe('when not found by id provided by ServiceProviderZugriffDo', () => {
it('should return null', async () => {
const serviceProviderZugriffDo: ServiceProviderZugriffDo<true> = DoFactory.createServiceProviderZugriff(true);
const foundServiceProvider: Option<ServiceProviderDo<true>[]> = await sut.findAll(serviceProviderZugriffDo);
expect(foundServiceProvider).toHaveLength(0);
});
});
});
});
2 changes: 1 addition & 1 deletion src/modules/rolle/persistence/service-provider.repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { ServiceProviderEntity } from './service-provider.entity.js';
export class ServiceProviderRepo {
public constructor(private readonly em: EntityManager, @Inject(getMapperToken()) private readonly mapper: Mapper) {}

public async findAll(serviceProviderZugriff: ServiceProviderZugriffDo<true>): Promise<ServiceProviderDo<true>[]> {
public async findAll(serviceProviderZugriff: ServiceProviderZugriffDo<boolean>): Promise<ServiceProviderDo<true>[]> {
const query: Record<string, unknown> = {};
if (serviceProviderZugriff.serviceProvider) {
query['id'] = { $ilike: serviceProviderZugriff.serviceProvider };
Expand Down
30 changes: 30 additions & 0 deletions test/utils/do-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { OrganisationsTyp } from '../../src/modules/organisation/domain/organisa
import { UserDo } from '../../src/modules/keycloak-administration/domain/user.do.js';
import { PersonenkontextDo } from '../../src/modules/person/domain/personenkontext.do.js';
import { Rolle, Jahrgangsstufe, Personenstatus } from '../../src/modules/person/domain/personenkontext.enums.js';
import {ServiceProviderZugriffDo} from "../../src/modules/rolle/domain/service-provider-zugriff.do";
import {ServiceProviderDo} from "../../src/modules/rolle/domain/service-provider.do";

export class DoFactory {
public static createPerson<WasPersisted extends boolean>(
Expand Down Expand Up @@ -76,4 +78,32 @@ export class DoFactory {

return Object.assign(new PersonenkontextDo<WasPersisted>(), user, props);
}

public static createServiceProvider<WasPersisted extends boolean>(
withId: WasPersisted,
props?: Partial<ServiceProviderDo<WasPersisted>>,
): ServiceProviderDo<WasPersisted> {
const serviceProvider: ServiceProviderDo<false> = {
name: faker.internet.domainWord(),
url: faker.internet.url(),
providedOnSchulstrukturknoten: faker.string.numeric(),
id: '1',
createdAt: withId ? faker.date.past() : undefined,
updatedAt: withId ? faker.date.recent() : undefined,
};
return Object.assign(new ServiceProviderDo<WasPersisted>(), serviceProvider, props);
}

public static createServiceProviderZugriff<WasPersisted extends boolean>(
withId: WasPersisted,
props?: Partial<ServiceProviderZugriffDo<WasPersisted>>,
): ServiceProviderZugriffDo<WasPersisted> {
const serviceProviderZugriff: ServiceProviderZugriffDo<false> = {
serviceProvider: faker.string.numeric(),
id: withId ? faker.string.uuid() : undefined,
createdAt: withId ? faker.date.past() : undefined,
updatedAt: withId ? faker.date.recent() : undefined,
};
return Object.assign(new ServiceProviderZugriffDo<WasPersisted>(), serviceProviderZugriff, props);
}
}

0 comments on commit c20d2e3

Please sign in to comment.