Skip to content

Commit 8f3bb33

Browse files
author
Andrei Neagu
committed
added tests for health
1 parent ab870e9 commit 8f3bb33

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

services/notifications/src/simcore_service_notifications/services/postgres/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@
1414
async def postgres_lifespan(app: FastAPI, state: State) -> AsyncIterator[State]:
1515
app.state.engine = state[PostgresLifespanState.POSTGRES_ASYNC_ENGINE]
1616

17-
app.state.postgres_health = PostgresLiveness(app)
17+
app.state.postgres_liveness = PostgresLiveness(app)
1818

1919
with log_context(_logger, logging.INFO, msg="setup postgres health"):
20-
await app.state.postgres_health.setup()
20+
await app.state.postgres_liveness.setup()
2121

2222
yield {}
2323

2424
with log_context(_logger, logging.INFO, msg="teardown postgres health"):
25-
await app.state.postgres_health.teardown()
25+
await app.state.postgres_liveness.teardown()
2626

2727

2828
def get_postgres_liveness(app: FastAPI) -> PostgresLiveness:
29-
assert isinstance(app.state.postgres_health, PostgresLiveness) # nosec
30-
return app.state.postgres_health
29+
assert isinstance(app.state.postgres_liveness, PostgresLiveness) # nosec
30+
return app.state.postgres_liveness
3131

3232

3333
__all__: tuple[str, ...] = ("PostgresLiveness",)

services/notifications/tests/unit/test_api_rest__health.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
# pylint: disable=protected-access
21
# pylint: disable=redefined-outer-name
2+
# pylint: disable=unused-argument
33

44

5+
import pytest
56
from fastapi import status
67
from fastapi.testclient import TestClient
78
from models_library.api_schemas__common.health import HealthCheckGet
9+
from models_library.errors import (
10+
POSRGRES_DATABASE_UNHEALTHY_MSG,
11+
RABBITMQ_CLIENT_UNHEALTHY_MSG,
12+
)
13+
from models_library.healthchecks import IsNonResponsive
14+
from pytest_mock import MockerFixture
15+
from simcore_service_notifications.api.rest._health import HealthCheckError
816

917
pytest_simcore_core_services_selection = [
1018
"postgres",
@@ -16,3 +24,33 @@ def test_health_ok(test_client: TestClient):
1624
response = test_client.get("/")
1725
assert response.status_code == status.HTTP_200_OK
1826
assert HealthCheckGet.model_validate(response.json())
27+
28+
29+
@pytest.fixture
30+
def mock_postgres_liveness(mocker: MockerFixture, test_client: TestClient) -> None:
31+
mocker.patch.object(
32+
test_client.app.state.postgres_liveness,
33+
"_liveness_result",
34+
new=IsNonResponsive(reason="fake"),
35+
)
36+
37+
38+
def test_health_postgres_unhealthy(
39+
mock_postgres_liveness: None, test_client: TestClient
40+
):
41+
with pytest.raises(HealthCheckError) as exc:
42+
test_client.get("/")
43+
assert POSRGRES_DATABASE_UNHEALTHY_MSG in f"{exc.value}"
44+
45+
46+
@pytest.fixture
47+
def mock_rabbit_healthy(mocker: MockerFixture, test_client: TestClient) -> None:
48+
mocker.patch.object(
49+
test_client.app.state.rabbitmq_rpc_server, "_healthy_state", new=False
50+
)
51+
52+
53+
def test_health_rabbit_unhealthy(mock_rabbit_healthy: None, test_client: TestClient):
54+
with pytest.raises(HealthCheckError) as exc:
55+
test_client.get("/")
56+
assert RABBITMQ_CLIENT_UNHEALTHY_MSG in f"{exc.value}"

0 commit comments

Comments
 (0)