From da96ba3ca5443e398a430f864f0ba81d6cf8b46a Mon Sep 17 00:00:00 2001 From: raggettii <22bsm029@iiitdmj.ac.in> Date: Mon, 23 Dec 2024 11:53:35 +0530 Subject: [PATCH] test file updated-1-a --- tests/helpers/volunteers.ts | 4 +- tests/services/deleteFile.spec.ts | 104 +++++++++++++++--------------- 2 files changed, 53 insertions(+), 55 deletions(-) diff --git a/tests/helpers/volunteers.ts b/tests/helpers/volunteers.ts index 026dc4313a..1b17d5d4d0 100644 --- a/tests/helpers/volunteers.ts +++ b/tests/helpers/volunteers.ts @@ -158,7 +158,7 @@ export const createVolunteerAndActions = async (): Promise< hoursHistory: [ { hours: 2, - date: yesterday, + date: today, }, { hours: 4, @@ -186,7 +186,7 @@ export const createVolunteerAndActions = async (): Promise< hoursHistory: [ { hours: 1, - date: yesterday, + date: today, }, { hours: 2, diff --git a/tests/services/deleteFile.spec.ts b/tests/services/deleteFile.spec.ts index 0acc40f036..08dae85ddb 100644 --- a/tests/services/deleteFile.spec.ts +++ b/tests/services/deleteFile.spec.ts @@ -15,29 +15,29 @@ vi.mock("../../src/models", () => ({ }, })); -vi.mock("../../src/REST/controllers/minio", () => ({ +vi.mock("../../src/REST/services/minio", () => ({ deleteFile: vi.fn(), })); -describe("src -> REST -> services -> file -> deleteFile", () => { - const objectKey = "test/file/path"; - const fileId = "12345"; - let serverRunning = false; +const objectKey = "test/file/path"; +const fileId = "12345"; +let serverRunning = false; - // Wait for server status check before running tests - beforeAll(async () => { - try { - serverRunning = await isMinioRunning(); - } catch (error) { - console.error("Error checking MinIO server status:", error); - serverRunning = false; - } - }); +beforeAll(async () => { + try { + serverRunning = await isMinioRunning(); + } catch (error) { + console.error("Error checking MinIO server status:", error); + } +}); - beforeEach(() => { - vi.clearAllMocks(); - }); +// Clear mocks before each test +beforeEach(() => { + vi.clearAllMocks(); +}); +// Generic file tests +describe("File deletion logic", () => { it("should return success:false and message if file is not found", async () => { vi.mocked(File.findOne).mockResolvedValueOnce(null); @@ -67,42 +67,6 @@ describe("src -> REST -> services -> file -> deleteFile", () => { expect(mockFile.save).toHaveBeenCalled(); }); - // Use describe block with a dynamic condition - describe("MinIO server integration tests", () => { - beforeEach(async () => { - serverRunning = await isMinioRunning(); - }); - - it("should delete the file from the database and storage if reference count is 1", async () => { - // Skip this test if server is not running - if (!serverRunning) { - console.log("Skipping MinIO test - server not running"); - return; - } - - const mockFile = { - referenceCount: 1, - _id: fileId, - id: fileId, - }; - vi.mocked(File.findOne).mockResolvedValueOnce(mockFile); - - const deleteFileFromBucketSpy = vi.spyOn(minioServices, "deleteFile"); - - const result = await deleteFile(objectKey, fileId); - - expect(result).toEqual({ - success: true, - message: "File deleted successfully", - }); - expect(File.deleteOne).toHaveBeenCalledWith({ _id: fileId }); - expect(deleteFileFromBucketSpy).toHaveBeenCalledWith( - BUCKET_NAME, - objectKey, - ); - }); - }); - it("should handle errors and return an error message", async () => { const mockError = new Error("Deletion failed"); vi.mocked(File.findOne).mockRejectedValueOnce(mockError); @@ -121,3 +85,37 @@ describe("src -> REST -> services -> file -> deleteFile", () => { ); }); }); + +// MinIO-specific tests +describe("MinIO server integration tests", () => { + beforeAll(async () => { + serverRunning = await isMinioRunning(); + }); + + it("should delete the file from the database and storage if reference count is 1", async () => { + if (!serverRunning) { + return; // Skip the test if the server isn't running + } + + const mockFile = { + referenceCount: 1, + _id: fileId, + id: fileId, + }; + vi.mocked(File.findOne).mockResolvedValueOnce(mockFile); + + const deleteFileFromBucketSpy = vi.spyOn(minioServices, "deleteFile"); + + const result = await deleteFile(objectKey, fileId); + + expect(result).toEqual({ + success: true, + message: "File deleted successfully", + }); + expect(File.deleteOne).toHaveBeenCalledWith({ _id: fileId }); + expect(deleteFileFromBucketSpy).toHaveBeenCalledWith( + BUCKET_NAME, + objectKey, + ); + }); +});