Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions health_check/contrib/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,15 @@ def __repr__(self):
client = self.client

try:
conn_kwargs = client.connection_pool.connection_kwargs
host = conn_kwargs["host"]
db = conn_kwargs["db"]
return f"Redis(client=RedisClient(host={host}, db={db}))"
except (AttributeError, KeyError):
safe_connection_str = ", ".join(
f"{key}={value!r}"
for key, value in sorted(
client.connection_pool.connection_kwargs.items()
)
if key in {"host", "db"}
)
return f"Redis({safe_connection_str})"
except AttributeError:
pass

try:
Expand Down
6 changes: 3 additions & 3 deletions tests/contrib/test_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def test_redis__repr_standard_client(self):
check = RedisHealthCheck(
client_factory=lambda: RedisClient(host="myhost", port=6379, db=2)
)
assert repr(check) == "Redis(client=RedisClient(host=myhost, db=2))"
assert repr(check) == "Redis(db=2, host='myhost')"
Comment thread
codingjoe marked this conversation as resolved.

def test_redis__repr_from_url(self):
"""Verify repr includes host and db when client is created via from_url."""
Expand All @@ -167,7 +167,7 @@ def test_redis__repr_from_url(self):
"redis://cache.example.com:6379/3"
)
)
assert "host=cache.example.com" in repr(check), (
assert "host='cache.example.com'" in repr(check), (
"repr should include the host from the Redis URL"
)
assert "db=3" in repr(check), "repr should include the db from the Redis URL"
Expand All @@ -180,7 +180,7 @@ def test_redis__repr_deprecated_client(self):
check = RedisHealthCheck(
client=RedisClient(host="oldhost", port=6379, db=5)
)
assert "host=oldhost" in repr(check), (
assert "host='oldhost'" in repr(check), (
"repr should include the host from the deprecated client"
)
assert "db=5" in repr(check), (
Expand Down