| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 | import osfrom collections.abc import Callablefrom typing import Literalimport pytest# import monkeypatchfrom _pytest.monkeypatch import MonkeyPatchfrom openai.resources.audio.transcriptions import Transcriptionsfrom openai.resources.chat import Completions as ChatCompletionsfrom openai.resources.completions import Completionsfrom openai.resources.embeddings import Embeddingsfrom openai.resources.models import Modelsfrom openai.resources.moderations import Moderationsfrom tests.integration_tests.model_runtime.__mock.openai_chat import MockChatClassfrom tests.integration_tests.model_runtime.__mock.openai_completion import MockCompletionsClassfrom tests.integration_tests.model_runtime.__mock.openai_embeddings import MockEmbeddingsClassfrom tests.integration_tests.model_runtime.__mock.openai_moderation import MockModerationClassfrom tests.integration_tests.model_runtime.__mock.openai_remote import MockModelClassfrom tests.integration_tests.model_runtime.__mock.openai_speech2text import MockSpeech2TextClassdef mock_openai(monkeypatch: MonkeyPatch, methods: list[Literal["completion", "chat", "remote", "moderation", "speech2text", "text_embedding"]]) -> Callable[[], None]:    """        mock openai module        :param monkeypatch: pytest monkeypatch fixture        :return: unpatch function    """    def unpatch() -> None:        monkeypatch.undo()    if "completion" in methods:        monkeypatch.setattr(Completions, "create", MockCompletionsClass.completion_create)    if "chat" in methods:        monkeypatch.setattr(ChatCompletions, "create", MockChatClass.chat_create)    if "remote" in methods:        monkeypatch.setattr(Models, "list", MockModelClass.list)    if "moderation" in methods:        monkeypatch.setattr(Moderations, "create", MockModerationClass.moderation_create)    if "speech2text" in methods:        monkeypatch.setattr(Transcriptions, "create", MockSpeech2TextClass.speech2text_create)    if "text_embedding" in methods:        monkeypatch.setattr(Embeddings, "create", MockEmbeddingsClass.create_embeddings)    return unpatchMOCK = os.getenv('MOCK_SWITCH', 'false').lower() == 'true'@pytest.fixturedef setup_openai_mock(request, monkeypatch):    methods = request.param if hasattr(request, 'param') else []    if MOCK:        unpatch = mock_openai(monkeypatch, methods=methods)        yield    if MOCK:        unpatch()
 |