Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/fmu/settings/models/project_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from pydantic import AwareDatetime, Field

from fmu.datamodels.fmu_results.fields import Masterdata
from fmu.datamodels.fmu_results.fields import Masterdata, Model
from fmu.settings import __version__
from fmu.settings.types import ResettableBaseModel, VersionStr # noqa TC001

Expand All @@ -21,6 +21,7 @@ class ProjectConfig(ResettableBaseModel):
created_at: AwareDatetime
created_by: str
masterdata: Masterdata | None = Field(default=None)
model: Model | None = Field(default=None)

@classmethod
def reset(cls: type[Self]) -> Self:
Expand All @@ -34,4 +35,5 @@ def reset(cls: type[Self]) -> Self:
created_at=datetime.now(UTC),
created_by=getpass.getuser(),
masterdata=None,
model=None,
)
2 changes: 1 addition & 1 deletion src/fmu/settings/resources/config_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def get(self: Self, key: str, default: Any = None) -> Any:
default: Value to return if key is not found. Default None

Returns:
The configuration value or deafult
The configuration value or default
"""
try:
config = self.load()
Expand Down
16 changes: 15 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def config_dict(unix_epoch_utc: datetime) -> dict[str, Any]:
"created_at": unix_epoch_utc,
"created_by": "user",
"masterdata": None,
"model": None,
}


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


@pytest.fixture
def model_dict() -> dict[str, Any]:
"""Example model information."""
return {
"name": "Drogon",
"revision": "21.0.0",
"description": None,
}


@pytest.fixture
def config_dict_with_masterdata(
unix_epoch_utc: datetime, masterdata_dict: dict[str, Any]
unix_epoch_utc: datetime,
masterdata_dict: dict[str, Any],
model_dict: dict[str, Any],
) -> dict[str, Any]:
"""A dictionary representing a .fmu config."""
return {
"version": __version__,
"created_at": unix_epoch_utc,
"created_by": "user",
"masterdata": masterdata_dict,
"model": model_dict,
}


Expand Down
39 changes: 38 additions & 1 deletion tests/test_resources/test_project_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import pytest

from fmu.datamodels.fmu_results.fields import Smda
from fmu.datamodels.fmu_results.fields import Model, Smda
from fmu.settings._fmu_dir import ProjectFMUDirectory, UserFMUDirectory
from fmu.settings.models.project_config import ProjectConfig
from fmu.settings.models.user_config import UserConfig
Expand Down Expand Up @@ -255,6 +255,43 @@ def test_set_smda(
assert config_on_disk_model == fmu_dir.config._cache


def test_set_model_invalid_fails(
fmu_dir: ProjectFMUDirectory, model_dict: dict[str, Any]
) -> None:
"""Tests setting the model value in the config using an invalid dictionary."""
assert fmu_dir.config.get("model") is None

# drop model.name to test validation
model_dict.pop("name")

with pytest.raises(ValueError, match="model.name"):
fmu_dir.set_config_value("model", model_dict)


def test_set_model(fmu_dir: ProjectFMUDirectory, model_dict: dict[str, Any]) -> None:
"""Tests setting the model value in the config."""
assert fmu_dir.config.get("model") is None
with open(fmu_dir.path / fmu_dir.config.relative_path, encoding="utf-8") as f:
config_on_disk = json.loads(f.read())
assert config_on_disk["model"] is None

fmu_dir.set_config_value("model", model_dict)

model = Model.model_validate(model_dict)

assert fmu_dir.get_config_value("model") == model
assert fmu_dir.get_config_value("model.revision") == "21.0.0"
assert fmu_dir.get_config_value("model.name") == "Drogon"

with open(fmu_dir.path / fmu_dir.config.relative_path, encoding="utf-8") as f:
config_on_disk = json.loads(f.read())

config_on_disk_model = ProjectConfig.model_validate(config_on_disk)
assert fmu_dir.config._cache is not None
assert fmu_dir.config._cache.model == model
assert config_on_disk_model == fmu_dir.config._cache


def test_update_config_when_it_does_not_exist(tmp_path: Path) -> None:
"""Tests getting a key when the config is missing."""
empty_fmu_dir = tmp_path / ".fmu"
Expand Down