Skip to content

Commit

Permalink
Apply asserts_component_equal to unit tests for components (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
nadijagraca authored Jan 29, 2024
1 parent 8b40813 commit aa6eaa2
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 238 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!--
A new scriv changelog fragment.
Uncomment the section that is right (remove the HTML comment wrapper).
-->

<!--
### Highlights ✨
- A bullet item for the Highlights ✨ category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Removed
- A bullet item for the Removed category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Added
- A bullet item for the Added category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Changed
- A bullet item for the Changed category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Deprecated
- A bullet item for the Deprecated category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Fixed
- A bullet item for the Fixed category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Security
- A bullet item for the Security category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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."""

Expand Down Expand Up @@ -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)
101 changes: 46 additions & 55 deletions vizro-core/tests/unit/vizro/models/_components/form/test_dropdown.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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."""

Expand Down Expand Up @@ -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)
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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."""

Expand Down Expand Up @@ -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)
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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)
Loading

0 comments on commit aa6eaa2

Please sign in to comment.