|
4 | 4 | from datetime import datetime |
5 | 5 | from pathlib import Path |
6 | 6 | from typing import Any |
7 | | - |
| 7 | +from copy import deepcopy |
8 | 8 | import pytest |
9 | 9 |
|
10 | | -from fmu.datamodels.fmu_results.fields import Smda |
| 10 | +from fmu.datamodels.fmu_results.fields import Model, Smda |
11 | 11 | from fmu.settings._fmu_dir import ProjectFMUDirectory, UserFMUDirectory |
12 | 12 | from fmu.settings.models.project_config import ProjectConfig |
13 | 13 | from fmu.settings.models.user_config import UserConfig |
@@ -255,6 +255,43 @@ def test_set_smda( |
255 | 255 | assert config_on_disk_model == fmu_dir.config._cache |
256 | 256 |
|
257 | 257 |
|
| 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 | + |
258 | 295 | def test_update_config_when_it_does_not_exist(tmp_path: Path) -> None: |
259 | 296 | """Tests getting a key when the config is missing.""" |
260 | 297 | empty_fmu_dir = tmp_path / ".fmu" |
|
0 commit comments