Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
superstar54 committed Oct 27, 2023
1 parent 54c0aaa commit 7621099
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 deletions.
5 changes: 2 additions & 3 deletions src/aiidalab_qe/app/configuration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,9 @@ def confirm(self, _=None):
self.state = self.State.SUCCESS
self.is_confirmed = True

def has_unsaved_changes(self):
"""Check if the current configuration is unsaved."""
def has_unconfirmed_changes(self):
"""Check if the current step has unconfirmed changes"""
new_parameters = self.get_configuration_parameters()
print(new_parameters)
return new_parameters != self.configuration_parameters

@tl.default("state")
Expand Down
30 changes: 20 additions & 10 deletions src/aiidalab_qe/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

import ipywidgets as ipw
import traitlets as tl
from aiida.orm import load_node
from aiidalab_widgets_base import WizardAppWidget, WizardAppWidgetStep

Expand All @@ -18,13 +19,15 @@
class App(ipw.VBox):
"""The main widget that combines all the application steps together."""

_submission_blockers = tl.List(tl.Unicode())

def __init__(self, qe_auto_setup=True):
# Create the application steps
self.structure_step = StructureSelectionStep(auto_advance=True)
self.structure_step.observe(self._observe_structure_selection, "structure")
self.configure_step = ConfigureQeAppWorkChainStep(auto_advance=True)
self.submit_step = SubmitQeAppWorkChainStep(
auto_advance=True, qe_auto_setup=qe_auto_setup
auto_advance=True, qe_auto_setup=qe_auto_setup, parent=self
)
self.results_step = ViewQeAppWorkChainStatusAndResultsStep()

Expand Down Expand Up @@ -66,6 +69,7 @@ def __init__(self, qe_auto_setup=True):
]
)
self._wizard_app_widget.observe(self._observe_selected_index, "selected_index")
self.observe(self._observe_submission_blockers, "_submission_blockers")

# Add process selection header
self.work_chain_selector = QeAppWorkChainSelector(
Expand All @@ -90,26 +94,32 @@ def __init__(self, qe_auto_setup=True):
def steps(self):
return self._wizard_app_widget.steps

# Check unsaved change in the step when leaving the step.
# Check unconfirmed change in the step when leaving the step.
def _observe_selected_index(self, change):
with self.submit_step.hold_sync():
if change["old"] is None:
return
# check if the step is confirmed before
# if not, we don't need to check the unsaved changes
# if not, we don't need to check the unconfirmed changes
previous_step = self.steps[change["old"]][1]
if getattr(previous_step, "is_confirmed", False) is False:
return
# check if the step is changed
if previous_step.has_unsaved_changes():
# update the blocker message of the submit step
blockers = [
f"Unsaved changes in the step: {self.steps[change['old']][0]}"
]
if previous_step.has_unconfirmed_changes():
# reset the state of the step
previous_step.state = WizardAppWidgetStep.State.CONFIGURED
# udpate the blocker message of the submit step
self.steps[2][1]._submission_blockers = blockers
# update the blocker message, this will trigger the observer
# to update the blocker message of the submit step
self._submission_blockers = [
f"There are unconfirmed changes in the Step {change['old']+1}: {self.steps[change['old']][0]}"
]

def _observe_submission_blockers(self, change):
if change["new"]:
# udpate the blocker message of the submit step
self.submit_step._submission_blockers = list(
self.submit_step._identify_submission_blockers()
)

# Reset all subsequent steps in case that a new structure is selected
def _observe_structure_selection(self, change):
Expand Down
4 changes: 2 additions & 2 deletions src/aiidalab_qe/app/structure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ def confirm(self, _=None):
self.message_area.value = ""
self.is_confirmed = True

def has_unsaved_changes(self):
"""Check if the current structure is unsaved."""
def has_unconfirmed_changes(self):
"""Check if the current structure is unconfirmed."""
return self.confirmed_structure != self.structure

def can_reset(self):
Expand Down
6 changes: 5 additions & 1 deletion src/aiidalab_qe/app/submission/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class SubmitQeAppWorkChainStep(ipw.VBox, WizardAppWidgetStep):
input_parameters = tl.Dict()
_submission_blockers = tl.List(tl.Unicode())

def __init__(self, qe_auto_setup=True, **kwargs):
def __init__(self, qe_auto_setup=True, parent=None, **kwargs):
self.parent = parent
self.message_area = ipw.Output()
self._submission_blocker_messages = ipw.HTML()

Expand Down Expand Up @@ -150,6 +151,9 @@ def _observe_submission_blockers(self, change):
self._submission_blocker_messages.value = ""

def _identify_submission_blockers(self):
# blocker messages from the app
for msg in self.parent._submission_blockers:
yield msg
# Do not submit while any of the background setup processes are running.
if self.qe_setup_status.busy or self.sssp_installation_status.busy:
yield "Background setup processes must finish."
Expand Down
18 changes: 18 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,21 @@ def test_reload_and_reset(submit_app_generator, generate_qeapp_workchain):
== 0
)
assert app.submit_step.resources_config.num_cpus.value == 1


def test_unconfirmed_changes(app_to_submit):
"""Test if the unconfirmed changes are handled correctly"""
from aiidalab_widgets_base import WizardAppWidgetStep

app = app_to_submit
# the number of blocker
n = len(app.submit_step._submission_blockers)
# go to the configue step, and make some changes
app._wizard_app_widget.selected_index = 1
app.configure_step.workchain_settings.relax_type.value = "positions"
# go to the submit step
app._wizard_app_widget.selected_index = 2
# the state of the configue step should be updated.
assert app.configure_step.state == WizardAppWidgetStep.State.CONFIGURED
# check if a new blocker is added
assert len(app.submit_step._submission_blockers) == n + 1

0 comments on commit 7621099

Please sign in to comment.