Skip to content

Commit 364e482

Browse files
authored
ENH: Add Model to ProjectConfig (#39)
1 parent 82760eb commit 364e482

4 files changed

Lines changed: 57 additions & 4 deletions

File tree

src/fmu/settings/models/project_config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from pydantic import AwareDatetime, Field
88

9-
from fmu.datamodels.fmu_results.fields import Masterdata
9+
from fmu.datamodels.fmu_results.fields import Masterdata, Model
1010
from fmu.settings import __version__
1111
from fmu.settings.types import ResettableBaseModel, VersionStr # noqa TC001
1212

@@ -21,6 +21,7 @@ class ProjectConfig(ResettableBaseModel):
2121
created_at: AwareDatetime
2222
created_by: str
2323
masterdata: Masterdata | None = Field(default=None)
24+
model: Model | None = Field(default=None)
2425

2526
@classmethod
2627
def reset(cls: type[Self]) -> Self:
@@ -34,4 +35,5 @@ def reset(cls: type[Self]) -> Self:
3435
created_at=datetime.now(UTC),
3536
created_by=getpass.getuser(),
3637
masterdata=None,
38+
model=None,
3739
)

src/fmu/settings/resources/config_managers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def get(self: Self, key: str, default: Any = None) -> Any:
7373
default: Value to return if key is not found. Default None
7474
7575
Returns:
76-
The configuration value or deafult
76+
The configuration value or default
7777
"""
7878
try:
7979
config = self.load()

tests/conftest.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def config_dict(unix_epoch_utc: datetime) -> dict[str, Any]:
2828
"created_at": unix_epoch_utc,
2929
"created_by": "user",
3030
"masterdata": None,
31+
"model": None,
3132
}
3233

3334

@@ -66,16 +67,29 @@ def masterdata_dict() -> dict[str, Any]:
6667
}
6768

6869

70+
@pytest.fixture
71+
def model_dict() -> dict[str, Any]:
72+
"""Example model information."""
73+
return {
74+
"name": "Drogon",
75+
"revision": "21.0.0",
76+
"description": None,
77+
}
78+
79+
6980
@pytest.fixture
7081
def config_dict_with_masterdata(
71-
unix_epoch_utc: datetime, masterdata_dict: dict[str, Any]
82+
unix_epoch_utc: datetime,
83+
masterdata_dict: dict[str, Any],
84+
model_dict: dict[str, Any],
7285
) -> dict[str, Any]:
7386
"""A dictionary representing a .fmu config."""
7487
return {
7588
"version": __version__,
7689
"created_at": unix_epoch_utc,
7790
"created_by": "user",
7891
"masterdata": masterdata_dict,
92+
"model": model_dict,
7993
}
8094

8195

tests/test_resources/test_project_config.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import pytest
99

10-
from fmu.datamodels.fmu_results.fields import Smda
10+
from fmu.datamodels.fmu_results.fields import Model, Smda
1111
from fmu.settings._fmu_dir import ProjectFMUDirectory, UserFMUDirectory
1212
from fmu.settings.models.project_config import ProjectConfig
1313
from fmu.settings.models.user_config import UserConfig
@@ -255,6 +255,43 @@ def test_set_smda(
255255
assert config_on_disk_model == fmu_dir.config._cache
256256

257257

258+
def test_set_model_invalid_fails(
259+
fmu_dir: ProjectFMUDirectory, model_dict: dict[str, Any]
260+
) -> None:
261+
"""Tests setting the model value in the config using an invalid dictionary."""
262+
assert fmu_dir.config.get("model") is None
263+
264+
# drop model.name to test validation
265+
model_dict.pop("name")
266+
267+
with pytest.raises(ValueError, match="model.name"):
268+
fmu_dir.set_config_value("model", model_dict)
269+
270+
271+
def test_set_model(fmu_dir: ProjectFMUDirectory, model_dict: dict[str, Any]) -> None:
272+
"""Tests setting the model value in the config."""
273+
assert fmu_dir.config.get("model") is None
274+
with open(fmu_dir.path / fmu_dir.config.relative_path, encoding="utf-8") as f:
275+
config_on_disk = json.loads(f.read())
276+
assert config_on_disk["model"] is None
277+
278+
fmu_dir.set_config_value("model", model_dict)
279+
280+
model = Model.model_validate(model_dict)
281+
282+
assert fmu_dir.get_config_value("model") == model
283+
assert fmu_dir.get_config_value("model.revision") == "21.0.0"
284+
assert fmu_dir.get_config_value("model.name") == "Drogon"
285+
286+
with open(fmu_dir.path / fmu_dir.config.relative_path, encoding="utf-8") as f:
287+
config_on_disk = json.loads(f.read())
288+
289+
config_on_disk_model = ProjectConfig.model_validate(config_on_disk)
290+
assert fmu_dir.config._cache is not None
291+
assert fmu_dir.config._cache.model == model
292+
assert config_on_disk_model == fmu_dir.config._cache
293+
294+
258295
def test_update_config_when_it_does_not_exist(tmp_path: Path) -> None:
259296
"""Tests getting a key when the config is missing."""
260297
empty_fmu_dir = tmp_path / ".fmu"

0 commit comments

Comments
 (0)