|
| 1 | +import io |
| 2 | +from contextlib import contextmanager |
| 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._observations import GeneralObservation |
| 11 | +from ert.observation_converters import convert_observations |
| 12 | +from ert.observation_converters.summary_to_yaml import YamlConverter |
| 13 | +from tests.ert.unit_tests.cli.test_summary_to_bulk import _make_summary_obs |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture(name="patched_writer") |
| 17 | +def patched_writing(monkeypatch): |
| 18 | + """Avoid writing to file. |
| 19 | + Fixture mock can be used to assert what has been written to file. |
| 20 | + """ |
| 21 | + write_buffer = io.StringIO() |
| 22 | + |
| 23 | + @contextmanager |
| 24 | + def mock_open(*args, **kwargs): |
| 25 | + yield write_buffer |
| 26 | + |
| 27 | + monkeypatch.setattr(Path, "open", mock_open) |
| 28 | + return write_buffer |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.usefixtures("snake_oil_case") |
| 32 | +def test_that_happy_path_on_snake_oil_produces_yaml_and_stdout(capsys): |
| 33 | + args = MagicMock(format="yaml", config="snake_oil.ert") |
| 34 | + convert_observations(args) |
| 35 | + |
| 36 | + expected_yaml_content = dedent("""\ |
| 37 | + smry: |
| 38 | + - key: WOPR:OP1 |
| 39 | + observations: |
| 40 | + - date: '2010-03-31T00:00:00' |
| 41 | + value: 0.1 |
| 42 | + error: 0.05 |
| 43 | + - date: '2010-12-26T00:00:00' |
| 44 | + value: 0.7 |
| 45 | + error: 0.07 |
| 46 | + - date: '2011-12-21T00:00:00' |
| 47 | + value: 0.5 |
| 48 | + error: 0.05 |
| 49 | + - date: '2012-12-15T00:00:00' |
| 50 | + value: 0.3 |
| 51 | + error: 0.075 |
| 52 | + - date: '2013-12-10T00:00:00' |
| 53 | + value: 0.2 |
| 54 | + error: 0.035 |
| 55 | + - date: '2015-03-15T00:00:00' |
| 56 | + value: 0.015 |
| 57 | + error: 0.01 |
| 58 | + """) |
| 59 | + yaml_content = Path("summary_observations.yaml").read_text(encoding="utf-8") |
| 60 | + assert yaml_content == expected_yaml_content |
| 61 | + |
| 62 | + expected_stdout = ( |
| 63 | + f"Successfully wrote summary observations to '{YamlConverter.target_file}'." |
| 64 | + ) |
| 65 | + stdout = capsys.readouterr().out |
| 66 | + assert expected_stdout in stdout |
| 67 | + |
| 68 | + |
| 69 | +def test_that_empty_observations_raises_ert_cli_error(): |
| 70 | + with pytest.raises(ErtCliError, match="No summary observations found"): |
| 71 | + YamlConverter([]) |
| 72 | + |
| 73 | + |
| 74 | +def test_that_no_summary_observations_raises_ert_cli_error(): |
| 75 | + gen_obs = GeneralObservation( |
| 76 | + name="foo", data="foo", value=1.0, error=1.0, restart=5, index=5 |
| 77 | + ) |
| 78 | + with pytest.raises(ErtCliError, match="No summary observations found"): |
| 79 | + YamlConverter([gen_obs]) |
| 80 | + |
| 81 | + |
| 82 | +def test_that_observations_with_same_summary_key_are_gathered_in_yaml_dict(use_tmpdir): |
| 83 | + k1, k2 = "foo", "bar" |
| 84 | + observations = 2 * [ |
| 85 | + _make_summary_obs(key=k1, well=None), |
| 86 | + _make_summary_obs(key=k2, well=None), |
| 87 | + ] |
| 88 | + |
| 89 | + converter = YamlConverter(observations=observations) |
| 90 | + summary_dicts = converter._summary_to_yaml_dict() |
| 91 | + |
| 92 | + keys = [entry["key"] for entry in summary_dicts["smry"]] |
| 93 | + assert len(keys) == 2 |
| 94 | + assert set(keys) == {k1, k2} |
| 95 | + |
| 96 | + observations = [s_d["observations"] for s_d in summary_dicts["smry"]] |
| 97 | + assert all(len(o) == 2 for o in observations) |
| 98 | + |
| 99 | + |
| 100 | +def test_that_observations_with_different_summary_keys_are_seperated_in_yaml_dict(): |
| 101 | + k1, k2 = "foo", "bar" |
| 102 | + observations = [ |
| 103 | + _make_summary_obs(key=k1, well=None), |
| 104 | + _make_summary_obs(key=k2, well=None), |
| 105 | + ] |
| 106 | + |
| 107 | + converter = YamlConverter(observations=observations) |
| 108 | + result = converter._summary_to_yaml_dict() |
| 109 | + |
| 110 | + keys = [entry["key"] for entry in result["smry"]] |
| 111 | + assert len(keys) == 2 |
| 112 | + assert set(keys) == {k1, k2} |
| 113 | + |
| 114 | + |
| 115 | +def test_that_dumping_to_yaml_is_skipped_when_file_already_exists(use_tmpdir): |
| 116 | + observations = [_make_summary_obs()] |
| 117 | + |
| 118 | + Path("summary_observations.yaml").write_text("existing", encoding="utf-8") |
| 119 | + assert Path("summary_observations.yaml").is_file() |
| 120 | + |
| 121 | + converter = YamlConverter(observations=observations) |
| 122 | + with pytest.raises( |
| 123 | + ErtCliError, |
| 124 | + match=( |
| 125 | + r"A file with name 'summary_observations.yaml' already exists. " |
| 126 | + "Will not overwrite it and exit instead." |
| 127 | + ), |
| 128 | + ): |
| 129 | + converter.export_yaml() |
0 commit comments