diff --git a/tests/test_postgres.py b/tests/test_postgres.py index d54f263d..58b0dbfc 100644 --- a/tests/test_postgres.py +++ b/tests/test_postgres.py @@ -15,52 +15,33 @@ def _get_db_service_name(dc: DockerClient) -> str: - """ - Use python-on-whales to retrieve the final Compose configuration - and return the DB service name that ends with '-db'. - Fallback to any service containing 'db' or 'postgres'. - Finally, fallback to 'db'. - """ config_data = dc.compose.config() - if ComposeConfig and isinstance(config_data, ComposeConfig): - services_dict = {s.name: s for s in config_data.services} + # (1) En versiones nuevas de python-on-whales + # config_data es ComposeConfig => config_data.services es un dict: + # { "nombre_servicio": ServiceConfig(...) } + # (2) En versiones viejas => config_data es un dict "legacy" + # => config_data["services"] => { "nombre_servicio": {...} } - elif isinstance(config_data, dict): - services_dict = config_data["services"] + if ComposeConfig and isinstance(config_data, ComposeConfig): + services_dict = config_data.services # <--- OJO: NO iterar con s.name else: - raise TypeError( - f"dc.compose.config() devolvió un tipo inesperado: {type(config_data)}" - ) - for svc_name in services_dict: - if svc_name.lower().endswith("-db"): - return svc_name - for svc_name in services_dict: - lower_svc_name = svc_name.lower() - if "postgres" in lower_svc_name or "db" in lower_svc_name: - return svc_name - return "db" + # Versión vieja => dict con clave "services" + services_dict = config_data["services"] + # Ahora "services_dict" es { "nombre_servicio": ... } en todos los casos -def _get_db_service_name(dc: DockerClient) -> str: - """ - Use python-on-whales to retrieve the final Compose configuration - and return the DB service name that ends with '-db'. Fallback to - any service containing 'db' or 'postgres'. Finally, fallback to 'db'. - """ - config_data = dc.compose.config() # Returns a dict with { "services": {...}, ... } - services_dict = config_data["services"] - # First pass: Look for a service name that exactly ends with '-db' + # 1ª pasada: servicio que termine con -db for svc_name in services_dict: if svc_name.lower().endswith("-db"): return svc_name - # Second pass: Fallback to any name containing 'db' or 'postgres' + # 2ª pasada: cualquier servicio que contenga db o postgres for svc_name in services_dict: if "postgres" in svc_name.lower() or "db" in svc_name.lower(): return svc_name - # Final fallback + # 3ª pasada: fallback return "db"