2
2
3
3
from pathlib import Path
4
4
5
- import pytest
6
5
from pydantic import AnyHttpUrl , Field
7
6
8
7
from pydantic_settings import (
12
11
)
13
12
14
13
15
- @pytest .fixture (autouse = True )
16
- def clear_env (monkeypatch ):
17
- monkeypatch .delenv ('FOO' , raising = False )
18
- monkeypatch .delenv ('BAR' , raising = False )
19
- monkeypatch .delenv ('NESTED' , raising = False )
20
- monkeypatch .delenv ('NESTED__X' , raising = False )
21
- monkeypatch .delenv ('NESTED__Y' , raising = False )
22
-
23
-
24
- def test_init_kwargs_override_env_for_alias_with_populate_by_name (monkeypatch ):
14
+ def test_init_kwargs_override_env_for_alias_with_populate_by_name (cli_test_env ):
25
15
class Settings (BaseSettings ):
26
16
abc : AnyHttpUrl = Field (validation_alias = 'my_abc' )
27
17
model_config = SettingsConfigDict (populate_by_name = True , extra = 'allow' )
28
18
29
- monkeypatch .setenv ('MY_ABC' , 'http://localhost.com/' )
30
-
19
+ cli_test_env .set ('MY_ABC' , 'http://localhost.com' )
31
20
# Passing by field name should be accepted (populate_by_name=True) and should
32
21
# override env-derived value. Also ensures init > env precedence with validation_alias.
33
22
assert str (Settings (abc = 'http://prod.localhost.com/' ).abc ) == 'http://prod.localhost.com/'
34
23
35
24
36
- def test_precedence_init_over_env (tmp_path : Path , monkeypatch ):
25
+ def test_precedence_init_over_env (tmp_path : Path , cli_test_env ):
37
26
class Settings (BaseSettings ):
38
27
foo : str
39
28
40
- monkeypatch .setenv ('FOO' , 'from-env' )
41
-
42
- # init should win over env
29
+ cli_test_env .set ('FOO' , 'from-env' )
43
30
s = Settings (foo = 'from-init' )
44
31
assert s .foo == 'from-init'
45
32
46
33
47
- def test_precedence_env_over_dotenv (tmp_path : Path , monkeypatch ):
34
+ def test_precedence_env_over_dotenv (tmp_path : Path , cli_test_env ):
48
35
env_file = tmp_path / '.env'
49
36
env_file .write_text ('FOO=from-dotenv\n ' )
50
37
@@ -53,13 +40,12 @@ class Settings(BaseSettings):
53
40
54
41
model_config = SettingsConfigDict (env_file = env_file )
55
42
56
- # env set should override dotenv
57
- monkeypatch .setenv ('FOO' , 'from-env' )
43
+ cli_test_env .set ('FOO' , 'from-env' )
58
44
s = Settings ()
59
45
assert s .foo == 'from-env'
60
46
61
47
62
- def test_precedence_dotenv_over_secrets (tmp_path : Path , monkeypatch ):
48
+ def test_precedence_dotenv_over_secrets (tmp_path : Path ):
63
49
# create dotenv
64
50
env_file = tmp_path / '.env'
65
51
env_file .write_text ('FOO=from-dotenv\n ' )
@@ -93,7 +79,7 @@ class Settings(BaseSettings):
93
79
assert s .foo == 'from-secrets'
94
80
95
81
96
- def test_merging_preserves_earlier_values (tmp_path : Path , monkeypatch ):
82
+ def test_merging_preserves_earlier_values (tmp_path : Path , cli_test_env ):
97
83
# Prove that merging preserves earlier source values: init -> env -> dotenv -> secrets -> defaults
98
84
# We'll populate nested from dotenv and env parts, then set a default for a, and init for b
99
85
env_file = tmp_path / '.env'
@@ -123,7 +109,7 @@ def settings_customise_sources(
123
109
return init_settings , env_settings , dotenv_settings , file_secret_settings
124
110
125
111
# env contributes nested.y and overrides dotenv nested.x=1 if set; we'll set only y to prove merge
126
- monkeypatch . setenv ('NESTED__y' , '3' )
112
+ cli_test_env . set ('NESTED__y' , '3' )
127
113
# init contributes b, defaults contribute a
128
114
s = Settings (b = 20 )
129
115
assert s .a == 10 # defaults preserved
0 commit comments