Skip to content

Commit

Permalink
test file updated-1-a
Browse files Browse the repository at this point in the history
  • Loading branch information
raggettii committed Dec 23, 2024
1 parent cc59977 commit da96ba3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 55 deletions.
4 changes: 2 additions & 2 deletions tests/helpers/volunteers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export const createVolunteerAndActions = async (): Promise<
hoursHistory: [
{
hours: 2,
date: yesterday,
date: today,
},
{
hours: 4,
Expand Down Expand Up @@ -186,7 +186,7 @@ export const createVolunteerAndActions = async (): Promise<
hoursHistory: [
{
hours: 1,
date: yesterday,
date: today,
},
{
hours: 2,
Expand Down
104 changes: 51 additions & 53 deletions tests/services/deleteFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -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,
);
});
});

0 comments on commit da96ba3

Please sign in to comment.