Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
huong-li-nguyen committed Jul 3, 2024
1 parent a3a4623 commit 8d43c11
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Uncomment the section that is right (remove the HTML comment wrapper).

### Fixed

- Ensure that selectors always return a list of values. ([#561](https://github.com/mckinsey/vizro/pull/561))
- Ensure that selectors always return a list of values. ([#562](https://github.com/mckinsey/vizro/pull/562))

<!--
### Security
Expand Down
28 changes: 28 additions & 0 deletions vizro-core/tests/unit/vizro/actions/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ def gapminder_2007(gapminder):
return gapminder.query("year == 2007")


@pytest.fixture
def iris():
return px.data.iris()


@pytest.fixture
def gapminder_dynamic_first_n_last_n_function(gapminder):
return lambda first_n=None, last_n=None: (
Expand Down Expand Up @@ -44,6 +49,16 @@ def scatter_chart(gapminder_2007, scatter_params):
return px.scatter(gapminder_2007, **scatter_params).update_layout(margin_t=24)


@pytest.fixture
def scatter_matrix_params():
return {"dimensions": ["sepal_width", "sepal_length", "petal_width", "petal_length"]}


@pytest.fixture
def scatter_matrix_chart(iris, scatter_matrix_params):
return px.scatter_matrix(iris, **scatter_matrix_params).update_layout(margin_t=24)


@pytest.fixture
def scatter_chart_dynamic_data_frame(scatter_params):
return px.scatter("gapminder_dynamic_first_n_last_n", **scatter_params).update_layout(margin_t=24)
Expand Down Expand Up @@ -110,3 +125,16 @@ def managers_one_page_two_graphs_one_table_one_aggrid_one_button(
],
)
Vizro._pre_build()


@pytest.fixture
def managers_one_page_one_graph_with_dict_param_input(scatter_matrix_chart):
"""Instantiates a model_manager and data_manager with a page and a graph that requires a list input."""
vm.Page(
id="test_page",
title="My first dashboard",
components=[
vm.Graph(id="scatter_matrix_chart", figure=scatter_matrix_chart),
],
)
Vizro._pre_build()
72 changes: 72 additions & 0 deletions vizro-core/tests/unit/vizro/actions/test_parameter_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ def target_scatter_parameter_y(request, gapminder_2007, scatter_params):
return px.scatter(gapminder_2007, **scatter_params).update_layout(margin_t=24)


@pytest.fixture
def target_scatter_matrix_parameter_dimensions(request, iris, scatter_matrix_params):
dimensions = request.param
scatter_matrix_params["dimensions"] = dimensions
return px.scatter_matrix(iris, **scatter_matrix_params).update_layout(margin_t=24)


@pytest.fixture
def target_scatter_parameter_hover_data(request, gapminder_2007, scatter_params):
hover_data = request.param
Expand Down Expand Up @@ -95,6 +102,38 @@ def ctx_parameter_y(request):
return context_value


@pytest.fixture
def ctx_parameter_dimensions(request):
"""Mock dash.ctx that represents `dimensions` Parameter value selection."""
y = request.param
mock_ctx = {
"args_grouping": {
"external": {
"filter_interaction": [],
"filters": [],
"parameters": [
CallbackTriggerDict(
id="dimensions_parameter",
property="value",
value=y,
str_id="dimensions_parameter",
triggered=False,
)
],
"theme_selector": CallbackTriggerDict(
id="theme_selector",
property="checked",
value=False,
str_id="theme_selector",
triggered=False,
),
}
}
}
context_value.set(AttributeDict(**mock_ctx))
return context_value


@pytest.fixture
def ctx_parameter_hover_data(request):
"""Mock dash.ctx that represents hover_data Parameter value selection."""
Expand Down Expand Up @@ -497,3 +536,36 @@ def test_data_frame_parameters_multiple_targets(
}

assert result == expected

@pytest.mark.usefixtures("managers_one_page_one_graph_with_dict_param_input")
@pytest.mark.parametrize(
"ctx_parameter_dimensions, target_scatter_matrix_parameter_dimensions",
[("ALL", ["sepal_length", "sepal_width", "petal_length", "petal_width"]), (["sepal_width"], ["sepal_width"])],
indirect=True,
)
def test_one_parameter_with_dict_input_as_options(
self, ctx_parameter_dimensions, target_scatter_matrix_parameter_dimensions
):
# If the options are provided as a list of dictionaries, the value should be correctly passed to the
# target as a list. So when "ALL" is selected, a list of all possible values should be returned.
dimensions_parameter = vm.Parameter(
id="test_parameter_dimensions",
targets=["scatter_matrix_chart.dimensions"],
selector=vm.RadioItems(
id="dimensions_parameter",
options=[
{"label": "sepal_length", "value": "sepal_length"},
{"label": "sepal_width", "value": "sepal_width"},
{"label": "petal_length", "value": "petal_length"},
{"label": "petal_width", "value": "petal_width"},
],
),
)
model_manager["test_page"].controls = [dimensions_parameter]
dimensions_parameter.pre_build()

# Run action by picking the above added action function and executing it with ()
result = model_manager[f"{PARAMETER_ACTION_PREFIX}_test_parameter_dimensions"].function()
expected = {"scatter_matrix_chart": target_scatter_matrix_parameter_dimensions}

assert result == expected

0 comments on commit 8d43c11

Please sign in to comment.