From 74ed0cdf96a6b83d24f69d6fa374ba3bcc418e1a Mon Sep 17 00:00:00 2001 From: petar-qb Date: Wed, 20 Mar 2024 08:37:17 +0100 Subject: [PATCH] Minor parameter and test_parameter improvements --- .../src/vizro/models/_controls/parameter.py | 22 +++------ .../vizro/models/_controls/test_parameter.py | 46 ++++++++----------- 2 files changed, 25 insertions(+), 43 deletions(-) diff --git a/vizro-core/src/vizro/models/_controls/parameter.py b/vizro-core/src/vizro/models/_controls/parameter.py index d228413a7..51a97b884 100644 --- a/vizro-core/src/vizro/models/_controls/parameter.py +++ b/vizro-core/src/vizro/models/_controls/parameter.py @@ -61,35 +61,27 @@ def check_duplicate_parameter_target(cls, targets): @_log_call def pre_build(self): - self._set_slider_values() - self._set_categorical_selectors_options() - self._set_date_picker_values() - self._set_selector() + self._check_numerical_and_temporal_selectors_values() + self._check_categorical_selectors_options() + self._set_selector_title() self._set_actions() @_log_call def build(self): return self.selector.build() - def _set_slider_values(self): - if isinstance(self.selector, (Slider, RangeSlider)): + def _check_numerical_and_temporal_selectors_values(self): + if isinstance(self.selector, (Slider, RangeSlider, DatePicker)): if self.selector.min is None or self.selector.max is None: raise TypeError( f"{self.selector.type} requires the arguments 'min' and 'max' when used within Parameter." ) - def _set_categorical_selectors_options(self): + def _check_categorical_selectors_options(self): if isinstance(self.selector, (Checklist, Dropdown, RadioItems)) and not self.selector.options: raise TypeError(f"{self.selector.type} requires the argument 'options' when used within Parameter.") - def _set_date_picker_values(self): - if isinstance(self.selector, DatePicker): - if self.selector.min is None or self.selector.max is None: - raise TypeError( - f"{self.selector.type} requires the arguments 'min' and 'max' when used within Parameter." - ) - - def _set_selector(self): + def _set_selector_title(self): if not self.selector.title: self.selector.title = ", ".join({target.rsplit(".")[-1] for target in self.targets}) diff --git a/vizro-core/tests/unit/vizro/models/_controls/test_parameter.py b/vizro-core/tests/unit/vizro/models/_controls/test_parameter.py index 46527bbaf..eeb687962 100644 --- a/vizro-core/tests/unit/vizro/models/_controls/test_parameter.py +++ b/vizro-core/tests/unit/vizro/models/_controls/test_parameter.py @@ -62,26 +62,8 @@ def test_set_target_and_title_valid(self, test_input, title): assert parameter.targets == ["scatter_chart.x"] assert parameter.selector.title == title - @pytest.mark.parametrize( - "test_input", - [ - (vm.Checklist(options=["lifeExp", "gdpPercap", "pop"], value=["lifeExp"])), - (vm.Dropdown(options=["lifeExp", "gdpPercap", "pop"], multi=False, value="lifeExp")), - (vm.RadioItems(options=["lifeExp", "gdpPercap", "pop"], value="lifeExp")), - ], - ) - def test_set_actions(self, test_input): - parameter = Parameter(targets=["scatter_chart.x"], selector=test_input) - page = model_manager["test_page"] - page.controls = [parameter] - parameter.pre_build() - default_action = parameter.selector.actions[0] - assert isinstance(default_action, ActionsChain) - assert isinstance(default_action.actions[0].function, CapturedCallable) - assert default_action.actions[0].id == f"parameter_action_{parameter.id}" - - @pytest.mark.parametrize("test_input", [vm.Slider(), vm.RangeSlider()]) - def test_slider_values_parameter_invalid(self, test_input): + @pytest.mark.parametrize("test_input", [vm.Slider(), vm.RangeSlider(), vm.DatePicker()]) + def test_numerical_and_temporal_selectors_missing_values(self, test_input): parameter = Parameter(targets=["scatter_chart.x"], selector=test_input) page = model_manager["test_page"] page.controls = [parameter] @@ -91,7 +73,7 @@ def test_slider_values_parameter_invalid(self, test_input): parameter.pre_build() @pytest.mark.parametrize("test_input", [vm.Checklist(), vm.Dropdown(), vm.RadioItems()]) - def test_set_categorical_selectors_with_missing_options(self, test_input): + def test_categorical_selectors_with_missing_options(self, test_input): parameter = Parameter(targets=["scatter_chart.x"], selector=test_input) page = model_manager["test_page"] page.controls = [parameter] @@ -100,15 +82,23 @@ def test_set_categorical_selectors_with_missing_options(self, test_input): ): parameter.pre_build() - def test_datepicker_values_parameter_invalid(self): - parameter = Parameter(targets=["scatter_chart.x"], selector=vm.DatePicker()) + @pytest.mark.parametrize( + "test_input", + [ + (vm.Checklist(options=["lifeExp", "gdpPercap", "pop"], value=["lifeExp"])), + (vm.Dropdown(options=["lifeExp", "gdpPercap", "pop"], multi=False, value="lifeExp")), + (vm.RadioItems(options=["lifeExp", "gdpPercap", "pop"], value="lifeExp")), + ], + ) + def test_set_actions(self, test_input): + parameter = Parameter(targets=["scatter_chart.x"], selector=test_input) page = model_manager["test_page"] page.controls = [parameter] - with pytest.raises( - TypeError, - match=f"{vm.DatePicker().type} requires the arguments 'min' and 'max' when used within Parameter.", - ): - parameter.pre_build() + parameter.pre_build() + default_action = parameter.selector.actions[0] + assert isinstance(default_action, ActionsChain) + assert isinstance(default_action.actions[0].function, CapturedCallable) + assert default_action.actions[0].id == f"parameter_action_{parameter.id}" @pytest.mark.usefixtures("managers_one_page_two_graphs")