-
-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathtest_source_json.py
More file actions
124 lines (100 loc) · 3.54 KB
/
Copy pathtest_source_json.py
File metadata and controls
124 lines (100 loc) · 3.54 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""
Test pydantic_settings.JsonConfigSettingsSource.
"""
import json
from typing import Tuple, Type, Union
import pytest
from pydantic import BaseModel
from pydantic_settings import (
BaseSettings,
JsonConfigSettingsSource,
PydanticBaseSettingsSource,
SettingsConfigDict,
SettingsError,
)
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: Union[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_nondict_json_file(tmp_path):
p = tmp_path / '.env'
p.write_text(
"""
"noway"
"""
)
class Settings(BaseSettings):
foobar: str
model_config = SettingsConfigDict(json_file=p)
@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),)
with pytest.raises(SettingsError, match='Failed to parse settings from .*, expecting an object'):
Settings()
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}