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.5 KB
Raw Blame
import base64
import hashlib
import os
from collections.abc import Generator
from io import BytesIO
from types import SimpleNamespace

import pytest
from baidubce.services.bos.bos_client import BosClient

from tests.helpers.unit.oss.__mock.base import (
    get_example_bucket,
    get_example_data,
    get_example_filename,
    get_example_filepath,
)


class MockBaiduObsClass:
    def __init__(self, *_args: object, **_kwargs: object) -> None:
        self.bucket_name = get_example_bucket()
        self.key = get_example_filename()
        self.content = get_example_data()
        self.filepath = get_example_filepath()

    def put_object(self, bucket_name: str, key: str, data: bytes, content_length: int, content_md5: bytes) -> None:
        assert bucket_name == self.bucket_name
        assert key == self.key
        assert data == self.content
        assert content_length == len(self.content)
        expected_md5 = base64.standard_b64encode(hashlib.md5(self.content).digest())
        assert content_md5 == expected_md5

    def get_object(self, bucket_name: str, key: str) -> SimpleNamespace:
        assert bucket_name == self.bucket_name
        assert key == self.key
        return SimpleNamespace(data=BytesIO(self.content))

    def get_object_to_file(self, bucket_name: str, key: str, file_name: str) -> None:
        assert bucket_name == self.bucket_name
        assert key == self.key
        assert file_name == self.filepath

    def get_object_meta_data(self, bucket_name: str, key: str) -> SimpleNamespace:
        assert bucket_name == self.bucket_name
        assert key == self.key
        return SimpleNamespace(status=200)

    def delete_object(self, bucket_name: str, key: str) -> None:
        assert bucket_name == self.bucket_name
        assert key == self.key


MOCK = os.getenv("MOCK_SWITCH", "false").lower() == "true"


@pytest.fixture
def setup_baidu_obs_mock(monkeypatch: pytest.MonkeyPatch) -> Generator[None]:
    if MOCK:
        monkeypatch.setattr(BosClient, "__init__", MockBaiduObsClass.__init__)
        monkeypatch.setattr(BosClient, "put_object", MockBaiduObsClass.put_object)
        monkeypatch.setattr(BosClient, "get_object", MockBaiduObsClass.get_object)
        monkeypatch.setattr(BosClient, "get_object_to_file", MockBaiduObsClass.get_object_to_file)
        monkeypatch.setattr(BosClient, "get_object_meta_data", MockBaiduObsClass.get_object_meta_data)
        monkeypatch.setattr(BosClient, "delete_object", MockBaiduObsClass.delete_object)

    yield

    if MOCK:
        monkeypatch.undo()