Skip to content

Commit e80920a

Browse files
committed
improve test cov
1 parent 51ea68e commit e80920a

File tree

4 files changed

+106
-1
lines changed

4 files changed

+106
-1
lines changed

apps/server/src/modules/deletion-console/deletion-client/deletion.client.spec.ts

+23
Original file line numberDiff line numberDiff line change
@@ -249,5 +249,28 @@ describe(DeletionClient.name, () => {
249249
await expect(client.executeDeletions()).rejects.toThrow(Error);
250250
});
251251
});
252+
253+
describe('when runFailed is true', () => {
254+
const setup = () => {
255+
setupConfig();
256+
257+
const response: AxiosResponse<DeletionRequestOutput> = axiosResponseFactory.build({
258+
status: 204,
259+
});
260+
261+
httpService.post.mockReturnValueOnce(of(response));
262+
263+
const fullUrl = 'http://api-admin:4030/admin/api/v1/deletionExecutions?runFailed=true';
264+
265+
return { fullUrl };
266+
};
267+
it('should call endpoint with runFailed param', async () => {
268+
const { fullUrl } = setup();
269+
270+
await client.executeDeletions(undefined, true);
271+
272+
expect(httpService.post).toHaveBeenCalledWith(fullUrl, null, expect.any(Object));
273+
});
274+
});
252275
});
253276
});

apps/server/src/modules/deletion-console/deletion-execution.console.ts

+2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ export class DeletionExecutionConsole {
1414
options: [
1515
{
1616
flags: '-l, --limit <value>',
17+
/* istanbul ignore next */
1718
fn: (value: string) => (value ? Number(value) : undefined),
1819
description: 'Limit of the requested deletion executions that should be performed.',
1920
required: false,
2021
},
2122
{
2223
flags: '-f, --runFailed <value>',
24+
/* istanbul ignore next */
2325
fn: (value: string) => /^(true|yes|1)$/i.test(value),
2426
description: 'Limit of the requested deletion executions that should be performed.',
2527
required: false,

apps/server/src/modules/deletion/api/uc/deletion-request.uc.spec.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { LegacyLogger } from '@core/logger';
22
import { createMock, DeepMocked } from '@golevelup/ts-jest';
33
import { MikroORM } from '@mikro-orm/core';
4-
import { ConfigModule } from '@nestjs/config';
4+
import { ConfigModule, ConfigService } from '@nestjs/config';
55
import { EventBus } from '@nestjs/cqrs';
66
import { Test, TestingModule } from '@nestjs/testing';
77
import { createConfigModuleOptions } from '@shared/common/config-module-options';
@@ -17,13 +17,15 @@ import { DeletionRequestLogResponseBuilder } from '../builder';
1717
import { DeletionRequestBodyProps } from '../controller/dto';
1818
import { DeletionLogStatisticBuilder, DeletionTargetRefBuilder } from '../controller/dto/builder';
1919
import { DeletionRequestUc } from './deletion-request.uc';
20+
import { DeletionConfig } from '../../deletion.config';
2021

2122
describe(DeletionRequestUc.name, () => {
2223
let module: TestingModule;
2324
let uc: DeletionRequestUc;
2425
let deletionRequestService: DeepMocked<DeletionRequestService>;
2526
let deletionLogService: DeepMocked<DeletionLogService>;
2627
let eventBus: DeepMocked<EventBus>;
28+
let configService: DeepMocked<ConfigService<DeletionConfig, true>>;
2729

2830
beforeAll(async () => {
2931
const orm = await setupEntities();
@@ -54,13 +56,18 @@ describe(DeletionRequestUc.name, () => {
5456
provide: MikroORM,
5557
useValue: orm,
5658
},
59+
{
60+
provide: ConfigService,
61+
useValue: createMock<ConfigService>(),
62+
},
5763
],
5864
}).compile();
5965

6066
uc = module.get(DeletionRequestUc);
6167
deletionRequestService = module.get(DeletionRequestService);
6268
deletionLogService = module.get(DeletionLogService);
6369
eventBus = module.get(EventBus);
70+
configService = module.get(ConfigService);
6471
});
6572

6673
beforeEach(() => {
@@ -183,6 +190,17 @@ describe(DeletionRequestUc.name, () => {
183190
new UserDeletedEvent(deletionRequest.id, deletionRequest.targetRefId)
184191
);
185192
});
193+
194+
it('should work with a delay of 0', async () => {
195+
const { deletionRequest } = setup();
196+
configService.get.mockReturnValueOnce(0);
197+
198+
deletionRequestService.findAllItemsToExecute.mockResolvedValueOnce([deletionRequest]);
199+
200+
await uc.executeDeletionRequests();
201+
202+
expect(deletionRequestService.markDeletionRequestAsFailed).not.toHaveBeenCalled();
203+
});
186204
});
187205

188206
describe('when an error occurred', () => {

apps/server/src/modules/deletion/repo/deletion-request.repo.spec.ts

+62
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,68 @@ describe(DeletionRequestRepo.name, () => {
224224
});
225225
});
226226

227+
describe('findAllFailedItems', () => {
228+
const setup = async () => {
229+
const limit = 10;
230+
const olderThan = new Date(2023, 11, 1);
231+
const newerThan = new Date(2023, 8, 1);
232+
233+
const deletionRequestEntity1: DeletionRequestEntity = deletionRequestEntityFactory.build({
234+
createdAt: new Date(2023, 7, 1),
235+
updatedAt: new Date(2023, 8, 2),
236+
deleteAfter: new Date(2023, 8, 1),
237+
status: StatusModel.PENDING,
238+
});
239+
const deletionRequestEntity2: DeletionRequestEntity = deletionRequestEntityFactory.build({
240+
createdAt: new Date(2023, 7, 1),
241+
updatedAt: new Date(2023, 8, 2),
242+
deleteAfter: new Date(2023, 8, 1),
243+
status: StatusModel.FAILED,
244+
});
245+
const deletionRequestEntity3: DeletionRequestEntity = deletionRequestEntityFactory.build({
246+
createdAt: new Date(2023, 8, 1),
247+
updatedAt: new Date(2023, 7, 1),
248+
deleteAfter: new Date(2023, 9, 1),
249+
status: StatusModel.FAILED,
250+
});
251+
const deletionRequestEntity4: DeletionRequestEntity = deletionRequestEntityFactory.build({
252+
createdAt: new Date(2023, 8, 1),
253+
updatedAt: new Date(2023, 8, 1),
254+
deleteAfter: new Date(2023, 9, 1),
255+
status: StatusModel.REGISTERED,
256+
});
257+
258+
await em.persistAndFlush([
259+
deletionRequestEntity1,
260+
deletionRequestEntity2,
261+
deletionRequestEntity3,
262+
deletionRequestEntity4,
263+
]);
264+
em.clear();
265+
266+
return { limit, olderThan, newerThan, deletionRequestEntity1, deletionRequestEntity2 };
267+
};
268+
269+
it('should throw an error if olderThan is less than newerThan', async () => {
270+
const { limit } = await setup();
271+
272+
const olderThan = new Date(2023, 10, 1);
273+
const newerThan = new Date(2023, 11, 1);
274+
275+
await expect(repo.findAllFailedItems(limit, olderThan, newerThan)).rejects.toThrow();
276+
});
277+
278+
it('should return failed deletion requests within the specified date range', async () => {
279+
const { limit, olderThan, newerThan, deletionRequestEntity1, deletionRequestEntity2 } = await setup();
280+
281+
const results = await repo.findAllFailedItems(limit, olderThan, newerThan);
282+
283+
expect(results.length).toBe(2);
284+
expect(results[0].id).toBe(deletionRequestEntity1.id);
285+
expect(results[1].id).toBe(deletionRequestEntity2.id);
286+
});
287+
});
288+
227289
describe('update', () => {
228290
describe('when updating deletionRequest', () => {
229291
const setup = async () => {

0 commit comments

Comments
 (0)