|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import asyncio |
| 3 | +import importlib |
| 4 | +from types import SimpleNamespace |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +import src.providers.factory as factory_module |
| 9 | +from src.providers.factory import ModelProviderFactory |
| 10 | +from src.providers.grok import GrokProvider |
| 11 | +from src.providers.openai import OpenAIProvider |
| 12 | +from src.providers.openai_compatible import OpenAICompatibleProvider |
| 13 | +from src.utils.config import ModelDetail |
| 14 | + |
| 15 | + |
| 16 | +class FailingAsyncClient: |
| 17 | + class chat: |
| 18 | + class completions: |
| 19 | + @staticmethod |
| 20 | + async def create(*_args, **_kwargs): |
| 21 | + raise RuntimeError("boom-chat") |
| 22 | + |
| 23 | + class embeddings: |
| 24 | + @staticmethod |
| 25 | + async def create(*_args, **_kwargs): |
| 26 | + raise RuntimeError("boom-embed") |
| 27 | + |
| 28 | + |
| 29 | +def test_openai_provider_async_failure_raises(monkeypatch): |
| 30 | + fake_settings = SimpleNamespace(openai_api_key="token", openai_api_base="http://example.com") |
| 31 | + monkeypatch.setattr("src.providers.openai.get_settings", lambda: fake_settings) |
| 32 | + |
| 33 | + provider = OpenAIProvider("demo") |
| 34 | + provider._aclient = FailingAsyncClient() |
| 35 | + |
| 36 | + async def consume() -> None: |
| 37 | + async for _chunk in provider.ainvoke("hello", stream=False): |
| 38 | + pass |
| 39 | + |
| 40 | + with pytest.raises(RuntimeError, match="boom-chat"): |
| 41 | + asyncio.run(consume()) |
| 42 | + |
| 43 | + with pytest.raises(RuntimeError, match="boom-embed"): |
| 44 | + asyncio.run(provider.aembed_documents(["doc"])) |
| 45 | + |
| 46 | + |
| 47 | +def test_openai_compatible_provider_async_failure_raises(monkeypatch): |
| 48 | + fake_settings = SimpleNamespace(deepseek_api_key="token", deepseek_base_url="http://example.com") |
| 49 | + monkeypatch.setattr("src.providers.openai_compatible.get_settings", lambda: fake_settings) |
| 50 | + |
| 51 | + provider = OpenAICompatibleProvider("demo", "deepseek") |
| 52 | + provider._aclient = FailingAsyncClient() |
| 53 | + |
| 54 | + async def consume() -> None: |
| 55 | + async for _chunk in provider.ainvoke("hello", stream=False): |
| 56 | + pass |
| 57 | + |
| 58 | + with pytest.raises(RuntimeError, match="boom-chat"): |
| 59 | + asyncio.run(consume()) |
| 60 | + |
| 61 | + with pytest.raises(RuntimeError, match="boom-embed"): |
| 62 | + asyncio.run(provider.aembed_documents(["doc"])) |
| 63 | + |
| 64 | + |
| 65 | +def test_grok_provider_can_be_loaded_by_factory(monkeypatch): |
| 66 | + fake_settings = SimpleNamespace( |
| 67 | + llm_configurations={"grok-test": ModelDetail(provider="grok", model_name="grok-1")}, |
| 68 | + grok_api_key="token", |
| 69 | + grok_base_url="https://api.x.ai/v1", |
| 70 | + ) |
| 71 | + monkeypatch.setattr(factory_module, "get_settings", lambda: fake_settings) |
| 72 | + monkeypatch.setattr("src.providers.openai_compatible.get_settings", lambda: fake_settings) |
| 73 | + |
| 74 | + provider = factory_module.ModelProviderFactory.get_llm_provider("grok-test") |
| 75 | + |
| 76 | + assert provider.__class__.__name__ == GrokProvider.__name__ |
| 77 | + assert provider.__class__.__module__ == "src.providers.grok" |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.parametrize( |
| 81 | + ("provider_name", "module_name", "class_name"), |
| 82 | + [ |
| 83 | + (provider_name, provider_info["module"], provider_info["class"]) |
| 84 | + for provider_name, provider_info in ModelProviderFactory._provider_map.items() |
| 85 | + ], |
| 86 | +) |
| 87 | +def test_provider_map_modules_are_importable(provider_name, module_name, class_name): |
| 88 | + module = importlib.import_module(module_name) |
| 89 | + provider_class = getattr(module, class_name) |
| 90 | + |
| 91 | + assert provider_class is not None, provider_name |
0 commit comments