Skip to content

Commit

Permalink
push tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CeEv committed Jul 26, 2024
1 parent cd3b4c9 commit 630ab6b
Showing 1 changed file with 80 additions and 4 deletions.
84 changes: 80 additions & 4 deletions apps/server/src/modules/files/uc/delete-files.uc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,14 @@ describe(DeleteFilesUc.name, () => {
};

it('should create correct log result', async () => {
const { thresholdDate, batchSize } = setup();
const { thresholdDate, batchSize, spy } = setup();

await service.deleteMarkedFiles(thresholdDate, batchSize);

expect(logger.log).toBeCalledTimes(4);
expect(logger.error).toBeCalledTimes(0);

spy.mockRestore();
});

it('should delete all marked files in S3', async () => {
Expand All @@ -109,16 +111,20 @@ describe(DeleteFilesUc.name, () => {
await service.deleteMarkedFiles(thresholdDate, batchSize);

expect(spy).toHaveBeenCalledTimes(2);

spy.mockRestore();
});

it('should delete all marked files in database', async () => {
const { thresholdDate, batchSize, exampleFiles } = setup();
const { thresholdDate, batchSize, exampleFiles, spy } = setup();

await service.deleteMarkedFiles(thresholdDate, batchSize);

for (const file of exampleFiles) {
expect(filesRepo.delete).toHaveBeenCalledWith(file);
}

spy.mockRestore();
});
});

Expand Down Expand Up @@ -158,19 +164,23 @@ describe(DeleteFilesUc.name, () => {
};

it('should log the error', async () => {
const { thresholdDate, batchSize, error } = setup();
const { thresholdDate, batchSize, error, spy } = setup();

await service.deleteMarkedFiles(thresholdDate, batchSize);

expect(logger.error).toHaveBeenCalledWith(error);

spy.mockRestore();
});

it('should not call delete on repo for that file', async () => {
const { thresholdDate, batchSize } = setup();
const { thresholdDate, batchSize, spy } = setup();

await service.deleteMarkedFiles(thresholdDate, batchSize);

expect(filesRepo.delete).toBeCalledTimes(0);

spy.mockRestore();
});

it('should continue with other files', async () => {
Expand All @@ -179,6 +189,72 @@ describe(DeleteFilesUc.name, () => {
await service.deleteMarkedFiles(thresholdDate, batchSize);

expect(spy).toBeCalledTimes(2);

spy.mockRestore();
});
});

describe('when no provider exists', () => {
const setup = () => {
const thresholdDate = new Date();
const batchSize = 3;

const userId = new ObjectId().toHexString();
const storageProvider = storageProviderFactory.build();

const exampleFiles = [
fileEntityFactory.buildWithId({
storageProvider,
ownerId: userId,
creatorId: userId,
permissions: [filePermissionEntityFactory.build({ refId: userId })],
}),
];

// Please note the second try, that found no more files that needs to be deleted.
filesRepo.findForCleanup.mockResolvedValueOnce(exampleFiles).mockResolvedValueOnce([]);
storageProviderRepo.findAll.mockResolvedValueOnce([]);

return { thresholdDate, batchSize };
};

it('should throw an error ', async () => {
const { thresholdDate, batchSize } = setup();

await service.deleteMarkedFiles(thresholdDate, batchSize);

expect(logger.error).toBeCalledTimes(2);
});
});

describe('when file without provider exists', () => {
const setup = () => {
const thresholdDate = new Date();
const batchSize = 3;

const userId = new ObjectId().toHexString();
const storageProvider = storageProviderFactory.build();

const exampleFiles = [
fileEntityFactory.buildWithId({
storageProvider: undefined,
ownerId: userId,
creatorId: userId,
permissions: [filePermissionEntityFactory.build({ refId: userId })],
}),
];

// Please note the second try, that found no more files that needs to be deleted.
filesRepo.findForCleanup.mockResolvedValueOnce(exampleFiles).mockResolvedValueOnce([]);
storageProviderRepo.findAll.mockResolvedValueOnce([storageProvider]);

return { thresholdDate, batchSize };
};

it('should throw an error ', async () => {
const { thresholdDate, batchSize } = setup();

await expect(() => service.deleteMarkedFiles(thresholdDate, batchSize)).rejects.toThrowError();
});
});
});
Expand Down

0 comments on commit 630ab6b

Please sign in to comment.