Skip to content

Commit 4de16fd

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

8 files changed

Lines changed: 68 additions & 140 deletions

File tree

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 & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
"""Tests for DatabaseSettings."""
22

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

65
import pytest
76

87
from aignostics_foundry_core.database import DatabaseSettings
9-
from aignostics_foundry_core.foundry import reset_context, set_context
8+
from aignostics_foundry_core.foundry import set_context
109
from tests.conftest import make_context
1110

1211
# Constants (SonarQube S1192)
@@ -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
# ---------------------------------------------------------------------------

tests/aignostics_foundry_core/foundry_test.py

Lines changed: 0 additions & 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
# ---------------------------------------------------------------------------
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.foundry import set_context
9+
from aignostics_foundry_core.gui import clear_page_registry
10+
from tests.aignostics_foundry_core.api import AUTH0_ROLE_CLAIM_VAR_NAME, INTERNAL_ORG_ID_VAR_NAME
11+
from tests.conftest import make_context
12+
13+
_INTERNAL_ORG = "org_internal"
14+
_ROLE_CLAIM = "https://example.com/role"
15+
16+
17+
@pytest.fixture(autouse=True)
18+
def _clear_registry() -> Generator[None, None, None]: # pyright: ignore[reportUnusedFunction]
19+
"""Ensure the page registry is clean before and after each test."""
20+
clear_page_registry()
21+
yield
22+
clear_page_registry()
23+
24+
25+
@pytest.fixture(autouse=True)
26+
def _context() -> None: # pyright: ignore[reportUnusedFunction]
27+
"""Install a minimal FoundryContext so get_context() works inside tests."""
28+
set_context(make_context())
29+
30+
31+
@pytest.fixture(autouse=True)
32+
def _gui_auth_context() -> Generator[None, None, None]: # pyright: ignore[reportUnusedFunction]
33+
"""Set required AuthSettings environment variables for GUI auth tests."""
34+
os.environ[INTERNAL_ORG_ID_VAR_NAME] = _INTERNAL_ORG
35+
os.environ[AUTH0_ROLE_CLAIM_VAR_NAME] = _ROLE_CLAIM
36+
yield
37+
os.environ.pop(INTERNAL_ORG_ID_VAR_NAME, None)
38+
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: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pytest
88
from pydantic import ValidationError
99

10+
from aignostics_foundry_core.foundry import set_context
1011
from aignostics_foundry_core.log import InterceptHandler, LogSettings, logging_initialize
1112
from tests.conftest import TEST_PROJECT_PREFIX, make_context
1213

@@ -18,18 +19,16 @@
1819
_SENTRY_MARKER = "sentry.io unique drop marker 2f4e"
1920

2021

22+
@pytest.fixture(autouse=True)
23+
def _context() -> None: # pyright: ignore[reportUnusedFunction]
24+
set_context(make_context())
25+
26+
2127
@pytest.mark.sequential
2228
@pytest.mark.unit
2329
class TestLoggingInitialize:
2430
"""Behavioural tests for logging_initialize()."""
2531

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-
3332
def test_logging_initialize_adds_stderr_handler(self, capsys: pytest.CaptureFixture[str]) -> None:
3433
"""After initialization with defaults, a log message appears on stderr."""
3534
logging_initialize()
@@ -117,10 +116,6 @@ def test_logging_initialize_respects_env_prefix_from_context(
117116
self, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
118117
) -> None:
119118
"""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-
)
124119
monkeypatch.setenv(f"{TEST_PROJECT_PREFIX}LOG_STDERR_ENABLED", "false")
125120
logging_initialize()
126121
from loguru import logger
@@ -133,20 +128,9 @@ def test_logging_initialize_respects_env_prefix_from_context(
133128
class TestLogSettings:
134129
"""Behavioural tests for LogSettings validation."""
135130

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-
143131
@pytest.mark.unit
144132
def test_log_settings_uses_context_env_prefix(self, monkeypatch: pytest.MonkeyPatch) -> None:
145133
"""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-
)
150134
monkeypatch.setenv(f"{TEST_PROJECT_PREFIX}LOG_STDERR_ENABLED", "false")
151135
settings = LogSettings() # pyright: ignore[reportCallIssue]
152136
assert settings.stderr_enabled is False

tests/aignostics_foundry_core/sentry_test.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
"""Tests for aignostics_foundry_core.sentry."""
22

3-
from collections.abc import Generator
43
from unittest.mock import MagicMock, patch
54

65
import pytest
76
from pydantic import ValidationError
87

9-
from aignostics_foundry_core.foundry import reset_context, set_context
8+
from aignostics_foundry_core.foundry import set_context
109
from aignostics_foundry_core.sentry import SentrySettings, sentry_initialize, set_sentry_user
1110
from tests.conftest import TEST_PROJECT_NAME, TEST_PROJECT_PREFIX, make_context
1211

12+
13+
@pytest.fixture(autouse=True)
14+
def _context() -> None: # pyright: ignore[reportUnusedFunction]
15+
"""Set a test context before every sentry test."""
16+
set_context(make_context())
17+
18+
1319
_VALID_DSN = "https://abc123def456@o99999.ingest.de.sentry.io/1234567"
1420
_SENTRY_SET_USER = "sentry_sdk.set_user"
1521
_AUTH0_USER = "auth0|x"
@@ -23,12 +29,6 @@
2329
class TestSentryInitialize:
2430
"""Behavioural tests for sentry_initialize()."""
2531

26-
@pytest.fixture(autouse=True)
27-
def _context(self) -> Generator[None, None, None]:
28-
set_context(make_context())
29-
yield
30-
reset_context()
31-
3232
def test_sentry_initialize_returns_false_when_disabled(self, monkeypatch: pytest.MonkeyPatch) -> None:
3333
"""Returns False when TESTPROJECT_SENTRY_ENABLED is not set (default False)."""
3434
monkeypatch.delenv(f"{_SENTRY_PREFIX}ENABLED", raising=False)
@@ -110,12 +110,6 @@ def test_sentry_initialize_uses_sentry_context_flags(self, monkeypatch: pytest.M
110110
class TestSentrySettingsDsnValidation:
111111
"""Tests for SentrySettings DSN edge-case validation paths."""
112112

113-
@pytest.fixture(autouse=True)
114-
def _context(self) -> Generator[None, None, None]:
115-
set_context(make_context())
116-
yield
117-
reset_context()
118-
119113
def test_dsn_missing_scheme_raises(self) -> None:
120114
"""DSN without a URL scheme raises ValidationError."""
121115
with pytest.raises(ValidationError):
@@ -136,12 +130,6 @@ def test_dsn_missing_at_sign_raises(self) -> None:
136130
class TestSentrySettings:
137131
"""Behavioural tests for SentrySettings validation."""
138132

139-
@pytest.fixture(autouse=True)
140-
def _context(self) -> Generator[None, None, None]:
141-
set_context(make_context())
142-
yield
143-
reset_context()
144-
145133
def test_sentry_settings_rejects_invalid_dsn_http_scheme(self) -> None:
146134
"""DSN with http:// scheme raises ValidationError."""
147135
with pytest.raises(ValidationError):

0 commit comments

Comments
 (0)