Skip to content

Commit 2a1af29

Browse files
author
Johannes Rueschel
committed
refactor: Use existing env fixture
1 parent b534e7c commit 2a1af29

File tree

2 files changed

+11
-25
lines changed

2 files changed

+11
-25
lines changed

tests/test_config_file_deep_merge.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
)
2929

3030

31-
def test_init_kwargs_override_env_for_alias_with_populate_by_name(monkeypatch):
31+
def test_init_kwargs_override_env_for_alias_with_populate_by_name(env):
3232
class Settings(BaseSettings):
3333
abc: AnyHttpUrl = Field(validation_alias='my_abc')
3434
model_config = SettingsConfigDict(populate_by_name=True, extra='allow')
3535

36-
monkeypatch.setenv('MY_ABC', 'http://localhost.com/')
36+
env.set('MY_ABC', 'http://localhost.com/')
3737

3838
# Passing by field name should be accepted (populate_by_name=True) and should
3939
# override env-derived value. Also ensures init > env precedence with validation_alias.

tests/test_precedence_and_merging.py

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from pathlib import Path
44

5-
import pytest
65
from pydantic import AnyHttpUrl, Field
76

87
from pydantic_settings import (
@@ -12,39 +11,27 @@
1211
)
1312

1413

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(env):
2515
class Settings(BaseSettings):
2616
abc: AnyHttpUrl = Field(validation_alias='my_abc')
2717
model_config = SettingsConfigDict(populate_by_name=True, extra='allow')
2818

29-
monkeypatch.setenv('MY_ABC', 'http://localhost.com/')
30-
19+
env.set('MY_ABC', 'http://localhost.com')
3120
# Passing by field name should be accepted (populate_by_name=True) and should
3221
# override env-derived value. Also ensures init > env precedence with validation_alias.
3322
assert str(Settings(abc='http://prod.localhost.com/').abc) == 'http://prod.localhost.com/'
3423

3524

36-
def test_precedence_init_over_env(tmp_path: Path, monkeypatch):
25+
def test_precedence_init_over_env(tmp_path: Path, env):
3726
class Settings(BaseSettings):
3827
foo: str
3928

40-
monkeypatch.setenv('FOO', 'from-env')
41-
42-
# init should win over env
29+
env.set('FOO', 'from-env')
4330
s = Settings(foo='from-init')
4431
assert s.foo == 'from-init'
4532

4633

47-
def test_precedence_env_over_dotenv(tmp_path: Path, monkeypatch):
34+
def test_precedence_env_over_dotenv(tmp_path: Path, env):
4835
env_file = tmp_path / '.env'
4936
env_file.write_text('FOO=from-dotenv\n')
5037

@@ -53,13 +40,12 @@ class Settings(BaseSettings):
5340

5441
model_config = SettingsConfigDict(env_file=env_file)
5542

56-
# env set should override dotenv
57-
monkeypatch.setenv('FOO', 'from-env')
43+
env.set('FOO', 'from-env')
5844
s = Settings()
5945
assert s.foo == 'from-env'
6046

6147

62-
def test_precedence_dotenv_over_secrets(tmp_path: Path, monkeypatch):
48+
def test_precedence_dotenv_over_secrets(tmp_path: Path):
6349
# create dotenv
6450
env_file = tmp_path / '.env'
6551
env_file.write_text('FOO=from-dotenv\n')
@@ -93,7 +79,7 @@ class Settings(BaseSettings):
9379
assert s.foo == 'from-secrets'
9480

9581

96-
def test_merging_preserves_earlier_values(tmp_path: Path, monkeypatch):
82+
def test_merging_preserves_earlier_values(tmp_path: Path, env):
9783
# Prove that merging preserves earlier source values: init -> env -> dotenv -> secrets -> defaults
9884
# We'll populate nested from dotenv and env parts, then set a default for a, and init for b
9985
env_file = tmp_path / '.env'
@@ -123,7 +109,7 @@ def settings_customise_sources(
123109
return init_settings, env_settings, dotenv_settings, file_secret_settings
124110

125111
# 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+
env.set('NESTED__y', '3')
127113
# init contributes b, defaults contribute a
128114
s = Settings(b=20)
129115
assert s.a == 10 # defaults preserved

0 commit comments

Comments
 (0)