Skip to content

Commit

Permalink
[FIX] Test postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
josep-tecnativa committed Feb 5, 2025
1 parent bb774d5 commit 64f1ba8
Showing 1 changed file with 13 additions and 32 deletions.
45 changes: 13 additions & 32 deletions tests/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"


Expand Down

0 comments on commit 64f1ba8

Please sign in to comment.