Skip to content

Commit

Permalink
Fix all test and clean the flow of template class
Browse files Browse the repository at this point in the history
  • Loading branch information
unkcpz committed Oct 20, 2023
1 parent 60d43e8 commit 18e23f0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
39 changes: 23 additions & 16 deletions aiidalab_widgets_base/computational_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,12 @@ def on_setup_code(self, _=None):
f"Code {kwargs['label']}@{computer.label} already exists, skipping creation.",
MessageLevel.INFO,
)
# TODO: (unkcpz) as callback function this return value not actually used
# meanwhile if the code already exists we still want to regard it as success (TBD).
# and the `_on_setup_code_success` callback functions will run.
# The return will break the flow and handle back the control to the caller.
# The proper way is to not return but raise an exception and handle it in the caller function
# for the afterward process.
return False

try:
Expand Down Expand Up @@ -1391,6 +1397,9 @@ class TemplateVariablesWidget(ipw.VBox):
# The output template is a dictionary of keyname and filled string.
filled_templates = tl.Dict(allow_none=True)

# the output metadata is a dictionary of keyname and metadata of this template.
metadata = tl.Dict(allow_none=True)

def __init__(self):
# A placeholder for the template variables widget.
self.template_variables = ipw.VBox()
Expand Down Expand Up @@ -1431,14 +1440,11 @@ def _templates_changed(self, _=None):

self._render()

self.metadata = self.templates.get("metadata", {})

# Update the output filled template.
# After `self._render` all the widgets are created.
# We can use the widget value to fill the template even not all the widgets are filled.
# templates = copy.deepcopy(self.templates)
# XXX don't delete but pass as trait
# if "metadata" in templates:
# del templates["metadata"]

self.fill()

def _render(self):
Expand Down Expand Up @@ -1656,8 +1662,8 @@ def __init__(
(self.template_computer_configure, "filled_templates"),
(self.aiida_computer_setup, "computer_configure"),
)
self.aiida_computer_setup.observe(
self._on_computer_configure_change, "computer_configure"
self.template_computer_configure.observe(
self._on_template_computer_configure_metadata_change, "metadata"
)
self.aiida_computer_setup.on_setup_computer_success(
self._on_setup_computer_success
Expand Down Expand Up @@ -1790,17 +1796,15 @@ def _on_toggle_detail_setup(self, change):
self.detailed_setup_widget.layout.display = "none"
self.quick_setup_button.disabled = False

def _on_computer_configure_change(self, change):
"""Update the computer trait"""
if not change["new"]:
def _on_template_computer_configure_metadata_change(self, change):
"""callback when the metadata of computer_configure template is changed."""
if change["new"] is None:
return

new_configure = change["new"]

# decide whether to show the ssh password box widget.
# Since for 2FA ssh credential, the password are not needed but set from
# independent mechanism.
self.ssh_auth = new_configure.get("metadata", {}).get("ssh_auth", None)
self.ssh_auth = change["new"].get("ssh_auth", None)
if self.ssh_auth is None:
self.ssh_auth = "password"

Expand Down Expand Up @@ -1875,8 +1879,8 @@ def _on_setup_code_success(self):

def reset(self):
"""Reset the widget."""
self.message = ""
self.success = False
# reset the database
self.comp_resources_database.reset()

# reset template variables
self.template_computer_setup.reset()
Expand All @@ -1888,7 +1892,6 @@ def reset(self):
self.aiida_computer_setup._reset()

self.ssh_computer_setup._reset()
self.ssh_auth = None

# essential, since if not, the same computer_configure won't trigger the `_observe_ssh_config` callback.
self.ssh_computer_setup.ssh_config = {}
Expand All @@ -1898,6 +1901,10 @@ def reset(self):
self.computer_configure = {}
self.code_setup = {}

self.message = ""
self.success = False
self.ssh_auth = None

# The layout also reset
self._update_layout()

Expand Down
15 changes: 9 additions & 6 deletions tests/test_computational_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,21 +546,20 @@ def test_resource_setup_widget_default():
w.ssh_computer_setup.username.value = "aiida"

# Since cscs is 2FA, test the password box is not displayed.
# XXX failed because of the metadata not passed from template widget
# assert w.ssh_computer_setup.password_box.layout.display == "none"
assert w.ssh_computer_setup.password_box.layout.display == "none"

w._on_quick_setup()

assert w.success
assert orm.load_code("pw-7.2@daint-mc")

# test select new resource reset the widget, success trait, and message trait.
w.comp_resources_database.reset()
w.reset()

assert w.ssh_auth is None
assert w.computer_setup == {}
assert w.computer_configure == {}
assert w.code_setup == {}
assert w.aiida_computer_setup.computer_setup == {}
assert w.aiida_computer_setup.computer_configure == {}
assert w.aiida_code_setup.code_setup == {}
assert w.success is False
assert w.message == ""
assert w.template_code._help_text.layout.display == "none"
Expand Down Expand Up @@ -638,6 +637,10 @@ def test_resource_setup_widget_for_password_configure(monkeypatch, tmp_path):
assert "User aiida" in content
assert "Host merlin-l-01.psi.ch" in content

# After reset the password box should be hidden again.
w.reset()
assert w.ssh_computer_setup.password_box.layout.display == "none"


@pytest.mark.usefixtures("aiida_profile_clean")
def test_resource_setup_widget_computer_change_code_reset():
Expand Down

0 comments on commit 18e23f0

Please sign in to comment.