Skip to content

Commit e75711a

Browse files
author
Julian Vanden Broeck
committed
Test raised ValueError on invalid settings file
1 parent a72fa73 commit e75711a

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

tests/test_source_json.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import json
66
from typing import Tuple, Type, Union
77

8+
import pytest
89
from pydantic import BaseModel
910

1011
from pydantic_settings import (
@@ -67,6 +68,33 @@ def settings_customise_sources(
6768
assert s.model_dump() == {}
6869

6970

71+
def test_nondict_json_file(tmp_path):
72+
p = tmp_path / '.env'
73+
p.write_text(
74+
"""
75+
"noway"
76+
"""
77+
)
78+
79+
class Settings(BaseSettings):
80+
foobar: str
81+
model_config = SettingsConfigDict(json_file=p)
82+
83+
@classmethod
84+
def settings_customise_sources(
85+
cls,
86+
settings_cls: Type[BaseSettings],
87+
init_settings: PydanticBaseSettingsSource,
88+
env_settings: PydanticBaseSettingsSource,
89+
dotenv_settings: PydanticBaseSettingsSource,
90+
file_secret_settings: PydanticBaseSettingsSource,
91+
) -> Tuple[PydanticBaseSettingsSource, ...]:
92+
return (JsonConfigSettingsSource(settings_cls),)
93+
94+
with pytest.raises(ValueError):
95+
Settings()
96+
97+
7098
def test_multiple_file_json(tmp_path):
7199
p5 = tmp_path / '.env.json5'
72100
p6 = tmp_path / '.env.json6'

tests/test_source_toml.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,30 @@ def settings_customise_sources(
7777
assert s.model_dump() == {}
7878

7979

80+
@pytest.mark.skipif(sys.version_info <= (3, 11) and tomli is None, reason='tomli/tomllib is not installed')
81+
def test_pyproject_nondict_toml(cd_tmp_path):
82+
pyproject = cd_tmp_path / 'pyproject.toml'
83+
pyproject.write_text(
84+
"""
85+
[tool.pydantic-settings]
86+
foobar
87+
"""
88+
)
89+
90+
class Settings(BaseSettings):
91+
foobar: str
92+
model_config = SettingsConfigDict()
93+
94+
@classmethod
95+
def settings_customise_sources(
96+
cls, settings_cls: Type[BaseSettings], **_kwargs: PydanticBaseSettingsSource
97+
) -> Tuple[PydanticBaseSettingsSource, ...]:
98+
return (TomlConfigSettingsSource(settings_cls, pyproject),)
99+
100+
with pytest.raises(ValueError):
101+
Settings()
102+
103+
80104
@pytest.mark.skipif(sys.version_info <= (3, 11) and tomli is None, reason='tomli/tomllib is not installed')
81105
def test_multiple_file_toml(tmp_path):
82106
p1 = tmp_path / '.env.toml1'

tests/test_source_yaml.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,30 @@ def settings_customise_sources(
8585
assert s.nested.nested_field == 'world!'
8686

8787

88+
@pytest.mark.skipif(yaml is None, reason='pyYaml is not installed')
89+
def test_nondict_yaml_file(tmp_path):
90+
p = tmp_path / '.env'
91+
p.write_text('test invalid yaml')
92+
93+
class Settings(BaseSettings):
94+
foobar: str
95+
model_config = SettingsConfigDict(yaml_file=p)
96+
97+
@classmethod
98+
def settings_customise_sources(
99+
cls,
100+
settings_cls: Type[BaseSettings],
101+
init_settings: PydanticBaseSettingsSource,
102+
env_settings: PydanticBaseSettingsSource,
103+
dotenv_settings: PydanticBaseSettingsSource,
104+
file_secret_settings: PydanticBaseSettingsSource,
105+
) -> Tuple[PydanticBaseSettingsSource, ...]:
106+
return (YamlConfigSettingsSource(settings_cls),)
107+
108+
with pytest.raises(ValueError):
109+
Settings()
110+
111+
88112
@pytest.mark.skipif(yaml is None, reason='pyYaml is not installed')
89113
def test_yaml_no_file():
90114
class Settings(BaseSettings):

0 commit comments

Comments
 (0)