|
| 1 | +"""Shared fixtures and utilities for SDK tests.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from types import SimpleNamespace |
| 6 | +from typing import Any |
| 7 | +from unittest.mock import Mock, AsyncMock |
| 8 | + |
| 9 | +import httpx |
| 10 | +import pytest |
| 11 | + |
| 12 | +from runloop_api_client import Runloop, AsyncRunloop |
| 13 | + |
| 14 | + |
| 15 | +def create_mock_httpx_client(methods: dict[str, Any] | None = None) -> AsyncMock: |
| 16 | + """ |
| 17 | + Create a mock httpx.AsyncClient with proper context manager setup. |
| 18 | +
|
| 19 | + Args: |
| 20 | + methods: Optional dict of method names to AsyncMock return values. |
| 21 | + Common keys: 'get', 'put' |
| 22 | +
|
| 23 | + Returns: |
| 24 | + Configured AsyncMock for httpx.AsyncClient |
| 25 | + """ |
| 26 | + mock_client = AsyncMock() |
| 27 | + mock_client.__aenter__ = AsyncMock(return_value=mock_client) |
| 28 | + mock_client.__aexit__ = AsyncMock(return_value=None) |
| 29 | + |
| 30 | + if methods: |
| 31 | + for method_name, return_value in methods.items(): |
| 32 | + setattr(mock_client, method_name, AsyncMock(return_value=return_value)) |
| 33 | + |
| 34 | + return mock_client |
| 35 | + |
| 36 | + |
| 37 | +def create_mock_httpx_response(**attrs: Any) -> Mock: |
| 38 | + """ |
| 39 | + Create a mock httpx.Response with specified attributes. |
| 40 | +
|
| 41 | + Args: |
| 42 | + **attrs: Attributes to set on the mock response. |
| 43 | + Common: content, text, encoding |
| 44 | +
|
| 45 | + Returns: |
| 46 | + Mock configured with httpx.Response spec and attributes |
| 47 | + """ |
| 48 | + mock_response = Mock(spec=httpx.Response) |
| 49 | + for key, value in attrs.items(): |
| 50 | + setattr(mock_response, key, value) |
| 51 | + return mock_response |
| 52 | + |
| 53 | + |
| 54 | +@pytest.fixture |
| 55 | +def mock_client() -> Mock: |
| 56 | + """Create a mock Runloop client.""" |
| 57 | + return Mock(spec=Runloop) |
| 58 | + |
| 59 | + |
| 60 | +@pytest.fixture |
| 61 | +def mock_async_client() -> AsyncMock: |
| 62 | + """Create a mock AsyncRunloop client.""" |
| 63 | + return AsyncMock(spec=AsyncRunloop) |
| 64 | + |
| 65 | + |
| 66 | +@pytest.fixture |
| 67 | +def devbox_view() -> SimpleNamespace: |
| 68 | + """Create a mock DevboxView.""" |
| 69 | + return SimpleNamespace( |
| 70 | + id="dev_123", |
| 71 | + status="running", |
| 72 | + name="test-devbox", |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | +@pytest.fixture |
| 77 | +def execution_view() -> SimpleNamespace: |
| 78 | + """Create a mock DevboxAsyncExecutionDetailView.""" |
| 79 | + return SimpleNamespace( |
| 80 | + execution_id="exec_123", |
| 81 | + devbox_id="dev_123", |
| 82 | + status="completed", |
| 83 | + exit_status=0, |
| 84 | + stdout="output", |
| 85 | + stderr="", |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +@pytest.fixture |
| 90 | +def snapshot_view() -> SimpleNamespace: |
| 91 | + """Create a mock DevboxSnapshotView.""" |
| 92 | + return SimpleNamespace( |
| 93 | + id="snap_123", |
| 94 | + status="completed", |
| 95 | + name="test-snapshot", |
| 96 | + ) |
| 97 | + |
| 98 | + |
| 99 | +@pytest.fixture |
| 100 | +def blueprint_view() -> SimpleNamespace: |
| 101 | + """Create a mock BlueprintView.""" |
| 102 | + return SimpleNamespace( |
| 103 | + id="bp_123", |
| 104 | + status="built", |
| 105 | + name="test-blueprint", |
| 106 | + ) |
| 107 | + |
| 108 | + |
| 109 | +@pytest.fixture |
| 110 | +def object_view() -> SimpleNamespace: |
| 111 | + """Create a mock ObjectView.""" |
| 112 | + return SimpleNamespace( |
| 113 | + id="obj_123", |
| 114 | + upload_url="https://upload.example.com/obj_123", |
| 115 | + name="test-object", |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +@pytest.fixture |
| 120 | +def mock_httpx_response() -> Mock: |
| 121 | + """Create a mock httpx.Response.""" |
| 122 | + response = Mock(spec=httpx.Response) |
| 123 | + response.status_code = 200 |
| 124 | + response.content = b"test content" |
| 125 | + response.text = "test content" |
| 126 | + response.encoding = "utf-8" |
| 127 | + response.raise_for_status = Mock() |
| 128 | + return response |
| 129 | + |
| 130 | + |
| 131 | +@pytest.fixture |
| 132 | +def mock_stream() -> Mock: |
| 133 | + """Create a mock Stream for testing.""" |
| 134 | + stream = Mock() |
| 135 | + stream.__iter__ = Mock(return_value=iter([])) |
| 136 | + stream.__enter__ = Mock(return_value=stream) |
| 137 | + stream.__exit__ = Mock(return_value=None) |
| 138 | + stream.close = Mock() |
| 139 | + return stream |
| 140 | + |
| 141 | + |
| 142 | +@pytest.fixture |
| 143 | +def mock_async_stream() -> AsyncMock: |
| 144 | + """Create a mock AsyncStream for testing.""" |
| 145 | + |
| 146 | + async def async_iter(): |
| 147 | + # Empty async iterator |
| 148 | + if False: |
| 149 | + yield |
| 150 | + |
| 151 | + stream = AsyncMock() |
| 152 | + stream.__aiter__ = Mock(return_value=async_iter()) |
| 153 | + stream.__aenter__ = AsyncMock(return_value=stream) |
| 154 | + stream.__aexit__ = AsyncMock(return_value=None) |
| 155 | + stream.close = AsyncMock() |
| 156 | + return stream |
0 commit comments