Skip to content

Commit

Permalink
Adding more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
petar-qb committed Oct 27, 2023
1 parent f44bd18 commit bd21f02
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
import vizro.models as vm
import vizro.plotly.express as px
from vizro import Vizro
from vizro.actions import export_data
from vizro.actions import export_data, filter_interaction
from vizro.actions._action_loop._get_action_loop_components import _get_action_loop_components
from vizro.managers import model_manager
from vizro.tables import dash_data_table


@pytest.fixture
Expand Down Expand Up @@ -68,14 +69,29 @@ def trigger_to_actions_chain_mapper_component(request):


@pytest.fixture
def managers_one_page_two_components_two_controls():
def dash_data_table_fixture_with_id(gapminder_2007):
return dash_data_table(id="underlying_table_id", data_frame=gapminder_2007)


@pytest.fixture
def managers_one_page_two_components_two_controls(dash_data_table_fixture_with_id):
"""Instantiates managers with one page that contains two controls and two components."""
vm.Dashboard(
pages=[
vm.Page(
id="test_page",
title="First page",
components=[
vm.Table(
id="vizro_table",
figure=dash_data_table_fixture_with_id,
actions=[
vm.Action(
id="table_filter_interaction_action",
function=filter_interaction(targets=["scatter_chart"]),
)
],
),
vm.Graph(
id="scatter_chart",
figure=px.scatter(px.data.gapminder(), x="lifeExp", y="gdpPercap"),
Expand Down Expand Up @@ -141,10 +157,10 @@ def test_no_components(self):
"trigger_to_actions_chain_mapper_component",
[
(
["test_page", "export_data_button", "filter_continent_selector", "parameter_x_selector"],
["test_page", "export_data_button", "filter_continent_selector", "parameter_x_selector"],
["test_page", "export_data_button", "filter_continent_selector", "parameter_x_selector"],
["test_page", "export_data_button", "filter_continent_selector", "parameter_x_selector"],
["test_page", "vizro_table", "export_data_button", "filter_continent_selector", "parameter_x_selector"],
["test_page", "vizro_table", "export_data_button", "filter_continent_selector", "parameter_x_selector"],
["test_page", "vizro_table", "export_data_button", "filter_continent_selector", "parameter_x_selector"],
["test_page", "vizro_table", "export_data_button", "filter_continent_selector", "parameter_x_selector"],
)
],
indirect=True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from vizro.actions import export_data, filter_interaction
from vizro.actions._callback_mapping._get_action_callback_mapping import _get_action_callback_mapping
from vizro.models.types import capture
from vizro.tables import dash_data_table


@capture("action")
Expand All @@ -29,7 +30,12 @@ def export_data():


@pytest.fixture
def managers_one_page_four_controls_two_graphs_filter_interaction(request):
def dash_data_table_fixture_with_id(gapminder_2007):
return dash_data_table(id="underlying_table_id", data_frame=gapminder_2007)


@pytest.fixture
def managers_one_page_four_controls_two_figures_filter_interaction(request, dash_data_table_fixture_with_id):
"""Instantiates managers with one page that contains four controls, two graphs and filter interaction."""
# If the fixture is parametrised set the targets. Otherwise, set export_data without targets.
export_data_action_function = export_data(targets=request.param) if hasattr(request, "param") else export_data()
Expand All @@ -50,6 +56,16 @@ def managers_one_page_four_controls_two_graphs_filter_interaction(request):
figure=px.scatter(px.data.gapminder(), x="lifeExp", y="gdpPercap", custom_data=["continent"]),
actions=[vm.Action(id="custom_action", function=custom_action_example())],
),
vm.Table(
id="vizro_table",
figure=dash_data_table_fixture_with_id,
actions=[
vm.Action(
id="table_filter_interaction_action",
function=filter_interaction(targets=["scatter_chart", "scatter_chart_2"]),
)
],
),
vm.Button(
id="export_data_button",
actions=[
Expand All @@ -63,7 +79,7 @@ def managers_one_page_four_controls_two_graphs_filter_interaction(request):
vm.Filter(id="filter_country", column="country", selector=vm.Dropdown(id="filter_country_selector")),
vm.Parameter(
id="parameter_x",
targets=["scatter_chart.x", "scatter_chart_2.x"],
targets=["scatter_chart.x", "scatter_chart_2.x", "vizro_table.columns"],
selector=vm.Dropdown(
id="parameter_x_selector",
options=["lifeExp", "gdpPercap", "pop"],
Expand All @@ -73,7 +89,7 @@ def managers_one_page_four_controls_two_graphs_filter_interaction(request):
),
vm.Parameter(
id="parameter_y",
targets=["scatter_chart.y", "scatter_chart_2.y"],
targets=["scatter_chart.y", "scatter_chart_2.y", "vizro_table.values"],
selector=vm.Dropdown(
id="parameter_y_selector",
options=["lifeExp", "gdpPercap", "pop"],
Expand All @@ -100,7 +116,11 @@ def action_callback_inputs_expected():
"filter_interaction": [
{
"clickData": dash.State("scatter_chart", "clickData"),
}
},
{
"active_cell": dash.State("underlying_table_id", "active_cell"),
"derived_viewport_data": dash.State("underlying_table_id", "derived_viewport_data"),
},
],
"theme_selector": dash.State("theme_selector", "on"),
}
Expand All @@ -125,7 +145,11 @@ def export_data_inputs_expected():
"filter_interaction": [
{
"clickData": dash.State("scatter_chart", "clickData"),
}
},
{
"active_cell": dash.State("underlying_table_id", "active_cell"),
"derived_viewport_data": dash.State("underlying_table_id", "derived_viewport_data"),
},
],
"theme_selector": [],
}
Expand All @@ -149,7 +173,7 @@ def export_data_components_expected(request):
]


@pytest.mark.usefixtures("managers_one_page_four_controls_two_graphs_filter_interaction")
@pytest.mark.usefixtures("managers_one_page_four_controls_two_figures_filter_interaction")
class TestCallbackMapping:
"""Tests action callback mapping for predefined and custom actions."""

Expand Down Expand Up @@ -180,21 +204,39 @@ def test_action_callback_mapping_inputs(self, action_id, callback_mapping_inputs
[
{"component_id": "scatter_chart", "component_property": "figure"},
{"component_id": "scatter_chart_2", "component_property": "figure"},
{"component_id": "vizro_table", "component_property": "children"},
],
),
("filter_interaction_action", [{"component_id": "scatter_chart_2", "component_property": "figure"}]),
(
"table_filter_interaction_action",
[
{"component_id": "scatter_chart", "component_property": "figure"},
{"component_id": "scatter_chart_2", "component_property": "figure"},
],
),
(
"parameter_action_parameter_x",
[
{"component_id": "scatter_chart", "component_property": "figure"},
{"component_id": "scatter_chart_2", "component_property": "figure"},
{"component_id": "vizro_table", "component_property": "children"},
],
),
(
"parameter_action_parameter_y",
[
{"component_id": "scatter_chart", "component_property": "figure"},
{"component_id": "scatter_chart_2", "component_property": "figure"},
{"component_id": "vizro_table", "component_property": "children"},
],
),
(
"on_page_load_action_action_test_page",
[
{"component_id": "scatter_chart", "component_property": "figure"},
{"component_id": "scatter_chart_2", "component_property": "figure"},
{"component_id": "vizro_table", "component_property": "children"},
],
),
],
Expand All @@ -209,7 +251,7 @@ def test_action_callback_mapping_outputs(self, action_id, action_callback_output

@pytest.mark.parametrize(
"export_data_outputs_expected",
[("scatter_chart", "scatter_chart_2")],
[("scatter_chart", "scatter_chart_2", "vizro_table")],
indirect=True,
)
def test_export_data_no_targets_set_mapping_outputs(self, export_data_outputs_expected):
Expand All @@ -221,17 +263,17 @@ def test_export_data_no_targets_set_mapping_outputs(self, export_data_outputs_ex
assert result == export_data_outputs_expected

@pytest.mark.parametrize(
"managers_one_page_four_controls_two_graphs_filter_interaction, export_data_outputs_expected",
"managers_one_page_four_controls_two_figures_filter_interaction, export_data_outputs_expected",
[
(None, ["scatter_chart", "scatter_chart_2"]),
([], ["scatter_chart", "scatter_chart_2"]),
(None, ["scatter_chart", "scatter_chart_2", "vizro_table"]),
([], ["scatter_chart", "scatter_chart_2", "vizro_table"]),
(["scatter_chart"], ["scatter_chart"]),
(["scatter_chart", "scatter_chart_2"], ["scatter_chart", "scatter_chart_2"]),
],
indirect=True,
)
def test_export_data_targets_set_mapping_outputs(
self, managers_one_page_four_controls_two_graphs_filter_interaction, export_data_outputs_expected
self, managers_one_page_four_controls_two_figures_filter_interaction, export_data_outputs_expected
):
result = _get_action_callback_mapping(
action_id="export_data_action",
Expand All @@ -242,7 +284,7 @@ def test_export_data_targets_set_mapping_outputs(

@pytest.mark.parametrize(
"export_data_components_expected",
[("scatter_chart", "scatter_chart_2")],
[("scatter_chart", "scatter_chart_2", "vizro_table")],
indirect=True,
)
def test_export_data_no_targets_set_mapping_components(self, export_data_components_expected):
Expand All @@ -256,17 +298,17 @@ def test_export_data_no_targets_set_mapping_components(self, export_data_compone
assert result == expected

@pytest.mark.parametrize(
"managers_one_page_four_controls_two_graphs_filter_interaction, export_data_components_expected",
"managers_one_page_four_controls_two_figures_filter_interaction, export_data_components_expected",
[
(None, ["scatter_chart", "scatter_chart_2"]),
([], ["scatter_chart", "scatter_chart_2"]),
(None, ["scatter_chart", "scatter_chart_2", "vizro_table"]),
([], ["scatter_chart", "scatter_chart_2", "vizro_table"]),
(["scatter_chart"], ["scatter_chart"]),
(["scatter_chart", "scatter_chart_2"], ["scatter_chart", "scatter_chart_2"]),
],
indirect=True,
)
def test_export_data_targets_set_mapping_components(
self, managers_one_page_four_controls_two_graphs_filter_interaction, export_data_components_expected
self, managers_one_page_four_controls_two_figures_filter_interaction, export_data_components_expected
):
result_components = _get_action_callback_mapping(
action_id="export_data_action",
Expand Down
8 changes: 4 additions & 4 deletions vizro-core/tests/unit/vizro/actions/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def scatter_chart(gapminder_2007, scatter_params):


@pytest.fixture
def dash_data_table_fixture(gapminder_2007):
return dash_data_table(data_frame=gapminder_2007)
def dash_data_table_fixture_with_id(gapminder_2007):
return dash_data_table(id="underlying_table_id", data_frame=gapminder_2007)


@pytest.fixture
Expand Down Expand Up @@ -66,15 +66,15 @@ def managers_one_page_two_graphs_one_button(box_chart, scatter_chart):


@pytest.fixture
def managers_one_page_two_graphs_one_table_one_button(box_chart, scatter_chart, dash_data_table_fixture):
def managers_one_page_two_graphs_one_table_one_button(box_chart, scatter_chart, dash_data_table_fixture_with_id):
"""Instantiates a simple model_manager and data_manager with a page, two graph models and the button component."""
vm.Page(
id="test_page",
title="My first dashboard",
components=[
vm.Graph(id="box_chart", figure=box_chart),
vm.Graph(id="scatter_chart", figure=scatter_chart),
vm.Table(id="vizro_table", figure=dash_data_table_fixture),
vm.Table(id="vizro_table", figure=dash_data_table_fixture_with_id),
vm.Button(id="button"),
],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ def callback_context_filter_interaction(request):
args_grouping_filter_interaction.append(
{
"active_cell": CallbackTriggerDict(
id="vizro_table",
id="underlying_table_id",
property="active_cell",
value={"row": 0, "column": 0, "column_id": "country"},
str_id="vizro_table",
triggered=False,
),
"derived_viewport_data": CallbackTriggerDict(
id="vizro_table",
id="underlying_table_id",
property="derived_viewport_data",
value=[
{
Expand Down Expand Up @@ -233,6 +233,7 @@ def test_table_filter_interaction_with_one_target(
]

model_manager["vizro_table"].actions = [vm.Action(function=filter_interaction(targets=["scatter_chart"]))]
model_manager["vizro_table"].pre_build()

# Run action by picking the above added action function and executing it with ()
result = model_manager["test_action"].function()
Expand Down Expand Up @@ -261,6 +262,7 @@ def test_table_filter_interaction_with_two_targets(
model_manager["vizro_table"].actions = [
vm.Action(function=filter_interaction(targets=["scatter_chart", "box_chart"]))
]
model_manager["vizro_table"].pre_build()

# Run action by picking the above added action function and executing it with ()
result = model_manager["test_action"].function()
Expand Down Expand Up @@ -290,6 +292,7 @@ def test_mixed_chart_and_table_filter_interaction_with_two_targets(
model_manager["vizro_table"].actions = [
vm.Action(function=filter_interaction(targets=["scatter_chart", "box_chart"]))
]
model_manager["vizro_table"].pre_build()

# Run action by picking the above added action function and executing it with ()
result = model_manager["test_action"].function()
Expand Down

0 comments on commit bd21f02

Please sign in to comment.