Skip to content

Commit 8833534

Browse files
olivermeyerclaude
andcommitted
refactor: clean duplicate test helpers/fixtures
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 710f1f5 commit 8833534

10 files changed

Lines changed: 49 additions & 147 deletions

File tree

tests/aignostics_foundry_core/api/auth_test.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
require_internal,
2020
require_internal_admin,
2121
)
22-
from aignostics_foundry_core.foundry import reset_context, set_context
2322
from tests.aignostics_foundry_core.api import AUTH0_ROLE_CLAIM_VAR_NAME, INTERNAL_ORG_ID_VAR_NAME
24-
from tests.conftest import make_context
2523

2624
_INTERNAL_ORG_ID = "org_internal_123"
2725
_OTHER_ORG_ID = "org_other_456"
@@ -38,13 +36,11 @@ def _auth_context() -> Generator[None, None, None]: # pyright: ignore[reportUnu
3836
Yields:
3937
None
4038
"""
41-
set_context(make_context())
4239
os.environ[INTERNAL_ORG_ID_VAR_NAME] = _INTERNAL_ORG_ID
4340
os.environ[AUTH0_ROLE_CLAIM_VAR_NAME] = _TEST_ROLE_CLAIM
4441
yield
4542
os.environ.pop(INTERNAL_ORG_ID_VAR_NAME, None)
4643
os.environ.pop(AUTH0_ROLE_CLAIM_VAR_NAME, None)
47-
reset_context()
4844

4945

5046
@pytest.mark.unit

tests/aignostics_foundry_core/boot_test.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import pytest
1212

1313
import aignostics_foundry_core.boot as boot_mod
14-
from aignostics_foundry_core.foundry import set_context
1514
from tests.conftest import TEST_PROJECT_NAME, TEST_PROJECT_PREFIX, make_context
1615

1716
_OTHER_PROJECT = "otherapp"
@@ -93,7 +92,6 @@ def test_boot_uses_global_context_when_none_provided(monkeypatch: pytest.MonkeyP
9392
monkeypatch.setattr(boot_mod, "truststore", None)
9493
monkeypatch.setattr(boot_mod, "certifi", None)
9594

96-
set_context(make_context())
9795
boot_mod.boot(sentry_integrations=None)
9896

9997
call_ctx = mock_logging.call_args.kwargs["context"]
@@ -110,7 +108,6 @@ def test_boot_explicit_context_overrides_global(monkeypatch: pytest.MonkeyPatch)
110108
monkeypatch.setattr(boot_mod, "truststore", None)
111109
monkeypatch.setattr(boot_mod, "certifi", None)
112110

113-
set_context(make_context())
114111
explicit_ctx = make_context(_OTHER_PROJECT)
115112
boot_mod.boot(context=explicit_ctx, sentry_integrations=None)
116113

tests/aignostics_foundry_core/console_test.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
import importlib
44
import sys
5-
from collections.abc import Generator
65

76
import pytest
87
from rich.console import Console
98

109
from aignostics_foundry_core.console import console
11-
from aignostics_foundry_core.foundry import reset_context, set_context
10+
from aignostics_foundry_core.foundry import set_context
1211
from tests.conftest import make_context
1312

1413
EXPECTED_THEME_KEYS = ["success", "info", "warning", "error", "debug", "logging.level.info"]
@@ -17,18 +16,6 @@
1716
CONSOLE_MODULE = "aignostics_foundry_core.console"
1817

1918

20-
@pytest.fixture(autouse=True)
21-
def _reset_context() -> Generator[None, None, None]: # pyright: ignore[reportUnusedFunction]
22-
"""Reset global _context to None before and after every test.
23-
24-
Yields:
25-
None
26-
"""
27-
reset_context()
28-
yield
29-
reset_context()
30-
31-
3219
class TestConsole:
3320
"""Tests for the themed rich console module."""
3421

tests/aignostics_foundry_core/database_settings_test.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Tests for DatabaseSettings."""
22

3-
from collections.abc import Generator
43
from pathlib import Path
54

65
import pytest
@@ -28,14 +27,6 @@
2827
TEST_DB_NAME_ENV = "TEST_DB_NAME"
2928

3029

31-
@pytest.fixture(autouse=True)
32-
def _reset_context() -> Generator[None, None, None]: # pyright: ignore[reportUnusedFunction]
33-
"""Reset global context before and after every test."""
34-
reset_context()
35-
yield
36-
reset_context()
37-
38-
3930
# ---------------------------------------------------------------------------
4031
# get_url behaviour
4132
# ---------------------------------------------------------------------------
@@ -201,5 +192,6 @@ def test_database_settings_explicit_env_file_overrides_context(tmp_path: Path) -
201192
@pytest.mark.integration
202193
def test_database_settings_no_context_raises_without_prefix() -> None:
203194
"""DatabaseSettings() raises RuntimeError when no context is installed and no prefix is given."""
195+
reset_context()
204196
with pytest.raises(RuntimeError, match="get_context\\(\\) called before set_context"):
205197
DatabaseSettings()

tests/aignostics_foundry_core/foundry_test.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import subprocess
88
import sys
99
import textwrap
10-
from collections.abc import Generator
1110
from importlib.machinery import ModuleSpec
1211
from pathlib import Path
1312

@@ -109,18 +108,6 @@ def test_from_package_metadata_is_package_metadata_instance() -> None:
109108
assert ctx.metadata == PackageMetadata.from_name(PACKAGE_NAME)
110109

111110

112-
@pytest.fixture(autouse=True)
113-
def _reset_context() -> Generator[None, None, None]: # pyright: ignore[reportUnusedFunction]
114-
"""Reset global _context to None before and after every test.
115-
116-
Yields:
117-
None
118-
"""
119-
reset_context()
120-
yield
121-
reset_context()
122-
123-
124111
# ---------------------------------------------------------------------------
125112
# from_package — name and version
126113
# ---------------------------------------------------------------------------
@@ -471,6 +458,7 @@ def test_set_context_makes_context_accessible() -> None:
471458
@pytest.mark.unit
472459
def test_context_raises_before_set_context() -> None:
473460
"""get_context() before set_context() raises RuntimeError."""
461+
reset_context()
474462
with pytest.raises(RuntimeError, match=ERROR_MSG_FRAGMENT):
475463
get_context()
476464

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Shared fixtures for GUI tests."""
2+
3+
import os
4+
from collections.abc import Generator
5+
6+
import pytest
7+
8+
from aignostics_foundry_core.gui import clear_page_registry
9+
from tests.aignostics_foundry_core.api import AUTH0_ROLE_CLAIM_VAR_NAME, INTERNAL_ORG_ID_VAR_NAME
10+
11+
_INTERNAL_ORG = "org_internal"
12+
_ROLE_CLAIM = "https://example.com/role"
13+
14+
15+
@pytest.fixture(autouse=True)
16+
def _clear_registry() -> Generator[None, None, None]: # pyright: ignore[reportUnusedFunction]
17+
"""Ensure the page registry is clean before and after each test."""
18+
clear_page_registry()
19+
yield
20+
clear_page_registry()
21+
22+
23+
@pytest.fixture(autouse=True)
24+
def _gui_auth_context() -> Generator[None, None, None]: # pyright: ignore[reportUnusedFunction]
25+
"""Set required AuthSettings environment variables for GUI auth tests."""
26+
os.environ[INTERNAL_ORG_ID_VAR_NAME] = _INTERNAL_ORG
27+
os.environ[AUTH0_ROLE_CLAIM_VAR_NAME] = _ROLE_CLAIM
28+
yield
29+
os.environ.pop(INTERNAL_ORG_ID_VAR_NAME, None)
30+
os.environ.pop(AUTH0_ROLE_CLAIM_VAR_NAME, None)

tests/aignostics_foundry_core/gui/gui_test.py

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
"""Tests for aignostics_foundry_core.gui.*."""
22

33
import asyncio
4-
import os
54
import sys
65
import time
7-
from collections.abc import Generator
86
from contextlib import contextmanager
97
from types import SimpleNamespace
108
from unittest.mock import AsyncMock, MagicMock, patch
119

1210
import pytest
1311

14-
from aignostics_foundry_core.foundry import reset_context, set_context
1512
from aignostics_foundry_core.gui.core import (
1613
BROWSER_RECONNECT_TIMEOUT,
1714
RESPONSE_TIMEOUT,
@@ -25,7 +22,6 @@
2522
NavItem,
2623
gui_get_nav_groups,
2724
)
28-
from tests.aignostics_foundry_core.api import AUTH0_ROLE_CLAIM_VAR_NAME, INTERNAL_ORG_ID_VAR_NAME
2925
from tests.conftest import TEST_PROJECT_NAME, make_context
3026

3127
_PATCH_GET_GUI_USER = "aignostics_foundry_core.gui.auth.get_gui_user"
@@ -34,9 +30,7 @@
3430
_PATH_CORE_LOCATE = "aignostics_foundry_core.gui.core.locate_subclasses"
3531

3632
_TEST_PATH = "/test-page"
37-
_INTERNAL_ORG = "org_internal"
3833
_OTHER_ORG = "org_other"
39-
_ROLE_CLAIM = "https://example.com/role"
4034
_FIXED_PORT = 9000
4135
_DOCS_PATH = "/docs"
4236
_USER_SUB = "auth0|x"
@@ -248,15 +242,6 @@ def test_browser_reconnect_timeout_is_long(self) -> None:
248242
class TestGuiRegisterPages:
249243
"""Tests for gui_register_pages behaviour."""
250244

251-
@pytest.fixture(autouse=True)
252-
def _clear_registry(self) -> Generator[None, None, None]: # pyright: ignore[reportUnusedFunction]
253-
"""Ensure the page registry is clean before and after each test."""
254-
from aignostics_foundry_core.gui import clear_page_registry
255-
256-
clear_page_registry()
257-
yield
258-
clear_page_registry()
259-
260245
def test_calls_register_pages_on_each_builder(self) -> None:
261246
"""gui_register_pages calls register_pages() on every discovered builder."""
262247
builder_a = MagicMock(spec=BasePageBuilder)
@@ -463,17 +448,6 @@ def test_gui_register_pages_called(self) -> None:
463448
class TestGetGuiUser:
464449
"""Tests for get_gui_user behaviour."""
465450

466-
@pytest.fixture(autouse=True)
467-
def _gui_context(self) -> Generator[None, None, None]: # pyright: ignore[reportUnusedFunction]
468-
"""Install a minimal context and required AuthSettings env vars."""
469-
set_context(make_context())
470-
os.environ[INTERNAL_ORG_ID_VAR_NAME] = _INTERNAL_ORG
471-
os.environ[AUTH0_ROLE_CLAIM_VAR_NAME] = _ROLE_CLAIM
472-
yield
473-
os.environ.pop(INTERNAL_ORG_ID_VAR_NAME, None)
474-
os.environ.pop(AUTH0_ROLE_CLAIM_VAR_NAME, None)
475-
reset_context()
476-
477451
async def test_returns_none_when_auth_client_raises(self) -> None:
478452
"""Returns None when get_auth_client raises (no auth configured)."""
479453
from aignostics_foundry_core.gui.auth import get_gui_user
@@ -549,17 +523,6 @@ async def test_returns_none_when_session_has_no_user_key(self) -> None:
549523
class TestRequireGuiUser:
550524
"""Tests for require_gui_user behaviour."""
551525

552-
@pytest.fixture(autouse=True)
553-
def _gui_context(self) -> Generator[None, None, None]: # pyright: ignore[reportUnusedFunction]
554-
"""Install a minimal context and required AuthSettings env vars."""
555-
set_context(make_context())
556-
os.environ[INTERNAL_ORG_ID_VAR_NAME] = _INTERNAL_ORG
557-
os.environ[AUTH0_ROLE_CLAIM_VAR_NAME] = _ROLE_CLAIM
558-
yield
559-
os.environ.pop(INTERNAL_ORG_ID_VAR_NAME, None)
560-
os.environ.pop(AUTH0_ROLE_CLAIM_VAR_NAME, None)
561-
reset_context()
562-
563526
async def test_redirects_to_login_when_no_user(self) -> None:
564527
"""Redirects to /auth/login when get_gui_user returns None."""
565528
from aignostics_foundry_core.gui.auth import require_gui_user
@@ -635,22 +598,6 @@ def _make_nicegui_mock() -> tuple[MagicMock, MagicMock]:
635598
class TestPageRegistryDecorators:
636599
"""Tests for page_* registry decorators (deferred registration)."""
637600

638-
@pytest.fixture(autouse=True)
639-
def _clear_registry(self) -> Generator[None, None, None]: # pyright: ignore[reportUnusedFunction]
640-
"""Ensure registry is clean before and after each test."""
641-
from aignostics_foundry_core.gui import clear_page_registry
642-
643-
clear_page_registry()
644-
yield
645-
clear_page_registry()
646-
647-
@pytest.fixture(autouse=True)
648-
def _context(self) -> Generator[None, None, None]: # pyright: ignore[reportUnusedFunction]
649-
"""Install a minimal FoundryContext so get_context() works inside page wrappers."""
650-
set_context(make_context())
651-
yield
652-
reset_context()
653-
654601
def _actualize_via_register_pages(self, frame_func: object = None) -> tuple[list[object], MagicMock]:
655602
"""Run gui_register_pages and return (wrappers, nicegui_mock).
656603
@@ -909,13 +856,6 @@ def my_page(user: object) -> None: ...
909856
class TestGUINamespace:
910857
"""Tests for GUINamespace and the gui singleton."""
911858

912-
@pytest.fixture(autouse=True)
913-
def _context(self) -> Generator[None, None, None]: # pyright: ignore[reportUnusedFunction]
914-
"""Install a minimal FoundryContext so get_context() works inside page wrappers."""
915-
set_context(make_context())
916-
yield
917-
reset_context()
918-
919859
def test_gui_exposes_all_decorator_methods(self) -> None:
920860
"""The gui singleton exposes all page decorator methods as callables."""
921861
from aignostics_foundry_core.gui.auth import gui

tests/aignostics_foundry_core/log_test.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from pydantic import ValidationError
99

1010
from aignostics_foundry_core.log import InterceptHandler, LogSettings, logging_initialize
11-
from tests.conftest import TEST_PROJECT_PREFIX, make_context
11+
from tests.conftest import TEST_PROJECT_PREFIX
1212

1313
_MARKER_MESSAGE = "log_test_unique_marker_4f2a"
1414
_STDLIB_MESSAGE = "stdlib_redirect_unique_marker_9b3c"
@@ -23,13 +23,6 @@
2323
class TestLoggingInitialize:
2424
"""Behavioural tests for logging_initialize()."""
2525

26-
@pytest.fixture(autouse=True)
27-
def _stub_get_context(self, monkeypatch: pytest.MonkeyPatch) -> None:
28-
monkeypatch.setattr(
29-
"aignostics_foundry_core.log.get_context",
30-
make_context,
31-
)
32-
3326
def test_logging_initialize_adds_stderr_handler(self, capsys: pytest.CaptureFixture[str]) -> None:
3427
"""After initialization with defaults, a log message appears on stderr."""
3528
logging_initialize()
@@ -117,10 +110,6 @@ def test_logging_initialize_respects_env_prefix_from_context(
117110
self, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
118111
) -> None:
119112
"""LogSettings reads env vars from the prefix of the active get_context()."""
120-
monkeypatch.setattr(
121-
"aignostics_foundry_core.log.get_context",
122-
make_context,
123-
)
124113
monkeypatch.setenv(f"{TEST_PROJECT_PREFIX}LOG_STDERR_ENABLED", "false")
125114
logging_initialize()
126115
from loguru import logger
@@ -133,20 +122,9 @@ def test_logging_initialize_respects_env_prefix_from_context(
133122
class TestLogSettings:
134123
"""Behavioural tests for LogSettings validation."""
135124

136-
@pytest.fixture(autouse=True)
137-
def _stub_get_context(self, monkeypatch: pytest.MonkeyPatch) -> None:
138-
monkeypatch.setattr(
139-
"aignostics_foundry_core.log.get_context",
140-
make_context,
141-
)
142-
143125
@pytest.mark.unit
144126
def test_log_settings_uses_context_env_prefix(self, monkeypatch: pytest.MonkeyPatch) -> None:
145127
"""LogSettings reads env vars using the env_prefix from the active FoundryContext."""
146-
monkeypatch.setattr(
147-
"aignostics_foundry_core.log.get_context",
148-
make_context,
149-
)
150128
monkeypatch.setenv(f"{TEST_PROJECT_PREFIX}LOG_STDERR_ENABLED", "false")
151129
settings = LogSettings() # pyright: ignore[reportCallIssue]
152130
assert settings.stderr_enabled is False

0 commit comments

Comments
 (0)