Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
huong-li-nguyen committed Jul 18, 2024
1 parent e42d9e7 commit 9f4481a
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 18 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

- Improve validation error message if `CapturedCallable` is directly provided. ([#587](https://github.com/mckinsey/vizro/pull/587))


<!--
### 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))
-->
5 changes: 2 additions & 3 deletions vizro-core/examples/scratch_dev/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@


home = vm.Page(
title="value_error.discriminated_union.missing_discriminator",
title="Page Title",
components=[
# vm.Card(text="I am a Card"),
kpi_card(data_frame=df_kpi, value_column="Actual", title="KPI with value"),
vm.Figure(figure=kpi_card(data_frame=df_kpi, value_column="Actual", title="KPI with value")),
],
)

Expand Down
5 changes: 3 additions & 2 deletions vizro-core/src/vizro/models/_components/_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from vizro.models import VizroBaseModel
from vizro.models._components.form import Checklist, Dropdown, RadioItems, RangeSlider, Slider
from vizro.models._layout import set_layout
from vizro.models._models_utils import _log_call, validate_components
from vizro.models._models_utils import _log_call, validate_components_type, validate_min_length
from vizro.models.types import _FormComponentType

if TYPE_CHECKING:
Expand All @@ -34,7 +34,8 @@ class Form(VizroBaseModel):
layout: Layout = None # type: ignore[assignment]

# Re-used validators
_validate_components = validator("components", allow_reuse=True, always=True, pre=True)(validate_components)
_validate_components_type = validator("components", allow_reuse=True, always=True, pre=True)(validate_components_type)
_validate_components_length = validator("components", allow_reuse=True, always=True)(validate_min_length)
_validate_layout = validator("layout", allow_reuse=True, always=True)(set_layout)

@_log_call
Expand Down
5 changes: 3 additions & 2 deletions vizro-core/src/vizro/models/_components/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from vizro.models import VizroBaseModel
from vizro.models._layout import set_layout
from vizro.models._models_utils import _log_call, validate_components
from vizro.models._models_utils import _log_call, validate_components_type, validate_min_length
from vizro.models.types import ComponentType

if TYPE_CHECKING:
Expand All @@ -36,7 +36,8 @@ class Container(VizroBaseModel):
layout: Layout = None # type: ignore[assignment]

# Re-used validators
_validate_components = validator("components", allow_reuse=True, always=True, pre=True)(validate_components)
_validate_components_type = validator("components", allow_reuse=True, always=True, pre=True)(validate_components_type)
_validate_components_length = validator("components", allow_reuse=True, always=True)(validate_min_length)
_validate_layout = validator("layout", allow_reuse=True, always=True)(set_layout)

@_log_call
Expand Down
4 changes: 2 additions & 2 deletions vizro-core/src/vizro/models/_components/tabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pydantic import validator

from vizro.models import VizroBaseModel
from vizro.models._models_utils import _log_call, _validate_min_length
from vizro.models._models_utils import _log_call, validate_min_length

if TYPE_CHECKING:
from vizro.models._components import Container
Expand All @@ -29,7 +29,7 @@ class Tabs(VizroBaseModel):
type: Literal["tabs"] = "tabs"
tabs: List[Container]

_validate_tabs = validator("tabs", allow_reuse=True, always=True)(_validate_min_length)
_validate_tabs = validator("tabs", allow_reuse=True, always=True)(validate_min_length)

@_log_call
def build(self):
Expand Down
10 changes: 4 additions & 6 deletions vizro-core/src/vizro/models/_models_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,19 @@ def _wrapper(self, *args, **kwargs):
return _wrapper



# Validators for reuse
def _validate_min_length(cls, field):
def validate_min_length(cls, field):
if not field:
raise ValueError("Ensure this value has at least 1 item.")
return field

def validate_components(cls, field):
_validate_min_length(cls, field)

# Validate CapturedCallable has been directly provided.
def validate_components_type(cls, field):
mode_to_error = {
"figure": "A callable of mode `figure` has been provided. Please wrap it inside the `vm.Figure(figure=...)`.",
"table": "A callable of mode `table` has been provided. Please wrap it inside the `vm.Table(figure=...)`.",
"ag_grid": "A callable of mode `ag_grid` has been provided. Please wrap it inside the `vm.AgGrid(figure=...)`.",
"graph": "A callable of mode `graph` has been provided. Please wrap it inside the `vm.AgGrid(figure=...)`.",
"graph": "A callable of mode `graph` has been provided. Please wrap it inside the `vm.Graph(figure=...)`.",
}

for component in field:
Expand Down
5 changes: 3 additions & 2 deletions vizro-core/src/vizro/models/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from vizro.models import Action, Layout, VizroBaseModel
from vizro.models._action._actions_chain import ActionsChain, Trigger
from vizro.models._layout import set_layout
from vizro.models._models_utils import _log_call, validate_components
from vizro.models._models_utils import _log_call, validate_components_type, validate_min_length

from .types import ComponentType, ControlType

Expand Down Expand Up @@ -52,7 +52,8 @@ class Page(VizroBaseModel):
actions: List[ActionsChain] = []

# Re-used validators
_validate_components = validator("components", allow_reuse=True, always=True, pre=True)(validate_components)
_validate_components_type = validator("components", allow_reuse=True, always=True, pre=True)(validate_components_type)
_validate_components_length = validator("components", allow_reuse=True, always=True)(validate_min_length)
_validate_layout = validator("layout", allow_reuse=True, always=True)(set_layout)

@root_validator(pre=True)
Expand Down
9 changes: 8 additions & 1 deletion vizro-core/tests/unit/vizro/models/test_models_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@


class TestSharedValidators:
def test_set_components_validator(self, model_with_layout):
def test_validate_min_length(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_validate_components_type(self, model_with_layout, standard_px_chart):
with pytest.raises(
ValidationError,
match="A callable of mode `graph` has been provided. " "Please wrap it inside the `vm.Graph(figure=...)`.",
):
model_with_layout(title="Title", components=[standard_px_chart])

def test_check_for_valid_component_types(self, model_with_layout):
with pytest.raises(
ValidationError,
Expand Down

0 comments on commit 9f4481a

Please sign in to comment.