-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: petar-qb <[email protected]> Co-authored-by: Antony Milne <[email protected]>
- Loading branch information
1 parent
2b6247c
commit 25a9ce4
Showing
7 changed files
with
206 additions
and
46 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
vizro-core/changelog.d/20240112_120344_huong_li_nguyen_containers.md
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,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)) | ||
--> |
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
57 changes: 57 additions & 0 deletions
57
vizro-core/tests/unit/vizro/models/_components/test_container.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,57 @@ | ||
"""Unit tests for vizro.models.Container.""" | ||
import dash_bootstrap_components as dbc | ||
import pytest | ||
from asserts import STRIP_ALL, assert_component_equal | ||
from dash import html | ||
|
||
try: | ||
from pydantic.v1 import ValidationError | ||
except ImportError: # pragma: no cov | ||
from pydantic import ValidationError | ||
|
||
import vizro.models as vm | ||
|
||
|
||
class TestContainerInstantiation: | ||
"""Tests model instantiation and the validators run at that time.""" | ||
|
||
def test_create_container_mandatory_only(self): | ||
container = vm.Container(title="Title", components=[vm.Button(), vm.Button()]) | ||
assert isinstance(container.components[0], vm.Button) and isinstance(container.components[1], vm.Button) | ||
assert container.layout.grid == [[0], [1]] | ||
assert container.title == "Title" | ||
|
||
def test_create_container_mandatory_and_optional(self): | ||
container = vm.Container( | ||
title="Title", | ||
components=[vm.Button(), vm.Button()], | ||
id="my-id", | ||
layout=vm.Layout(grid=[[0, 1]]), | ||
) | ||
assert isinstance(container.components[0], vm.Button) and isinstance(container.components[1], vm.Button) | ||
assert container.layout.grid == [[0, 1]] | ||
assert container.title == "Title" | ||
assert container.id == "my-id" | ||
|
||
def test_mandatory_title_missing(self): | ||
with pytest.raises(ValidationError, match="field required"): | ||
vm.Container(components=[vm.Button()]) | ||
|
||
def test_mandatory_components_missing(self): | ||
with pytest.raises(ValidationError, match="field required"): | ||
vm.Container(title="Title") | ||
|
||
|
||
class TestContainerBuildMethod: | ||
def test_container_build(self): | ||
result = vm.Container( | ||
id="container", title="Title", components=[vm.Button()], layout=vm.Layout(id="layout_id", grid=[[0]]) | ||
).build() | ||
assert_component_equal( | ||
result, html.Div(className="page-component-container", id="container"), keys_to_strip={"children"} | ||
) | ||
assert_component_equal(result.children, [html.H3(), html.Div()], keys_to_strip=STRIP_ALL) | ||
# We still want to test the exact H3 produced in Container.build: | ||
assert_component_equal(result.children[0], html.H3("Title")) | ||
# And also that a button has been inserted in the right place: | ||
assert_component_equal(result["layout_id_0"].children.children, dbc.Button(), keys_to_strip=STRIP_ALL) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import re | ||
|
||
import pytest | ||
|
||
try: | ||
from pydantic.v1 import ValidationError | ||
except ImportError: # pragma: no cov | ||
from pydantic import ValidationError | ||
|
||
import vizro.models as vm | ||
|
||
|
||
class TestSharedValidators: | ||
def test_set_components_validator(self, model_with_layout): | ||
with pytest.raises(ValidationError, match="Ensure this value has at least 1 item."): | ||
model_with_layout(title="Title", components=[]) | ||
|
||
def test_check_for_valid_component_types(self, model_with_layout): | ||
with pytest.raises( | ||
ValidationError, match=re.escape("(allowed values: 'button', 'card', 'container', 'graph', 'table')") | ||
): | ||
model_with_layout(title="Page Title", components=[vm.Checklist()]) |
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