[feature] Refactor uploadObject; For bug 73502

This commit is contained in:
Sergey Konovalov
2025-04-03 12:11:41 +03:00
parent c75a88aecb
commit 3dc487b856
3 changed files with 72 additions and 27 deletions

View File

@ -119,7 +119,7 @@ function runTestForDir(ctx, isMultitenantMode, specialDir) {
});
} else {
test("uploadObject", async () => {
const spy = jest.spyOn(fs, 'createReadStream').mockReturnValue(testFileData3);
const spy = jest.spyOn(fs, 'createReadStream').mockReturnValue(Readable.from(testFileData3));
let res = await storage.uploadObject(ctx, testFile3, "createReadStream.txt", specialDir);
expect(res).toEqual(undefined);
let list = await storage.listObjects(ctx, testDir, specialDir);
@ -127,6 +127,31 @@ function runTestForDir(ctx, isMultitenantMode, specialDir) {
expect(list.sort()).toEqual([testFile1, testFile2, testFile3].sort());
spy.mockRestore();
});
test("uploadObject - stream error handling", async () => {
const streamErrorMessage = "Test stream error";
const mockStream = new Readable({
read() {
this.emit('error', new Error(streamErrorMessage));
}
});
const spy = jest.spyOn(fs, 'createReadStream').mockReturnValue(mockStream);
// Verify that the uploadObject function rejects when the stream emits an error
await expect(storage.uploadObject(ctx, "test-error-file.txt", "nonexistent.txt", specialDir))
.rejects.toThrow(streamErrorMessage);
spy.mockRestore();
});
test("uploadObject - non-existent file handling", async () => {
const nonExistentFile = 'definitely-does-not-exist-' + Date.now() + '.txt';
// Verify the file actually doesn't exist
expect(fs.existsSync(nonExistentFile)).toBe(false);
// Verify that uploadObject properly handles and propagates the error
await expect(storage.uploadObject(ctx, "test-error-file.txt", nonExistentFile, specialDir))
.rejects.toThrow(/ENOENT/);
});
}
test("copyObject", async () => {
let res = await storage.copyObject(ctx, testFile3, testFile4, specialDir, specialDir);