-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa055a5
commit 5548fa2
Showing
6 changed files
with
143 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
vizro-core/tests/unit/vizro/models/_components/test_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
"""Unit tests for vizro.models.Table.""" | ||
import json | ||
|
||
import pytest | ||
from dash import dcc | ||
from pydantic import ValidationError | ||
|
||
import vizro.models as vm | ||
import vizro.plotly.express as px | ||
from vizro.managers import data_manager | ||
from vizro.models._action._action import Action | ||
from vizro.tables import dash_data_table | ||
import plotly | ||
from dash import dash_table, html | ||
|
||
import pandas as pd | ||
|
||
|
||
@pytest.fixture | ||
def standard_dash_table(): | ||
return dash_data_table(data_frame=px.data.gapminder()) | ||
|
||
@pytest.fixture | ||
def dash_table_with_arguments(): | ||
return dash_data_table(data_frame=px.data.gapminder(), style_header={"border": "1px solid green"}) | ||
|
||
@pytest.fixture | ||
def dash_table_with_str_dataframe(): | ||
return dash_data_table(data_frame="gapminder") | ||
|
||
@pytest.fixture | ||
def expected_table(): | ||
return html.Div( | ||
dash_table.DataTable(pd.DataFrame().to_dict("records"), []), id="text_table" | ||
) | ||
|
||
class TestDunderMethodsTable: | ||
def test_create_graph_mandatory_only(self, standard_dash_table): | ||
table = vm.Table(figure=standard_dash_table) | ||
|
||
assert hasattr(table, "id") | ||
assert table.type == "table" | ||
assert table.figure == standard_dash_table | ||
assert table.actions == [] | ||
|
||
@pytest.mark.parametrize("id", ["id_1", "id_2"]) | ||
def test_create_table_mandatory_and_optional(self, standard_dash_table, id): | ||
table = vm.Table( | ||
figure=standard_dash_table, | ||
id=id, | ||
actions=[], | ||
) | ||
|
||
assert table.id == id | ||
assert table.type == "table" | ||
assert table.figure == standard_dash_table | ||
|
||
def test_mandatory_figure_missing(self): | ||
with pytest.raises(ValidationError, match="field required"): | ||
vm.Table() | ||
|
||
def test_failed_graph_with_no_captured_callable(self, standard_go_chart): | ||
with pytest.raises(ValidationError, match="must provide a valid CapturedCallable object"): | ||
vm.Table( | ||
figure=standard_go_chart, | ||
) | ||
|
||
@pytest.mark.xfail(reason="This test is failing as we are not yet detecting different types of captured callables") | ||
def test_failed_graph_with_no_captured_callable(self, standard_px_chart): | ||
with pytest.raises(ValidationError, match="must provide a valid table function vm.Table"): | ||
vm.Table( | ||
figure=standard_px_chart, | ||
) | ||
|
||
def test_getitem_known_args(self, dash_table_with_arguments): | ||
table = vm.Table(figure=dash_table_with_arguments) | ||
assert table["style_header"] == {"border": "1px solid green"} | ||
assert table["type"] == "table" | ||
|
||
def test_getitem_unknown_args(self, standard_dash_table): | ||
table = vm.Table(figure=standard_dash_table) | ||
with pytest.raises(KeyError): | ||
table["unknown_args"] | ||
|
||
# @pytest.mark.parametrize("title, expected", [(None, 24), ("Test", None)]) | ||
# def test_title_margin_adjustment(self, gapminder, title, expected): | ||
# figure = vm.Graph(figure=px.bar(data_frame=gapminder, x="year", y="pop", title=title)).__call__() | ||
|
||
# assert figure.layout.margin.t == expected | ||
# assert figure.layout.template.layout.margin.t == 64 | ||
# assert figure.layout.template.layout.margin.l == 80 | ||
# assert figure.layout.template.layout.margin.b == 64 | ||
# assert figure.layout.template.layout.margin.r == 12 | ||
|
||
def test_set_action_via_validator(self, standard_dash_table, test_action_function): | ||
table = vm.Table(figure=standard_dash_table, actions=[Action(function=test_action_function)]) | ||
actions_chain = table.actions[0] | ||
assert actions_chain.trigger.component_property == "active_cell" | ||
|
||
|
||
class TestProcessTableDataFrame: | ||
def test_process_figure_data_frame_str_df(self, dash_table_with_str_dataframe, gapminder): | ||
data_manager["gapminder"] = gapminder | ||
table_with_str_df = vm.Table( | ||
id="table", | ||
figure=dash_table_with_str_dataframe, | ||
) | ||
assert data_manager._get_component_data("table").equals(gapminder) | ||
assert table_with_str_df["data_frame"] == "gapminder" | ||
|
||
def test_process_figure_data_frame_df(self, standard_dash_table, gapminder): | ||
table_with_str_df = vm.Table( | ||
id="table", | ||
figure=standard_dash_table, | ||
) | ||
assert data_manager._get_component_data("table").equals(gapminder) | ||
with pytest.raises(KeyError, match="'data_frame'"): | ||
table_with_str_df.figure["data_frame"] | ||
|
||
|
||
|
||
class TestBuildTable: | ||
def test_graph_build(self, standard_dash_table, expected_table): | ||
table = vm.Table( | ||
id="text_table", | ||
figure=standard_dash_table, | ||
) | ||
|
||
result = json.loads(json.dumps(table.build(), cls=plotly.utils.PlotlyJSONEncoder)) | ||
expected = json.loads(json.dumps(expected_table, cls=plotly.utils.PlotlyJSONEncoder)) | ||
assert result == expected |