From 568f7ca56b480e48065c6a69ef25cfecb82bf4d7 Mon Sep 17 00:00:00 2001 From: Phil Elson Date: Mon, 26 Oct 2020 14:36:54 +0100 Subject: [PATCH] Fix the broken mask selection widget. --- app/cara.ipynb | 2 +- cara/apps.py | 7 +++++-- cara/state.py | 3 ++- cara/tests/test_state.py | 6 +++++- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/app/cara.ipynb b/app/cara.ipynb index 5f8ba0d5..11b0d079 100644 --- a/app/cara.ipynb +++ b/app/cara.ipynb @@ -52,4 +52,4 @@ }, "nbformat": 4, "nbformat_minor": 4 -} +} \ No newline at end of file diff --git a/cara/apps.py b/cara/apps.py index 3625a64c..96edf037 100644 --- a/cara/apps.py +++ b/cara/apps.py @@ -214,8 +214,7 @@ class WidgetView: mask_choice = widgets.Select(options=list(models.Mask.types.keys()), value=name) def on_mask_change(change): - mask = models.Mask.types[change['new']] - node.dcs_update_from(mask) + node.dcs_select(change['new']) mask_choice.observe(on_mask_change, names=['value']) return widget_group( @@ -322,6 +321,10 @@ class ExpertApplication: state_builder=CARAStateBuilder(), ) self.model_state.dcs_update_from(baseline_model) + # For the time-being, we have to initialise the select states. Careful + # as values might not correspond to what the baseline model says. + self.model_state.infected.mask.dcs_select('No mask') + self.view = WidgetView(self.model_state) @property diff --git a/cara/state.py b/cara/state.py index 771eba69..145a1c2b 100644 --- a/cara/state.py +++ b/cara/state.py @@ -294,6 +294,7 @@ class DataclassStatePredefined(DataclassInstanceState): raise ValueError(f'The choice {name} is not valid. Possible options are {", ".join(self._choices)}') self._selected = name self._instance = self._choices[name] + self._fire_observers() def dcs_instance(self): return self._choices[self._selected] @@ -351,7 +352,7 @@ class DataclassStateNamed(DataclassState): if name not in self._states: raise ValueError(f'The choice {name} is not valid. Possible options are {", ".join(self._states)}') self._selected = name - self._selected_state()._fire_observers() + self._fire_observers() def _selected_state(self): return self._states[self._selected] diff --git a/cara/tests/test_state.py b/cara/tests/test_state.py index 34815e55..2102b96e 100644 --- a/cara/tests/test_state.py +++ b/cara/tests/test_state.py @@ -158,6 +158,11 @@ def test_DCS_predefined(): s.dcs_update_from(opt1) assert s.dcs_instance() == opt2 + observer = Mock() + s.dcs_observe(observer) + s.dcs_select('option 1') + observer.assert_called_once_with() + def test_DCS_named(): opt1 = DCSimpleSubclass('a', 1, 3.14) @@ -201,7 +206,6 @@ def test_DCS_named(): s.dcs_select('option 1') opt1_observer.reset_mock() - # ASDA with s.dcs_state_transaction(): s.dcs_select('option 2') s.dcs_update_from(opt2)