Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
virgilchiriac committed Feb 4, 2025
1 parent 490e35b commit 4ebe5f8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,18 @@ describe(DeletionRequestUc.name, () => {
deletionLogService = module.get(DeletionLogService);
eventBus = module.get(EventBus);
configService = module.get(ConfigService);

jest.useFakeTimers();
});

beforeEach(() => {
afterEach(() => {
jest.clearAllMocks();
jest.useRealTimers();
});

describe('createDeletionRequest', () => {
describe('when creating a deletionRequest', () => {
const setup = () => {
jest.useFakeTimers().setSystemTime(new Date());

const deleteAfterMinutes = 1;
const deletionRequestToCreate: DeletionRequestBodyProps = {
targetRef: {
Expand Down Expand Up @@ -158,7 +159,7 @@ describe(DeletionRequestUc.name, () => {

describe('executeDeletionRequests', () => {
describe('when deletionRequests to execute exists', () => {
const setup = () => {
const setup = (noDelay = false) => {
deletionRequestService.findInProgressCount.mockResolvedValue(1);

configService.get.mockImplementation((key) => {
Expand All @@ -175,8 +176,9 @@ describe(DeletionRequestUc.name, () => {
return 2;
}
if (key === 'ADMIN_API__DELETION_DELAY_MILLISECONDS') {
return 100;
return noDelay ? 0 : 100;
}
return deletionTestConfig()[key];
});

const deletionRequest = deletionRequestFactory.buildWithId({ deleteAfter: new Date('2023-01-01') });
Expand Down Expand Up @@ -249,24 +251,13 @@ describe(DeletionRequestUc.name, () => {
);
});

/*
it('should work with a delay of 0', async () => {
const deletionRequest = deletionRequestFactory.buildWithId({ deleteAfter: new Date('2023-01-01') });
configService.get.mockImplementation((key) => {
if (key === 'ADMIN_API__DELETION_DELAY_MILLISECONDS') {
return 0;
}
return deletionTestConfig()[key];
});
deletionRequestService.findAllItemsToExecute.mockResolvedValueOnce([deletionRequest]);
deletionRequestService.findInProgressCount.mockResolvedValueOnce(0);
setup(true);

await uc.executeDeletionRequests();

expect(deletionRequestService.markDeletionRequestAsFailed).not.toHaveBeenCalled();
});
*/
});

describe('when an error occurred', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ describe(DeletionRequestService.name, () => {

await setupEntities();

jest.useFakeTimers();
jest.setSystemTime(new Date());
jest.useFakeTimers().setSystemTime(new Date());
});

beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@ import { DeletionConfig } from '../../deletion.config';

@Injectable()
export class DeletionRequestService {
private olderThan: Date;
private thresholdOlder: number;

private newerThan: Date;
private thresholdNewer: number;

constructor(
private readonly deletionRequestRepo: DeletionRequestRepo,
private readonly configService: ConfigService<DeletionConfig, true>
) {
const thresholdOlder = this.configService.get<number>('ADMIN_API__DELETION_MODIFICATION_THRESHOLD_MS');
this.olderThan = new Date(Date.now() - thresholdOlder);
this.thresholdOlder = this.configService.get<number>('ADMIN_API__DELETION_MODIFICATION_THRESHOLD_MS');

const thresholdNewer = this.configService.get<number>('ADMIN_API__DELETION_CONSIDER_FAILED_AFTER_MS');
this.newerThan = new Date(Date.now() - thresholdNewer);
this.thresholdNewer = this.configService.get<number>('ADMIN_API__DELETION_CONSIDER_FAILED_AFTER_MS');
}

async createDeletionRequest(
Expand Down Expand Up @@ -49,15 +47,18 @@ export class DeletionRequestService {
}

public async findAllItemsToExecute(limit: number, getFailed = false): Promise<DeletionRequest[]> {
const newerThan = new Date(Date.now() - this.thresholdNewer);
const olderThan = new Date(Date.now() - this.thresholdOlder);
const deletionRequests = getFailed
? await this.deletionRequestRepo.findAllFailedItems(limit, this.olderThan, this.newerThan)
? await this.deletionRequestRepo.findAllFailedItems(limit, olderThan, newerThan)
: await this.deletionRequestRepo.findAllItems(limit);

return deletionRequests;
}

public async findInProgressCount(): Promise<number> {
const count = await this.deletionRequestRepo.findInProgressCount(this.newerThan);
const newerThan = new Date(Date.now() - this.thresholdNewer);
const count = await this.deletionRequestRepo.findInProgressCount(newerThan);
return count;
}

Expand Down

0 comments on commit 4ebe5f8

Please sign in to comment.