|
2 | 2 | # pylint: disable=unused-argument |
3 | 3 | # pylint: disable=unused-variable |
4 | 4 |
|
5 | | -""" Tests subtle details about pydantic models |
| 5 | +"""Tests subtle details about pydantic models |
6 | 6 |
|
7 | 7 | This test suite intends to "freeze" some concepts/invariants from pydantic upon which we are going |
8 | 8 | to build this libraries. |
|
12 | 12 |
|
13 | 13 | """ |
14 | 14 |
|
15 | | -from typing import Annotated |
| 15 | +from collections.abc import Callable |
| 16 | +from typing import Annotated, Any |
16 | 17 |
|
17 | 18 | import pytest |
18 | 19 | from common_library.basic_types import LogLevel |
19 | 20 | from common_library.pydantic_fields_extension import is_nullable |
20 | | -from pydantic import AliasChoices, Field, ValidationInfo, field_validator |
| 21 | +from pydantic import ( |
| 22 | + AliasChoices, |
| 23 | + AmqpDsn, |
| 24 | + BaseModel, |
| 25 | + Field, |
| 26 | + ImportString, |
| 27 | + PostgresDsn, |
| 28 | + RedisDsn, |
| 29 | + ValidationInfo, |
| 30 | + field_validator, |
| 31 | +) |
21 | 32 | from pydantic_core import PydanticUndefined |
22 | | -from pydantic_settings import BaseSettings |
| 33 | +from pydantic_settings import BaseSettings, SettingsConfigDict |
23 | 34 | from pytest_simcore.helpers.monkeypatch_envs import setenvs_from_dict |
24 | 35 | from settings_library.application import BaseApplicationSettings |
25 | 36 |
|
@@ -198,3 +209,46 @@ def test_pydantic_serialization_user_warning(monkeypatch: pytest.MonkeyPatch): |
198 | 209 | settings = _TestSettings.create_from_envs() |
199 | 210 | assert settings.APP_LOGLEVEL == LogLevel.DEBUG |
200 | 211 | assert settings.model_dump_json(indent=2) |
| 212 | + |
| 213 | + |
| 214 | +def test_it(): |
| 215 | + class SubModel(BaseModel): |
| 216 | + foo: str = "bar" |
| 217 | + apple: int = 1 |
| 218 | + |
| 219 | + class Settings(BaseSettings): |
| 220 | + auth_key: str = Field(validation_alias="my_auth_key") |
| 221 | + |
| 222 | + api_key: str = Field(alias="my_api_key") |
| 223 | + |
| 224 | + redis_dsn: RedisDsn = Field( |
| 225 | + "redis://user:pass@localhost:6379/1", |
| 226 | + validation_alias=AliasChoices("service_redis_dsn", "redis_url"), |
| 227 | + ) |
| 228 | + pg_dsn: PostgresDsn = "postgres://user:pass@localhost:5432/foobar" |
| 229 | + amqp_dsn: AmqpDsn = "amqp://user:pass@localhost:5672/" |
| 230 | + |
| 231 | + special_function: ImportString[Callable[[Any], Any]] = "math.cos" |
| 232 | + |
| 233 | + # to override domains: |
| 234 | + # export my_prefix_domains='["foo.com", "bar.com"]' |
| 235 | + domains: set[str] = set() |
| 236 | + |
| 237 | + # to override more_settings: |
| 238 | + # export my_prefix_more_settings='{"foo": "x", "apple": 1}' |
| 239 | + more_settings: SubModel = SubModel() |
| 240 | + |
| 241 | + model_config = SettingsConfigDict(env_prefix="my_prefix_") |
| 242 | + |
| 243 | + import math |
| 244 | + |
| 245 | + assert Settings().model_dump() == { |
| 246 | + "auth_key": "xxx", |
| 247 | + "api_key": "xxx", |
| 248 | + "redis_dsn": RedisDsn("redis://user:pass@localhost:6379/1"), |
| 249 | + "pg_dsn": PostgresDsn("postgres://user:pass@localhost:5432/foobar"), |
| 250 | + "amqp_dsn": AmqpDsn("amqp://user:pass@localhost:5672/"), |
| 251 | + "special_function": math.cos, |
| 252 | + "domains": set(), |
| 253 | + "more_settings": {"foo": "bar", "apple": 1}, |
| 254 | + } |
0 commit comments