Skip to content

Commit

Permalink
fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
virgilchiriac committed Jan 20, 2025
1 parent 7a4d942 commit 979f793
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ export class DeletionRequestService {

async findAllItemsToExecute(limit: number): Promise<DeletionRequest[]> {
const deletionRequests = await this.deletionRequestRepo.findAllItemsToExecution(
limit,
this.olderThan,
this.newerThan,
limit
this.newerThan
);

return deletionRequests;
Expand Down
85 changes: 55 additions & 30 deletions apps/server/src/modules/deletion/repo/deletion-request.repo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,25 +105,35 @@ describe(DeletionRequestRepo.name, () => {
describe('findAllItemsToExecution', () => {
describe('when there is no deletionRequest for execution', () => {
const setup = () => {
const threshold = 1000;
const limit = 1000;
const olderThan = new Date();
const newerThan = new Date();

return {
threshold,
limit,
olderThan,
newerThan,
};
};
it('should return empty array', async () => {
const { threshold } = setup();
const result = await repo.findAllItemsToExecution(threshold);
const { limit, olderThan, newerThan } = setup();
const result = await repo.findAllItemsToExecution(limit, olderThan, newerThan);

expect(result).toEqual([]);
});
});

describe('when there are deletionRequests for execution', () => {
const setup = async () => {
const threshold = 1000;
const limit = 1000;
const olderThan = new Date();
olderThan.setDate(olderThan.getDate() + 1);
const newerThan = new Date();
newerThan.setDate(newerThan.getDate() - 1);

const dateInFuture = new Date();
dateInFuture.setDate(dateInFuture.getDate() + 30);

const deletionRequestEntity1: DeletionRequestEntity = deletionRequestEntityFactory.build({
createdAt: new Date(2023, 7, 1),
updatedAt: new Date(2023, 8, 2),
Expand All @@ -140,14 +150,17 @@ describe(DeletionRequestRepo.name, () => {
createdAt: new Date(2023, 8, 1),
updatedAt: new Date(2023, 8, 1),
deleteAfter: new Date(2023, 9, 1),
status: StatusModel.REGISTERED,
});
const deletionRequestEntity4: DeletionRequestEntity = deletionRequestEntityFactory.build({
createdAt: new Date(2023, 9, 1),
updatedAt: new Date(2023, 9, 1),
deleteAfter: new Date(2023, 10, 1),
status: StatusModel.REGISTERED,
});
const deletionRequestEntity5: DeletionRequestEntity = deletionRequestEntityFactory.build({
deleteAfter: dateInFuture,
status: StatusModel.REGISTERED,
});

await em.persistAndFlush([
Expand Down Expand Up @@ -178,34 +191,22 @@ describe(DeletionRequestRepo.name, () => {
createdAt: deletionRequestEntity3.createdAt,
updatedAt: deletionRequestEntity3.updatedAt,
},
{
id: deletionRequestEntity2.id,
targetRefDomain: deletionRequestEntity2.targetRefDomain,
deleteAfter: deletionRequestEntity2.deleteAfter,
targetRefId: deletionRequestEntity2.targetRefId,
status: deletionRequestEntity2.status,
createdAt: deletionRequestEntity2.createdAt,
updatedAt: deletionRequestEntity2.updatedAt,
},
];

return { deletionRequestEntity1, deletionRequestEntity5, expectedArray, threshold };
return { deletionRequestEntity1, deletionRequestEntity5, expectedArray, limit, olderThan, newerThan };
};

it('should find deletionRequests with deleteAfter smaller then today and status with value registered or failed', async () => {
const { deletionRequestEntity1, deletionRequestEntity5, expectedArray, threshold } = await setup();
it('should find deletionRequests with deleteAfter smaller then today and status with value registered', async () => {
const { deletionRequestEntity1, deletionRequestEntity5, expectedArray, limit, olderThan, newerThan } =
await setup();

const results = await repo.findAllItemsToExecution(threshold);
const results = await repo.findAllItemsToExecution(limit, olderThan, newerThan);

expect(results.length).toEqual(3);
expect(results.length).toEqual(2);

// Verify explicit fields.
expect(results).toEqual(
expect.arrayContaining([
expect.objectContaining(expectedArray[0]),
expect.objectContaining(expectedArray[1]),
expect.objectContaining(expectedArray[2]),
])
expect.arrayContaining([expect.objectContaining(expectedArray[0]), expect.objectContaining(expectedArray[1])])
);

const result1: DeletionRequest = await repo.findById(deletionRequestEntity1.id);
Expand All @@ -218,9 +219,9 @@ describe(DeletionRequestRepo.name, () => {
});

it('should find deletionRequests to execute with limit = 2', async () => {
const { expectedArray, threshold } = await setup();
const { expectedArray, olderThan, newerThan } = await setup();

const results = await repo.findAllItemsToExecution(threshold, 2);
const results = await repo.findAllItemsToExecution(2, olderThan, newerThan);

expect(results.length).toEqual(2);

Expand All @@ -234,15 +235,39 @@ describe(DeletionRequestRepo.name, () => {

describe('countPendingDeletionRequests', () => {
describe('when there is no deletionRequest with status pending', () => {
const setup = async () => {
const olderThan = new Date();
olderThan.setDate(olderThan.getDate() + 1);
const newerThan = new Date();
newerThan.setDate(newerThan.getDate() - 1);
const deletionRequestWithStatusRegistered: DeletionRequestEntity[] =
deletionRequestEntityFactory.buildListWithId(5, {
status: StatusModel.REGISTERED,
});

await em.persistAndFlush(deletionRequestWithStatusRegistered);
em.clear();

return {
olderThan,
newerThan,
};
};
it('should return zero', async () => {
const result = await repo.countPendingDeletionRequests();
const { olderThan, newerThan } = await setup();
const result = await repo.countPendingDeletionRequests(olderThan, newerThan);

expect(result).toEqual(0);
});
});

describe('when there are deletionRequests with status pending', () => {
const setup = async () => {
const olderThan = new Date();
olderThan.setDate(olderThan.getDate() + 1);
const newerThan = new Date();
newerThan.setDate(newerThan.getDate() - 1);

const deletionRequestWithStatusPending: DeletionRequestEntity[] = deletionRequestEntityFactory.buildListWithId(
5,
{
Expand All @@ -260,13 +285,13 @@ describe(DeletionRequestRepo.name, () => {
await em.persistAndFlush([...deletionRequestWithStatusPending, ...deletionRequestWithStatusRegistered]);
em.clear();

return { expectedNumberDeletionRequestWithStatusPending };
return { expectedNumberDeletionRequestWithStatusPending, olderThan, newerThan };
};

it('should count deletionRequests with status pending and return proper number', async () => {
const { expectedNumberDeletionRequestWithStatusPending } = await setup();
const { expectedNumberDeletionRequestWithStatusPending, olderThan, newerThan } = await setup();

const result = await repo.countPendingDeletionRequests();
const result = await repo.countPendingDeletionRequests(olderThan, newerThan);

expect(result).toEqual(expectedNumberDeletionRequestWithStatusPending);
});
Expand Down
25 changes: 12 additions & 13 deletions apps/server/src/modules/deletion/repo/deletion-request.repo.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { EntityManager } from '@mikro-orm/mongodb';
import { Injectable } from '@nestjs/common';
import { SortOrder } from '@shared/domain/interface';
import { EntityId } from '@shared/domain/types';
import { DeletionRequest } from '../domain/do';
import { DeletionRequestEntity } from './entity';
import { DeletionRequestMapper } from './mapper';
import { DeletionRequestScope } from './scope';
import { StatusModel } from '../domain/types';
import {EntityManager} from '@mikro-orm/mongodb';

Check failure on line 1 in apps/server/src/modules/deletion/repo/deletion-request.repo.ts

View workflow job for this annotation

GitHub Actions / nest_lint

Replace `EntityManager` with `·EntityManager·`
import {Injectable} from '@nestjs/common';

Check failure on line 2 in apps/server/src/modules/deletion/repo/deletion-request.repo.ts

View workflow job for this annotation

GitHub Actions / nest_lint

Replace `Injectable` with `·Injectable·`
import {SortOrder} from '@shared/domain/interface';

Check failure on line 3 in apps/server/src/modules/deletion/repo/deletion-request.repo.ts

View workflow job for this annotation

GitHub Actions / nest_lint

Replace `SortOrder` with `·SortOrder·`
import {EntityId} from '@shared/domain/types';

Check failure on line 4 in apps/server/src/modules/deletion/repo/deletion-request.repo.ts

View workflow job for this annotation

GitHub Actions / nest_lint

Replace `EntityId` with `·EntityId·`
import {DeletionRequest} from '../domain/do';

Check failure on line 5 in apps/server/src/modules/deletion/repo/deletion-request.repo.ts

View workflow job for this annotation

GitHub Actions / nest_lint

Replace `DeletionRequest` with `·DeletionRequest·`
import {DeletionRequestEntity} from './entity';

Check failure on line 6 in apps/server/src/modules/deletion/repo/deletion-request.repo.ts

View workflow job for this annotation

GitHub Actions / nest_lint

Replace `DeletionRequestEntity` with `·DeletionRequestEntity·`
import {DeletionRequestMapper} from './mapper';

Check failure on line 7 in apps/server/src/modules/deletion/repo/deletion-request.repo.ts

View workflow job for this annotation

GitHub Actions / nest_lint

Replace `DeletionRequestMapper` with `·DeletionRequestMapper·`
import {DeletionRequestScope} from './scope';

Check failure on line 8 in apps/server/src/modules/deletion/repo/deletion-request.repo.ts

View workflow job for this annotation

GitHub Actions / nest_lint

Replace `DeletionRequestScope` with `·DeletionRequestScope·`
import {StatusModel} from '../domain/types';

Check failure on line 9 in apps/server/src/modules/deletion/repo/deletion-request.repo.ts

View workflow job for this annotation

GitHub Actions / nest_lint

Replace `StatusModel` with `·StatusModel·`

@Injectable()
export class DeletionRequestRepo {
Expand All @@ -32,15 +32,15 @@ export class DeletionRequestRepo {
await this.em.flush();
}

async findAllItemsToExecution(olderThan: Date, newerThan: Date, limit: number): Promise<DeletionRequest[]> {
async findAllItemsToExecution(limit: number, olderThan: Date, newerThan: Date): Promise<DeletionRequest[]> {
if (olderThan < newerThan) {
throw new Error('olderThan must be greater than newerThan');
}
const scope = new DeletionRequestScope();
scope.byDeleteAfter(new Date());

const statusScope = new DeletionRequestScope('$or');
statusScope.byStatusAndDate(StatusModel.REGISTERED);
statusScope.byStatusAndDate([StatusModel.REGISTERED]);
this.addScopeForFailedRequests(statusScope, olderThan, newerThan);

scope.addQuery(statusScope.query);
Expand All @@ -58,8 +58,7 @@ export class DeletionRequestRepo {
}

private addScopeForFailedRequests(scope: DeletionRequestScope, olderThan: Date, newerThan: Date): void {
scope.byStatusAndDate(StatusModel.FAILED, olderThan, newerThan);
scope.byStatusAndDate(StatusModel.PENDING, olderThan, newerThan);
scope.byStatusAndDate([StatusModel.FAILED, StatusModel.PENDING], olderThan, newerThan);
}

async countPendingDeletionRequests(olderThan: Date, newerThan: Date): Promise<number> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export class DeletionRequestScope extends Scope<DeletionRequestEntity> {
return this;
}

byStatusAndDate(status: StatusModel, olderThan?: Date, newerThan?: Date): this {
let query = { status };
byStatusAndDate(status: StatusModel[], olderThan?: Date, newerThan?: Date): this {
let query = { status: { $in: status } };
if (olderThan) {
const olderThanQuery = { updatedAt: { $lt: olderThan } };
query = { ...query, ...olderThanQuery };
Expand Down

0 comments on commit 979f793

Please sign in to comment.