chore: add local storage test (#9827)
This commit is contained in:
58
api/tests/unit_tests/oss/__mock/base.py
Normal file
58
api/tests/unit_tests/oss/__mock/base.py
Normal file
@@ -0,0 +1,58 @@
|
||||
from collections.abc import Generator
|
||||
|
||||
import pytest
|
||||
|
||||
from extensions.storage.base_storage import BaseStorage
|
||||
|
||||
|
||||
def get_example_folder() -> str:
|
||||
return "/dify"
|
||||
|
||||
|
||||
def get_example_bucket() -> str:
|
||||
return "dify"
|
||||
|
||||
|
||||
def get_example_filename() -> str:
|
||||
return "test.txt"
|
||||
|
||||
|
||||
def get_example_data() -> bytes:
|
||||
return b"test"
|
||||
|
||||
|
||||
def get_example_filepath() -> str:
|
||||
return "/test"
|
||||
|
||||
|
||||
class BaseStorageTest:
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_method(self):
|
||||
"""Should be implemented in child classes to setup specific storage."""
|
||||
self.storage = BaseStorage()
|
||||
|
||||
def test_save(self):
|
||||
"""Test saving data."""
|
||||
self.storage.save(get_example_filename(), get_example_data())
|
||||
|
||||
def test_load_once(self):
|
||||
"""Test loading data once."""
|
||||
assert self.storage.load_once(get_example_filename()) == get_example_data()
|
||||
|
||||
def test_load_stream(self):
|
||||
"""Test loading data as a stream."""
|
||||
generator = self.storage.load_stream(get_example_filename())
|
||||
assert isinstance(generator, Generator)
|
||||
assert next(generator) == get_example_data()
|
||||
|
||||
def test_download(self):
|
||||
"""Test downloading data."""
|
||||
self.storage.download(get_example_filename(), get_example_filepath())
|
||||
|
||||
def test_exists(self):
|
||||
"""Test checking if a file exists."""
|
||||
assert self.storage.exists(get_example_filename())
|
||||
|
||||
def test_delete(self):
|
||||
"""Test deleting a file."""
|
||||
self.storage.delete(get_example_filename())
|
||||
57
api/tests/unit_tests/oss/__mock/local.py
Normal file
57
api/tests/unit_tests/oss/__mock/local.py
Normal file
@@ -0,0 +1,57 @@
|
||||
import os
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock, mock_open, patch
|
||||
|
||||
import pytest
|
||||
from _pytest.monkeypatch import MonkeyPatch
|
||||
|
||||
from tests.unit_tests.oss.__mock.base import (
|
||||
get_example_data,
|
||||
get_example_filename,
|
||||
get_example_filepath,
|
||||
get_example_folder,
|
||||
)
|
||||
|
||||
|
||||
class MockLocalFSClass:
|
||||
def write_bytes(self, data):
|
||||
assert data == get_example_data()
|
||||
|
||||
def read_bytes(self):
|
||||
return get_example_data()
|
||||
|
||||
@staticmethod
|
||||
def copyfile(src, dst):
|
||||
assert src == os.path.join(get_example_folder(), get_example_filename())
|
||||
assert dst == get_example_filepath()
|
||||
|
||||
@staticmethod
|
||||
def exists(path):
|
||||
assert path == os.path.join(get_example_folder(), get_example_filename())
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def remove(path):
|
||||
assert path == os.path.join(get_example_folder(), get_example_filename())
|
||||
|
||||
|
||||
MOCK = os.getenv("MOCK_SWITCH", "false").lower() == "true"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def setup_local_fs_mock(monkeypatch: MonkeyPatch):
|
||||
if MOCK:
|
||||
monkeypatch.setattr(Path, "write_bytes", MockLocalFSClass.write_bytes)
|
||||
monkeypatch.setattr(Path, "read_bytes", MockLocalFSClass.read_bytes)
|
||||
monkeypatch.setattr(shutil, "copyfile", MockLocalFSClass.copyfile)
|
||||
monkeypatch.setattr(os.path, "exists", MockLocalFSClass.exists)
|
||||
monkeypatch.setattr(os, "remove", MockLocalFSClass.remove)
|
||||
|
||||
os.makedirs = MagicMock()
|
||||
|
||||
with patch("builtins.open", mock_open(read_data=get_example_data())):
|
||||
yield
|
||||
|
||||
if MOCK:
|
||||
monkeypatch.undo()
|
||||
@@ -1,5 +1,4 @@
|
||||
import os
|
||||
from typing import Union
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
@@ -7,28 +6,19 @@ from _pytest.monkeypatch import MonkeyPatch
|
||||
from tos import TosClientV2
|
||||
from tos.clientv2 import DeleteObjectOutput, GetObjectOutput, HeadObjectOutput, PutObjectOutput
|
||||
|
||||
from tests.unit_tests.oss.__mock.base import (
|
||||
get_example_bucket,
|
||||
get_example_data,
|
||||
get_example_filename,
|
||||
get_example_filepath,
|
||||
)
|
||||
|
||||
|
||||
class AttrDict(dict):
|
||||
def __getattr__(self, item):
|
||||
return self.get(item)
|
||||
|
||||
|
||||
def get_example_bucket() -> str:
|
||||
return "dify"
|
||||
|
||||
|
||||
def get_example_filename() -> str:
|
||||
return "test.txt"
|
||||
|
||||
|
||||
def get_example_data() -> bytes:
|
||||
return b"test"
|
||||
|
||||
|
||||
def get_example_filepath() -> str:
|
||||
return "/test"
|
||||
|
||||
|
||||
class MockVolcengineTosClass:
|
||||
def __init__(self, ak="", sk="", endpoint="", region=""):
|
||||
self.bucket_name = get_example_bucket()
|
||||
|
||||
Reference in New Issue
Block a user