From bd21f02c424df98f20e2595e7736a008747c8494 Mon Sep 17 00:00:00 2001 From: petar-qb Date: Fri, 27 Oct 2023 12:50:54 +0200 Subject: [PATCH] Adding more tests --- .../test_get_action_loop_components.py | 28 +++++-- .../test_get_action_callback_mapping.py | 74 +++++++++++++++---- .../tests/unit/vizro/actions/conftest.py | 8 +- .../actions/test_filter_interaction_action.py | 7 +- 4 files changed, 89 insertions(+), 28 deletions(-) diff --git a/vizro-core/tests/unit/vizro/actions/_action_loop/test_get_action_loop_components.py b/vizro-core/tests/unit/vizro/actions/_action_loop/test_get_action_loop_components.py index 68d23cbfb..15e3ae93c 100644 --- a/vizro-core/tests/unit/vizro/actions/_action_loop/test_get_action_loop_components.py +++ b/vizro-core/tests/unit/vizro/actions/_action_loop/test_get_action_loop_components.py @@ -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 @@ -68,7 +69,12 @@ 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=[ @@ -76,6 +82,16 @@ def managers_one_page_two_components_two_controls(): 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"), @@ -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, diff --git a/vizro-core/tests/unit/vizro/actions/_callback_mapping/test_get_action_callback_mapping.py b/vizro-core/tests/unit/vizro/actions/_callback_mapping/test_get_action_callback_mapping.py index a1f2bd395..a1865e6ba 100644 --- a/vizro-core/tests/unit/vizro/actions/_callback_mapping/test_get_action_callback_mapping.py +++ b/vizro-core/tests/unit/vizro/actions/_callback_mapping/test_get_action_callback_mapping.py @@ -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") @@ -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() @@ -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=[ @@ -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"], @@ -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"], @@ -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"), } @@ -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": [], } @@ -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.""" @@ -180,14 +204,31 @@ 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"}, ], ), ( @@ -195,6 +236,7 @@ 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"}, ], ), ], @@ -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): @@ -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", @@ -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): @@ -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", diff --git a/vizro-core/tests/unit/vizro/actions/conftest.py b/vizro-core/tests/unit/vizro/actions/conftest.py index ab4473ee6..5fa39e9f1 100644 --- a/vizro-core/tests/unit/vizro/actions/conftest.py +++ b/vizro-core/tests/unit/vizro/actions/conftest.py @@ -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 @@ -66,7 +66,7 @@ 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", @@ -74,7 +74,7 @@ def managers_one_page_two_graphs_one_table_one_button(box_chart, scatter_chart, 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"), ], ) diff --git a/vizro-core/tests/unit/vizro/actions/test_filter_interaction_action.py b/vizro-core/tests/unit/vizro/actions/test_filter_interaction_action.py index a72693583..6a5ff2783 100644 --- a/vizro-core/tests/unit/vizro/actions/test_filter_interaction_action.py +++ b/vizro-core/tests/unit/vizro/actions/test_filter_interaction_action.py @@ -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=[ { @@ -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() @@ -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() @@ -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()