Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

2.1 KB
Raw Blame
from collections.abc import Generator
from io import BytesIO

import pytest
from requests import Response

from extensions.storage.base_storage import BaseStorage


def object_response(content: bytes) -> Response:
    response = Response()
    response.status_code = 200
    response.headers["Content-Length"] = str(len(content))
    response.raw = BytesIO(content)
    return response


def get_example_folder() -> str:
    return "~/dify"


def get_example_bucket() -> str:
    return "dify"


def get_opendal_bucket() -> str:
    import os

    return os.environ.get("OPENDAL_FS_ROOT", "/tmp/dify-storage")


def get_example_filename() -> str:
    return "test.txt"


def get_example_data(length: int = 4) -> bytes:
    chars = "test"
    result = "".join(chars[i % len(chars)] for i in range(length)).encode()
    assert len(result) == length
    return result


def get_example_filepath() -> str:
    return "~/test"


class BaseStorageTest:
    @pytest.fixture(autouse=True)
    def setup_method(self, *_args: object, **_kwargs: object) -> None:
        """Should be implemented in child classes to setup specific storage."""
        self.storage: BaseStorage

    def test_save(self) -> None:
        """Test saving data."""
        self.storage.save(get_example_filename(), get_example_data())

    def test_load_once(self) -> None:
        """Test loading data once."""
        assert self.storage.load_once(get_example_filename()) == get_example_data()

    def test_load_stream(self) -> None:
        """Test loading data as a stream."""
        generator = self.storage.load_stream(get_example_filename())
        assert isinstance(generator, Generator)
        assert next(generator) == get_example_data()
        assert list(generator) == []

    def test_download(self) -> None:
        """Test downloading data."""
        self.storage.download(get_example_filename(), get_example_filepath())

    def test_exists(self) -> None:
        """Test checking if a file exists."""
        assert self.storage.exists(get_example_filename())

    def test_delete(self) -> None:
        """Test deleting a file."""
        self.storage.delete(get_example_filename())