1- # pylint: disable=protected-access
21# pylint: disable=redefined-outer-name
2+ # pylint: disable=unused-argument
33
44
5+ import pytest
56from fastapi import status
67from fastapi .testclient import TestClient
78from 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
917pytest_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