Skip to content

Commit 6eb6a3d

Browse files
committed
minor
1 parent e62498f commit 6eb6a3d

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

packages/models-library/tests/test__pydantic_models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ class MyModel(BaseModel):
214214
assert model.model_dump(exclude_unset=True) == data
215215

216216

217-
# BELOW some tests related to depreacated `populate_by_name` in pydantic v2.11+ !!
217+
# BELOW some tests related to deprecated `populate_by_name` in pydantic v2.11+ !!
218218
#
219219
# https://docs.pydantic.dev/latest/api/config/#pydantic.config.ConfigDict.populate_by_name
220220
#

packages/settings-library/tests/test__pydantic_settings.py

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# pylint: disable=unused-argument
33
# pylint: disable=unused-variable
44

5-
""" Tests subtle details about pydantic models
5+
"""Tests subtle details about pydantic models
66
77
This test suite intends to "freeze" some concepts/invariants from pydantic upon which we are going
88
to build this libraries.
@@ -12,14 +12,25 @@
1212
1313
"""
1414

15-
from typing import Annotated
15+
from collections.abc import Callable
16+
from typing import Annotated, Any
1617

1718
import pytest
1819
from common_library.basic_types import LogLevel
1920
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+
)
2132
from pydantic_core import PydanticUndefined
22-
from pydantic_settings import BaseSettings
33+
from pydantic_settings import BaseSettings, SettingsConfigDict
2334
from pytest_simcore.helpers.monkeypatch_envs import setenvs_from_dict
2435
from settings_library.application import BaseApplicationSettings
2536

@@ -198,3 +209,46 @@ def test_pydantic_serialization_user_warning(monkeypatch: pytest.MonkeyPatch):
198209
settings = _TestSettings.create_from_envs()
199210
assert settings.APP_LOGLEVEL == LogLevel.DEBUG
200211
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

Comments
 (0)