Skip to content

Commit

Permalink
feat: wip implement change title
Browse files Browse the repository at this point in the history
  • Loading branch information
collettemathieu committed Sep 25, 2024
1 parent be3a086 commit 2223b8b
Show file tree
Hide file tree
Showing 32 changed files with 288 additions and 120 deletions.
4 changes: 2 additions & 2 deletions apps/pathway-design/server/cucumber.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module.exports = {
default: {
paths: ['apps/pathway-design/server/src/test/**/*.feature'],
paths: ['apps/pathway-design/server/src/**/*.feature'],
requireModule: ['ts-node/register', 'tsconfig-paths/register'],
require: ['apps/pathway-design/server/src/test/**/*.step.ts'],
require: ['apps/pathway-design/server/src/**/*.step.ts'],
format: [
'json:dist/reports/apps/pathway-design/server/test-feature/index.json',
'html:dist/reports/apps/pathway-design/server/test-feature/index.html',
Expand Down
7 changes: 1 addition & 6 deletions apps/pathway-design/server/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { Module } from '@nestjs/common';

import { PDSPIInitializePathwayPersistenceInfrastructureModule } from '@bewoak/pathway-design-server-pathway-infrastructure';
import { PDSPIAInitializePathwayInterfaceAdaptersModule } from '@bewoak/pathway-design-server-pathway-interface-adapters';
import { PDSPPPathwayPresentersModule } from '@bewoak/pathway-design-server-pathway-presenters';
import { CqrsModule } from '@nestjs/cqrs';

@Module({
imports: [
PDSPIAInitializePathwayInterfaceAdaptersModule.withPersistence(
PDSPIInitializePathwayPersistenceInfrastructureModule.use('inMemory')
)
.withPresenter(PDSPPPathwayPresentersModule.use('toJson'))
.build(),
PDSPIAInitializePathwayInterfaceAdaptersModule.withPresenter(PDSPPPathwayPresentersModule.use('toJson')).build(),
CqrsModule.forRoot(),
],
controllers: [],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Feature: Change the title of a pathway in a memory database with json presenter

Scenario: I want to change the title of a learning pathway
Given I am authenticated on the platform for change the title of the pathway in memory persistence and json presenter
Given I have a pathway recorded in memory with these data
| title | description | researchField |
| My Pathway | A test pathway | biology |
When I want to change the title of the pathway in memory to "My New Pathway"
Then I should receive from memory the new title of the pathway
| title |
| My New Pathway |

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { strict as assert } from 'node:assert';
import type { Http2Server } from 'node:http2';
import {
PDSPIAChangeTitlePathwayInterfaceAdaptersModule,
PDSPIAInitializePathwayInterfaceAdaptersModule,
} from '@bewoak/pathway-design-server-pathway-interface-adapters';
import { PDSPPPathwayPresentersModule } from '@bewoak/pathway-design-server-pathway-presenters';
import type { DataTable } from '@cucumber/cucumber';
import type { INestApplication } from '@nestjs/common';
import { CqrsModule } from '@nestjs/cqrs';
import { Test } from '@nestjs/testing';
import { binding, given, then, when } from 'cucumber-tsflow';
import request from 'supertest';

@binding()
class ControllerSteps {
private app: INestApplication;
private httpServer: Http2Server;
private response: request.Response;

@given('I am authenticated on the platform for change the title of the pathway in memory persistence and json presenter')
public async withInMemoryPeristenceAndJsonPresenter() {
const testingModule = await Test.createTestingModule({
imports: [
PDSPIAChangeTitlePathwayInterfaceAdaptersModule.withPresenter(PDSPPPathwayPresentersModule.use('toJson')).build(),
PDSPIAInitializePathwayInterfaceAdaptersModule.withPresenter(PDSPPPathwayPresentersModule.use('toJson')).build(),
CqrsModule.forRoot(),
],
}).compile();

this.app = testingModule.createNestApplication();
await this.app.init();
this.httpServer = this.app.getHttpServer();
}

@given('I have a pathway recorded in memory with these data')
public async givenIHaveAPathwayRecordedInMemroy(dataTable: DataTable) {
const firstRow = dataTable.hashes()[0];

this.response = await request(this.httpServer).post('/pathway/init').send({
title: firstRow.title,
description: firstRow.description,
researchField: firstRow.researchField,
});
}

@when('I want to change the title of the pathway in memory to {string}')
public async whenIChangeTheTitleOfThePathwayTo(title: string) {
this.response = await request(this.httpServer).patch(`/pathway/change-title/${this.response.body.id}`).send({
title,
});
}

@then('I should receive from memory the new title of the pathway')
public async thenIShouldReceiveTheNewTitleOfThePathway(dataTable: DataTable) {
const firstRow = dataTable.hashes()[0];

assert.strictEqual(this.response.body.title, firstRow.title);
}
}

export = ControllerSteps;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Feature: Initialize Pathway in a memory database
Feature: Initialize Pathway in a memory database with json presenter

Scenario: I want to initialize a learning pathway
Given I am authenticated on the platform
Given I am authenticated on the platform for initialize a pathway in memory persistence and json presenter
When I want to initialize a pathway with these data
| title | description | researchField |
| My Pathway | A test pathway | biology |
Expand All @@ -10,13 +10,4 @@ Feature: Initialize Pathway in a memory database
| My Pathway | A test pathway | biology |
Then The pathway should be have a unique identifier

Scenario: I want to initialize another learning pathway with different data
Given I am authenticated on the platform
When I want to initialize a pathway with these data
| title | description | researchField |
| Arterial stiffness | Understand the role of the arterial stiffness | biomedical |
Then I should retrieve a pathway initialized with its data
| title | description | researchField |
| Arterial stiffness | Understand the role of the arterial stiffness | biomedical |
Then The pathway should be have a unique identifier

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { strict as assert } from 'node:assert';
import type { Http2Server } from 'node:http2';
import { PDSPIInitializePathwayPersistenceInfrastructureModule } from '@bewoak/pathway-design-server-pathway-infrastructure';
import { PDSPIAInitializePathwayInterfaceAdaptersModule } from '@bewoak/pathway-design-server-pathway-interface-adapters';
import { PDSPPPathwayPresentersModule } from '@bewoak/pathway-design-server-pathway-presenters';
import type { DataTable } from '@cucumber/cucumber';
Expand All @@ -13,23 +12,19 @@ import request from 'supertest';
@binding()
class ControllerSteps {
private app: INestApplication;
private response: request.Response;
private httpServer: Http2Server;
private response: request.Response;

@given('I am authenticated on the platform')
public async givenAmIAuthenticatedOnThePlatform() {
const module = await Test.createTestingModule({
@given('I am authenticated on the platform for initialize a pathway in memory persistence and json presenter')
public async withInMemoryPeristenceAndJsonPresenter() {
const testingModule = await Test.createTestingModule({
imports: [
PDSPIAInitializePathwayInterfaceAdaptersModule.withPersistence(
PDSPIInitializePathwayPersistenceInfrastructureModule.use('inMemory')
)
.withPresenter(PDSPPPathwayPresentersModule.use('toJson'))
.build(),
PDSPIAInitializePathwayInterfaceAdaptersModule.withPresenter(PDSPPPathwayPresentersModule.use('toJson')).build(),
CqrsModule.forRoot(),
],
}).compile();

this.app = module.createNestApplication();
this.app = testingModule.createNestApplication();
await this.app.init();
this.httpServer = this.app.getHttpServer();
}
Expand Down
5 changes: 5 additions & 0 deletions libs/pathway-design/server/pathway/application/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ export { PDSPAInitializePathwayCommand } from './lib/initialize/command/initiali
export { PDSPAInitializePathwayCommandHandler } from './lib/initialize/command/initialize-pathway.command-handler';
export { PDSPAInitializePathwayService } from './lib/initialize/service/initialize-pathway.service';
export { PDSPAIUInitializePathwayUsecase } from './lib/initialize/usecase/initialize-pathway.usecase';

export { PDSPAChangeTitlePathwayCommand } from './lib/change-title/command/change-title-pathway.command';
export { PDSPAChangeTitlePathwayCommandHandler } from './lib/change-title/command/change-title-pathway.command-handler';
export { PDSPAChangeTitlePathwayService } from './lib/change-title/service/change-title-pathway.service';
export { PDSPACUChangeTitlePathwayUseCase } from './lib/change-title/usecase/change-title-pathway.usecase';
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class PDSPAChangeTitlePathwayCommandHandler implements ICommandHandler<PD
this.pDSPBPPathwayPresenterPort,
this.eventPublisher,
{
pathwayId: pDSPAChangeTitlePathwayCommand.pathwayId,
pathwayId: pDSPAChangeTitlePathwayCommand.pathwayId, // Il faut changer le nom de la propriété pathway en pathwayId
title: pDSPAChangeTitlePathwayCommand.title,
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,25 @@ import sinon from 'sinon';
import { PDSPACUChangeTitlePathwayUseCase } from '../usecase/change-title-pathway.usecase';

class FakeChangeTitlePathwayPersistence implements PDSPBPChangeTitlePathwayPersistencePort {
changeTitle(pDSPBEPathwayEntity: PDSPBEPathwayEntity, title: string) {
private pDSPBEPathwayEntity: PDSPBEPathwayEntity | undefined;

save(pDSPBEPathwayEntity: PDSPBEPathwayEntity) {
this.pDSPBEPathwayEntity = pDSPBEPathwayEntity;
}

changeTitle(pathwayId: string, title: string) {
if (this.pDSPBEPathwayEntity === undefined) {
throw new Error('Pathway is not initialized');
}

if (this.pDSPBEPathwayEntity.id !== pathwayId) {
throw new Error('Pathway id does not match');
}

const pathwayWithTitleChanged = pDSPBFPathwayFactory({
description: pDSPBEPathwayEntity.description,
id: pDSPBEPathwayEntity.id,
researchField: pDSPBEPathwayEntity.researchField,
description: this.pDSPBEPathwayEntity.description,
id: this.pDSPBEPathwayEntity.id,
researchField: this.pDSPBEPathwayEntity.researchField,
title,
});
return Promise.resolve(pathwayWithTitleChanged);
Expand Down Expand Up @@ -78,6 +92,8 @@ export default class ControllerSteps {
description: firstRow.description,
researchField: firstRow.researchField,
});

this.fakeChangeTitlePathwayPersistence.save(this.pDSPBEPathwayEntity);
}

@when('I want to change the title of the pathway in application to {string}')
Expand All @@ -91,7 +107,7 @@ export default class ControllerSteps {
this.fakePathwayPresenter,
this.fakeEventPublisher as EventPublisher,
{
pathway: this.pDSPBEPathwayEntity,
pathwayId: this.pDSPBEPathwayEntity.id,
title,
}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type {
PDSPBEPathwayEntity,
PDSPBPChangeTitlePathwayPersistencePort,
PDSPBPPathwayPresenterPort,
} from '@bewoak/pathway-design-server-pathway-business';
Expand All @@ -11,17 +10,17 @@ export class PDSPACUChangeTitlePathwayUseCase {
pDSPBPPathwayPresenterPort: PDSPBPPathwayPresenterPort,
eventPublisher: EventPublisher,
{
pathway,
pathwayId,
title,
}: {
pathway: PDSPBEPathwayEntity;
pathwayId: string;
title: string;
}
) {
const pathwayFromPersistence = await pDSPBPChangeTitlePathwayPersistencePort.changeTitle(pathway, title);
const pathwayFromPersistence = await pDSPBPChangeTitlePathwayPersistencePort.changeTitle(pathwayId, title);

eventPublisher.mergeObjectContext(pathway);
pathway.commit();
eventPublisher.mergeObjectContext(pathwayFromPersistence);
pathwayFromPersistence.commit();

return pDSPBPPathwayPresenterPort.present(pathwayFromPersistence);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { strict as assert } from 'node:assert';
import type {
PDSPBEPathwayEntity,
PDSPBPInitializePathwayPersistencePort,
Expand All @@ -7,7 +8,6 @@ import type {
import type { DataTable } from '@cucumber/cucumber';
import type { EventPublisher } from '@nestjs/cqrs';
import { before, binding, then, when } from 'cucumber-tsflow';
import { strict as assert } from 'node:assert';
import sinon from 'sinon';
import { PDSPAIUInitializePathwayUsecase } from '../usecase/initialize-pathway.usecase';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { PDSPBEPathwayEntity } from '../../../entities/pathway';

export interface PDSPBPChangeTitlePathwayPersistencePort {
changeTitle: (pDSPBEPathwayEntity: PDSPBEPathwayEntity, title: string) => Promise<PDSPBEPathwayEntity>;
changeTitle: (pathwayId: string, title: string) => Promise<PDSPBEPathwayEntity>;
}

export const PDSPBP_CHANGE_TITLE_PATHWAY_PERSISTENCE_PORT = Symbol('PDSPBPChangeTitlePathwayPersistencePort');
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { strict as assert } from 'node:assert';
import type { DataTable } from '@cucumber/cucumber';
import { binding, given, then, when } from 'cucumber-tsflow';
import { strict as assert } from 'node:assert';
import sinon from 'sinon';
import type { PDSPBEPathwayEntity } from '../../entities/pathway';
import { PDSPBEPathwayTitleChangedEvent } from '../../events/pathway-title-changed.event';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { strict as assert } from 'node:assert';
import type { DataTable } from '@cucumber/cucumber';
import { binding, given, then, when } from 'cucumber-tsflow';
import { strict as assert } from 'node:assert';
import sinon from 'sinon';
import { PDSPBEPathwayEntity } from '../../entities/pathway';
import { PDSPBEPathwayInitializedEvent } from '../../events/pathway-initialized.event';
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { PDSPIInitializePathwayPersistenceInfrastructureModule } from './lib/persistence/initialize/initialize-pathway-persistence-infrastructure.module';
export { PDSPIPPathwayPersistenceInfrastructureModule } from './lib/persistence/pathway-persistence-infrastructure.module';

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { PDSPBP_CHANGE_TITLE_PATHWAY_PERSISTENCE_PORT } from '@bewoak/pathway-design-server-pathway-business';
import { Module } from '@nestjs/common';
import { PathwayInMemoryRepository } from '../../common/in-memory/repositories/in-memory-pathway.repository';
import { ChangeTitlePathwayInMemoryPersistence } from './change-title-pathway-in-memory.persistence';

@Module({
imports: [],
providers: [
PathwayInMemoryRepository,
ChangeTitlePathwayInMemoryPersistence,
{
provide: PDSPBP_CHANGE_TITLE_PATHWAY_PERSISTENCE_PORT,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { beforeEach, describe, expect, spyOn, test } from 'bun:test';
import { type PDSPBEPathwayEntity, pDSPBFPathwayFactory } from '@bewoak/pathway-design-server-pathway-business';
import { NotFoundException } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { beforeEach, describe, expect, spyOn, test } from 'bun:test';
import { PathwayInMemoryRepository } from '../../common/in-memory/repositories/in-memory-pathway.repository';
import { ChangeTitlePathwayInMemoryPersistence } from './change-title-pathway-in-memory.persistence';

Expand Down Expand Up @@ -36,13 +36,13 @@ describe('ChangeTitlePathwayInMemoryPersistence', () => {
spyOn(pathwayInMemoryRepository, 'patch');
spyOn(pathwayInMemoryRepository, 'get');

result = await changeTitlePathwayInMemoryPersistence.changeTitle(pDSPBEPathwayEntity, newTitle);
result = await changeTitlePathwayInMemoryPersistence.changeTitle(pDSPBEPathwayEntity.id, newTitle);
});

test('should call the change title method with the title of the pathway in parameter', () => {
expect(changeTitlePathwayInMemoryPersistence).toBeDefined();
expect(changeTitlePathwayInMemoryPersistence.changeTitle).toHaveBeenCalledWith(
pDSPBEPathwayEntity,
pDSPBEPathwayEntity.id,
'new pathway title'
);
});
Expand Down Expand Up @@ -82,7 +82,7 @@ describe('ChangeTitlePathwayInMemoryPersistence', () => {

test('should throw an error', async () => {
try {
await changeTitlePathwayInMemoryPersistence.changeTitle(pDSPBEPathwayEntity, newTitle);
await changeTitlePathwayInMemoryPersistence.changeTitle(pDSPBEPathwayEntity.id, newTitle);
} catch (error) {
expect(error).toBeInstanceOf(NotFoundException);
expect((error as NotFoundException).message).toMatch(
Expand Down Expand Up @@ -124,7 +124,7 @@ describe('ChangeTitlePathwayInMemoryPersistence', () => {

test('should throw an error', async () => {
try {
await changeTitlePathwayInMemoryPersistence.changeTitle(pDSPBEPathwayEntity, newTitle);
await changeTitlePathwayInMemoryPersistence.changeTitle(pDSPBEPathwayEntity.id, newTitle);
} catch (error) {
expect(error).toBeInstanceOf(NotFoundException);
expect((error as NotFoundException).message).toBe('Pathway not found in memory');
Expand Down
Loading

0 comments on commit 2223b8b

Please sign in to comment.