Skip to content

Commit

Permalink
add test cases and services
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechGrancow committed Oct 27, 2023
1 parent 5838b81 commit 7df7c23
Show file tree
Hide file tree
Showing 32 changed files with 1,340 additions and 195 deletions.
49 changes: 0 additions & 49 deletions apps/server/src/apps/deletion.app.ts

This file was deleted.

3 changes: 1 addition & 2 deletions apps/server/src/config/database.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ interface GlobalConstants {
DB_URL: string;
DB_PASSWORD?: string;
DB_USERNAME?: string;
DEL_DB_URL: string;
}

const usedGlobals: GlobalConstants = globals;

/** Database URL */
export const { DB_URL, DB_PASSWORD, DB_USERNAME, DEL_DB_URL } = usedGlobals;
export const { DB_URL, DB_PASSWORD, DB_USERNAME } = usedGlobals;
57 changes: 29 additions & 28 deletions apps/server/src/modules/deletion/deletion.module.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
import { Module, NotFoundException } from '@nestjs/common';
import { DB_PASSWORD, DB_USERNAME, DEL_DB_URL } from '@src/config';
import { CoreModule } from '@src/core';
import { Module } from '@nestjs/common';
// import { Module, NotFoundException } from '@nestjs/common';
// import { DB_PASSWORD, DB_USERNAME, DEL_DB_URL } from '@src/config';
// import { CoreModule } from '@src/core';
import { Logger } from '@src/core/logger';
import { MikroOrmModule, MikroOrmModuleSyncOptions } from '@mikro-orm/nestjs';
import { AuthenticationModule } from '@src/modules/authentication/authentication.module';
import { AuthorizationModule } from '@src/modules';
import { RabbitMQWrapperTestModule } from '@shared/infra/rabbitmq';
import { Dictionary, IPrimaryKey } from '@mikro-orm/core';
// import { MikroOrmModule, MikroOrmModuleSyncOptions } from '@mikro-orm/nestjs';
// import { AuthenticationModule } from '@src/modules/authentication/authentication.module';
// import { AuthorizationModule } from '@src/modules';
// import { RabbitMQWrapperTestModule } from '@shared/infra/rabbitmq';
// import { Dictionary, IPrimaryKey } from '@mikro-orm/core';
import { DeletionRequestService } from './services/deletion-request.service';
import { DeletionRequestRepo } from './repo/deletion-request.repo';

const defaultMikroOrmOptions: MikroOrmModuleSyncOptions = {
findOneOrFailHandler: (entityName: string, where: Dictionary | IPrimaryKey) =>
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
new NotFoundException(`The requested ${entityName}: ${where} has not been found.`),
};
// const defaultMikroOrmOptions: MikroOrmModuleSyncOptions = {
// findOneOrFailHandler: (entityName: string, where: Dictionary | IPrimaryKey) =>
// // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
// new NotFoundException(`The requested ${entityName}: ${where} has not been found.`),
// };

@Module({
imports: [
AuthorizationModule,
AuthenticationModule,
CoreModule,
RabbitMQWrapperTestModule,
MikroOrmModule.forRoot({
...defaultMikroOrmOptions,
type: 'mongo',
clientUrl: DEL_DB_URL,
password: DB_PASSWORD,
user: DB_USERNAME,
entities: [],
}),
// ConfigModule.forRoot(createConfigModuleOptions(config)),
],
// imports: [
// AuthorizationModule,
// AuthenticationModule,
// CoreModule,
// RabbitMQWrapperTestModule,
// MikroOrmModule.forRoot({
// ...defaultMikroOrmOptions,
// type: 'mongo',
// clientUrl: DEL_DB_URL,
// password: DB_PASSWORD,
// user: DB_USERNAME,
// entities: [],
// }),
// ConfigModule.forRoot(createConfigModuleOptions(config)),
// ],
providers: [Logger, DeletionRequestService, DeletionRequestRepo],
})
export class DeletionModule {}
64 changes: 64 additions & 0 deletions apps/server/src/modules/deletion/domain/deletion-log.do.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { ObjectId } from '@mikro-orm/mongodb';
import { deletionLogFactory } from './testing/factory/deletion-log.factory';
import { DeletionLog } from './deletion-log.do';
import { DeletionOperationModel } from './types/deletion-operation-model.enum';
import { DeletionDomainModel } from './types/deletion-domain-model.enum';

describe(DeletionLog.name, () => {
describe('constructor', () => {
describe('When constructor is called', () => {
it('should create a deletionRequest by passing required properties', () => {
const domainObject: DeletionLog = deletionLogFactory.build();

expect(domainObject instanceof DeletionLog).toEqual(true);
});
});

describe('when passed a valid id', () => {
const setup = () => {
const domainObject: DeletionLog = deletionLogFactory.buildWithId();

return { domainObject };
};

it('should set the id', () => {
const { domainObject } = setup();

const deletionLogDomainObject: DeletionLog = new DeletionLog(domainObject);

expect(deletionLogDomainObject.id).toEqual(domainObject.id);
});
});
});

describe('getters', () => {
describe('When getters are used', () => {
it('getters should return proper values', () => {
const props = {
id: new ObjectId().toHexString(),
domain: DeletionDomainModel.USER,
operation: DeletionOperationModel.DELETE,
modifiedCounter: 0,
deletedCounter: 1,
deletionRequestId: new ObjectId().toHexString(),
createdAt: new Date(),
updatedAt: new Date(),
};

const deletionLogDo = new DeletionLog(props);
const gettersValues = {
id: deletionLogDo.id,
domain: deletionLogDo.domain,
operation: deletionLogDo.operation,
modifiedCounter: deletionLogDo.modifiedCounter,
deletedCounter: deletionLogDo.deletedCounter,
deletionRequestId: deletionLogDo.deletionRequestId,
createdAt: deletionLogDo.createdAt,
updatedAt: deletionLogDo.updatedAt,
};

expect(gettersValues).toEqual(props);
});
});
});
});
27 changes: 17 additions & 10 deletions apps/server/src/modules/deletion/domain/deletion-log.do.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { EntityId } from '@shared/domain/types';
import { AuthorizableObject, DomainObject } from '@shared/domain/domain-object';
import { DeletionDomainModel } from './types/deletion-domain-model.enum';
import { DeletionOperationModel } from './types/deletion-operation-model.enum';

export interface DeletionLogProps extends AuthorizableObject {
createdAt: Date;
updatedAt: Date;
scope?: string;
operation?: string;
docIds?: EntityId[];
domain?: DeletionDomainModel;
operation?: DeletionOperationModel;
modifiedCounter?: number;
deletedCounter?: number;
deletionRequestId?: EntityId;
}

Expand All @@ -19,19 +22,23 @@ export class DeletionLog extends DomainObject<DeletionLogProps> {
return this.props.updatedAt;
}

get scope(): string | undefined {
return this.props.scope;
get domain(): DeletionDomainModel | undefined {
return this.props.domain;
}

get operation(): string | undefined {
get operation(): DeletionOperationModel | undefined {
return this.props.operation;
}

get deletionRequestId(): EntityId | undefined {
return this.props.deletionRequestId;
get modifiedCounter(): number | undefined {
return this.props.modifiedCounter;
}

get deletedCounter(): number | undefined {
return this.props.deletedCounter;
}

get docIds(): EntityId[] | undefined {
return this.props.docIds;
get deletionRequestId(): EntityId | undefined {
return this.props.deletionRequestId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { ObjectId } from '@mikro-orm/mongodb';
import { DeletionRequest } from './deletion-request.do';
import { DeletionDomainModel } from './types/deletion-domain-model.enum';
import { deletionRequestFactory } from './testing/factory/deletion-request.factory';
import { DeletionStatusModel } from './types/deletion-status-model.enum';

describe(DeletionRequest.name, () => {
describe('constructor', () => {
describe('When constructor is called', () => {
it('should create a deletionRequest by passing required properties', () => {
const domainObject: DeletionRequest = deletionRequestFactory.build();

expect(domainObject instanceof DeletionRequest).toEqual(true);
});
});

describe('when passed a valid id', () => {
const setup = () => {
const domainObject: DeletionRequest = deletionRequestFactory.buildWithId();

return { domainObject };
};

it('should set the id', () => {
const { domainObject } = setup();

const deletionRequestDomainObject: DeletionRequest = new DeletionRequest(domainObject);

expect(deletionRequestDomainObject.id).toEqual(domainObject.id);
});
});
});

describe('getters', () => {
describe('When getters are used', () => {
it('getters should return proper values', () => {
const props = {
id: new ObjectId().toHexString(),
domain: DeletionDomainModel.USER,
deleteAfter: new Date(),
itemId: new ObjectId().toHexString(),
status: DeletionStatusModel.REGISTERED,
createdAt: new Date(),
updatedAt: new Date(),
};

const deletionRequestDo = new DeletionRequest(props);
const gettersValues = {
id: deletionRequestDo.id,
domain: deletionRequestDo.domain,
deleteAfter: deletionRequestDo.deleteAfter,
itemId: deletionRequestDo.itemId,
status: deletionRequestDo.status,
createdAt: deletionRequestDo.createdAt,
updatedAt: deletionRequestDo.updatedAt,
};

expect(gettersValues).toEqual(props);
});
});
});
});
19 changes: 13 additions & 6 deletions apps/server/src/modules/deletion/domain/deletion-request.do.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { EntityId } from '@shared/domain/types';
import { AuthorizableObject, DomainObject } from '@shared/domain/domain-object';
import { DeletionDomainModel } from './types/deletion-domain-model.enum';
import { DeletionStatusModel } from './types/deletion-status-model.enum';

export interface DeletionRequestProps extends AuthorizableObject {
createdAt?: Date;
updatedAt?: Date;
source?: string;
domain?: DeletionDomainModel;
deleteAfter?: Date;
userId?: EntityId;
itemId?: EntityId;
status?: DeletionStatusModel;
}

export class DeletionRequest extends DomainObject<DeletionRequestProps> {
Expand All @@ -18,15 +21,19 @@ export class DeletionRequest extends DomainObject<DeletionRequestProps> {
return this.props.updatedAt;
}

get source(): string | undefined {
return this.props.source;
get domain(): DeletionDomainModel | undefined {
return this.props.domain;
}

get deleteAfter(): Date | undefined {
return this.props.deleteAfter;
}

get userId(): EntityId | undefined {
return this.props.userId;
get itemId(): EntityId | undefined {
return this.props.itemId;
}

get status(): DeletionStatusModel | undefined {
return this.props.status;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { DoBaseFactory } from '@shared/testing';
import { ObjectId } from '@mikro-orm/mongodb';
import { DeletionLog, DeletionLogProps } from '../../deletion-log.do';
import { DeletionOperationModel } from '../../types/deletion-operation-model.enum';
import { DeletionDomainModel } from '../../types/deletion-domain-model.enum';

export const deletionLogFactory = DoBaseFactory.define<DeletionLog, DeletionLogProps>(DeletionLog, () => {
return {
id: new ObjectId().toHexString(),
domain: DeletionDomainModel.USER,
operation: DeletionOperationModel.DELETE,
modifiedCounter: 0,
deletedCounter: 1,
deletionRequestId: new ObjectId().toHexString(),
createdAt: new Date(),
updatedAt: new Date(),
};
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { DoBaseFactory } from '@shared/testing';
import { ObjectId } from '@mikro-orm/mongodb';
import { DeepPartial } from 'fishery';
import { DeletionRequest, DeletionRequestProps } from '../../deletion-request.do';
import { DeletionDomainModel } from '../../types/deletion-domain-model.enum';
import { DeletionStatusModel } from '../../types/deletion-status-model.enum';

class DeletionRequestFactory extends DoBaseFactory<DeletionRequest, DeletionRequestProps> {
withUserIds(itemId: string): this {
const params: DeepPartial<DeletionRequestProps> = {
itemId,
};

return this.params(params);
}
}

export const deletionRequestFactory = DeletionRequestFactory.define(DeletionRequest, () => {
return {
id: new ObjectId().toHexString(),
domain: DeletionDomainModel.USER,
deleteAfter: new Date(),
itemId: new ObjectId().toHexString(),
status: DeletionStatusModel.REGISTERED,
createdAt: new Date(),
updatedAt: new Date(),
};
});
Loading

0 comments on commit 7df7c23

Please sign in to comment.