|
12 | 12 |
|
13 | 13 | """ |
14 | 14 |
|
| 15 | +from typing import Annotated |
| 16 | + |
| 17 | +import pytest |
| 18 | +from common_library.basic_types import LogLevel |
15 | 19 | from common_library.pydantic_fields_extension import is_nullable |
16 | | -from pydantic import ValidationInfo, field_validator |
| 20 | +from pydantic import AliasChoices, Field, ValidationInfo, field_validator |
17 | 21 | from pydantic_core import PydanticUndefined |
18 | 22 | from pydantic_settings import BaseSettings |
19 | 23 | from pytest_simcore.helpers.monkeypatch_envs import setenvs_from_dict |
| 24 | +from settings_library.application import BaseApplicationSettings |
20 | 25 |
|
21 | 26 |
|
22 | 27 | def assert_field_specs( |
@@ -168,3 +173,28 @@ def test_construct(monkeypatch): |
168 | 173 | assert settings_from_both == settings_from_init.model_copy( |
169 | 174 | update={"VALUE_NULLABLE_REQUIRED": 3} |
170 | 175 | ) |
| 176 | + |
| 177 | + |
| 178 | +class _TestSettings(BaseApplicationSettings): |
| 179 | + APP_LOGLEVEL: Annotated[ |
| 180 | + LogLevel, |
| 181 | + Field( |
| 182 | + validation_alias=AliasChoices("APP_LOGLEVEL", "LOG_LEVEL"), |
| 183 | + ), |
| 184 | + ] = LogLevel.WARNING |
| 185 | + |
| 186 | + |
| 187 | +@pytest.mark.filterwarnings("error") |
| 188 | +def test_pydantic_serialization_user_warning(monkeypatch: pytest.MonkeyPatch): |
| 189 | + # This test is exploring the reason for `UserWarning` |
| 190 | + # |
| 191 | + # /python3.11/site-packages/pydantic/main.py:477: UserWarning: Pydantic serializer warnings: |
| 192 | + # Expected `enum` but got `str` with value `'WARNING'` - serialized value may not be as expected |
| 193 | + # return self.__pydantic_serializer__.to_json( |
| 194 | + # |
| 195 | + # NOTE: it seems settings.model_dump_json(warnings='none') is not the cause here of `UserWarning` |
| 196 | + monkeypatch.setenv("LOG_LEVEL", "DEBUG") |
| 197 | + |
| 198 | + settings = _TestSettings.create_from_envs() |
| 199 | + assert settings.APP_LOGLEVEL == LogLevel.DEBUG |
| 200 | + assert settings.model_dump_json(indent=2) |
0 commit comments