66functionality with PostgreSQL backends.
77"""
88
9+ import os
910import pytest
1011import 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 = [
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+
148210def _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" )
155217def 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" )
174234async 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" )
194251def 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" )
227282async 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" )
264316def 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" )
292342def 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" )
0 commit comments