1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
"""
Test pydantic_settings.JsonConfigSettingsSource.
"""
import json
from pathlib import Path
from pydantic import BaseModel
from pydantic_settings import (
BaseSettings,
JsonConfigSettingsSource,
PydanticBaseSettingsSource,
SettingsConfigDict,
)
def test_repr() -> None:
source = JsonConfigSettingsSource(BaseSettings, Path('config.json'))
assert repr(source) == 'JsonConfigSettingsSource(json_file=config.json)'
def test_json_file(tmp_path):
p = tmp_path / '.env'
p.write_text(
"""
{"foobar": "Hello", "nested": {"nested_field": "world!"}, "null_field": null}
"""
)
class Nested(BaseModel):
nested_field: str
class Settings(BaseSettings):
model_config = SettingsConfigDict(json_file=p)
foobar: str
nested: Nested
null_field: str | None
@classmethod
def settings_customise_sources(
cls,
settings_cls: type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> tuple[PydanticBaseSettingsSource, ...]:
return (JsonConfigSettingsSource(settings_cls),)
s = Settings()
assert s.foobar == 'Hello'
assert s.nested.nested_field == 'world!'
def test_json_no_file():
class Settings(BaseSettings):
model_config = SettingsConfigDict(json_file=None)
@classmethod
def settings_customise_sources(
cls,
settings_cls: type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> tuple[PydanticBaseSettingsSource, ...]:
return (JsonConfigSettingsSource(settings_cls),)
s = Settings()
assert s.model_dump() == {}
def test_multiple_file_json(tmp_path):
p5 = tmp_path / '.env.json5'
p6 = tmp_path / '.env.json6'
with open(p5, 'w') as f5:
json.dump({'json5': 5}, f5)
with open(p6, 'w') as f6:
json.dump({'json6': 6}, f6)
class Settings(BaseSettings):
json5: int
json6: int
@classmethod
def settings_customise_sources(
cls,
settings_cls: type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> tuple[PydanticBaseSettingsSource, ...]:
return (JsonConfigSettingsSource(settings_cls, json_file=[p5, p6]),)
s = Settings()
assert s.model_dump() == {'json5': 5, 'json6': 6}
|