Skip to content

Commit

Permalink
ENH: Add test of dunder accessors to Pydantic iterable RootModels
Browse files Browse the repository at this point in the history
  • Loading branch information
ErichSuter committed Aug 27, 2024
1 parent 616b740 commit 5bad7aa
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/fmu/dataio/_model/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class Parameters(RootModel):
def __iter__(self) -> Dict[str, Union[Parameters, int, float, str]]:
return iter(self.root)

def __getitem__(self, item: int) -> Union[Parameters, int, float, str]:
def __getitem__(self, item: str) -> Union[Parameters, int, float, str]:
return self.root[item]


Expand Down
2 changes: 1 addition & 1 deletion src/fmu/dataio/_model/global_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class Stratigraphy(RootModel[Dict[str, StratigraphyElement]]):
def __iter__(self) -> Dict[str, StratigraphyElement]:
return iter(self.root)

def __getitem__(self, item: int) -> StratigraphyElement:
def __getitem__(self, item: str) -> StratigraphyElement:
return self.root[item]


Expand Down
80 changes: 80 additions & 0 deletions tests/test_units/test_dunder_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"""Explicitly test all dunder methods."""

from fmu.dataio._model.fields import Parameters
from fmu.dataio._model.global_configuration import Stratigraphy, StratigraphyElement

# --------------------------------------------------------------------------------------
# Parameters
# --------------------------------------------------------------------------------------

testset_stratigraphy = Stratigraphy(
root={
"TopStratUnit1": StratigraphyElement(
name="Stratigraphic Unit 1",
stratigraphic=True,
alias=["TopSU1", "TopLayer1"],
),
"TopStratUnit2": StratigraphyElement(
name="Stratigraphic Unit 2",
stratigraphic=True,
alias=["TopSU2", "TopLayer2"],
),
"TopStratUnit3": StratigraphyElement(
name="Stratigraphic Unit 3",
stratigraphic=True,
alias=["TopSU3", "TopLayer3"],
),
}
)


def test_stratigraphy_dunder_iter():
try:
count = 0
for item in testset_stratigraphy:
count += 1
assert count == 3
except Exception:
assert False, "Stratigraphy class does not have __iter__()"


def test_stratigraphy_dunder_getitem():
try:
testset_stratigraphy["TopStratUnit2"]
except Exception:
assert False, "Stratigraphy class does not have __getitem__()"


# --------------------------------------------------------------------------------------
# Parameters
# --------------------------------------------------------------------------------------

testset_params = Parameters(
root={
"p1": 42,
"p2": "less nested",
"p3": Parameters(
root={
"p3_1": 42.3,
"p3_2": "nested",
}
),
}
)


def test_parameters_dunder_iter():
try:
count = 0
for item in testset_params:
count += 1
assert count == 3
except Exception:
assert False, "Parameters class does not have __iter__()"


def test_parameters_dunder_getitem():
try:
testset_params["p2"]
except Exception:
assert False, "Parameters class does not have __getitem__()"

0 comments on commit 5bad7aa

Please sign in to comment.