Skip to content

Commit d042c21

Browse files
committed
refactor(test): eliminate tests.* imports in introspection tests
Replace `from tests.providers.scenarios` and `from tests.rhosocial...` imports with local implementations to match the coding style used in other test files. This ensures tests can run with both `pytest tests/` and `python -m pytest tests/` without path issues.
1 parent 3225ca6 commit d042c21

2 files changed

Lines changed: 95 additions & 16 deletions

File tree

‎tests/rhosocial/activerecord_postgres_test/feature/backend/introspection/conftest.py‎

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@
66
functionality with PostgreSQL backends.
77
"""
88

9+
import os
910
import pytest
1011
import pytest_asyncio
12+
import yaml
13+
from typing import Dict, Any, Tuple, Type
14+
15+
from rhosocial.activerecord.backend.impl.postgres import PostgresBackend, AsyncPostgresBackend
16+
from rhosocial.activerecord.backend.impl.postgres.config import PostgresConnectionConfig
1117

1218
# SQL statements for test tables
1319
_TABLES_SQL = [
@@ -145,6 +151,62 @@
145151
]
146152

147153

154+
# --- Scenario Loading Logic (consistent with feature/backend/conftest.py) ---
155+
156+
SCENARIO_MAP: Dict[str, Dict[str, Any]] = {}
157+
158+
159+
def _load_scenarios_from_config():
160+
"""Load scenarios from configuration file."""
161+
config_path = None
162+
env_config_path = os.getenv("POSTGRES_SCENARIOS_CONFIG_PATH")
163+
164+
if env_config_path and os.path.exists(env_config_path):
165+
config_path = env_config_path
166+
else:
167+
default_path = os.path.join(os.path.dirname(__file__), "../../../../../config", "postgres_scenarios.yaml")
168+
if os.path.exists(default_path):
169+
config_path = default_path
170+
171+
if not config_path:
172+
raise FileNotFoundError(
173+
"No PostgreSQL scenarios configuration file found. "
174+
"Set POSTGRES_SCENARIOS_CONFIG_PATH or place postgres_scenarios.yaml in tests/config."
175+
)
176+
177+
with open(config_path, 'r', encoding='utf-8') as f:
178+
config_data = yaml.safe_load(f)
179+
180+
if 'scenarios' not in config_data:
181+
raise ValueError(f"Configuration file {config_path} does not contain 'scenarios' key")
182+
183+
for scenario_name, config in config_data['scenarios'].items():
184+
if config:
185+
SCENARIO_MAP[scenario_name] = config
186+
187+
188+
_load_scenarios_from_config()
189+
190+
191+
def get_scenario(name: str) -> Tuple[Type[PostgresBackend], PostgresConnectionConfig]:
192+
"""Get backend class and connection config for a scenario."""
193+
if name not in SCENARIO_MAP:
194+
if SCENARIO_MAP:
195+
name = next(iter(SCENARIO_MAP))
196+
else:
197+
raise ValueError("No scenarios registered")
198+
199+
config_dict = SCENARIO_MAP[name].copy()
200+
config_dict.pop('enabled', None)
201+
config = PostgresConnectionConfig(**config_dict)
202+
return PostgresBackend, config
203+
204+
205+
def get_enabled_scenarios() -> Dict[str, Any]:
206+
"""Return all enabled scenarios."""
207+
return SCENARIO_MAP
208+
209+
148210
def _execute_sql_list(backend, sql_list):
149211
"""Execute a list of SQL statements."""
150212
for sql in sql_list:
@@ -154,8 +216,6 @@ def _execute_sql_list(backend, sql_list):
154216
@pytest.fixture(scope="function")
155217
def postgres_backend_single(request):
156218
"""Fixture providing a single PostgreSQL backend for introspection tests."""
157-
from tests.providers.scenarios import get_scenario, get_enabled_scenarios
158-
159219
# Get first available scenario
160220
enabled_scenarios = get_enabled_scenarios()
161221
if not enabled_scenarios:
@@ -173,9 +233,6 @@ def postgres_backend_single(request):
173233
@pytest_asyncio.fixture(scope="function")
174234
async def async_postgres_backend_single(request):
175235
"""Async fixture providing a single PostgreSQL backend for introspection tests."""
176-
from tests.providers.scenarios import get_scenario, get_enabled_scenarios
177-
from rhosocial.activerecord.backend.impl.postgres import AsyncPostgresBackend
178-
179236
# Get first available scenario
180237
enabled_scenarios = get_enabled_scenarios()
181238
if not enabled_scenarios:
@@ -193,8 +250,6 @@ async def async_postgres_backend_single(request):
193250
@pytest.fixture(scope="function")
194251
def backend_with_tables(request):
195252
"""Fixture providing backend with test tables created."""
196-
from tests.providers.scenarios import get_scenario, get_enabled_scenarios
197-
198253
# Get first available scenario
199254
enabled_scenarios = get_enabled_scenarios()
200255
if not enabled_scenarios:
@@ -226,9 +281,6 @@ def backend_with_tables(request):
226281
@pytest_asyncio.fixture(scope="function")
227282
async def async_backend_with_tables(request):
228283
"""Async fixture providing backend with test tables created."""
229-
from tests.providers.scenarios import get_scenario, get_enabled_scenarios
230-
from rhosocial.activerecord.backend.impl.postgres import AsyncPostgresBackend
231-
232284
# Get first available scenario
233285
enabled_scenarios = get_enabled_scenarios()
234286
if not enabled_scenarios:
@@ -263,8 +315,6 @@ async def async_backend_with_tables(request):
263315
@pytest.fixture(scope="function")
264316
def backend_with_view(request):
265317
"""Fixture providing backend with test view created."""
266-
from tests.providers.scenarios import get_scenario, get_enabled_scenarios
267-
268318
enabled_scenarios = get_enabled_scenarios()
269319
if not enabled_scenarios:
270320
pytest.skip("No PostgreSQL scenarios configured")
@@ -291,8 +341,6 @@ def backend_with_view(request):
291341
@pytest.fixture(scope="function")
292342
def backend_with_trigger(request):
293343
"""Fixture providing backend with test trigger created."""
294-
from tests.providers.scenarios import get_scenario, get_enabled_scenarios
295-
296344
enabled_scenarios = get_enabled_scenarios()
297345
if not enabled_scenarios:
298346
pytest.skip("No PostgreSQL scenarios configured")

‎tests/rhosocial/activerecord_postgres_test/feature/backend/introspection/test_introspection_views.py‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,39 @@
1010
from rhosocial.activerecord.backend.introspection.types import ViewInfo
1111

1212

13+
# SQL statements for async view tests (duplicated from conftest to avoid import issues)
14+
_VIEW_SQL = [
15+
"DROP VIEW IF EXISTS user_summary CASCADE",
16+
"DROP TABLE IF EXISTS user_stats CASCADE",
17+
"DROP TABLE IF EXISTS users CASCADE",
18+
"""
19+
CREATE TABLE users (
20+
id SERIAL PRIMARY KEY,
21+
name VARCHAR(100) NOT NULL,
22+
email VARCHAR(255) NOT NULL
23+
)
24+
""",
25+
"""
26+
CREATE TABLE user_stats (
27+
user_id INTEGER PRIMARY KEY,
28+
post_count INTEGER DEFAULT 0
29+
)
30+
""",
31+
"""
32+
CREATE VIEW user_summary AS
33+
SELECT u.id, u.name, u.email, COALESCE(s.post_count, 0) as post_count
34+
FROM users u
35+
LEFT JOIN user_stats s ON u.id = s.user_id
36+
""",
37+
]
38+
39+
_CLEANUP_VIEW_SQL = [
40+
"DROP VIEW IF EXISTS user_summary CASCADE",
41+
"DROP TABLE IF EXISTS user_stats CASCADE",
42+
"DROP TABLE IF EXISTS users CASCADE",
43+
]
44+
45+
1346
class TestListViews:
1447
"""Tests for list_views method."""
1548

@@ -102,7 +135,6 @@ class TestAsyncViewIntrospection:
102135
async def test_async_list_views(self, async_postgres_backend_single):
103136
"""Test async list_views returns view info."""
104137
# Create view manually for async test
105-
from tests.rhosocial.activerecord_postgres_test.feature.backend.introspection.conftest import _VIEW_SQL, _CLEANUP_VIEW_SQL
106138
for sql in _VIEW_SQL:
107139
await async_postgres_backend_single.execute(sql)
108140
async_postgres_backend_single.introspector.clear_cache()
@@ -119,7 +151,6 @@ async def test_async_list_views(self, async_postgres_backend_single):
119151
@pytest.mark.asyncio
120152
async def test_async_get_view_info(self, async_postgres_backend_single):
121153
"""Test async get_view_info for existing view."""
122-
from tests.rhosocial.activerecord_postgres_test.feature.backend.introspection.conftest import _VIEW_SQL, _CLEANUP_VIEW_SQL
123154
for sql in _VIEW_SQL:
124155
await async_postgres_backend_single.execute(sql)
125156
async_postgres_backend_single.introspector.clear_cache()

0 commit comments

Comments
 (0)