Skip to content

Commit d9bd491

Browse files
committed
Test case.
1 parent b833d2d commit d9bd491

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

tests/test_source_cli.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
DirectoryPath,
2020
Discriminator,
2121
Field,
22+
RootModel,
2223
Tag,
2324
ValidationError,
2425
field_validator,
@@ -1778,20 +1779,31 @@ class Settings(BaseSettings):
17781779

17791780

17801781
def test_cli_enforce_required(env):
1782+
class MyRootModel(RootModel[str]):
1783+
root: str
1784+
17811785
class Settings(BaseSettings, cli_exit_on_error=False):
17821786
my_required_field: str
1787+
my_root_model_required_field: MyRootModel
17831788

17841789
env.set('MY_REQUIRED_FIELD', 'hello from environment')
1790+
env.set('MY_ROOT_MODEL_REQUIRED_FIELD', 'hi from environment')
17851791

17861792
assert Settings(_cli_parse_args=[], _cli_enforce_required=False).model_dump() == {
1787-
'my_required_field': 'hello from environment'
1793+
'my_required_field': 'hello from environment',
1794+
'my_root_model_required_field': 'hi from environment',
17881795
}
17891796

17901797
with pytest.raises(
17911798
SettingsError, match='error parsing CLI: the following arguments are required: --my_required_field'
17921799
):
17931800
Settings(_cli_parse_args=[], _cli_enforce_required=True).model_dump()
17941801

1802+
with pytest.raises(
1803+
SettingsError, match='error parsing CLI: the following arguments are required: --my_root_model_required_field'
1804+
):
1805+
Settings(_cli_parse_args=['--my_required_field', 'hello from cli'], _cli_enforce_required=True).model_dump()
1806+
17951807

17961808
def test_cli_exit_on_error(capsys, monkeypatch):
17971809
class Settings(BaseSettings, cli_parse_args=True): ...

0 commit comments

Comments
 (0)