From 0364e5bdb93a0f30c76a285e2160177ab7795cec Mon Sep 17 00:00:00 2001 From: Antony Milne Date: Wed, 6 Dec 2023 10:36:39 +0000 Subject: [PATCH 01/14] Update assert_component_equal --- vizro-core/tests/tests_utils/asserts.py | 14 ++++++------- .../models/_navigation/test_accordion.py | 6 ++++-- .../vizro/models/_navigation/test_nav_bar.py | 12 +++++++---- .../models/_navigation/test_navigation.py | 20 +++++++++++++------ 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/vizro-core/tests/tests_utils/asserts.py b/vizro-core/tests/tests_utils/asserts.py index 1a32c93dd..a7ada0655 100644 --- a/vizro-core/tests/tests_utils/asserts.py +++ b/vizro-core/tests/tests_utils/asserts.py @@ -20,11 +20,11 @@ def component_to_dict(component: dash.development.base_component.Component) -> d return json.loads(json.dumps(component, cls=plotly.utils.PlotlyJSONEncoder)) -# TODO: implement some sort of depth limit to comparison so can use in high level tests, roll out more widely across -# tests -def assert_component_equal(left, right, keys_to_strip=None): - # Note we check for None explicitly because {} is a valid value for keys_to_strip. - keys_to_strip = keys_to_strip if keys_to_strip is not None else {"id", "class_name", "className"} - left = strip_keys(component_to_dict(left), keys_to_strip) - right = strip_keys(component_to_dict(right), keys_to_strip) +def assert_component_equal(left, right, extra_strip_keys=None, keep_keys=None): + extra_strip_keys = extra_strip_keys or set() + keep_keys = keep_keys or set() + total_strip_keys = {"id", "class_name", "className"} | extra_strip_keys - keep_keys + + left = strip_keys(component_to_dict(left), total_strip_keys) + right = strip_keys(component_to_dict(right), total_strip_keys) assert left == right diff --git a/vizro-core/tests/unit/vizro/models/_navigation/test_accordion.py b/vizro-core/tests/unit/vizro/models/_navigation/test_accordion.py index 894275c73..f9b7dc537 100644 --- a/vizro-core/tests/unit/vizro/models/_navigation/test_accordion.py +++ b/vizro-core/tests/unit/vizro/models/_navigation/test_accordion.py @@ -109,9 +109,11 @@ class TestAccordionBuild: @pytest.mark.parametrize("pages, expected", test_cases) def test_accordion(self, pages, expected): accordion = vm.Accordion(id="accordion", pages=pages).build(active_page_id="Page 1") - assert_component_equal(accordion, html.Div(id="nav_panel_outer"), keys_to_strip={"children", "className"}) + assert_component_equal( + accordion, html.Div(id="nav_panel_outer"), extra_strip_keys={"children"}, keep_keys={"id"} + ) assert_component_equal(accordion["accordion"], expected) def test_accordion_one_page(self): accordion = vm.Accordion(pages={"Group": ["Page 1"]}).build(active_page_id="Page 1") - assert_component_equal(accordion, html.Div(hidden=True, id="nav_panel_outer"), keys_to_strip={}) + assert_component_equal(accordion, html.Div(hidden=True, id="nav_panel_outer"), keep_keys={"id"}) diff --git a/vizro-core/tests/unit/vizro/models/_navigation/test_nav_bar.py b/vizro-core/tests/unit/vizro/models/_navigation/test_nav_bar.py index ff633a6a6..a99c3596e 100644 --- a/vizro-core/tests/unit/vizro/models/_navigation/test_nav_bar.py +++ b/vizro-core/tests/unit/vizro/models/_navigation/test_nav_bar.py @@ -80,7 +80,10 @@ def test_nav_bar_active_pages_as_dict(self, pages_as_dict): expected_button = html.Div([dbc.Button(children=[html.Span(children="filter_1")], active=True, href="/")]) assert_component_equal(built_nav_bar["nav_bar_outer"], expected_button) assert_component_equal( - built_nav_bar["nav_panel_outer"], html.Div(id="nav_panel_outer"), keys_to_strip={"children", "className"} + built_nav_bar["nav_panel_outer"], + html.Div(id="nav_panel_outer"), + extra_strip_keys={"children"}, + keep_keys={"id"}, ) assert all(isinstance(child, dbc.Accordion) for child in built_nav_bar["nav_panel_outer"].children) @@ -98,7 +101,8 @@ def test_nav_bar_active_pages_as_list(self, pages_as_list): assert_component_equal( built_nav_bar["nav_panel_outer"], html.Div(id="nav_panel_outer", hidden=True), - keys_to_strip={"children", "className"}, + extra_strip_keys={"children"}, + keep_keys={"id"}, ) def test_nav_bar_not_active_pages_as_dict(self, pages_as_dict): @@ -108,7 +112,7 @@ def test_nav_bar_not_active_pages_as_dict(self, pages_as_dict): expected_button = html.Div([dbc.Button(children=[html.Span(children="filter_1")], active=False, href="/")]) assert_component_equal(built_nav_bar["nav_bar_outer"], expected_button) assert_component_equal( - built_nav_bar["nav_panel_outer"], html.Div(hidden=True, id="nav_panel_outer"), keys_to_strip={} + built_nav_bar["nav_panel_outer"], html.Div(hidden=True, id="nav_panel_outer"), keep_keys={"id"} ) def test_nav_bar_not_active_pages_as_list(self, pages_as_list): @@ -125,5 +129,5 @@ def test_nav_bar_not_active_pages_as_list(self, pages_as_list): assert_component_equal( built_nav_bar["nav_panel_outer"], html.Div(id="nav_panel_outer", hidden=True), - keys_to_strip={}, + keep_keys={"id"}, ) diff --git a/vizro-core/tests/unit/vizro/models/_navigation/test_navigation.py b/vizro-core/tests/unit/vizro/models/_navigation/test_navigation.py index 73abd6fdc..64da6f129 100644 --- a/vizro-core/tests/unit/vizro/models/_navigation/test_navigation.py +++ b/vizro-core/tests/unit/vizro/models/_navigation/test_navigation.py @@ -83,10 +83,14 @@ def test_default_nav_selector(self, pages, request): assert_component_equal( built_navigation["nav_bar_outer"], html.Div(className="hidden", id="nav_bar_outer"), - keys_to_strip={"children"}, + extra_strip_keys={"children"}, + keep_keys={"id"}, ) assert_component_equal( - built_navigation["nav_panel_outer"], html.Div(id="nav_panel_outer"), keys_to_strip={"children", "className"} + built_navigation["nav_panel_outer"], + html.Div(id="nav_panel_outer"), + extra_strip_keys={"children"}, + keep_keys={"id"}, ) assert all(isinstance(child, dbc.Accordion) for child in built_navigation["nav_panel_outer"].children) @@ -97,12 +101,14 @@ def test_non_default_nav_selector_pags_as_dict(self, pages_as_dict): assert_component_equal( built_navigation["nav_bar_outer"], html.Div(id="nav_bar_outer", className="nav-bar"), - keys_to_strip={"children"}, + extra_strip_keys={"children"}, + keep_keys={"id"}, ) assert_component_equal( built_navigation["nav_panel_outer"], html.Div(id="nav_panel_outer"), - keys_to_strip={"children", "className"}, + extra_strip_keys={"children"}, + keep_keys={"id"}, ) assert all(isinstance(child, dbc.Accordion) for child in built_navigation["nav_panel_outer"].children) @@ -113,10 +119,12 @@ def test_non_default_nav_selector_pages_as_list(self, pages_as_list): assert_component_equal( built_navigation["nav_bar_outer"], html.Div(id="nav_bar_outer", className="nav-bar"), - keys_to_strip={"children"}, + extra_strip_keys={"children"}, + keep_keys={"id"}, ) assert_component_equal( built_navigation["nav_panel_outer"], html.Div(id="nav_panel_outer", hidden=True), - keys_to_strip={"children"}, + extra_strip_keys={"children"}, + keep_keys={"id"}, ) From a81e79cab64cfdd06d0df8662eaa587577755a69 Mon Sep 17 00:00:00 2001 From: Antony Milne Date: Wed, 6 Dec 2023 11:17:23 +0000 Subject: [PATCH 02/14] Make assert_component_equal strict by default --- .../src/vizro/models/_components/button.py | 8 +------ vizro-core/tests/tests_utils/asserts.py | 10 ++++---- .../models/_navigation/test_accordion.py | 6 ++--- .../vizro/models/_navigation/test_nav_bar.py | 19 +++++++-------- .../vizro/models/_navigation/test_nav_item.py | 16 +++++++++++-- .../models/_navigation/test_navigation.py | 24 +++++++------------ 6 files changed, 38 insertions(+), 45 deletions(-) diff --git a/vizro-core/src/vizro/models/_components/button.py b/vizro-core/src/vizro/models/_components/button.py index 140c7e7e7..233e6b1d9 100644 --- a/vizro-core/src/vizro/models/_components/button.py +++ b/vizro-core/src/vizro/models/_components/button.py @@ -28,13 +28,7 @@ class Button(VizroBaseModel): @_log_call def build(self): return html.Div( - [ - dbc.Button( - id=self.id, - children=self.text, - className="button_primary", - ), - ], + dbc.Button(id=self.id, children=self.text, className="button_primary"), className="button_container", id=f"{self.id}_outer", ) diff --git a/vizro-core/tests/tests_utils/asserts.py b/vizro-core/tests/tests_utils/asserts.py index a7ada0655..ddd198e5e 100644 --- a/vizro-core/tests/tests_utils/asserts.py +++ b/vizro-core/tests/tests_utils/asserts.py @@ -20,11 +20,9 @@ def component_to_dict(component: dash.development.base_component.Component) -> d return json.loads(json.dumps(component, cls=plotly.utils.PlotlyJSONEncoder)) -def assert_component_equal(left, right, extra_strip_keys=None, keep_keys=None): - extra_strip_keys = extra_strip_keys or set() - keep_keys = keep_keys or set() - total_strip_keys = {"id", "class_name", "className"} | extra_strip_keys - keep_keys +def assert_component_equal(left, right, keys_to_strip=None): + keys_to_strip = keys_to_strip or {} - left = strip_keys(component_to_dict(left), total_strip_keys) - right = strip_keys(component_to_dict(right), total_strip_keys) + left = strip_keys(component_to_dict(left), keys_to_strip) + right = strip_keys(component_to_dict(right), keys_to_strip) assert left == right diff --git a/vizro-core/tests/unit/vizro/models/_navigation/test_accordion.py b/vizro-core/tests/unit/vizro/models/_navigation/test_accordion.py index f9b7dc537..2762c1dc0 100644 --- a/vizro-core/tests/unit/vizro/models/_navigation/test_accordion.py +++ b/vizro-core/tests/unit/vizro/models/_navigation/test_accordion.py @@ -110,10 +110,10 @@ class TestAccordionBuild: def test_accordion(self, pages, expected): accordion = vm.Accordion(id="accordion", pages=pages).build(active_page_id="Page 1") assert_component_equal( - accordion, html.Div(id="nav_panel_outer"), extra_strip_keys={"children"}, keep_keys={"id"} + accordion, html.Div(id="nav_panel_outer", className="nav_panel"), keys_to_strip={"children"} ) - assert_component_equal(accordion["accordion"], expected) + assert_component_equal(accordion["accordion"], expected, keys_to_strip={"id", "className", "class_name"}) def test_accordion_one_page(self): accordion = vm.Accordion(pages={"Group": ["Page 1"]}).build(active_page_id="Page 1") - assert_component_equal(accordion, html.Div(hidden=True, id="nav_panel_outer"), keep_keys={"id"}) + assert_component_equal(accordion, html.Div(hidden=True, id="nav_panel_outer")) diff --git a/vizro-core/tests/unit/vizro/models/_navigation/test_nav_bar.py b/vizro-core/tests/unit/vizro/models/_navigation/test_nav_bar.py index a99c3596e..40bedd0f3 100644 --- a/vizro-core/tests/unit/vizro/models/_navigation/test_nav_bar.py +++ b/vizro-core/tests/unit/vizro/models/_navigation/test_nav_bar.py @@ -78,12 +78,11 @@ def test_nav_bar_active_pages_as_dict(self, pages_as_dict): nav_bar.pre_build() built_nav_bar = nav_bar.build(active_page_id="Page 1") expected_button = html.Div([dbc.Button(children=[html.Span(children="filter_1")], active=True, href="/")]) - assert_component_equal(built_nav_bar["nav_bar_outer"], expected_button) + assert_component_equal(built_nav_bar["nav_bar_outer"], expected_button, keys_to_strip={"id", "className"}) assert_component_equal( built_nav_bar["nav_panel_outer"], - html.Div(id="nav_panel_outer"), - extra_strip_keys={"children"}, - keep_keys={"id"}, + html.Div(id="nav_panel_outer", className="nav_panel"), + keys_to_strip={"children"}, ) assert all(isinstance(child, dbc.Accordion) for child in built_nav_bar["nav_panel_outer"].children) @@ -97,12 +96,10 @@ def test_nav_bar_active_pages_as_list(self, pages_as_list): dbc.Button(children=[html.Span(children="filter_2")], active=False, href="/page-2"), ] ) - assert_component_equal(built_nav_bar["nav_bar_outer"], expected_buttons) + assert_component_equal(built_nav_bar["nav_bar_outer"], expected_buttons, keys_to_strip={"id", "className"}) assert_component_equal( built_nav_bar["nav_panel_outer"], html.Div(id="nav_panel_outer", hidden=True), - extra_strip_keys={"children"}, - keep_keys={"id"}, ) def test_nav_bar_not_active_pages_as_dict(self, pages_as_dict): @@ -110,9 +107,10 @@ def test_nav_bar_not_active_pages_as_dict(self, pages_as_dict): nav_bar.pre_build() built_nav_bar = nav_bar.build(active_page_id="Page 3") expected_button = html.Div([dbc.Button(children=[html.Span(children="filter_1")], active=False, href="/")]) - assert_component_equal(built_nav_bar["nav_bar_outer"], expected_button) + assert_component_equal(built_nav_bar["nav_bar_outer"], expected_button, keys_to_strip={"id", "className"}) assert_component_equal( - built_nav_bar["nav_panel_outer"], html.Div(hidden=True, id="nav_panel_outer"), keep_keys={"id"} + built_nav_bar["nav_panel_outer"], + html.Div(hidden=True, id="nav_panel_outer"), ) def test_nav_bar_not_active_pages_as_list(self, pages_as_list): @@ -125,9 +123,8 @@ def test_nav_bar_not_active_pages_as_list(self, pages_as_list): dbc.Button(children=[html.Span(children="filter_2")], active=False, href="/page-2"), ] ) - assert_component_equal(built_nav_bar["nav_bar_outer"], expected_buttons) + assert_component_equal(built_nav_bar["nav_bar_outer"], expected_buttons, keys_to_strip={"id", "className"}) assert_component_equal( built_nav_bar["nav_panel_outer"], html.Div(id="nav_panel_outer", hidden=True), - keep_keys={"id"}, ) diff --git a/vizro-core/tests/unit/vizro/models/_navigation/test_nav_item.py b/vizro-core/tests/unit/vizro/models/_navigation/test_nav_item.py index 71710ed2a..27013b986 100644 --- a/vizro-core/tests/unit/vizro/models/_navigation/test_nav_item.py +++ b/vizro-core/tests/unit/vizro/models/_navigation/test_nav_item.py @@ -73,7 +73,13 @@ def test_nav_link_active(self, pages, request): nav_link = vm.NavLink(id="nav_link", label="Label", icon="icon", pages=pages) nav_link.pre_build() built_nav_link = nav_link.build(active_page_id="Page 1") - expected_button = dbc.Button(id="nav_link", children=[html.Span("icon")], active=True, href="/") + expected_button = dbc.Button( + id="nav_link", + children=[html.Span("icon", className="material-symbols-outlined")], + active=True, + href="/", + className="icon-button", + ) assert_component_equal(built_nav_link["nav_link"], expected_button) assert all(isinstance(child, dbc.Accordion) for child in built_nav_link["nav_panel_outer"].children) @@ -82,6 +88,12 @@ def test_nav_link_not_active(self, pages, request): nav_link = vm.NavLink(id="nav_link", label="Label", icon="icon", pages=pages) nav_link.pre_build() built_nav_link = nav_link.build(active_page_id="Page 3") - expected_button = dbc.Button(id="nav_link", children=[html.Span("icon")], active=False, href="/") + expected_button = dbc.Button( + id="nav_link", + children=[html.Span("icon", className="material-symbols-outlined")], + active=False, + href="/", + className="icon-button", + ) assert_component_equal(built_nav_link["nav_link"], expected_button) assert "nav_panel_outer" not in built_nav_link diff --git a/vizro-core/tests/unit/vizro/models/_navigation/test_navigation.py b/vizro-core/tests/unit/vizro/models/_navigation/test_navigation.py index 64da6f129..b6b9df1bc 100644 --- a/vizro-core/tests/unit/vizro/models/_navigation/test_navigation.py +++ b/vizro-core/tests/unit/vizro/models/_navigation/test_navigation.py @@ -83,14 +83,12 @@ def test_default_nav_selector(self, pages, request): assert_component_equal( built_navigation["nav_bar_outer"], html.Div(className="hidden", id="nav_bar_outer"), - extra_strip_keys={"children"}, - keep_keys={"id"}, + keys_to_strip={"children"}, ) assert_component_equal( built_navigation["nav_panel_outer"], - html.Div(id="nav_panel_outer"), - extra_strip_keys={"children"}, - keep_keys={"id"}, + html.Div(id="nav_panel_outer", className="nav_panel"), + keys_to_strip={"children"}, ) assert all(isinstance(child, dbc.Accordion) for child in built_navigation["nav_panel_outer"].children) @@ -101,14 +99,12 @@ def test_non_default_nav_selector_pags_as_dict(self, pages_as_dict): assert_component_equal( built_navigation["nav_bar_outer"], html.Div(id="nav_bar_outer", className="nav-bar"), - extra_strip_keys={"children"}, - keep_keys={"id"}, + keys_to_strip={"children"}, ) assert_component_equal( built_navigation["nav_panel_outer"], - html.Div(id="nav_panel_outer"), - extra_strip_keys={"children"}, - keep_keys={"id"}, + html.Div(id="nav_panel_outer", className="nav_panel"), + keys_to_strip={"children"}, ) assert all(isinstance(child, dbc.Accordion) for child in built_navigation["nav_panel_outer"].children) @@ -119,12 +115,8 @@ def test_non_default_nav_selector_pages_as_list(self, pages_as_list): assert_component_equal( built_navigation["nav_bar_outer"], html.Div(id="nav_bar_outer", className="nav-bar"), - extra_strip_keys={"children"}, - keep_keys={"id"}, + keys_to_strip={"children"}, ) assert_component_equal( - built_navigation["nav_panel_outer"], - html.Div(id="nav_panel_outer", hidden=True), - extra_strip_keys={"children"}, - keep_keys={"id"}, + built_navigation["nav_panel_outer"], html.Div(id="nav_panel_outer", hidden=True), keys_to_strip={"children"} ) From 360e4bab3a30e2dffd6649448d4f7f09347b5511 Mon Sep 17 00:00:00 2001 From: Antony Milne Date: Wed, 6 Dec 2023 11:22:10 +0000 Subject: [PATCH 03/14] Do test_button.py example --- .../vizro/models/_components/test_button.py | 34 ++++++------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/vizro-core/tests/unit/vizro/models/_components/test_button.py b/vizro-core/tests/unit/vizro/models/_components/test_button.py index 6584ab85e..780e46445 100644 --- a/vizro-core/tests/unit/vizro/models/_components/test_button.py +++ b/vizro-core/tests/unit/vizro/models/_components/test_button.py @@ -1,30 +1,14 @@ """Unit tests for vizro.models.Button.""" -import json import dash_bootstrap_components as dbc -import plotly import pytest +from asserts import assert_component_equal from dash import html import vizro.models as vm from vizro.actions import export_data -@pytest.fixture -def expected_button(): - return html.Div( - [ - dbc.Button( - id="button_id", - children="Click me!", - className="button_primary", - ), - ], - className="button_container", - id="button_id_outer", - ) - - class TestButtonInstantiation: """Tests model instantiation and the validators run at that time.""" @@ -53,9 +37,13 @@ def test_set_action_via_validator(self): class TestBuildMethod: - def test_button_build(self, expected_button): - button = vm.Button(id="button_id", text="Click me!").build() - result = json.loads(json.dumps(button, cls=plotly.utils.PlotlyJSONEncoder)) - expected = json.loads(json.dumps(expected_button, cls=plotly.utils.PlotlyJSONEncoder)) - - assert result == expected + def test_button_build(self): + button = vm.Button(id="button_id", text="My text").build() + assert_component_equal( + button, + html.Div( + dbc.Button(id="button_id", children="My text", className="button_primary"), + className="button_container", + id="button_id_outer", + ), + ) From 9d7ed40c693d3b0ed03614b25966696fdc7f691f Mon Sep 17 00:00:00 2001 From: Antony Milne Date: Tue, 19 Dec 2023 13:27:30 +0000 Subject: [PATCH 04/14] Update use of assert_component_equal --- .../models/_navigation/test_accordion.py | 4 ++-- .../vizro/models/_navigation/test_nav_bar.py | 17 ++++++++-------- .../vizro/models/_navigation/test_nav_link.py | 20 +++++++++++++++++-- .../models/_navigation/test_navigation.py | 4 +--- .../tests/unit/vizro/models/test_dashboard.py | 2 +- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/vizro-core/tests/unit/vizro/models/_navigation/test_accordion.py b/vizro-core/tests/unit/vizro/models/_navigation/test_accordion.py index 45df42a89..829005266 100644 --- a/vizro-core/tests/unit/vizro/models/_navigation/test_accordion.py +++ b/vizro-core/tests/unit/vizro/models/_navigation/test_accordion.py @@ -55,7 +55,7 @@ def test_invalid_page(self, pages): class TestAccordionBuild: """Tests accordion build method.""" - common_args = {"always_open": True, "persistence": True, "persistence_type": "session"} + common_args = {"always_open": True, "persistence": True, "persistence_type": "session", "id": "accordion"} test_cases = [ ( @@ -116,7 +116,7 @@ def test_accordion(self, pages, expected): assert_component_equal( accordion, html.Div(id="nav_panel_outer", className="nav_panel"), keys_to_strip={"children"} ) - assert_component_equal(accordion["accordion"], expected, keys_to_strip={"id", "className", "class_name"}) + assert_component_equal(accordion["accordion"], expected, keys_to_strip={"class_name", "className"}) def test_accordion_one_page(self): accordion = vm.Accordion(pages={"Group": ["Page 1"]}).build(active_page_id="Page 1") diff --git a/vizro-core/tests/unit/vizro/models/_navigation/test_nav_bar.py b/vizro-core/tests/unit/vizro/models/_navigation/test_nav_bar.py index 0e1dbb48e..5cd7293b9 100644 --- a/vizro-core/tests/unit/vizro/models/_navigation/test_nav_bar.py +++ b/vizro-core/tests/unit/vizro/models/_navigation/test_nav_bar.py @@ -93,9 +93,11 @@ def test_nav_bar_active_pages_as_dict(self, pages_as_dict): ) ] ) - assert_component_equal(built_nav_bar["nav_bar_outer"], expected_button) + assert_component_equal(built_nav_bar["nav_bar_outer"], expected_button, keys_to_strip={"id", "className"}) assert_component_equal( - built_nav_bar["nav_panel_outer"], html.Div(id="nav_panel_outer"), keys_to_strip={"children", "className"} + built_nav_bar["nav_panel_outer"], + html.Div(id="nav_panel_outer", className="nav_panel"), + keys_to_strip={"children"}, ) assert all(isinstance(child, dbc.Accordion) for child in built_nav_bar["nav_panel_outer"].children) @@ -117,11 +119,10 @@ def test_nav_bar_active_pages_as_list(self, pages_as_list): ), ] ) - assert_component_equal(built_nav_bar["nav_bar_outer"], expected_buttons) + assert_component_equal(built_nav_bar["nav_bar_outer"], expected_buttons, keys_to_strip={"id", "className"}) assert_component_equal( built_nav_bar["nav_panel_outer"], html.Div(id="nav_panel_outer", hidden=True), - keys_to_strip={"children", "className"}, ) def test_nav_bar_not_active_pages_as_dict(self, pages_as_dict): @@ -137,10 +138,8 @@ def test_nav_bar_not_active_pages_as_dict(self, pages_as_dict): ) ] ) - assert_component_equal(built_nav_bar["nav_bar_outer"], expected_button) - assert_component_equal( - built_nav_bar["nav_panel_outer"], html.Div(hidden=True, id="nav_panel_outer") - ) + assert_component_equal(built_nav_bar["nav_bar_outer"], expected_button, keys_to_strip={"id", "className"}) + assert_component_equal(built_nav_bar["nav_panel_outer"], html.Div(hidden=True, id="nav_panel_outer")) def test_nav_bar_not_active_pages_as_list(self, pages_as_list): nav_bar = vm.NavBar(pages=pages_as_list) @@ -160,7 +159,7 @@ def test_nav_bar_not_active_pages_as_list(self, pages_as_list): ), ] ) - assert_component_equal(built_nav_bar["nav_bar_outer"], expected_buttons) + assert_component_equal(built_nav_bar["nav_bar_outer"], expected_buttons, keys_to_strip={"id", "className"}) assert_component_equal( built_nav_bar["nav_panel_outer"], html.Div(id="nav_panel_outer", hidden=True), diff --git a/vizro-core/tests/unit/vizro/models/_navigation/test_nav_link.py b/vizro-core/tests/unit/vizro/models/_navigation/test_nav_link.py index 1f90c8efc..2ef948900 100644 --- a/vizro-core/tests/unit/vizro/models/_navigation/test_nav_link.py +++ b/vizro-core/tests/unit/vizro/models/_navigation/test_nav_link.py @@ -86,9 +86,17 @@ def test_nav_link_active(self, pages, request): nav_link.pre_build() built_nav_link = nav_link.build(active_page_id="Page 1") expected_button = dbc.Button( - children=[dmc.Tooltip(label="Label", children=[html.Span("icon")], **self.common_args)], + children=[ + dmc.Tooltip( + label="Label", + children=[html.Span("icon", className="material-symbols-outlined")], + **self.common_args, + ) + ], active=True, href="/", + className="icon-button", + id="nav_link", ) assert_component_equal(built_nav_link["nav_link"], expected_button) assert all(isinstance(child, dbc.Accordion) for child in built_nav_link["nav_panel_outer"].children) @@ -99,9 +107,17 @@ def test_nav_link_not_active(self, pages, request): nav_link.pre_build() built_nav_link = nav_link.build(active_page_id="Page 3") expected_button = dbc.Button( - children=[dmc.Tooltip(label="Label", children=[html.Span("icon")], **self.common_args)], + children=[ + dmc.Tooltip( + label="Label", + children=[html.Span("icon", className="material-symbols-outlined")], + **self.common_args, + ) + ], active=False, href="/", + className="icon-button", + id="nav_link", ) assert_component_equal(built_nav_link["nav_link"], expected_button) assert "nav_panel_outer" not in built_nav_link diff --git a/vizro-core/tests/unit/vizro/models/_navigation/test_navigation.py b/vizro-core/tests/unit/vizro/models/_navigation/test_navigation.py index 6878f5f6e..c8068199f 100644 --- a/vizro-core/tests/unit/vizro/models/_navigation/test_navigation.py +++ b/vizro-core/tests/unit/vizro/models/_navigation/test_navigation.py @@ -84,9 +84,7 @@ def test_default_nav_selector(self, pages, request): navigation = vm.Navigation(pages=pages) navigation.pre_build() built_navigation = navigation.build(active_page_id="Page 1") - assert_component_equal( - built_navigation["nav_bar_outer"], html.Div(hidden=True, id="nav_bar_outer"), keys_to_strip={} - ) + assert_component_equal(built_navigation["nav_bar_outer"], html.Div(hidden=True, id="nav_bar_outer")) assert_component_equal( built_navigation["nav_panel_outer"], html.Div(id="nav_panel_outer", className="nav_panel"), diff --git a/vizro-core/tests/unit/vizro/models/test_dashboard.py b/vizro-core/tests/unit/vizro/models/test_dashboard.py index 91036aa72..d6dabe54e 100644 --- a/vizro-core/tests/unit/vizro/models/test_dashboard.py +++ b/vizro-core/tests/unit/vizro/models/test_dashboard.py @@ -130,7 +130,7 @@ def test_make_page_404_layout(self, vizro_app): className="page_error_container", ) - assert_component_equal(vm.Dashboard._make_page_404_layout(), expected, {}) + assert_component_equal(vm.Dashboard._make_page_404_layout(), expected) class TestDashboardBuild: From 0bc2a22abf2014ba3baa83526b8458c85593ca0f Mon Sep 17 00:00:00 2001 From: Antony Milne Date: Tue, 19 Dec 2023 13:34:10 +0000 Subject: [PATCH 05/14] Test the new pre-commit ci --- .../tests/unit/vizro/models/_navigation/test_accordion.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/vizro-core/tests/unit/vizro/models/_navigation/test_accordion.py b/vizro-core/tests/unit/vizro/models/_navigation/test_accordion.py index 829005266..c7e785d1d 100644 --- a/vizro-core/tests/unit/vizro/models/_navigation/test_accordion.py +++ b/vizro-core/tests/unit/vizro/models/_navigation/test_accordion.py @@ -110,6 +110,8 @@ class TestAccordionBuild: ), ] + + @pytest.mark.parametrize("pages, expected", test_cases) def test_accordion(self, pages, expected): accordion = vm.Accordion(id="accordion", pages=pages).build(active_page_id="Page 1") From e4ea7bbc7e43b31916bb1512c003f55c11c27dc6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 19 Dec 2023 13:34:27 +0000 Subject: [PATCH 06/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../tests/unit/vizro/models/_navigation/test_accordion.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/vizro-core/tests/unit/vizro/models/_navigation/test_accordion.py b/vizro-core/tests/unit/vizro/models/_navigation/test_accordion.py index c7e785d1d..829005266 100644 --- a/vizro-core/tests/unit/vizro/models/_navigation/test_accordion.py +++ b/vizro-core/tests/unit/vizro/models/_navigation/test_accordion.py @@ -110,8 +110,6 @@ class TestAccordionBuild: ), ] - - @pytest.mark.parametrize("pages, expected", test_cases) def test_accordion(self, pages, expected): accordion = vm.Accordion(id="accordion", pages=pages).build(active_page_id="Page 1") From fe64caf73c80a07aafc6596f944986906e8180ef Mon Sep 17 00:00:00 2001 From: Antony Milne Date: Tue, 19 Dec 2023 14:01:37 +0000 Subject: [PATCH 07/14] Update docstrings --- vizro-core/tests/tests_utils/asserts.py | 26 +++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/vizro-core/tests/tests_utils/asserts.py b/vizro-core/tests/tests_utils/asserts.py index ddd198e5e..c7c609a0f 100644 --- a/vizro-core/tests/tests_utils/asserts.py +++ b/vizro-core/tests/tests_utils/asserts.py @@ -4,25 +4,31 @@ import plotly -def strip_keys(object, keys): - """Strips all entries with key "id" from a dictionary, regardless of how deeply it's nested. - - This makes it easy to compare dictionaries generated from Dash components we've created that contain random IDs. - """ +def _strip_keys(object, keys): + """Strips from a JSON object all entries where the key is in keys, regardless of how deeply it's nested.""" if isinstance(object, dict): - object = {key: strip_keys(value, keys) for key, value in object.items() if key not in keys} + object = {key: _strip_keys(value, keys) for key, value in object.items() if key not in keys} elif isinstance(object, list): - object = [strip_keys(item, keys) for item in object] + object = [_strip_keys(item, keys) for item in object] return object -def component_to_dict(component: dash.development.base_component.Component) -> dict: +def _component_to_dict(component: dash.development.base_component.Component) -> dict: + """Prepares a Dash component for comparison by conversion to JSON object.""" return json.loads(json.dumps(component, cls=plotly.utils.PlotlyJSONEncoder)) def assert_component_equal(left, right, keys_to_strip=None): + """Checks that the left and right Dash components are equal, ignoring keys_to_strip. + + Examples: + >>> from dash import html + >>> assert_component_equal(html.Div(), html.Div()) + >>> assert_component_equal(html.Div(id="a"), html.Div(), keys_to_strip={"id"}) + >>> assert_component_equal(html.Div(html.Div()), html.Div(), keys_to_strip={"children"}) + """ keys_to_strip = keys_to_strip or {} - left = strip_keys(component_to_dict(left), keys_to_strip) - right = strip_keys(component_to_dict(right), keys_to_strip) + left = _strip_keys(_component_to_dict(left), keys_to_strip) + right = _strip_keys(_component_to_dict(right), keys_to_strip) assert left == right From 2efc44ebb5f4638675fe375bcad9f3fceb7509ce Mon Sep 17 00:00:00 2001 From: Antony Milne Date: Tue, 19 Dec 2023 17:00:20 +0000 Subject: [PATCH 08/14] Add demo --- vizro-core/tests/tests_utils/demo_asserts.py | 45 +++++++++++++++++++ .../tests/unit/vizro/models/test_demo.py | 41 +++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 vizro-core/tests/tests_utils/demo_asserts.py create mode 100644 vizro-core/tests/unit/vizro/models/test_demo.py diff --git a/vizro-core/tests/tests_utils/demo_asserts.py b/vizro-core/tests/tests_utils/demo_asserts.py new file mode 100644 index 000000000..e7b9d2962 --- /dev/null +++ b/vizro-core/tests/tests_utils/demo_asserts.py @@ -0,0 +1,45 @@ +"""Demo to show how to use asserts. These are not real tests that are run as part of testing, just a teaching aid.""" +from typing import List + +from asserts import assert_component_equal +from vizro.models import VizroBaseModel +from dash import html + + +class X(VizroBaseModel): + # Low-level contents model. + text: str + + def build(self): + return html.Div( + [html.H1("Heading"), html.P(self.text, id=self.id), html.Hr(), html.H2("Something")], className="inner" + ) + + +class Y(VizroBaseModel): + # Higher-level container model. + children: List[X] + + def build(self): + return html.Div([child.build() for child in self.children], id=self.id, className="container") + + +def test_X_build(): + # Test for low-level contents: compare the whole component tree. + # Sometimes setting keys_to_strip={"className"} is useful here. + result = X(id="x", text="Hello world").build() + expected = html.Div( + [html.H1("Heading"), html.P("Hello world", id="x"), html.Hr(), html.H2("Something")], className="inner" + ) + assert_component_equal(result, expected) + + +def test_Y_build(): + # Test for higher-level container. + many_x = [X(text="Hello world") for _ in range(4)] + result = Y(id="y", children=many_x).build() + # We don't want to compare the whole component tree here. Instead compare the bit that's specifically in Y and + # ignore the children: + assert_component_equal(result, html.Div(id="y", className="container"), keys_to_strip={"children"}) + # And also compare the "interface" between X and Y: + assert all(isinstance(child, html.Div) for child in result.children) diff --git a/vizro-core/tests/unit/vizro/models/test_demo.py b/vizro-core/tests/unit/vizro/models/test_demo.py new file mode 100644 index 000000000..0ead4b6c9 --- /dev/null +++ b/vizro-core/tests/unit/vizro/models/test_demo.py @@ -0,0 +1,41 @@ +from typing import List + +from asserts import assert_component_equal +from vizro.models import VizroBaseModel +from dash import html + + +class X(VizroBaseModel): + text: str + + def build(self): + return html.Div( + [html.H1("Heading"), html.P(self.text, id=self.id), html.Hr(), html.H2("Something")], className="blah" + ) + + +class Y(VizroBaseModel): + children: List[X] + + def build(self): + return html.Div([child.build() for child in self.children], id=self.id, className="container") + + +def test_X_build(): + # Test for low-level contents. + result = X(id="x", text="Hello world").build() + expected = html.Div( + [html.H1("Heading"), html.P("Hello world", id="x"), html.Hr(), html.H2("Something")], className="blah" + ) + assert_component_equal(result, expected) + + +def test_Y_build(): + # Test for higher-level container. + many_x = [X(text="Hello world") for _ in range(4)] + result = Y(id="y", children=many_x).build() + # We don't want to compare the whole component tree here. Instead compare the bit that's specifically in Y and + # ignore the children: + assert_component_equal(result, html.Div(id="y", className="container"), keys_to_strip={"children"}) + # And also compare the "interface" between X and Y: + assert all(isinstance(child, html.Div) for child in result.children) From 79297bd16a724e1abc0fc39161c4fe0e9f6b1e27 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 19 Dec 2023 17:00:53 +0000 Subject: [PATCH 09/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- vizro-core/tests/tests_utils/demo_asserts.py | 3 ++- vizro-core/tests/unit/vizro/models/test_demo.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/vizro-core/tests/tests_utils/demo_asserts.py b/vizro-core/tests/tests_utils/demo_asserts.py index e7b9d2962..fdc54a930 100644 --- a/vizro-core/tests/tests_utils/demo_asserts.py +++ b/vizro-core/tests/tests_utils/demo_asserts.py @@ -2,9 +2,10 @@ from typing import List from asserts import assert_component_equal -from vizro.models import VizroBaseModel from dash import html +from vizro.models import VizroBaseModel + class X(VizroBaseModel): # Low-level contents model. diff --git a/vizro-core/tests/unit/vizro/models/test_demo.py b/vizro-core/tests/unit/vizro/models/test_demo.py index 0ead4b6c9..bf65a1c00 100644 --- a/vizro-core/tests/unit/vizro/models/test_demo.py +++ b/vizro-core/tests/unit/vizro/models/test_demo.py @@ -1,9 +1,10 @@ from typing import List from asserts import assert_component_equal -from vizro.models import VizroBaseModel from dash import html +from vizro.models import VizroBaseModel + class X(VizroBaseModel): text: str From 6c65d2999c2896368f3fd5ad7f63a04e9660a27d Mon Sep 17 00:00:00 2001 From: Antony Milne <49395058+antonymilne@users.noreply.github.com> Date: Tue, 19 Dec 2023 17:06:24 +0000 Subject: [PATCH 10/14] Delete vizro-core/tests/unit/vizro/models/test_demo.py Signed-off-by: Antony Milne <49395058+antonymilne@users.noreply.github.com> --- .../tests/unit/vizro/models/test_demo.py | 42 ------------------- 1 file changed, 42 deletions(-) delete mode 100644 vizro-core/tests/unit/vizro/models/test_demo.py diff --git a/vizro-core/tests/unit/vizro/models/test_demo.py b/vizro-core/tests/unit/vizro/models/test_demo.py deleted file mode 100644 index bf65a1c00..000000000 --- a/vizro-core/tests/unit/vizro/models/test_demo.py +++ /dev/null @@ -1,42 +0,0 @@ -from typing import List - -from asserts import assert_component_equal -from dash import html - -from vizro.models import VizroBaseModel - - -class X(VizroBaseModel): - text: str - - def build(self): - return html.Div( - [html.H1("Heading"), html.P(self.text, id=self.id), html.Hr(), html.H2("Something")], className="blah" - ) - - -class Y(VizroBaseModel): - children: List[X] - - def build(self): - return html.Div([child.build() for child in self.children], id=self.id, className="container") - - -def test_X_build(): - # Test for low-level contents. - result = X(id="x", text="Hello world").build() - expected = html.Div( - [html.H1("Heading"), html.P("Hello world", id="x"), html.Hr(), html.H2("Something")], className="blah" - ) - assert_component_equal(result, expected) - - -def test_Y_build(): - # Test for higher-level container. - many_x = [X(text="Hello world") for _ in range(4)] - result = Y(id="y", children=many_x).build() - # We don't want to compare the whole component tree here. Instead compare the bit that's specifically in Y and - # ignore the children: - assert_component_equal(result, html.Div(id="y", className="container"), keys_to_strip={"children"}) - # And also compare the "interface" between X and Y: - assert all(isinstance(child, html.Div) for child in result.children) From af62c34a506379da1b90015536a0a1c2858ddb5e Mon Sep 17 00:00:00 2001 From: Antony Milne Date: Tue, 19 Dec 2023 17:13:31 +0000 Subject: [PATCH 11/14] Fix integration testst --- ...18_antony.milne_assert_components_equal.md | 48 +++++++++++++++++++ .../tests/integration/test_navigation.py | 2 +- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 vizro-core/changelog.d/20231219_171218_antony.milne_assert_components_equal.md diff --git a/vizro-core/changelog.d/20231219_171218_antony.milne_assert_components_equal.md b/vizro-core/changelog.d/20231219_171218_antony.milne_assert_components_equal.md new file mode 100644 index 000000000..f1f65e73c --- /dev/null +++ b/vizro-core/changelog.d/20231219_171218_antony.milne_assert_components_equal.md @@ -0,0 +1,48 @@ + + + + + + + + + diff --git a/vizro-core/tests/integration/test_navigation.py b/vizro-core/tests/integration/test_navigation.py index 1910fd6e2..ade0737aa 100644 --- a/vizro-core/tests/integration/test_navigation.py +++ b/vizro-core/tests/integration/test_navigation.py @@ -151,4 +151,4 @@ def label_cases(cases, label): def test_navigation_build(dashboard_result, dashboard_expected): result = dashboard_result.navigation.build() expected = dashboard_expected.navigation.build() - assert_component_equal(result, expected) + assert_component_equal(result, expected, keys_to_strip={"id"}) From fdacf2b5aa93d3b6e3e5e4007f41ae9a23ac4e9d Mon Sep 17 00:00:00 2001 From: Antony Milne Date: Wed, 20 Dec 2023 10:57:28 +0000 Subject: [PATCH 12/14] Made test_button more readable --- .../unit/vizro/models/_components/test_button.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/vizro-core/tests/unit/vizro/models/_components/test_button.py b/vizro-core/tests/unit/vizro/models/_components/test_button.py index 780e46445..85a02e472 100644 --- a/vizro-core/tests/unit/vizro/models/_components/test_button.py +++ b/vizro-core/tests/unit/vizro/models/_components/test_button.py @@ -39,11 +39,9 @@ def test_set_action_via_validator(self): class TestBuildMethod: def test_button_build(self): button = vm.Button(id="button_id", text="My text").build() - assert_component_equal( - button, - html.Div( - dbc.Button(id="button_id", children="My text", className="button_primary"), - className="button_container", - id="button_id_outer", - ), + expected = html.Div( + dbc.Button(id="button_id", children="My text", className="button_primary"), + className="button_container", + id="button_id_outer", ) + assert_component_equal(button, expected) From 85dae66647274a914ebe5d3d4273ac4b6068368c Mon Sep 17 00:00:00 2001 From: Antony Milne Date: Wed, 20 Dec 2023 11:11:45 +0000 Subject: [PATCH 13/14] Introduce STRIP_ALL option --- vizro-core/tests/tests_utils/asserts.py | 13 +++++++++++-- vizro-core/tests/tests_utils/demo_asserts.py | 8 +++++--- .../unit/vizro/models/_navigation/test_nav_bar.py | 4 ++-- .../unit/vizro/models/_navigation/test_nav_link.py | 4 ++-- .../vizro/models/_navigation/test_navigation.py | 8 ++++---- 5 files changed, 24 insertions(+), 13 deletions(-) diff --git a/vizro-core/tests/tests_utils/asserts.py b/vizro-core/tests/tests_utils/asserts.py index c7c609a0f..c46d12e25 100644 --- a/vizro-core/tests/tests_utils/asserts.py +++ b/vizro-core/tests/tests_utils/asserts.py @@ -3,6 +3,8 @@ import dash.development import plotly +STRIP_ALL = object() + def _strip_keys(object, keys): """Strips from a JSON object all entries where the key is in keys, regardless of how deeply it's nested.""" @@ -18,16 +20,23 @@ def _component_to_dict(component: dash.development.base_component.Component) -> return json.loads(json.dumps(component, cls=plotly.utils.PlotlyJSONEncoder)) -def assert_component_equal(left, right, keys_to_strip=None): +def assert_component_equal(left, right, *, keys_to_strip=None): """Checks that the left and right Dash components are equal, ignoring keys_to_strip. + If keys_to_strip is set to STRIP_ALL then only the type and namespace of component + will be compared, similar to doing isinstance. + Examples: >>> from dash import html >>> assert_component_equal(html.Div(), html.Div()) >>> assert_component_equal(html.Div(id="a"), html.Div(), keys_to_strip={"id"}) - >>> assert_component_equal(html.Div(html.Div()), html.Div(), keys_to_strip={"children"}) + >>> assert_component_equal(html.Div([html.P(), html.P()], id="a"), html.Div(id="a"), keys_to_strip={"children"}) + >>> assert_component_equal(html.Div(html.P(), className="blah", id="a"), html.Div(), keys_to_strip=STRIP_ALL) """ keys_to_strip = keys_to_strip or {} + if keys_to_strip is STRIP_ALL: + # Remove all properties from the component dictionary, leaving just "type" and "namespace" behind. + keys_to_strip = {"props"} left = _strip_keys(_component_to_dict(left), keys_to_strip) right = _strip_keys(_component_to_dict(right), keys_to_strip) diff --git a/vizro-core/tests/tests_utils/demo_asserts.py b/vizro-core/tests/tests_utils/demo_asserts.py index fdc54a930..bcf84ceaf 100644 --- a/vizro-core/tests/tests_utils/demo_asserts.py +++ b/vizro-core/tests/tests_utils/demo_asserts.py @@ -1,7 +1,7 @@ """Demo to show how to use asserts. These are not real tests that are run as part of testing, just a teaching aid.""" from typing import List -from asserts import assert_component_equal +from asserts import assert_component_equal, STRIP_ALL from dash import html from vizro.models import VizroBaseModel @@ -42,5 +42,7 @@ def test_Y_build(): # We don't want to compare the whole component tree here. Instead compare the bit that's specifically in Y and # ignore the children: assert_component_equal(result, html.Div(id="y", className="container"), keys_to_strip={"children"}) - # And also compare the "interface" between X and Y: - assert all(isinstance(child, html.Div) for child in result.children) + # And also compare the "interface" between X and Y. Use STRIP_ALL to not look at any properties of the html.Div. + # This is basically the same as doing: + # assert all(isinstance(child, html.Div) for child in result.children) and len(result.children) == 4 + assert_component_equal(result.children, [html.Div()] * 4, keys_to_strip=STRIP_ALL) diff --git a/vizro-core/tests/unit/vizro/models/_navigation/test_nav_bar.py b/vizro-core/tests/unit/vizro/models/_navigation/test_nav_bar.py index 5cd7293b9..4b2bc70ba 100644 --- a/vizro-core/tests/unit/vizro/models/_navigation/test_nav_bar.py +++ b/vizro-core/tests/unit/vizro/models/_navigation/test_nav_bar.py @@ -4,7 +4,7 @@ import dash_bootstrap_components as dbc import dash_mantine_components as dmc import pytest -from asserts import assert_component_equal +from asserts import assert_component_equal, STRIP_ALL from dash import html try: @@ -99,7 +99,7 @@ def test_nav_bar_active_pages_as_dict(self, pages_as_dict): html.Div(id="nav_panel_outer", className="nav_panel"), keys_to_strip={"children"}, ) - assert all(isinstance(child, dbc.Accordion) for child in built_nav_bar["nav_panel_outer"].children) + assert_component_equal(built_nav_bar["nav_panel_outer"].children, [dbc.Accordion()], keys_to_strip=STRIP_ALL) def test_nav_bar_active_pages_as_list(self, pages_as_list): nav_bar = vm.NavBar(pages=pages_as_list) diff --git a/vizro-core/tests/unit/vizro/models/_navigation/test_nav_link.py b/vizro-core/tests/unit/vizro/models/_navigation/test_nav_link.py index 2ef948900..07696b9d4 100644 --- a/vizro-core/tests/unit/vizro/models/_navigation/test_nav_link.py +++ b/vizro-core/tests/unit/vizro/models/_navigation/test_nav_link.py @@ -4,7 +4,7 @@ import dash_bootstrap_components as dbc import dash_mantine_components as dmc import pytest -from asserts import assert_component_equal +from asserts import assert_component_equal, STRIP_ALL from dash import html try: @@ -99,7 +99,7 @@ def test_nav_link_active(self, pages, request): id="nav_link", ) assert_component_equal(built_nav_link["nav_link"], expected_button) - assert all(isinstance(child, dbc.Accordion) for child in built_nav_link["nav_panel_outer"].children) + assert_component_equal(built_nav_link["nav_panel_outer"].children, [dbc.Accordion()], keys_to_strip=STRIP_ALL) def test_nav_link_not_active(self, pages, request): pages = request.getfixturevalue(pages) diff --git a/vizro-core/tests/unit/vizro/models/_navigation/test_navigation.py b/vizro-core/tests/unit/vizro/models/_navigation/test_navigation.py index c8068199f..fec6e1b78 100644 --- a/vizro-core/tests/unit/vizro/models/_navigation/test_navigation.py +++ b/vizro-core/tests/unit/vizro/models/_navigation/test_navigation.py @@ -3,7 +3,7 @@ import dash_bootstrap_components as dbc import pytest -from asserts import assert_component_equal +from asserts import assert_component_equal, STRIP_ALL from dash import html try: @@ -90,9 +90,9 @@ def test_default_nav_selector(self, pages, request): html.Div(id="nav_panel_outer", className="nav_panel"), keys_to_strip={"children"}, ) - assert all(isinstance(child, dbc.Accordion) for child in built_navigation["nav_panel_outer"].children) + assert_component_equal(built_navigation["nav_panel_outer"].children, [dbc.Accordion()], keys_to_strip=STRIP_ALL) - def test_non_default_nav_selector_pags_as_dict(self, pages_as_dict): + def test_non_default_nav_selector_pags_as_dict(self, pages_as_dict, built_nav_link=None): navigation = vm.Navigation(pages=pages_as_dict, nav_selector=vm.NavBar()) navigation.pre_build() built_navigation = navigation.build(active_page_id="Page 1") @@ -106,7 +106,7 @@ def test_non_default_nav_selector_pags_as_dict(self, pages_as_dict): html.Div(id="nav_panel_outer", className="nav_panel"), keys_to_strip={"children"}, ) - assert all(isinstance(child, dbc.Accordion) for child in built_navigation["nav_panel_outer"].children) + assert_component_equal(built_navigation["nav_panel_outer"].children, [dbc.Accordion()], keys_to_strip=STRIP_ALL) def test_non_default_nav_selector_pages_as_list(self, pages_as_list): navigation = vm.Navigation(pages=pages_as_list, nav_selector=vm.NavBar()) From 029072436c34924367e5e16d7a695e430ab437b9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 20 Dec 2023 11:12:02 +0000 Subject: [PATCH 14/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- vizro-core/tests/tests_utils/demo_asserts.py | 2 +- vizro-core/tests/unit/vizro/models/_navigation/test_nav_bar.py | 2 +- vizro-core/tests/unit/vizro/models/_navigation/test_nav_link.py | 2 +- .../tests/unit/vizro/models/_navigation/test_navigation.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/vizro-core/tests/tests_utils/demo_asserts.py b/vizro-core/tests/tests_utils/demo_asserts.py index bcf84ceaf..c61c439f7 100644 --- a/vizro-core/tests/tests_utils/demo_asserts.py +++ b/vizro-core/tests/tests_utils/demo_asserts.py @@ -1,7 +1,7 @@ """Demo to show how to use asserts. These are not real tests that are run as part of testing, just a teaching aid.""" from typing import List -from asserts import assert_component_equal, STRIP_ALL +from asserts import STRIP_ALL, assert_component_equal from dash import html from vizro.models import VizroBaseModel diff --git a/vizro-core/tests/unit/vizro/models/_navigation/test_nav_bar.py b/vizro-core/tests/unit/vizro/models/_navigation/test_nav_bar.py index 4b2bc70ba..372d2f5d5 100644 --- a/vizro-core/tests/unit/vizro/models/_navigation/test_nav_bar.py +++ b/vizro-core/tests/unit/vizro/models/_navigation/test_nav_bar.py @@ -4,7 +4,7 @@ import dash_bootstrap_components as dbc import dash_mantine_components as dmc import pytest -from asserts import assert_component_equal, STRIP_ALL +from asserts import STRIP_ALL, assert_component_equal from dash import html try: diff --git a/vizro-core/tests/unit/vizro/models/_navigation/test_nav_link.py b/vizro-core/tests/unit/vizro/models/_navigation/test_nav_link.py index 07696b9d4..9956f8ecc 100644 --- a/vizro-core/tests/unit/vizro/models/_navigation/test_nav_link.py +++ b/vizro-core/tests/unit/vizro/models/_navigation/test_nav_link.py @@ -4,7 +4,7 @@ import dash_bootstrap_components as dbc import dash_mantine_components as dmc import pytest -from asserts import assert_component_equal, STRIP_ALL +from asserts import STRIP_ALL, assert_component_equal from dash import html try: diff --git a/vizro-core/tests/unit/vizro/models/_navigation/test_navigation.py b/vizro-core/tests/unit/vizro/models/_navigation/test_navigation.py index fec6e1b78..6f6530a0f 100644 --- a/vizro-core/tests/unit/vizro/models/_navigation/test_navigation.py +++ b/vizro-core/tests/unit/vizro/models/_navigation/test_navigation.py @@ -3,7 +3,7 @@ import dash_bootstrap_components as dbc import pytest -from asserts import assert_component_equal, STRIP_ALL +from asserts import STRIP_ALL, assert_component_equal from dash import html try: