Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
virgilchiriac committed Jan 23, 2025
1 parent eb96b3f commit b9b6c83
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ describe(TriggerDeletionExecutionOptionsBuilder.name, () => {
describe('when called with proper arguments', () => {
const setup = () => {
const limit = 1000;
const runFailed = false;

const expectedOutput: TriggerDeletionExecutionOptions = { limit };
const expectedOutput: TriggerDeletionExecutionOptions = { limit, runFailed };

return { limit, expectedOutput };
return { expectedOutput, limit, runFailed };
};

it('should return valid object with expected values', () => {
const { limit, expectedOutput } = setup();
const { expectedOutput, limit, runFailed } = setup();

const output = TriggerDeletionExecutionOptionsBuilder.build(limit);
const output = TriggerDeletionExecutionOptionsBuilder.build(limit, runFailed);

expect(output).toStrictEqual(expectedOutput);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ describe(DeletionRequestService.name, () => {

await service.findAllItemsToExecute(limit);

// TODO fix date comparison
expect(deletionRequestRepo.findAllItems).toBeCalledWith(limit, expect.any(Date), expect.any(Date));
expect(deletionRequestRepo.findAllItems).toBeCalledWith(limit);
});

it('should return array of two deletionRequests to execute', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,18 @@ describe(DeletionRequestRepo.name, () => {
});
});

describe('findAllItemsToExecution', () => {
describe('findAllItems', () => {
describe('when there is no deletionRequest for execution', () => {
const setup = () => {
const limit = 1000;
const olderThan = new Date();
const newerThan = new Date();

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

expect(result).toEqual([]);
});
Expand All @@ -126,10 +122,6 @@ describe(DeletionRequestRepo.name, () => {
describe('when there are deletionRequests for execution', () => {
const setup = async () => {
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);
Expand Down Expand Up @@ -193,14 +185,13 @@ describe(DeletionRequestRepo.name, () => {
},
];

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

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 { deletionRequestEntity1, deletionRequestEntity5, expectedArray, limit } = await setup();

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

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

Expand All @@ -219,9 +210,9 @@ describe(DeletionRequestRepo.name, () => {
});

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,57 @@ describe(DeletionRequestScope.name, () => {

describe('byStatusAndDate', () => {
const setup = () => {
const fifteenMinutesAgo = new Date(Date.now() - 15 * 60 * 1000); // 15 minutes ago
const expectedQuery = {
$or: [
{ status: StatusModel.FAILED },
{
$and: [
{ status: [StatusModel.REGISTERED, StatusModel.PENDING] },
{ updatedAt: { $lt: fifteenMinutesAgo } },
],
},
],
const newerThan = new Date(Date.now() - 15 * 60 * 1000); // 15 minutes ago
const olderThan = new Date(Date.now() - 5 * 60 * 1000); // 5 minutes ago

const expectedQueryOlder = {
status: {
$in: [StatusModel.FAILED, StatusModel.PENDING],
},
updatedAt: {
$lt: olderThan,
},
};
const expectedQueryNewer = {
status: {
$in: [StatusModel.FAILED, StatusModel.PENDING],
},
updatedAt: {
$gte: newerThan,
},
};
return {
expectedQuery,
fifteenMinutesAgo,
expectedQueryOlder,
expectedQueryNewer,
olderThan,
newerThan,
};
};
describe('when fifteenMinutesAgo is set', () => {
describe('when olderThan is set', () => {
it('should add query', () => {
const { expectedQuery, fifteenMinutesAgo } = setup();
const { expectedQueryOlder, olderThan } = setup();

const result = scope.byStatusAndDate(StatusModel.FAILED, fifteenMinutesAgo);
const result = scope.byStatusAndDate([StatusModel.FAILED, StatusModel.PENDING], olderThan);

expect(result).toBeInstanceOf(DeletionRequestScope);
expect(scope.query).toEqual(expectedQuery);
expect(scope.query).toEqual(expectedQueryOlder);
});
});
describe('when newerThan is set', () => {
it('should add query', () => {
const { expectedQueryNewer, newerThan } = setup();

const result = scope.byStatusAndDate([StatusModel.FAILED, StatusModel.PENDING], undefined, newerThan);

expect(result).toBeInstanceOf(DeletionRequestScope);
expect(scope.query).toEqual(expectedQueryNewer);
});
});
});

describe('byStatusAndDate for Pending', () => {
const setup = () => {
const expectedQuery = { status: [StatusModel.PENDING] };
const expectedQuery = { status: { $in: [StatusModel.PENDING] } };

return { expectedQuery };
};
Expand All @@ -74,7 +93,7 @@ describe(DeletionRequestScope.name, () => {
it('should add query', () => {
const { expectedQuery } = setup();

const result = scope.byStatusAndDate(StatusModel.PENDING);
const result = scope.byStatusAndDate([StatusModel.PENDING]);

expect(result).toBeInstanceOf(DeletionRequestScope);
expect(scope.query).toEqual(expectedQuery);
Expand Down

0 comments on commit b9b6c83

Please sign in to comment.