Skip to content

Commit

Permalink
refactor: rename health status options to match RFC
Browse files Browse the repository at this point in the history
  • Loading branch information
hartungstenio committed Jan 24, 2025
1 parent 3fd4bc7 commit ce40bdf
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 36 deletions.
11 changes: 11 additions & 0 deletions src/django_healthy/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,27 @@
from typing_extensions import TypeAlias

if sys.version_info >= (3, 11):
from enum import StrEnum
from typing import NotRequired, Self, TypedDict
else:
from enum import Enum
from typing import no_type_check

from typing_extensions import NotRequired, Self, TypedDict

class StrEnum(str, Enum):
@no_type_check
@staticmethod
def _generate_next_value_(name, start, count, last_values): # noqa: ARG004
return name.lower()


__all__ = [
"Mapping",
"MutableMapping",
"NotRequired",
"Self",
"StrEnum",
"TypeAlias",
"TypedDict",
]
18 changes: 10 additions & 8 deletions src/django_healthy/health_checks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from enum import Enum, auto
from enum import auto
from typing import TYPE_CHECKING, Any

from django_healthy._compat import StrEnum

if TYPE_CHECKING:
from django_healthy._compat import Self


class HealthStatus(Enum):
UNHEALTHY = auto()
DEGRADED = auto()
HEALTHY = auto()
class HealthStatus(StrEnum):
FAIL = auto()
WARN = auto()
PASS = auto()


@dataclass
Expand All @@ -30,7 +32,7 @@ def healthy(
data: dict[str, Any] | None = None,
) -> Self:
return cls(
status=HealthStatus.HEALTHY,
status=HealthStatus.PASS,
description=description,
exception=exception,
data=data or {},
Expand All @@ -44,7 +46,7 @@ def degraded(
data: dict[str, Any] | None = None,
) -> Self:
return cls(
status=HealthStatus.DEGRADED,
status=HealthStatus.WARN,
description=description,
exception=exception,
data=data or {},
Expand All @@ -58,7 +60,7 @@ def unhealthy(
data: dict[str, Any] | None = None,
) -> Self:
return cls(
status=HealthStatus.UNHEALTHY,
status=HealthStatus.FAIL,
description=description,
exception=exception,
data=data or {},
Expand Down
2 changes: 1 addition & 1 deletion src/django_healthy/templates/django_healthy/report.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{% for system, reported in report.entries.items %}
<tr>
<td>{{ system }}</td>
<td>{{ reported.status.name }}</td>
<td>{{ reported.status }}</td>
<td>{{ reported.duration.total_seconds }}</td>
</tr>
{% endfor %}
Expand Down
12 changes: 6 additions & 6 deletions tests/health_checks/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class TestHealthCheckResult:
def test_healthy_defaults(self):
got = HealthCheckResult.healthy()

assert got.status == HealthStatus.HEALTHY
assert got.status == HealthStatus.PASS
assert got.description is None
assert got.exception is None
assert got.data == {}
Expand All @@ -21,15 +21,15 @@ def test_healthy_params(self, faker):
data=given_data,
)

assert got.status == HealthStatus.HEALTHY
assert got.status == HealthStatus.PASS
assert got.description == given_description
assert got.exception == given_exception
assert got.data == given_data

def test_degraded_defaults(self):
got = HealthCheckResult.degraded()

assert got.status == HealthStatus.DEGRADED
assert got.status == HealthStatus.WARN
assert got.description is None
assert got.exception is None
assert got.data == {}
Expand All @@ -45,15 +45,15 @@ def test_degraded_params(self, faker):
data=given_data,
)

assert got.status == HealthStatus.DEGRADED
assert got.status == HealthStatus.WARN
assert got.description == given_description
assert got.exception == given_exception
assert got.data == given_data

def test_unhealthy_defaults(self):
got = HealthCheckResult.unhealthy()

assert got.status == HealthStatus.UNHEALTHY
assert got.status == HealthStatus.FAIL
assert got.description is None
assert got.exception is None
assert got.data == {}
Expand All @@ -69,7 +69,7 @@ def test_unhealthy_params(self, faker):
data=given_data,
)

assert got.status == HealthStatus.UNHEALTHY
assert got.status == HealthStatus.FAIL
assert got.description == given_description
assert got.exception == given_exception
assert got.data == given_data
6 changes: 3 additions & 3 deletions tests/health_checks/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ async def test_check_health_with_working_cache(self):

got = await health_check.check_health()

assert got.status == HealthStatus.HEALTHY
assert got.status == HealthStatus.PASS

async def test_check_health_with_invalid_value(self):
health_check = CacheHealthCheck(alias="dummy")

got = await health_check.check_health()

assert got.status == HealthStatus.DEGRADED
assert got.status == HealthStatus.WARN
assert "given" in got.data
assert "got" in got.data

Expand All @@ -32,5 +32,5 @@ async def test_check_health_with_broken_cache(self):
with mock.patch.object(cache, "aset", side_effect=RuntimeError):
got = await health_check.check_health()

assert got.status == HealthStatus.UNHEALTHY
assert got.status == HealthStatus.FAIL
assert isinstance(got.exception, RuntimeError)
16 changes: 8 additions & 8 deletions tests/health_checks/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,28 @@ async def test_check_health_with_working_database(self):

got = await health_check.check_health()

assert got.status == HealthStatus.HEALTHY
assert got.status == HealthStatus.PASS

async def test_check_health_with_working_database_custom_query(self):
health_check = DatabasePingHealthCheck(query="SELECT 1")

got = await health_check.check_health()

assert got.status == HealthStatus.HEALTHY
assert got.status == HealthStatus.PASS

async def test_check_health_with_working_database_invalid_query(self):
health_check = DatabasePingHealthCheck(query="INVALID QUERY")

got = await health_check.check_health()

assert got.status == HealthStatus.UNHEALTHY
assert got.status == HealthStatus.FAIL

async def test_check_health_with_broken_database(self):
health_check = DatabasePingHealthCheck(alias="dummy")

got = await health_check.check_health()

assert got.status == HealthStatus.UNHEALTHY
assert got.status == HealthStatus.FAIL


@pytest.mark.asyncio
Expand All @@ -47,28 +47,28 @@ async def test_check_health_with_working_database(self):

got = await health_check.check_health()

assert got.status == HealthStatus.HEALTHY
assert got.status == HealthStatus.PASS

async def test_check_health_with_creation_error(self):
health_check = DatabaseModelHealthCheck()

with mock.patch("django_healthy.health_checks.db.Test.asave", side_effect=DatabaseError):
got = await health_check.check_health()

assert got.status == HealthStatus.UNHEALTHY
assert got.status == HealthStatus.FAIL

async def test_check_health_with_update_error(self):
health_check = DatabaseModelHealthCheck()

with mock.patch("django_healthy.health_checks.db.Test.asave", side_effect=[True, DatabaseError]):
got = await health_check.check_health()

assert got.status == HealthStatus.UNHEALTHY
assert got.status == HealthStatus.FAIL

async def test_check_health_with_deletion_error(self):
health_check = DatabaseModelHealthCheck()

with mock.patch("django_healthy.health_checks.db.Test.adelete", side_effect=DatabaseError):
got = await health_check.check_health()

assert got.status == HealthStatus.UNHEALTHY
assert got.status == HealthStatus.FAIL
8 changes: 4 additions & 4 deletions tests/health_checks/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async def test_with_default_handler(self):

got = await service.check_health()

assert got.status == HealthStatus.HEALTHY
assert got.status == HealthStatus.PASS
assert set(got.entries.keys()) == set(settings.HEALTH_CHECKS.keys())

async def test_with_custom_handler(self):
Expand All @@ -31,7 +31,7 @@ async def test_with_custom_handler(self):

got = await service.check_health()

assert got.status == HealthStatus.HEALTHY
assert got.status == HealthStatus.PASS
assert set(got.entries.keys()) == {"test"}

async def test_with_unhealthy_service(self):
Expand All @@ -50,7 +50,7 @@ async def test_with_unhealthy_service(self):

got = await service.check_health()

assert got.status == HealthStatus.UNHEALTHY
assert got.status == HealthStatus.FAIL
assert set(got.entries.keys()) == {"test"}

async def test_with_multiple_service_status_gets_worst_case(self):
Expand All @@ -75,5 +75,5 @@ async def test_with_multiple_service_status_gets_worst_case(self):

got = await service.check_health()

assert got.status == HealthStatus.UNHEALTHY
assert got.status == HealthStatus.FAIL
assert set(got.entries.keys()) == {"healthy", "unhealthy"}
12 changes: 6 additions & 6 deletions tests/health_checks/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async def test_with_working_storage(self):

got = await health_check.check_health()

assert got.status == HealthStatus.HEALTHY
assert got.status == HealthStatus.PASS

async def test_without_saving(self):
health_check = StorageHealthCheck()
Expand All @@ -23,7 +23,7 @@ async def test_without_saving(self):
with mock.patch.object(storage, "save"):
got = await health_check.check_health()

assert got.status == HealthStatus.DEGRADED
assert got.status == HealthStatus.WARN
assert "filename" in got.data

async def test_without_deleting(self):
Expand All @@ -33,7 +33,7 @@ async def test_without_deleting(self):
with mock.patch.object(storage, "delete"):
got = await health_check.check_health()

assert got.status == HealthStatus.DEGRADED
assert got.status == HealthStatus.WARN
assert "filename" in got.data

async def test_with_save_error(self):
Expand All @@ -43,7 +43,7 @@ async def test_with_save_error(self):
with mock.patch.object(storage, "save", side_effect=Exception):
got = await health_check.check_health()

assert got.status == HealthStatus.UNHEALTHY
assert got.status == HealthStatus.FAIL

async def test_with_delete_error(self):
health_check = StorageHealthCheck()
Expand All @@ -52,7 +52,7 @@ async def test_with_delete_error(self):
with mock.patch.object(storage, "delete", side_effect=Exception):
got = await health_check.check_health()

assert got.status == HealthStatus.UNHEALTHY
assert got.status == HealthStatus.FAIL

async def test_with_exists_error(self):
health_check = StorageHealthCheck()
Expand All @@ -61,4 +61,4 @@ async def test_with_exists_error(self):
with mock.patch.object(storage, "exists", side_effect=Exception):
got = await health_check.check_health()

assert got.status == HealthStatus.UNHEALTHY
assert got.status == HealthStatus.FAIL

0 comments on commit ce40bdf

Please sign in to comment.