|
| 1 | +# pylint: disable=protected-access |
| 2 | +# pylint: disable=redefined-outer-name |
| 3 | +# pylint: disable=too-many-arguments |
| 4 | +# pylint: disable=unused-argument |
| 5 | +# pylint: disable=unused-variable |
| 6 | + |
| 7 | +from collections.abc import AsyncIterator |
| 8 | +from typing import Annotated, Any |
| 9 | + |
| 10 | +import pytest |
| 11 | +import servicelib.fastapi.redis_lifespan |
| 12 | +from asgi_lifespan import LifespanManager as ASGILifespanManager |
| 13 | +from fastapi import FastAPI |
| 14 | +from fastapi_lifespan_manager import LifespanManager, State |
| 15 | +from pydantic import Field |
| 16 | +from pytest_mock import MockerFixture, MockType |
| 17 | +from pytest_simcore.helpers.monkeypatch_envs import setenvs_from_dict |
| 18 | +from pytest_simcore.helpers.typing_env import EnvVarsDict |
| 19 | +from servicelib.fastapi.redis_lifespan import ( |
| 20 | + RedisConfigurationError, |
| 21 | + RedisLifespanState, |
| 22 | + redis_database_lifespan, |
| 23 | +) |
| 24 | +from settings_library.application import BaseApplicationSettings |
| 25 | +from settings_library.redis import RedisDatabase, RedisSettings |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def mock_redis_client_sdk(mocker: MockerFixture) -> MockType: |
| 30 | + return mocker.patch.object( |
| 31 | + servicelib.fastapi.redis_lifespan, |
| 32 | + "RedisClientSDK", |
| 33 | + return_value=mocker.AsyncMock(), |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture |
| 38 | +def app_environment(monkeypatch: pytest.MonkeyPatch) -> EnvVarsDict: |
| 39 | + return setenvs_from_dict( |
| 40 | + monkeypatch, RedisSettings.model_json_schema()["examples"][0] |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +@pytest.fixture |
| 45 | +def app_lifespan( |
| 46 | + app_environment: EnvVarsDict, |
| 47 | + mock_redis_client_sdk: MockType, |
| 48 | +) -> LifespanManager: |
| 49 | + assert app_environment |
| 50 | + |
| 51 | + class AppSettings(BaseApplicationSettings): |
| 52 | + CATALOG_REDIS: Annotated[ |
| 53 | + RedisSettings, |
| 54 | + Field(json_schema_extra={"auto_default_from_env": True}), |
| 55 | + ] |
| 56 | + |
| 57 | + async def my_app_settings(app: FastAPI) -> AsyncIterator[State]: |
| 58 | + app.state.settings = AppSettings.create_from_envs() |
| 59 | + |
| 60 | + yield RedisLifespanState( |
| 61 | + REDIS_SETTINGS=app.state.settings.CATALOG_REDIS, |
| 62 | + REDIS_CLIENT_NAME="test_client", |
| 63 | + REDIS_CLIENT_DB=RedisDatabase.LOCKS, |
| 64 | + ).model_dump() |
| 65 | + |
| 66 | + app_lifespan = LifespanManager() |
| 67 | + app_lifespan.add(my_app_settings) |
| 68 | + app_lifespan.add(redis_database_lifespan) |
| 69 | + |
| 70 | + assert not mock_redis_client_sdk.called |
| 71 | + |
| 72 | + return app_lifespan |
| 73 | + |
| 74 | + |
| 75 | +async def test_lifespan_redis_database_in_an_app( |
| 76 | + is_pdb_enabled: bool, |
| 77 | + app_environment: EnvVarsDict, |
| 78 | + mock_redis_client_sdk: MockType, |
| 79 | + app_lifespan: LifespanManager, |
| 80 | +): |
| 81 | + app = FastAPI(lifespan=app_lifespan) |
| 82 | + |
| 83 | + async with ASGILifespanManager( |
| 84 | + app, |
| 85 | + startup_timeout=None if is_pdb_enabled else 10, |
| 86 | + shutdown_timeout=None if is_pdb_enabled else 10, |
| 87 | + ) as asgi_manager: |
| 88 | + # Verify that the Redis client SDK was created |
| 89 | + mock_redis_client_sdk.assert_called_once_with( |
| 90 | + app.state.settings.CATALOG_REDIS.build_redis_dsn(RedisDatabase.LOCKS), |
| 91 | + client_name="test_client", |
| 92 | + ) |
| 93 | + |
| 94 | + # Verify that the Redis client SDK is in the lifespan manager state |
| 95 | + assert "REDIS_CLIENT_SDK" in asgi_manager._state # noqa: SLF001 |
| 96 | + assert app.state.settings.CATALOG_REDIS |
| 97 | + assert ( |
| 98 | + asgi_manager._state["REDIS_CLIENT_SDK"] # noqa: SLF001 |
| 99 | + == mock_redis_client_sdk.return_value |
| 100 | + ) |
| 101 | + |
| 102 | + # Verify that the Redis client SDK was shut down |
| 103 | + redis_client: Any = mock_redis_client_sdk.return_value |
| 104 | + redis_client.shutdown.assert_called_once() |
| 105 | + |
| 106 | + |
| 107 | +async def test_lifespan_redis_database_with_invalid_settings( |
| 108 | + is_pdb_enabled: bool, |
| 109 | +): |
| 110 | + async def my_app_settings(app: FastAPI) -> AsyncIterator[State]: |
| 111 | + yield {"REDIS_SETTINGS": None} |
| 112 | + |
| 113 | + app_lifespan = LifespanManager() |
| 114 | + app_lifespan.add(my_app_settings) |
| 115 | + app_lifespan.add(redis_database_lifespan) |
| 116 | + |
| 117 | + app = FastAPI(lifespan=app_lifespan) |
| 118 | + |
| 119 | + with pytest.raises(RedisConfigurationError, match="Invalid redis") as excinfo: |
| 120 | + async with ASGILifespanManager( |
| 121 | + app, |
| 122 | + startup_timeout=None if is_pdb_enabled else 10, |
| 123 | + shutdown_timeout=None if is_pdb_enabled else 10, |
| 124 | + ): |
| 125 | + ... |
| 126 | + |
| 127 | + exception = excinfo.value |
| 128 | + assert isinstance(exception, RedisConfigurationError) |
| 129 | + assert exception.validation_error |
| 130 | + assert exception.state["REDIS_SETTINGS"] is None |
0 commit comments