-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from NowanIlfideme/fix/scrict-classes-settings
Fix: Strict Classes Support
- Loading branch information
Showing
2 changed files
with
52 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"""Test strict models and BaseSettings subclasses.""" | ||
|
||
import pytest | ||
from pydantic import BaseModel, BaseSettings | ||
from typing_extensions import Literal | ||
|
||
from pydantic_kedro import load_model, save_model | ||
|
||
|
||
class ExSettings(BaseSettings): | ||
"""Settings class.""" | ||
|
||
val: str | ||
|
||
|
||
class ExModel(BaseModel): | ||
"""Model class.""" | ||
|
||
x: int = 1 | ||
settings: ExSettings | ||
|
||
|
||
class StrictModel(BaseModel): | ||
"""Strict no-extras values.""" | ||
|
||
val: str | ||
|
||
class Config: | ||
"""Pydantic model configuration.""" | ||
|
||
extra = "forbid" | ||
|
||
|
||
@pytest.mark.parametrize("format", ["auto", "zip", "folder", "yaml", "json"]) | ||
def test_rt_settings(tmpdir: str, format: Literal["auto", "zip", "folder", "yaml", "json"]): | ||
"""Test settings round-trip.""" | ||
obj = ExModel(settings=ExSettings(val="val")) | ||
save_model(obj, f"{tmpdir}/obj", format=format) | ||
obj2 = load_model(f"{tmpdir}/obj", ExModel) | ||
assert obj.settings == obj2.settings | ||
|
||
|
||
@pytest.mark.parametrize("format", ["auto", "zip", "folder", "yaml", "json"]) | ||
def test_rt_strict_model(tmpdir: str, format: Literal["auto", "zip", "folder", "yaml", "json"]): | ||
"""Test strict_model round-trip.""" | ||
obj = StrictModel(val="val") | ||
save_model(obj, f"{tmpdir}/obj", format=format) | ||
obj2 = load_model(f"{tmpdir}/obj", StrictModel) | ||
assert obj.val == obj2.val |