Skip to content

Commit 6d710bf

Browse files
committed
✅ Isolate tests from config
Shortcake-Parent: main
1 parent fae7dee commit 6d710bf

11 files changed

Lines changed: 144 additions & 108 deletions

tests/conftest.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1+
import os
12
import sys
3+
import tempfile
24
from collections.abc import Generator
35
from dataclasses import dataclass
46
from pathlib import Path
5-
from unittest.mock import patch
67

78
import pytest
9+
import respx
810
from typer import rich_utils
911

12+
from fastapi_cloud_cli.config import Settings
13+
from fastapi_cloud_cli.utils.config import CONFIG_DIR_ENV_VAR
14+
1015
from .utils import create_jwt_token
1116

1217

18+
@pytest.fixture(autouse=True)
19+
def isolate_config() -> Generator[None]:
20+
with tempfile.TemporaryDirectory() as tmpdir:
21+
os.environ[CONFIG_DIR_ENV_VAR] = str(tmpdir)
22+
23+
yield
24+
25+
1326
@pytest.fixture(autouse=True)
1427
def reset_syspath() -> Generator[None, None, None]:
1528
initial_python_path = sys.path.copy()
@@ -26,6 +39,23 @@ def setup_terminal() -> None:
2639
return
2740

2841

42+
@pytest.fixture
43+
def settings() -> Settings:
44+
return Settings.get()
45+
46+
47+
@pytest.fixture
48+
def respx_mock(
49+
request: pytest.FixtureRequest, settings: Settings
50+
) -> Generator[respx.MockRouter, None, None]:
51+
respx_marker = request.node.get_closest_marker("respx")
52+
kwargs = {} if respx_marker is None else dict(respx_marker.kwargs)
53+
kwargs.setdefault("base_url", settings.base_api_url)
54+
55+
with respx.mock(**kwargs) as mock_router:
56+
yield mock_router
57+
58+
2959
@pytest.fixture
3060
def logged_in_cli(temp_auth_config: Path) -> Generator[None, None, None]:
3161
valid_token = create_jwt_token({"sub": "test_user_12345"})
@@ -63,10 +93,10 @@ def configured_app(tmp_path: Path) -> ConfiguredApp:
6393

6494

6595
@pytest.fixture
66-
def temp_auth_config(tmp_path: Path) -> Generator[Path, None, None]:
96+
def temp_auth_config(
97+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
98+
) -> Generator[Path, None, None]:
6799
"""Provides a temporary auth config setup for testing file operations."""
68100

69-
with patch(
70-
"fastapi_cloud_cli.utils.config.get_config_folder", return_value=tmp_path
71-
):
72-
yield tmp_path / "auth.json"
101+
monkeypatch.setenv(CONFIG_DIR_ENV_VAR, str(tmp_path))
102+
yield tmp_path / "auth.json"

tests/test_api_client.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
)
1818
from tests.utils import build_logs_response
1919

20-
settings = Settings.get()
21-
2220

2321
@pytest.fixture
2422
def client() -> httpx.Client:
@@ -31,7 +29,7 @@ def deployment_id() -> str:
3129
return "test-deployment-123"
3230

3331

34-
api_mock = respx.mock(base_url=settings.base_api_url)
32+
api_mock = respx.mock(base_url=Settings().base_api_url)
3533

3634

3735
@pytest.fixture
@@ -284,7 +282,7 @@ def test_stream_build_logs_empty_lines_are_skipped(
284282
assert logs[1].type == "complete"
285283

286284

287-
@respx.mock(base_url=settings.base_api_url)
285+
@respx.mock(base_url=Settings().base_api_url)
288286
def test_stream_build_logs_continue_after_timeout(
289287
respx_mock: respx.MockRouter,
290288
client: APIClient,

0 commit comments

Comments
 (0)