Skip to content

Commit

Permalink
Action loop and callback mapping tests - missing changes (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
petar-qb authored Oct 11, 2023
1 parent c5202f7 commit b49a42b
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!--
A new scriv changelog fragment.
Uncomment the section that is right (remove the HTML comment wrapper).
-->

<!--
### Removed
- A bullet item for the Removed category.
-->
<!--
### Added
- A bullet item for the Added category.
-->
<!--
### Changed
- A bullet item for the Changed category.
-->
<!--
### Deprecated
- A bullet item for the Deprecated category.
-->
<!--
### Fixed
- A bullet item for the Fixed category.
-->
<!--
### Security
- A bullet item for the Security category.
-->
Original file line number Diff line number Diff line change
Expand Up @@ -71,46 +71,56 @@ def trigger_to_actions_chain_mapper_component(request):
@pytest.fixture
def managers_one_page_two_components_two_controls():
"""Instantiates managers with one page that contains two controls and two components."""
page = vm.Page(
id="test_page",
title="First page",
components=[
vm.Graph(
id="scatter_chart",
figure=px.scatter(px.data.gapminder(), x="lifeExp", y="gdpPercap"),
),
vm.Button(
id="export_data_button",
actions=[vm.Action(id="export_data_action", function=export_data())],
),
],
controls=[
vm.Filter(id="filter_continent", column="continent", selector=vm.Dropdown(id="filter_continent_selector")),
vm.Parameter(
id="parameter_x",
targets=["scatter_chart.x"],
selector=vm.Dropdown(
id="parameter_x_selector",
options=["lifeExp", "gdpPercap", "pop"],
),
),
],
vm.Dashboard(
pages=[
vm.Page(
id="test_page",
title="First page",
components=[
vm.Graph(
id="scatter_chart",
figure=px.scatter(px.data.gapminder(), x="lifeExp", y="gdpPercap"),
),
vm.Button(
id="export_data_button",
actions=[vm.Action(id="export_data_action", function=export_data())],
),
],
controls=[
vm.Filter(
id="filter_continent", column="continent", selector=vm.Dropdown(id="filter_continent_selector")
),
vm.Parameter(
id="parameter_x",
targets=["scatter_chart.x"],
selector=vm.Dropdown(
id="parameter_x_selector",
options=["lifeExp", "gdpPercap", "pop"],
),
),
],
)
]
)
# TODO: Call the Dashboard._pre_build() method once the pages registration is moved into this method.
yield Vizro().build(vm.Dashboard(pages=[page]))

yield Vizro._pre_build()
del dash.page_registry["test_page"]


@pytest.fixture
def managers_one_page_no_actions():
"""Instantiates managers with one "empty" page."""
page = vm.Page(
id="test_page_no_actions",
title="Second page",
components=[vm.Card(text="")],
vm.Dashboard(
pages=[
vm.Page(
id="test_page_no_actions",
title="Second page",
components=[vm.Card(text="")],
)
]
)
# TODO: Call the Dashboard._pre_build() method once the pages registration is moved into this method.
yield Vizro().build(vm.Dashboard(pages=[page]))

yield Vizro._pre_build()
del dash.page_registry["test_page_no_actions"]


Expand All @@ -122,8 +132,7 @@ def test_no_components(self):
result = _get_action_loop_components()
result = json.loads(json.dumps(result, cls=plotly.utils.PlotlyJSONEncoder))

expected = html.Div(id="action_loop_components_div")
expected = json.loads(json.dumps(expected, cls=plotly.utils.PlotlyJSONEncoder))
expected = json.loads(json.dumps(html.Div(id="action_loop_components_div"), cls=plotly.utils.PlotlyJSONEncoder))

assert result == expected

Expand Down Expand Up @@ -151,8 +160,7 @@ def test_all_action_loop_components( # noqa: PLR0913 # pylint: disable=too-man
action_trigger_actions_id_component,
trigger_to_actions_chain_mapper_component,
):
result = _get_action_loop_components()
result = json.loads(json.dumps(result, cls=plotly.utils.PlotlyJSONEncoder))
result = json.loads(json.dumps(_get_action_loop_components(), cls=plotly.utils.PlotlyJSONEncoder))

all_action_loop_components_expected = (
fundamental_components
Expand All @@ -162,7 +170,11 @@ def test_all_action_loop_components( # noqa: PLR0913 # pylint: disable=too-man
+ [trigger_to_actions_chain_mapper_component]
)

expected = html.Div(children=all_action_loop_components_expected, id="action_loop_components_div")
expected = json.loads(json.dumps(expected, cls=plotly.utils.PlotlyJSONEncoder))
expected = json.loads(
json.dumps(
html.Div(children=all_action_loop_components_expected, id="action_loop_components_div"),
cls=plotly.utils.PlotlyJSONEncoder,
)
)

assert result == expected
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ def test_known_action_unknown_argument(self):
)
assert result == {}

# "export_data_custom_action" represents a unique scenario within custom actions, where the function's name
# coincides with an already predefined action function (in this instance, "export_data").
# It requires handling them as conventional custom actions.
@pytest.mark.parametrize("action_id", ["custom_action", "export_data_custom_action"])
@pytest.mark.parametrize(
"argument, expected",
[
Expand All @@ -288,25 +292,9 @@ def test_known_action_unknown_argument(self):
("unknown-argument", {}),
],
)
def test_custom_action_mapping(self, argument, expected):
def test_custom_action_mapping(self, action_id, argument, expected):
result = _get_action_callback_mapping(
action_id="custom_action",
argument=argument,
)
assert result == expected

@pytest.mark.parametrize(
"argument, expected",
[
("inputs", {}),
("outputs", {}),
("components", []),
("unknown-argument", {}),
],
)
def test_custom_action_with_predefined_name_mapping(self, argument, expected):
result = _get_action_callback_mapping(
action_id="export_data_custom_action",
action_id=action_id,
argument=argument,
)
assert result == expected

0 comments on commit b49a42b

Please sign in to comment.