Skip to content

Commit

Permalink
Add test for * in csv_export
Browse files Browse the repository at this point in the history
  • Loading branch information
eivindjahren committed Aug 22, 2024
1 parent c90e52b commit 04d8d61
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 42 deletions.
23 changes: 4 additions & 19 deletions tests/unit_tests/gui/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,11 @@ def opened_main_window(
_new_poly_example(source_root, tmp_path)
with _open_main_window(tmp_path / "poly.ert") as (
gui,
storage,
_,
config,
), StorageService.init_service(
project=os.path.abspath(config.ens_path),
):
_add_default_ensemble(storage, gui, config)
yield gui


Expand All @@ -87,18 +86,6 @@ def _new_poly_example(source_root, destination):
print(line, end="")


def _add_default_ensemble(storage: Storage, gui: ErtMainWindow, config: ErtConfig):
gui.notifier.set_current_ensemble(
storage.create_experiment(
parameters=config.ensemble_config.parameter_configuration,
observations=config.observations,
).create_ensemble(
name="default",
ensemble_size=config.model_config.num_realizations,
)
)


@contextmanager
def _open_main_window(
path,
Expand Down Expand Up @@ -147,11 +134,10 @@ def _esmda_run(run_experiment, source_root, tmp_path_factory):
_new_poly_example(source_root, path)
with pytest.MonkeyPatch.context() as mp, _open_main_window(path / "poly.ert") as (
gui,
storage,
_,
config,
):
mp.chdir(path)
_add_default_ensemble(storage, gui, config)
run_experiment(MultipleDataAssimilation, gui)
# Check that we produce update log
log_paths = list(Path(config.analysis_config.log_path).iterdir())
Expand All @@ -169,8 +155,8 @@ def _ensemble_experiment_run(
_new_poly_example(source_root, path)
with pytest.MonkeyPatch.context() as mp, _open_main_window(path / "poly.ert") as (
gui,
storage,
config,
_,
_,
):
mp.chdir(path)
if failing_reals:
Expand Down Expand Up @@ -207,7 +193,6 @@ def _evaluate(coeffs, x):
| stat.S_IXGRP
| stat.S_IXOTH,
)
_add_default_ensemble(storage, gui, config)
run_experiment(EnsembleExperiment, gui)

return path
Expand Down
34 changes: 20 additions & 14 deletions tests/unit_tests/gui/test_csv_export.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from pathlib import Path

import pytest
from qtpy.QtCore import Qt, QTimer
from qtpy.QtWidgets import (
QMessageBox,
Expand All @@ -17,7 +18,8 @@
)


def test_csv_export(esmda_has_run, qtbot):
@pytest.mark.parametrize("ensemble_select", ["default_0", "*"])
def test_csv_export(esmda_has_run, qtbot, ensemble_select):
gui = esmda_has_run

file_name = None
Expand All @@ -29,8 +31,8 @@ def handle_export_dialog():
export_dialog = wait_for_child(gui, qtbot, ExportDialog)
case_selection = get_child(export_dialog, ListEditBox)

# Select default_0 as the case to be exported
case_selection._list_edit_line.setText("default_0")
# Select ensemble_select as the case to be exported
case_selection._list_edit_line.setText(ensemble_select)
path_chooser = get_child(export_dialog, PathChooser)
file_name = path_chooser._path_line.text()
assert case_selection.isValid()
Expand All @@ -55,14 +57,18 @@ def handle_finished_box():
qtbot.waitUntil(lambda: os.path.exists(file_name))

file_content = Path(file_name).read_text(encoding="utf-8")
ensemble = gui.notifier.storage.get_ensemble_by_name("default_0")
gen_kw_data = ensemble.load_all_gen_kw_data()

facade = LibresFacade.from_config_file("poly.ert")
misfit_data = facade.load_all_misfit_data(ensemble)

for i in range(ensemble.ensemble_size):
assert (
f"{i},0,,default_0,{gen_kw_data.iloc[i]['COEFFS:a']},{gen_kw_data.iloc[i]['COEFFS:b']},{gen_kw_data.iloc[i]['COEFFS:c']},{misfit_data.iloc[i]['MISFIT:POLY_OBS']},{misfit_data.iloc[i]['MISFIT:TOTAL']}"
in file_content
)
ensemble_names = [ensemble_select]
if ensemble_select == "*":
ensemble_names = [e.name for e in gui.notifier.storage.ensembles]
for name in ensemble_names:
ensemble = gui.notifier.storage.get_ensemble_by_name(name)
gen_kw_data = ensemble.load_all_gen_kw_data()

facade = LibresFacade.from_config_file("poly.ert")
misfit_data = facade.load_all_misfit_data(ensemble)

for i in range(ensemble.ensemble_size):
assert (
f",{name},{gen_kw_data.iloc[i]['COEFFS:a']},{gen_kw_data.iloc[i]['COEFFS:b']},{gen_kw_data.iloc[i]['COEFFS:c']},{misfit_data.iloc[i]['MISFIT:POLY_OBS']},{misfit_data.iloc[i]['MISFIT:TOTAL']}"
in file_content
)
4 changes: 2 additions & 2 deletions tests/unit_tests/gui/test_full_manual_update_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def test_manual_analysis_workflow(ensemble_experiment_has_run, qtbot):
tree_view.expandAll()

model = tree_view.model()
assert model is not None and model.rowCount() == 2
assert "iter-0_1" in model.index(1, 0, model.index(1, 0)).data(0)
assert model is not None and model.rowCount() == 1
assert "iter-0_1" in model.index(1, 0, model.index(0, 0)).data(0)

experiments_panel.close()

Expand Down
9 changes: 2 additions & 7 deletions tests/unit_tests/gui/test_main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
import shutil
import stat
from datetime import datetime
from pathlib import Path
from textwrap import dedent
from unittest.mock import MagicMock, Mock, patch
Expand Down Expand Up @@ -337,7 +336,6 @@ def test_that_the_plot_window_contains_the_expected_elements(
):
gui = esmda_has_run
expected_ensembles = [
f"{datetime.today().strftime('%Y-%m-%d')} : default",
"es_mda : default_0",
"es_mda : default_1",
"es_mda : default_2",
Expand Down Expand Up @@ -431,11 +429,8 @@ def test_that_the_manage_experiments_tool_can_be_used(
tree_view = get_child(storage_widget, QTreeView)
tree_view.expandAll()

# The storage view should contain the expected experiments and ensembles
# Two experiments. The first experiment with one ensemble the second with four
assert tree_view.model().rowCount() == 2
assert tree_view.model().rowCount(tree_view.model().index(0, 0)) == 1
assert tree_view.model().rowCount(tree_view.model().index(1, 0)) == 4
assert tree_view.model().rowCount() == 1
assert tree_view.model().rowCount(tree_view.model().index(0, 0)) == 4

def handle_add_dialog():
dialog = wait_for_child(current_tab, qtbot, CreateExperimentDialog)
Expand Down

0 comments on commit 04d8d61

Please sign in to comment.