|
| 1 | +import logging |
| 2 | +import warnings |
| 3 | +from pathlib import Path |
| 4 | +from textwrap import dedent |
| 5 | +from unittest.mock import MagicMock |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from ert.cli.main import ErtCliError |
| 10 | +from ert.config import ObservationType |
| 11 | +from ert.config.parsing import ObservationDict |
| 12 | +from ert.observation_converters import convert_observations |
| 13 | +from ert.observation_converters.summary_to_yaml import YamlConverter |
| 14 | + |
| 15 | + |
| 16 | +def _make_obs_dict( |
| 17 | + obs_type: ObservationType = ObservationType.SUMMARY, |
| 18 | + key: str = "WOPR", |
| 19 | + well: str = "OP1", |
| 20 | +) -> ObservationDict: |
| 21 | + key = f"{key}:{well}" if well else key |
| 22 | + return ObservationDict( |
| 23 | + { |
| 24 | + "type": obs_type, |
| 25 | + "KEY": key, |
| 26 | + "ERROR": 5, |
| 27 | + "VALUE": 10, |
| 28 | + "DATE": "2010-10-10", |
| 29 | + }, |
| 30 | + context=MagicMock(), |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.usefixtures("snake_oil_case") |
| 35 | +def test_that_happy_path_on_snake_oil_produces_yaml_and_stdout(capsys): |
| 36 | + args = MagicMock(format="yaml", config="snake_oil.ert") |
| 37 | + convert_observations(args) |
| 38 | + |
| 39 | + expected_yaml_content = dedent("""\ |
| 40 | + smry: |
| 41 | + - key: WOPR:OP1 |
| 42 | + observations: |
| 43 | + - date: '2010-03-31' |
| 44 | + value: 0.1 |
| 45 | + error: 0.05 |
| 46 | + - date: '2010-12-26' |
| 47 | + value: 0.7 |
| 48 | + error: 0.07 |
| 49 | + - date: '2011-12-21' |
| 50 | + value: 0.5 |
| 51 | + error: 0.05 |
| 52 | + - date: '2012-12-15' |
| 53 | + value: 0.3 |
| 54 | + error: 0.075 |
| 55 | + - date: '2013-12-10' |
| 56 | + value: 0.2 |
| 57 | + error: 0.035 |
| 58 | + - date: '2015-03-15' |
| 59 | + value: 0.015 |
| 60 | + error: 0.01 |
| 61 | + """) |
| 62 | + yaml_content = Path("summary_observations.yaml").read_text(encoding="utf-8") |
| 63 | + assert yaml_content == expected_yaml_content |
| 64 | + |
| 65 | + expected_stdout = ( |
| 66 | + f"Successfully wrote summary observations to '{YamlConverter.target_file}'." |
| 67 | + ) |
| 68 | + stdout = capsys.readouterr().out |
| 69 | + assert expected_stdout in stdout |
| 70 | + |
| 71 | + |
| 72 | +def test_that_empty_observations_raises_ert_cli_error(): |
| 73 | + with pytest.raises(ErtCliError, match="No summary observations in configuration"): |
| 74 | + YamlConverter([]) |
| 75 | + |
| 76 | + |
| 77 | +def test_that_no_summary_observations_raises_ert_cli_error(): |
| 78 | + gen_obs = _make_obs_dict(obs_type=ObservationType.GENERAL) |
| 79 | + with pytest.raises(ErtCliError, match="No summary observations in configuration"): |
| 80 | + YamlConverter([gen_obs]) |
| 81 | + |
| 82 | + |
| 83 | +def test_that_observations_with_same_summary_key_are_gathered_in_yaml_dict(use_tmpdir): |
| 84 | + k1, k2 = "foo", "bar" |
| 85 | + observations = 2 * [ |
| 86 | + _make_obs_dict(key=k1, well=""), |
| 87 | + _make_obs_dict(key=k2, well=""), |
| 88 | + ] |
| 89 | + |
| 90 | + converter = YamlConverter(observations=observations) |
| 91 | + summary_dicts = converter._summary_to_yaml_dict() |
| 92 | + |
| 93 | + keys = [entry["key"] for entry in summary_dicts["smry"]] |
| 94 | + assert len(keys) == 2 |
| 95 | + assert set(keys) == {k1, k2} |
| 96 | + |
| 97 | + observations = [s_d["observations"] for s_d in summary_dicts["smry"]] |
| 98 | + assert all(len(o) == 2 for o in observations) |
| 99 | + |
| 100 | + |
| 101 | +def test_that_observations_with_different_summary_keys_are_separated_in_yaml_dict(): |
| 102 | + k1, k2 = "foo", "bar" |
| 103 | + observations = [ |
| 104 | + _make_obs_dict(key=k1, well=""), |
| 105 | + _make_obs_dict(key=k2, well=""), |
| 106 | + ] |
| 107 | + |
| 108 | + converter = YamlConverter(observations=observations) |
| 109 | + result = converter._summary_to_yaml_dict() |
| 110 | + |
| 111 | + keys = [entry["key"] for entry in result["smry"]] |
| 112 | + assert len(keys) == 2 |
| 113 | + assert set(keys) == {k1, k2} |
| 114 | + |
| 115 | + |
| 116 | +def test_that_dumping_to_yaml_is_skipped_when_file_already_exists(use_tmpdir): |
| 117 | + observations = [_make_obs_dict()] |
| 118 | + |
| 119 | + Path("summary_observations.yaml").write_text("existing", encoding="utf-8") |
| 120 | + assert Path("summary_observations.yaml").is_file() |
| 121 | + |
| 122 | + converter = YamlConverter(observations=observations) |
| 123 | + with pytest.raises( |
| 124 | + ErtCliError, |
| 125 | + match=( |
| 126 | + r"A file with name 'summary_observations.yaml' already exists. " |
| 127 | + "Will not overwrite it and exit instead." |
| 128 | + ), |
| 129 | + ): |
| 130 | + converter.export_yaml() |
| 131 | + |
| 132 | + |
| 133 | +def test_that_config_warnings_are_caught_instead_of_printed_to_terminal( |
| 134 | + caplog, use_tmpdir |
| 135 | +): |
| 136 | + caplog.set_level(logging.INFO) |
| 137 | + config = "config.ert" |
| 138 | + obs_config = "obs.txt" |
| 139 | + # This setup expects the warning: |
| 140 | + # 'Config contains a SUMMARY key but no forward model steps' |
| 141 | + # to be raised |
| 142 | + config_content = f"""\ |
| 143 | + NUM_REALIZATIONS 5 |
| 144 | + SUMMARY * |
| 145 | + ECLBASE FOO |
| 146 | + OBS_CONFIG {obs_config} |
| 147 | + """ |
| 148 | + obs_config_content = """\ |
| 149 | + SUMMARY_OBSERVATION { |
| 150 | + KEY=FOPR; |
| 151 | + VALUE=10; |
| 152 | + ERROR=5; |
| 153 | + DATE=2010-10-10; |
| 154 | + };""" |
| 155 | + Path(config).write_text(config_content, encoding="utf-8") |
| 156 | + Path(obs_config).write_text(obs_config_content, encoding="utf-8") |
| 157 | + |
| 158 | + args = MagicMock(format="yaml", config=config) |
| 159 | + with warnings.catch_warnings(record=True) as w: |
| 160 | + convert_observations(args) |
| 161 | + |
| 162 | + assert len(w) == 0 |
0 commit comments