|
| 1 | +import typing |
| 2 | + |
| 3 | +import fastapi |
| 4 | +import litestar |
| 5 | +from httpx import AsyncClient |
| 6 | +from litestar import status_codes |
| 7 | +from litestar.testing import AsyncTestClient |
| 8 | + |
| 9 | +from microbootstrap.bootstrappers.fastapi import FastApiHealthChecksInstrument |
| 10 | +from microbootstrap.bootstrappers.litestar import LitestarHealthChecksInstrument |
| 11 | +from microbootstrap.instruments.health_checks_instrument import ( |
| 12 | + HealthChecksConfig, |
| 13 | + HealthChecksInstrument, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +def test_health_checks_is_ready(minimal_health_checks_config: HealthChecksConfig) -> None: |
| 18 | + health_checks_instrument: typing.Final = HealthChecksInstrument(minimal_health_checks_config) |
| 19 | + assert health_checks_instrument.is_ready() |
| 20 | + |
| 21 | + |
| 22 | +def test_health_checks_bootstrap_is_not_ready(minimal_health_checks_config: HealthChecksConfig) -> None: |
| 23 | + minimal_health_checks_config.health_checks_enabled = False |
| 24 | + health_checks_instrument: typing.Final = HealthChecksInstrument(minimal_health_checks_config) |
| 25 | + assert not health_checks_instrument.is_ready() |
| 26 | + |
| 27 | + |
| 28 | +def test_health_checks_bootstrap_after( |
| 29 | + default_litestar_app: litestar.Litestar, |
| 30 | + minimal_health_checks_config: HealthChecksConfig, |
| 31 | +) -> None: |
| 32 | + health_checks_instrument: typing.Final = HealthChecksInstrument(minimal_health_checks_config) |
| 33 | + assert health_checks_instrument.bootstrap_after(default_litestar_app) == default_litestar_app |
| 34 | + |
| 35 | + |
| 36 | +def test_health_checks_teardown( |
| 37 | + minimal_health_checks_config: HealthChecksConfig, |
| 38 | +) -> None: |
| 39 | + health_checks_instrument: typing.Final = HealthChecksInstrument(minimal_health_checks_config) |
| 40 | + assert health_checks_instrument.teardown() is None # type: ignore[func-returns-value] |
| 41 | + |
| 42 | + |
| 43 | +async def test_litestar_health_checks_bootstrap() -> None: |
| 44 | + test_health_checks_path: typing.Final = "/test-path/" |
| 45 | + heatlh_checks_config: typing.Final = HealthChecksConfig(health_checks_path=test_health_checks_path) |
| 46 | + health_checks_instrument: typing.Final = LitestarHealthChecksInstrument(heatlh_checks_config) |
| 47 | + |
| 48 | + health_checks_instrument.bootstrap() |
| 49 | + litestar_application: typing.Final = litestar.Litestar( |
| 50 | + **health_checks_instrument.bootstrap_before(), |
| 51 | + ) |
| 52 | + |
| 53 | + async with AsyncTestClient(app=litestar_application) as async_client: |
| 54 | + response = await async_client.get(heatlh_checks_config.health_checks_path) |
| 55 | + assert response.status_code == status_codes.HTTP_200_OK |
| 56 | + |
| 57 | + |
| 58 | +async def test_fastapi_health_checks_bootstrap() -> None: |
| 59 | + test_health_checks_path: typing.Final = "/test-path/" |
| 60 | + heatlh_checks_config: typing.Final = HealthChecksConfig(health_checks_path=test_health_checks_path) |
| 61 | + health_checks_instrument: typing.Final = FastApiHealthChecksInstrument(heatlh_checks_config) |
| 62 | + |
| 63 | + health_checks_instrument.bootstrap() |
| 64 | + fastapi_application = fastapi.FastAPI( |
| 65 | + **health_checks_instrument.bootstrap_before(), |
| 66 | + ) |
| 67 | + fastapi_application = health_checks_instrument.bootstrap_after(fastapi_application) |
| 68 | + |
| 69 | + async with AsyncClient(app=fastapi_application, base_url="http://testserver") as async_client: |
| 70 | + response = await async_client.get(heatlh_checks_config.health_checks_path) |
| 71 | + assert response.status_code == status_codes.HTTP_200_OK |
0 commit comments