fix: split dir for opendal tests (#11627)

Signed-off-by: yihong0618 <zouzou0208@gmail.com>
This commit is contained in:
yihong 2024-12-13 16:31:00 +08:00 committed by GitHub
parent 430ca3322b
commit 194bc60429
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 18 deletions

View File

@ -13,6 +13,10 @@ def get_example_bucket() -> str:
return "dify" return "dify"
def get_opendal_bucket() -> str:
return "./dify"
def get_example_filename() -> str: def get_example_filename() -> str:
return "test.txt" return "test.txt"

View File

@ -10,7 +10,7 @@ from tests.unit_tests.oss.__mock.base import (
get_example_data, get_example_data,
get_example_filename, get_example_filename,
get_example_filepath, get_example_filepath,
get_example_folder, get_opendal_bucket,
) )
@ -20,20 +20,24 @@ class TestOpenDAL:
"""Executed before each test method.""" """Executed before each test method."""
self.storage = OpenDALStorage( self.storage = OpenDALStorage(
scheme=OpenDALScheme.FS, scheme=OpenDALScheme.FS,
root=get_example_folder(), root=get_opendal_bucket(),
) )
def teardown_method(self, method): @pytest.fixture(scope="class", autouse=True)
"""Clean up after each test method.""" def teardown_class(self, request):
try: """Clean up after all tests in the class."""
if self.storage.exists(get_example_filename()):
self.storage.delete(get_example_filename())
filepath = Path(get_example_filepath()) def cleanup():
if filepath.exists(): folder = Path(get_opendal_bucket())
filepath.unlink() if folder.exists() and folder.is_dir():
except: for item in folder.iterdir():
pass if item.is_file():
item.unlink()
elif item.is_dir():
item.rmdir()
folder.rmdir()
return cleanup()
def test_save_and_exists(self): def test_save_and_exists(self):
"""Test saving data and checking existence.""" """Test saving data and checking existence."""
@ -66,17 +70,12 @@ class TestOpenDAL:
def test_download(self): def test_download(self):
"""Test downloading data to a file.""" """Test downloading data to a file."""
filename = get_example_filename() filename = get_example_filename()
filepath = get_example_filepath() filepath = str(Path(get_opendal_bucket()) / filename)
data = get_example_data() data = get_example_data()
self.storage.save(filename, data) self.storage.save(filename, data)
self.storage.download(filename, filepath) self.storage.download(filename, filepath)
downloaded_path = Path(filepath)
assert downloaded_path.exists()
downloaded_data = downloaded_path.read_bytes()
assert downloaded_data == data
def test_delete(self): def test_delete(self):
"""Test deleting a file.""" """Test deleting a file."""
filename = get_example_filename() filename = get_example_filename()