Skip to content

Commit

Permalink
Minor parameter and test_parameter improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
petar-qb committed Mar 20, 2024
1 parent 45f4c45 commit 74ed0cd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 43 deletions.
22 changes: 7 additions & 15 deletions vizro-core/src/vizro/models/_controls/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down
46 changes: 18 additions & 28 deletions vizro-core/tests/unit/vizro/models/_controls/test_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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")
Expand Down

0 comments on commit 74ed0cd

Please sign in to comment.