Skip to content

Commit

Permalink
Add error testing
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindeide committed Feb 12, 2021
1 parent c506e34 commit a58efa8
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 13 deletions.
17 changes: 7 additions & 10 deletions ert_data/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,10 @@ def _extract_data(
data_map = {}
obs_types = [facade.get_impl_type_name_for_obs_key(key) for key in obs_keys]
data_keys = [facade.get_data_key_for_obs_key(key) for key in obs_keys]
assert (
len(set(obs_types)) == 1
), f"\nExpected only {expected_obs} observation type. Found: {obs_types} for {obs_keys}"
assert (
len(set(data_keys)) == 1
), f"\nExpected all obs keys ({obs_keys}) to have the same data key, found: {data_keys} "

if len(set(obs_types)) != 1:
raise ObservationError(f"\nExpected only {expected_obs} observation type. Found: {obs_types} for {obs_keys}")
if len(set(data_keys)) != 1:
raise ObservationError(f"\nExpected all obs keys ({obs_keys}) to have the same data key, found: {data_keys} ")
if load_data:
# Because all observations in this loop are pointing to the same data key,
# we can use any of them as input to the response loader.
Expand All @@ -58,9 +55,9 @@ def _extract_data(
data = None
obs = obs_loader(facade, obs_keys, case_name)
if obs.empty:
raise ObservationError("No observations loaded")
raise ObservationError(f"No observations loaded for observations keys: {obs_keys}")
for obs_key in obs_keys:
data_for_key = _get_data(data, obs[obs_key])
data_for_key = _filter_df1_on_df2_by_index(data, obs[obs_key])
data_map[obs_key] = pd.concat([obs[obs_key], data_for_key])

return pd.concat(data_map, axis=1).astype(float)
Expand Down Expand Up @@ -278,7 +275,7 @@ def _remove_inactive_report_steps(data, facade, observation_key, *args):
return data


def _get_data(data, obs):
def _filter_df1_on_df2_by_index(data, obs):
if data is None:
return None
else:
Expand Down
17 changes: 14 additions & 3 deletions tests/data/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,25 @@ def test_gen_obs_and_summary_index_range(monkeypatch, facade_snake_oil):
assert df.data.loc["STD"].values == pytest.approx([0.2, 0.1])


def test_no_storage(monkeypatch, copy_snake_oil):
@pytest.mark.parametrize(
"obs_key, expected_error",
[
("FOPR", r"No observations loaded for observations keys: \['FOPR'\]"),
("SNAKE_OIL_WPR_DIFF", "No data key for obs key: SNAKE_OIL_WPR_DIFF"),
],
)
@pytest.mark.usefixtures("copy_snake_oil")
def test_no_storage(monkeypatch, obs_key, expected_error):
shutil.rmtree("storage")
res_config = ResConfig("snake_oil.ert")
ert = EnKFMain(res_config)

facade = LibresFacade(ert)
with pytest.raises(loader.ObservationError):
MeasuredData(facade, ["FOPR", "SNAKE_OIL_WPR_DIFF"])
with pytest.raises(
loader.ObservationError,
match=expected_error,
):
MeasuredData(facade, [obs_key])


def create_summary_observation():
Expand Down
58 changes: 58 additions & 0 deletions tests/data/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,61 @@ def test_load_summary_response(facade, monkeypatch):
assert result.columns.to_list() == ["2010-01-10", "2010-01-20", "2021-02-10"]
assert result.index.to_list() == [0]
assert all(result.values.flatten() == [9.9, 19.9, 29.9])


def test_no_obs_error(facade, monkeypatch):
obs_mock = pd.DataFrame()
obs_loader = Mock(return_value=obs_mock)
response_loader = Mock(return_value=pd.DataFrame([1, 2]))
monkeypatch.setattr(loader, "_create_multi_index", MagicMock(return_value=[1]))
facade.get_impl_type_name_for_obs_key.return_value = "SUMMARY_OBS"
with pytest.raises(loader.ObservationError):
loader._extract_data(
facade,
"some_key",
"a_random_name",
response_loader,
obs_loader,
"SUMMARY_OBS",
)


def test_multiple_obs_types(facade, monkeypatch):
facade.get_impl_type_name_for_obs_key.side_effect = [
"SUMMARY_OBS",
"SUMMARY_OBS",
"BLOCK_OBS",
]
with pytest.raises(
loader.ObservationError,
match=r"Found: \['SUMMARY_OBS', 'SUMMARY_OBS', 'BLOCK_OBS'\]",
):
loader._extract_data(
facade,
["some_summary_key", "another_summary_key", "block_key"],
"a_random_name",
Mock(),
Mock(),
"SUMMARY_OBS",
)


def test_different_data_key(facade, monkeypatch):
facade.get_impl_type_name_for_obs_key.return_value = "SUMMARY_OBS"
facade.get_data_key_for_obs_key.side_effect = [
"data_key",
"another_data_key",
"data_key",
]
with pytest.raises(
loader.ObservationError,
match=r"found: \['data_key', 'another_data_key', 'data_key'\]",
):
loader._extract_data(
facade,
["obs_1", "obs_2", "obs_3"],
"a_random_name",
Mock(),
Mock(),
"SUMMARY_OBS",
)

0 comments on commit a58efa8

Please sign in to comment.