Skip to content

Commit

Permalink
Add proper validation to load results manually tool
Browse files Browse the repository at this point in the history
  • Loading branch information
larsevj committed Sep 4, 2024
1 parent 08c3f6c commit 0303a8a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/ert/gui/tools/load_results/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
from .load_results_panel import LoadResultsPanel # noqa: F401
from .load_results_tool import LoadResultsTool # noqa: F401
from .load_results_panel import LoadResultsPanel
from .load_results_tool import LoadResultsTool

__all__ = ["LoadResultsPanel", "LoadResultsTool"]
23 changes: 21 additions & 2 deletions src/ert/gui/tools/load_results/load_results_panel.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from qtpy.QtCore import Qt
from qtpy.QtCore import Qt, Signal
from qtpy.QtWidgets import QFormLayout, QMessageBox, QTextEdit, QWidget

from ert.gui.ertnotifier import ErtNotifier
Expand All @@ -18,6 +18,8 @@


class LoadResultsPanel(QWidget):
panelConfigurationChanged = Signal()

def __init__(self, facade: LibresFacade, notifier: ErtNotifier):
self._facade = facade
QWidget.__init__(self)
Expand Down Expand Up @@ -50,7 +52,10 @@ def __init__(self, facade: LibresFacade, notifier: ErtNotifier):
self._active_realizations_model, # type: ignore
"load_results_manually/Realizations",
)
self._active_realizations_field.setValidator(RangeStringArgument())
self._active_realizations_field.setValidator(
RangeStringArgument(self._facade.get_ensemble_size()),
)
self._active_realizations_field.setObjectName("active_realizations_lrm")
layout.addRow("Realizations to load:", self._active_realizations_field)

self._iterations_model = ValueModel(0) # type: ignore
Expand All @@ -59,8 +64,16 @@ def __init__(self, facade: LibresFacade, notifier: ErtNotifier):
"load_results_manually/iterations",
)
self._iterations_field.setValidator(IntegerArgument(from_value=0))
self._iterations_field.setObjectName("iterations_field_lrm")
layout.addRow("Iteration to load:", self._iterations_field)

self._active_realizations_field.getValidationSupport().validationChanged.connect( # noqa
self.panelConfigurationChanged
)
self._iterations_field.getValidationSupport().validationChanged.connect( # noqa
self.panelConfigurationChanged
)

self.setLayout(layout)

def readCurrentRunPath(self) -> str:
Expand All @@ -70,6 +83,12 @@ def readCurrentRunPath(self) -> str:
run_path = run_path.replace("<ERT-CASE>", current_ensemble)
return run_path

def isConfigurationValid(self) -> bool:
return (
self._active_realizations_field.isValid()
and self._iterations_field.isValid()
)

def load(self) -> int:
selected_ensemble = self._notifier.current_ensemble
realizations = self._active_realizations_model.getActiveRealizationsMask()
Expand Down
10 changes: 10 additions & 0 deletions src/ert/gui/tools/load_results/load_results_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def trigger(self) -> None:
if not self._import_widget._ensemble_selector.isEnabled():
loadButton.setEnabled(False)
loadButton.setToolTip("Must load into a ensemble")
self._import_widget.panelConfigurationChanged.connect(
self.validationStatusChanged
)
self._dialog.exec_()

def load(self, _: Any) -> None:
Expand All @@ -42,3 +45,10 @@ def load(self, _: Any) -> None:
self._dialog.toggleButton(caption="Load", enabled=False)
self._import_widget.load()
self._dialog.accept()

def validationStatusChanged(self) -> None:
assert self._dialog is not None
assert self._import_widget is not None
self._dialog.toggleButton(
caption="Load", enabled=self._import_widget.isConfigurationValid()
)
52 changes: 52 additions & 0 deletions tests/unit_tests/gui/tools/test_load_results_manually.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from qtpy.QtCore import Qt, QTimer
from qtpy.QtWidgets import QPushButton

from ert.gui.ertwidgets import ClosableDialog, StringBox
from ert.gui.ertwidgets.ensembleselector import EnsembleSelector
from ert.gui.tools.load_results import LoadResultsPanel

from ..conftest import (
get_child,
wait_for_child,
)


def test_validation(ensemble_experiment_has_run_no_failure, qtbot):
gui = ensemble_experiment_has_run_no_failure
ensemble_name = "iter-0"

def handle_load_results_dialog():
dialog = wait_for_child(gui, qtbot, ClosableDialog)
panel = get_child(dialog, LoadResultsPanel)

ensemble_selector = get_child(panel, EnsembleSelector)
index = ensemble_selector.findText(ensemble_name, Qt.MatchFlag.MatchContains)
ensemble_selector.setCurrentIndex(index)

load_button = get_child(panel.parent(), QPushButton, name="Load")

active_realizations = get_child(
panel, StringBox, name="active_realizations_lrm"
)
default_value_active_reals = active_realizations.get_text
active_realizations.setText(
f"0-{ensemble_selector.selected_ensemble.ensemble_size + 1}"
)

assert not load_button.isEnabled()
active_realizations.setText(default_value_active_reals)
assert load_button.isEnabled()

iterations_field = get_child(panel, StringBox, name="iterations_field_lrm")
default_value_iteration = iterations_field.get_text
iterations_field.setText("-10")

assert not load_button.isEnabled()
iterations_field.setText(default_value_iteration)
assert load_button.isEnabled()

dialog.close()

QTimer.singleShot(1000, handle_load_results_dialog)
load_results_tool = gui.tools["Load results manually"]
load_results_tool.trigger()

0 comments on commit 0303a8a

Please sign in to comment.