|
| 1 | +import json |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from ert.storage.migration.to37 import migrate |
| 7 | + |
| 8 | + |
| 9 | +def _write_index(ens_path: Path, index_data: dict) -> Path: |
| 10 | + ens_path.mkdir(parents=True) |
| 11 | + index_file = ens_path / "index.json" |
| 12 | + index_file.write_text(json.dumps(index_data), encoding="utf-8") |
| 13 | + return index_file |
| 14 | + |
| 15 | + |
| 16 | +@pytest.mark.parametrize( |
| 17 | + ("original", "expected"), |
| 18 | + [ |
| 19 | + pytest.param( |
| 20 | + { |
| 21 | + "id": "ens-id", |
| 22 | + "ensemble": {"name": "batch_0", "iteration": 0, "is_improvement": True}, |
| 23 | + }, |
| 24 | + {"id": "ens-id", "ensemble": {"name": "batch_0", "iteration": 0}}, |
| 25 | + id="removes_is_improvement", |
| 26 | + ), |
| 27 | + pytest.param( |
| 28 | + {"ensemble": {"name": "batch_1", "iteration": 1, "is_improvement": False}}, |
| 29 | + {"ensemble": {"name": "batch_1", "iteration": 1}}, |
| 30 | + id="removes_is_improvement_false", |
| 31 | + ), |
| 32 | + pytest.param( |
| 33 | + {"ensemble": {"name": "batch_5", "iteration": 3, "is_improvement": True}}, |
| 34 | + {"ensemble": {"name": "batch_5", "iteration": 3}}, |
| 35 | + id="removes_is_improvement_other_values", |
| 36 | + ), |
| 37 | + pytest.param( |
| 38 | + {"ensemble": {"name": "batch_0", "iteration": 0}}, |
| 39 | + {"ensemble": {"name": "batch_0", "iteration": 0}}, |
| 40 | + id="leaves_untouched_when_missing", |
| 41 | + ), |
| 42 | + ], |
| 43 | +) |
| 44 | +def test_that_migration_updates_ensemble_index(tmp_path, original, expected): |
| 45 | + root = tmp_path / "project" |
| 46 | + index_file = _write_index(root / "ensembles" / "ensemble_1", original) |
| 47 | + |
| 48 | + migrate(root) |
| 49 | + |
| 50 | + assert json.loads(index_file.read_text(encoding="utf-8")) == expected |
| 51 | + |
| 52 | + |
| 53 | +def test_that_migration_does_not_fail_on_unexpectedly_structured_dirs(tmp_path): |
| 54 | + root = tmp_path / "project" |
| 55 | + root.mkdir() |
| 56 | + migrate(root) |
| 57 | + |
| 58 | + ensembles_dir = root / "ensembles" |
| 59 | + ensembles_dir.mkdir() |
| 60 | + migrate(root) |
| 61 | + |
| 62 | + not_an_ensemble = ensembles_dir / "not_a_directory.json" |
| 63 | + not_an_ensemble.write_text("{}", encoding="utf-8") |
| 64 | + migrate(root) |
| 65 | + |
| 66 | + ensemble_without_index_json = ensembles_dir / "ensemble_1" |
| 67 | + ensemble_without_index_json.mkdir() |
| 68 | + migrate(root) |
| 69 | + |
| 70 | + assert not_an_ensemble.read_text(encoding="utf-8") == "{}" |
| 71 | + |
| 72 | + |
| 73 | +def test_that_migration_does_not_fail_on_index_without_ensemble_entry(tmp_path): |
| 74 | + root = tmp_path / "project" |
| 75 | + index_file = _write_index(root / "ensembles" / "ensemble_1", {"id": "ens-id"}) |
| 76 | + |
| 77 | + migrate(root) |
| 78 | + |
| 79 | + assert json.loads(index_file.read_text(encoding="utf-8")) == {"id": "ens-id"} |
0 commit comments