|
| 1 | +""" |
| 2 | +Shared test fixtures and configuration for metorial tests. |
| 3 | +
|
| 4 | +Includes sync/async client parametrization pattern. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +from typing import TYPE_CHECKING, Literal |
| 10 | +from unittest.mock import AsyncMock, MagicMock |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | +if TYPE_CHECKING: |
| 15 | + from metorial import Metorial, MetorialSync |
| 16 | + |
| 17 | + |
| 18 | +# -------------------------------------------------------------------------- |
| 19 | +# Pytest configuration |
| 20 | +# -------------------------------------------------------------------------- |
| 21 | + |
| 22 | + |
| 23 | +def pytest_configure(config: pytest.Config) -> None: |
| 24 | + """Register custom markers.""" |
| 25 | + config.addinivalue_line( |
| 26 | + "markers", |
| 27 | + "sync_and_async: mark test to run with both sync and async clients", |
| 28 | + ) |
| 29 | + |
| 30 | + |
| 31 | +# -------------------------------------------------------------------------- |
| 32 | +# Sync/Async client parametrization |
| 33 | +# -------------------------------------------------------------------------- |
| 34 | + |
| 35 | + |
| 36 | +@pytest.fixture(params=["sync", "async"]) |
| 37 | +def client_type(request: pytest.FixtureRequest) -> Literal["sync", "async"]: |
| 38 | + """Parametrize tests to run with both sync and async clients.""" |
| 39 | + return request.param |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture |
| 43 | +def metorial_client( |
| 44 | + client_type: Literal["sync", "async"], |
| 45 | + mock_metorial_config: dict[str, str], |
| 46 | +) -> Metorial | MetorialSync: |
| 47 | + """Create a Metorial client based on client_type fixture. |
| 48 | +
|
| 49 | + This allows tests marked with @pytest.mark.sync_and_async to run |
| 50 | + automatically with both sync and async clients. |
| 51 | + """ |
| 52 | + if client_type == "sync": |
| 53 | + from metorial import MetorialSync |
| 54 | + |
| 55 | + return MetorialSync(api_key=mock_metorial_config["apiKey"]) |
| 56 | + else: |
| 57 | + from metorial import Metorial |
| 58 | + |
| 59 | + return Metorial(api_key=mock_metorial_config["apiKey"]) |
| 60 | + |
| 61 | + |
| 62 | +@pytest.fixture |
| 63 | +def async_metorial_client(mock_metorial_config: dict[str, str]) -> Metorial: |
| 64 | + """Create an async-only Metorial client for async-specific tests.""" |
| 65 | + from metorial import Metorial |
| 66 | + |
| 67 | + return Metorial(api_key=mock_metorial_config["apiKey"]) |
| 68 | + |
| 69 | + |
| 70 | +@pytest.fixture |
| 71 | +def sync_metorial_client(mock_metorial_config: dict[str, str]) -> MetorialSync: |
| 72 | + """Create a sync-only Metorial client for sync-specific tests.""" |
| 73 | + from metorial import MetorialSync |
| 74 | + |
| 75 | + return MetorialSync(api_key=mock_metorial_config["apiKey"]) |
| 76 | + |
| 77 | + |
| 78 | +# -------------------------------------------------------------------------- |
| 79 | +# Mock fixtures |
| 80 | +# -------------------------------------------------------------------------- |
| 81 | + |
| 82 | + |
| 83 | +@pytest.fixture |
| 84 | +def mock_tool_manager() -> MagicMock: |
| 85 | + """Mock tool manager for testing.""" |
| 86 | + manager = MagicMock() |
| 87 | + manager.get_tools.return_value = [] |
| 88 | + manager.call_tool = AsyncMock(return_value={"content": "result"}) |
| 89 | + manager.get_tool.return_value = None |
| 90 | + return manager |
| 91 | + |
| 92 | + |
| 93 | +@pytest.fixture |
| 94 | +def mock_metorial_config() -> dict[str, str]: |
| 95 | + """Mock configuration for testing.""" |
| 96 | + return { |
| 97 | + "apiKey": "test-api-key", |
| 98 | + "apiHost": "https://api.metorial.com", |
| 99 | + "mcpHost": "https://mcp.metorial.com", |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | +@pytest.fixture |
| 104 | +def mock_mcp_tool() -> MagicMock: |
| 105 | + """Mock MCP tool object.""" |
| 106 | + tool = MagicMock() |
| 107 | + tool.name = "test_tool" |
| 108 | + tool.description = "A test tool" |
| 109 | + tool.parameters = {"type": "object", "properties": {"param1": {"type": "string"}}} |
| 110 | + return tool |
| 111 | + |
| 112 | + |
| 113 | +@pytest.fixture |
| 114 | +def mock_mcp_session() -> MagicMock: |
| 115 | + """Mock MCP session for testing.""" |
| 116 | + session = MagicMock() |
| 117 | + session.get_tool_manager = AsyncMock() |
| 118 | + session.close = AsyncMock() |
| 119 | + return session |
| 120 | + |
| 121 | + |
| 122 | +@pytest.fixture |
| 123 | +def mock_http_response() -> MagicMock: |
| 124 | + """Mock HTTP response for testing RawResponse.""" |
| 125 | + response = MagicMock() |
| 126 | + response.status_code = 200 |
| 127 | + response.headers = { |
| 128 | + "X-Request-ID": "req-test-123", |
| 129 | + "Content-Type": "application/json", |
| 130 | + } |
| 131 | + return response |
0 commit comments