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.

1.6 KB
Raw Blame
import os
import shutil
from collections.abc import Generator
from pathlib import Path
from unittest.mock import MagicMock, mock_open, patch

import pytest

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


class MockLocalFSClass:
    def write_bytes(self, data: bytes) -> None:
        assert data == get_example_data()

    def read_bytes(self) -> bytes:
        return get_example_data()

    @staticmethod
    def copyfile(src: str, dst: str) -> None:
        assert src == os.path.join(get_example_folder(), get_example_filename())
        assert dst == get_example_filepath()

    @staticmethod
    def exists(path: str) -> bool:
        assert path == os.path.join(get_example_folder(), get_example_filename())
        return True

    @staticmethod
    def remove(path: str) -> None:
        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: pytest.MonkeyPatch) -> Generator[None]:
    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()