|
| 1 | +import sys |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from dash.development._py_components_generation import generate_class_string |
| 7 | +from dash.development._py_prop_typing import shapes |
| 8 | + |
| 9 | +CONDITIONAL_STYLE_TYPE = { |
| 10 | + "name": "arrayOf", |
| 11 | + "value": { |
| 12 | + "name": "shape", |
| 13 | + "value": { |
| 14 | + "if": { |
| 15 | + "name": "exact", |
| 16 | + "value": { |
| 17 | + "column_id": {"name": "string", "required": False}, |
| 18 | + }, |
| 19 | + "required": False, |
| 20 | + }, |
| 21 | + }, |
| 22 | + }, |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.parametrize( |
| 27 | + "prop_name, shape_name", |
| 28 | + [ |
| 29 | + ("style_cell_conditional", "StyleCellConditional"), |
| 30 | + ("style_data_conditional", "StyleDataConditional"), |
| 31 | + ("style_filter_conditional", "StyleFilterConditional"), |
| 32 | + ("style_header_conditional", "StyleHeaderConditional"), |
| 33 | + ], |
| 34 | +) |
| 35 | +def test_datatable_conditional_styles_allow_style_keys( |
| 36 | + monkeypatch, prop_name, shape_name |
| 37 | +): |
| 38 | + dash_table_dir = Path(__file__).resolve().parents[3] / "components" / "dash-table" |
| 39 | + monkeypatch.syspath_prepend(str(dash_table_dir)) |
| 40 | + sys.modules.pop("dash_prop_typing", None) |
| 41 | + shapes.clear() |
| 42 | + |
| 43 | + generated = generate_class_string( |
| 44 | + typename="DataTable", |
| 45 | + props={ |
| 46 | + prop_name: { |
| 47 | + "type": CONDITIONAL_STYLE_TYPE, |
| 48 | + "required": False, |
| 49 | + "description": "Conditional style", |
| 50 | + }, |
| 51 | + }, |
| 52 | + description="DataTable", |
| 53 | + namespace="dash_table", |
| 54 | + custom_typing_module="dash_prop_typing", |
| 55 | + ) |
| 56 | + |
| 57 | + assert ( |
| 58 | + f'{prop_name}: typing.Optional[typing.Sequence[typing.Union["{shape_name}", ' |
| 59 | + "typing.Dict[str, typing.Any]]]] = None" |
| 60 | + ) in generated |
| 61 | + assert f'"if": NotRequired["{shape_name}If"]' in generated |
| 62 | + |
| 63 | + shapes.clear() |
| 64 | + sys.modules.pop("dash_prop_typing", None) |
0 commit comments