From aa6eaa2a4a3c756ea29a448d0340fd61f074e79f Mon Sep 17 00:00:00 2001 From: nadijagraca <108531476+nadijagraca@users.noreply.github.com> Date: Mon, 29 Jan 2024 09:51:15 +0100 Subject: [PATCH] Apply `asserts_component_equal` to unit tests for components (#283) --- ...aca_implementing_assets_component_equal.md | 48 +++++++++ .../models/_components/form/test_checklist.py | 46 ++++---- .../models/_components/form/test_dropdown.py | 101 ++++++++---------- .../_components/form/test_radioitems.py | 47 ++++---- .../_components/form/test_range_slider.py | 19 +--- .../models/_components/form/test_slider.py | 11 +- .../vizro/models/_components/test_card.py | 37 +++---- .../vizro/models/_components/test_graph.py | 59 +++++----- .../vizro/models/_components/test_table.py | 87 ++++++--------- 9 files changed, 217 insertions(+), 238 deletions(-) create mode 100644 vizro-core/changelog.d/20240124_104847_nadija_ratkusic_graca_implementing_assets_component_equal.md diff --git a/vizro-core/changelog.d/20240124_104847_nadija_ratkusic_graca_implementing_assets_component_equal.md b/vizro-core/changelog.d/20240124_104847_nadija_ratkusic_graca_implementing_assets_component_equal.md new file mode 100644 index 000000000..f1f65e73c --- /dev/null +++ b/vizro-core/changelog.d/20240124_104847_nadija_ratkusic_graca_implementing_assets_component_equal.md @@ -0,0 +1,48 @@ + + + + + + + + + diff --git a/vizro-core/tests/unit/vizro/models/_components/form/test_checklist.py b/vizro-core/tests/unit/vizro/models/_components/form/test_checklist.py index 94f744e9c..30d47a737 100755 --- a/vizro-core/tests/unit/vizro/models/_components/form/test_checklist.py +++ b/vizro-core/tests/unit/vizro/models/_components/form/test_checklist.py @@ -1,8 +1,7 @@ """Unit tests for vizro.models.Checklist.""" -import json -import plotly import pytest +from asserts import assert_component_equal from dash import dcc, html try: @@ -14,25 +13,6 @@ from vizro.models._components.form import Checklist -@pytest.fixture() -def expected_checklist(): - return html.Div( - [ - html.P("Title"), - dcc.Checklist( - id="checklist_id", - options=["ALL", "A", "B", "C"], - value=["ALL"], - className="selector_body_checklist", - persistence=True, - persistence_type="session", - ), - ], - className="selector_container", - id="checklist_id_outer", - ) - - class TestChecklistInstantiation: """Tests model instantiation.""" @@ -148,9 +128,21 @@ def test_set_action_via_validator(self, identity_action_function): class TestChecklistBuild: """Tests model build method.""" - def test_checklist_build(self, expected_checklist): - checklist = Checklist(options=["A", "B", "C"], id="checklist_id", title="Title").build() - - result = json.loads(json.dumps(checklist, cls=plotly.utils.PlotlyJSONEncoder)) - expected = json.loads(json.dumps(expected_checklist, cls=plotly.utils.PlotlyJSONEncoder)) - assert result == expected + def test_checklist_build(self): + checklist = Checklist(id="checklist_id", options=["A", "B", "C"], title="Title").build() + expected_checklist = html.Div( + [ + html.P("Title"), + dcc.Checklist( + id="checklist_id", + options=["ALL", "A", "B", "C"], + value=["ALL"], + className="selector_body_checklist", + persistence=True, + persistence_type="session", + ), + ], + className="selector_container", + id="checklist_id_outer", + ) + assert_component_equal(checklist, expected_checklist) diff --git a/vizro-core/tests/unit/vizro/models/_components/form/test_dropdown.py b/vizro-core/tests/unit/vizro/models/_components/form/test_dropdown.py index e9f630e46..be05fa380 100755 --- a/vizro-core/tests/unit/vizro/models/_components/form/test_dropdown.py +++ b/vizro-core/tests/unit/vizro/models/_components/form/test_dropdown.py @@ -1,8 +1,7 @@ """Unit tests for vizro.models.Dropdown.""" -import json -import plotly import pytest +from asserts import assert_component_equal from dash import dcc, html try: @@ -14,46 +13,6 @@ from vizro.models._components.form import Dropdown -@pytest.fixture() -def expected_dropdown_with_all(): - return html.Div( - [ - html.P("Title"), - dcc.Dropdown( - id="dropdown_id", - options=["ALL", "A", "B", "C"], - value="ALL", - multi=True, - persistence=True, - persistence_type="session", - className="selector_body_dropdown", - ), - ], - className="selector_dropdown_container", - id="dropdown_id_outer", - ) - - -@pytest.fixture() -def expected_dropdown_without_all(): - return html.Div( - [ - html.P("Title"), - dcc.Dropdown( - id="dropdown_id", - options=["A", "B", "C"], - value="A", - multi=False, - persistence=True, - persistence_type="session", - className="selector_body_dropdown", - ), - ], - className="selector_dropdown_container", - id="dropdown_id_outer", - ) - - class TestDropdownInstantiation: """Tests model instantiation.""" @@ -188,16 +147,48 @@ def test_set_action_via_validator(self, identity_action_function): class TestDropdownBuild: """Tests model build method.""" - def test_dropdown_with_all_option(self, expected_dropdown_with_all): - dropdown = Dropdown(options=["A", "B", "C"], id="dropdown_id", title="Title").build() - - result = json.loads(json.dumps(dropdown, cls=plotly.utils.PlotlyJSONEncoder)) - expected = json.loads(json.dumps(expected_dropdown_with_all, cls=plotly.utils.PlotlyJSONEncoder)) - assert result == expected - - def test_dropdown_without_all_option(self, expected_dropdown_without_all): - dropdown = Dropdown(options=["A", "B", "C"], multi=False, id="dropdown_id", title="Title").build() - - result = json.loads(json.dumps(dropdown, cls=plotly.utils.PlotlyJSONEncoder)) - expected = json.loads(json.dumps(expected_dropdown_without_all, cls=plotly.utils.PlotlyJSONEncoder)) - assert result == expected + def test_dropdown_with_all_option(self): + dropdown = Dropdown( + options=["A", "B", "C"], + title="Title", + id="dropdown_id", + ).build() + expected_dropdown = html.Div( + [ + html.P("Title"), + dcc.Dropdown( + id="dropdown_id", + options=["ALL", "A", "B", "C"], + value="ALL", + multi=True, + persistence=True, + persistence_type="session", + className="selector_body_dropdown", + ), + ], + className="selector_dropdown_container", + id="dropdown_id_outer", + ) + + assert_component_equal(dropdown, expected_dropdown) + + def test_dropdown_without_all_option(self): + dropdown = Dropdown(id="dropdown_id", options=["A", "B", "C"], multi=False, title="Title").build() + expected_dropdown = html.Div( + [ + html.P("Title"), + dcc.Dropdown( + id="dropdown_id", + options=["A", "B", "C"], + value="A", + multi=False, + persistence=True, + persistence_type="session", + className="selector_body_dropdown", + ), + ], + className="selector_dropdown_container", + id="dropdown_id_outer", + ) + + assert_component_equal(dropdown, expected_dropdown) diff --git a/vizro-core/tests/unit/vizro/models/_components/form/test_radioitems.py b/vizro-core/tests/unit/vizro/models/_components/form/test_radioitems.py index f87eb833e..6ee3f6a1b 100755 --- a/vizro-core/tests/unit/vizro/models/_components/form/test_radioitems.py +++ b/vizro-core/tests/unit/vizro/models/_components/form/test_radioitems.py @@ -1,8 +1,7 @@ """Unit tests for vizro.models.RadioItems.""" -import json -import plotly import pytest +from asserts import assert_component_equal from dash import dcc, html try: @@ -14,25 +13,6 @@ from vizro.models._components.form import RadioItems -@pytest.fixture() -def expected_radio_items(): - return html.Div( - [ - html.P("Title"), - dcc.RadioItems( - id="radio_items_id", - options=["A", "B", "C"], - value="A", - className="selector_body_radio_items", - persistence=True, - persistence_type="session", - ), - ], - className="selector_container", - id="radio_items_id_outer", - ) - - class TestRadioItemsInstantiation: """Tests model instantiation.""" @@ -148,9 +128,22 @@ def test_set_action_via_validator(self, identity_action_function): class TestRadioItemsBuild: """Tests model build method.""" - def test_radio_items_build(self, expected_radio_items): - radio_items = RadioItems(options=["A", "B", "C"], id="radio_items_id", title="Title").build() - - result = json.loads(json.dumps(radio_items, cls=plotly.utils.PlotlyJSONEncoder)) - expected = json.loads(json.dumps(expected_radio_items, cls=plotly.utils.PlotlyJSONEncoder)) - assert result == expected + def test_radio_items_build(self): + radio_items = RadioItems(id="radio_items_id", options=["A", "B", "C"], title="Title").build() + expected_radio_items = html.Div( + [ + html.P("Title"), + dcc.RadioItems( + id="radio_items_id", + options=["A", "B", "C"], + value="A", + className="selector_body_radio_items", + persistence=True, + persistence_type="session", + ), + ], + className="selector_container", + id="radio_items_id_outer", + ) + + assert_component_equal(radio_items, expected_radio_items) diff --git a/vizro-core/tests/unit/vizro/models/_components/form/test_range_slider.py b/vizro-core/tests/unit/vizro/models/_components/form/test_range_slider.py index 924203766..ddc3677b6 100644 --- a/vizro-core/tests/unit/vizro/models/_components/form/test_range_slider.py +++ b/vizro-core/tests/unit/vizro/models/_components/form/test_range_slider.py @@ -1,8 +1,7 @@ """Unit tests for hyphen.models.slider.""" -import json -import plotly import pytest +from asserts import assert_component_equal from dash import dcc, html try: @@ -311,13 +310,9 @@ class TestRangeSliderBuild: """Tests model build method.""" def test_range_slider_build_default(self, expected_range_slider_default): - range_slider = vm.RangeSlider(id="range_slider") - component = range_slider.build() + range_slider = vm.RangeSlider(id="range_slider").build() - result = json.loads(json.dumps(component, cls=plotly.utils.PlotlyJSONEncoder)) - expected = json.loads(json.dumps(expected_range_slider_default, cls=plotly.utils.PlotlyJSONEncoder)) - - assert result == expected + assert_component_equal(range_slider, expected_range_slider_default) def test_range_slider_build_with_optional(self, expected_range_slider_with_optional): range_slider = vm.RangeSlider( @@ -328,10 +323,6 @@ def test_range_slider_build_with_optional(self, expected_range_slider_with_optio id="range_slider_with_all", title="Title", marks={1: "1", 5: "5", 10: "10"}, - ) - component = range_slider.build() - - result = json.loads(json.dumps(component, cls=plotly.utils.PlotlyJSONEncoder)) - expected = json.loads(json.dumps(expected_range_slider_with_optional, cls=plotly.utils.PlotlyJSONEncoder)) + ).build() - assert result == expected + assert_component_equal(range_slider, expected_range_slider_with_optional) diff --git a/vizro-core/tests/unit/vizro/models/_components/form/test_slider.py b/vizro-core/tests/unit/vizro/models/_components/form/test_slider.py index e4c81f5d8..e55245d85 100755 --- a/vizro-core/tests/unit/vizro/models/_components/form/test_slider.py +++ b/vizro-core/tests/unit/vizro/models/_components/form/test_slider.py @@ -1,8 +1,7 @@ """Unit tests for hyphen.models.slider.""" -import json -import plotly import pytest +from asserts import assert_component_equal from dash import dcc, html try: @@ -213,10 +212,6 @@ def test_set_action_via_validator(self, identity_action_function): class TestBuildMethod: def test_slider_build(self, expected_slider): - slider = vm.Slider(min=0, max=10, step=1, value=5, id="slider_id", title="Test title") - slider = slider.build() + slider = vm.Slider(min=0, max=10, step=1, value=5, id="slider_id", title="Test title").build() - result = json.loads(json.dumps(slider, cls=plotly.utils.PlotlyJSONEncoder)) - expected = json.loads(json.dumps(expected_slider, cls=plotly.utils.PlotlyJSONEncoder)) - - assert result == expected + assert_component_equal(slider, expected_slider) diff --git a/vizro-core/tests/unit/vizro/models/_components/test_card.py b/vizro-core/tests/unit/vizro/models/_components/test_card.py index 1d9b4821a..ef31a3e47 100755 --- a/vizro-core/tests/unit/vizro/models/_components/test_card.py +++ b/vizro-core/tests/unit/vizro/models/_components/test_card.py @@ -1,9 +1,8 @@ """Unit tests for vizro.models.Card.""" -import json import dash_bootstrap_components as dbc -import plotly import pytest +from asserts import assert_component_equal from dash import dcc, html try: @@ -14,20 +13,6 @@ import vizro.models as vm -@pytest.fixture -def expected_card(): - text = dcc.Markdown("Hello", className="card_text", dangerously_allow_html=False, id="card_id") - button = html.Div( - dbc.Button( - href="https://www.google.com", - className="card_button", - ), - className="button_container", - ) - - return html.Div([text, button], className="nav_card_container", id="card_id_outer") - - class TestCardInstantiation: """Tests model instantiation.""" @@ -60,12 +45,22 @@ def test_none_as_text(self): class TestBuildMethod: """Tests build method.""" - def test_card_build(self, expected_card): - card = vm.Card(text="Hello", id="card_id", href="https://www.google.com") + def test_card_build(self): + card = vm.Card(id="card_id", text="Hello", href="https://www.google.com") card = card.build() - result = json.loads(json.dumps(card, cls=plotly.utils.PlotlyJSONEncoder)) - expected = json.loads(json.dumps(expected_card, cls=plotly.utils.PlotlyJSONEncoder)) - assert result == expected + + expected_card = html.Div( + [ + dcc.Markdown("Hello", className="card_text", dangerously_allow_html=False, id="card_id"), + html.Div( + dbc.Button(href="https://www.google.com", className="card_button"), className="button_container" + ), + ], + className="nav_card_container", + id="card_id_outer", + ) + + assert_component_equal(card, expected_card) @pytest.mark.parametrize( "test_text, expected", diff --git a/vizro-core/tests/unit/vizro/models/_components/test_graph.py b/vizro-core/tests/unit/vizro/models/_components/test_graph.py index 509309b1e..438bc79f6 100644 --- a/vizro-core/tests/unit/vizro/models/_components/test_graph.py +++ b/vizro-core/tests/unit/vizro/models/_components/test_graph.py @@ -1,9 +1,8 @@ """Unit tests for vizro.models.Graph.""" -import json -import plotly import plotly.graph_objects as go import pytest +from asserts import assert_component_equal from dash import dcc from dash._callback_context import context_value from dash._utils import AttributeDict @@ -33,31 +32,6 @@ def standard_px_chart_with_str_dataframe(): ) -@pytest.fixture -def expected_graph(): - return dcc.Loading( - dcc.Graph( - id="text_graph", - figure=go.Figure( - layout={ - "paper_bgcolor": "rgba(0,0,0,0)", - "plot_bgcolor": "rgba(0,0,0,0)", - "xaxis": {"visible": False}, - "yaxis": {"visible": False}, - } - ), - config={ - "autosizable": True, - "frameMargins": 0, - "responsive": True, - }, - className="chart_container", - ), - color="grey", - parent_className="loading-container", - ) - - class TestDunderMethodsGraph: def test_create_graph_mandatory_only(self, standard_px_chart): graph = vm.Graph(figure=standard_px_chart) @@ -148,9 +122,28 @@ def test_process_figure_data_frame_df(self, standard_px_chart, gapminder): class TestBuild: - def test_graph_build(self, standard_px_chart, expected_graph): - graph = vm.Graph(id="text_graph", figure=standard_px_chart) - - result = json.loads(json.dumps(graph.build(), cls=plotly.utils.PlotlyJSONEncoder)) - expected = json.loads(json.dumps(expected_graph, cls=plotly.utils.PlotlyJSONEncoder)) - assert result == expected + def test_graph_build(self, standard_px_chart): + graph = vm.Graph(id="text_graph", figure=standard_px_chart).build() + + expected_graph = dcc.Loading( + dcc.Graph( + id="text_graph", + figure=go.Figure( + layout={ + "paper_bgcolor": "rgba(0,0,0,0)", + "plot_bgcolor": "rgba(0,0,0,0)", + "xaxis": {"visible": False}, + "yaxis": {"visible": False}, + } + ), + config={ + "autosizable": True, + "frameMargins": 0, + "responsive": True, + }, + className="chart_container", + ), + color="grey", + parent_className="loading-container", + ) + assert_component_equal(graph, expected_graph) diff --git a/vizro-core/tests/unit/vizro/models/_components/test_table.py b/vizro-core/tests/unit/vizro/models/_components/test_table.py index 56142f0b8..ec390c06f 100644 --- a/vizro-core/tests/unit/vizro/models/_components/test_table.py +++ b/vizro-core/tests/unit/vizro/models/_components/test_table.py @@ -1,8 +1,7 @@ """Unit tests for vizro.models.Table.""" -import json -import plotly import pytest +from asserts import assert_component_equal from dash import dash_table, dcc, html try: @@ -28,38 +27,6 @@ def dash_table_with_str_dataframe(): return dash_data_table(data_frame="gapminder") -@pytest.fixture -def expected_table(): - return dcc.Loading( - html.Div( - [ - None, - html.Div(dash_table.DataTable(), id="text_table"), - ], - className="table-container", - id="text_table_outer", - ), - color="grey", - parent_className="loading-container", - ) - - -@pytest.fixture -def expected_table_with_id(): - return dcc.Loading( - html.Div( - [ - None, - html.Div(dash_table.DataTable(id="underlying_table_id"), id="text_table"), - ], - className="table-container", - id="text_table_outer", - ), - color="grey", - parent_className="loading-container", - ) - - @pytest.fixture def filter_interaction_action(): return vm.Action(function=filter_interaction()) @@ -170,27 +137,41 @@ def test_pre_build_actions_underlying_table_id(self, dash_data_table_with_id, fi class TestBuildTable: - def test_table_build_mandatory_only(self, standard_dash_table, expected_table): - table = vm.Table( - id="text_table", - figure=standard_dash_table, - ) - + def test_table_build_mandatory_only(self, standard_dash_table): + table = vm.Table(id="text_table", figure=standard_dash_table) table.pre_build() - - 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 - - def test_table_build_with_id(self, dash_data_table_with_id, filter_interaction_action, expected_table_with_id): - table = vm.Table( - id="text_table", - figure=dash_data_table_with_id, - actions=[filter_interaction_action], + table = table.build() + expected_table = dcc.Loading( + html.Div( + [ + None, + html.Div(dash_table.DataTable(), id="text_table"), + ], + className="table-container", + id="text_table_outer", + ), + color="grey", + parent_className="loading-container", ) + assert_component_equal(table, expected_table) + + def test_table_build_with_underlying_id(self, dash_data_table_with_id, filter_interaction_action): + table = vm.Table(id="text_table", figure=dash_data_table_with_id, actions=[filter_interaction_action]) table.pre_build() + table = table.build() + + expected_table = dcc.Loading( + html.Div( + [ + None, + html.Div(dash_table.DataTable(id="underlying_table_id"), id="text_table"), + ], + className="table-container", + id="text_table_outer", + ), + color="grey", + parent_className="loading-container", + ) - result = json.loads(json.dumps(table.build(), cls=plotly.utils.PlotlyJSONEncoder)) - expected = json.loads(json.dumps(expected_table_with_id, cls=plotly.utils.PlotlyJSONEncoder)) - assert result == expected + assert_component_equal(table, expected_table)