|
| 1 | +"""Pattern-matching callback support — schemas and descriptions for wildcard IDs. |
| 2 | +
|
| 3 | +Covers callbacks whose Input/Output use ``ALL``, ``MATCH``, or ``ALLSMALLER`` |
| 4 | +wildcards in dict-based component IDs: the input is typed as an array (ALL) |
| 5 | +or object (MATCH) of ``{id, property, value}`` entries, and descriptions |
| 6 | +surface the wildcard kind and ID pattern. |
| 7 | +""" |
| 8 | + |
| 9 | +from dash import Dash, html, Input, Output, ALL, MATCH, dcc |
| 10 | + |
| 11 | +from tests.unit.mcp.conftest import _tools_list, _user_tool, _schema_for, _desc_for |
| 12 | + |
| 13 | + |
| 14 | +# --------------------------------------------------------------------------- |
| 15 | +# Schema shape |
| 16 | +# --------------------------------------------------------------------------- |
| 17 | + |
| 18 | + |
| 19 | +def test_mcpm001_all_produces_array_schema(): |
| 20 | + app = Dash(__name__) |
| 21 | + app.layout = html.Div( |
| 22 | + [ |
| 23 | + html.Div(id={"type": "item", "index": 0}, children="A"), |
| 24 | + html.Div(id={"type": "item", "index": 1}, children="B"), |
| 25 | + html.Div(id="result"), |
| 26 | + ] |
| 27 | + ) |
| 28 | + |
| 29 | + @app.callback( |
| 30 | + Output("result", "children"), |
| 31 | + Input({"type": "item", "index": ALL}, "children"), |
| 32 | + ) |
| 33 | + def combine(values): |
| 34 | + return ", ".join(values) |
| 35 | + |
| 36 | + tool = _user_tool(_tools_list(app)) |
| 37 | + schema = _schema_for(tool) |
| 38 | + assert schema["type"] == "array" |
| 39 | + assert schema["items"]["type"] == "object" |
| 40 | + assert "value" in schema["items"]["properties"] |
| 41 | + |
| 42 | + |
| 43 | +def test_mcpm002_match_produces_object_schema(): |
| 44 | + app = Dash(__name__) |
| 45 | + app.layout = html.Div( |
| 46 | + [ |
| 47 | + html.Div(id={"type": "item", "index": 0}, children="A"), |
| 48 | + html.Div(id="result"), |
| 49 | + ] |
| 50 | + ) |
| 51 | + |
| 52 | + @app.callback( |
| 53 | + Output("result", "children"), |
| 54 | + Input({"type": "item", "index": MATCH}, "children"), |
| 55 | + ) |
| 56 | + def echo(value): |
| 57 | + return value |
| 58 | + |
| 59 | + tool = _user_tool(_tools_list(app)) |
| 60 | + schema = _schema_for(tool) |
| 61 | + assert schema["type"] == "object" |
| 62 | + assert "value" in schema["properties"] |
| 63 | + |
| 64 | + |
| 65 | +def test_mcpm003_annotation_narrows_value_schema(): |
| 66 | + app = Dash(__name__) |
| 67 | + app.layout = html.Div( |
| 68 | + [ |
| 69 | + dcc.Dropdown(id={"type": "filter", "index": 0}, options=["a", "b"]), |
| 70 | + dcc.Dropdown(id={"type": "filter", "index": 1}, options=["c", "d"]), |
| 71 | + html.Div(id="result"), |
| 72 | + ] |
| 73 | + ) |
| 74 | + |
| 75 | + @app.callback( |
| 76 | + Output("result", "children"), |
| 77 | + Input({"type": "filter", "index": ALL}, "options"), |
| 78 | + ) |
| 79 | + def combine(options: list[str]): |
| 80 | + return str(options) |
| 81 | + |
| 82 | + tool = _user_tool(_tools_list(app)) |
| 83 | + schema = _schema_for(tool) |
| 84 | + assert schema["type"] == "array" |
| 85 | + value_schema = schema["items"]["properties"]["value"] |
| 86 | + # Annotation narrows value to list[str] instead of the broad introspected type |
| 87 | + assert value_schema == {"items": {"type": "string"}, "type": "array"} |
| 88 | + |
| 89 | + |
| 90 | +# --------------------------------------------------------------------------- |
| 91 | +# Descriptions |
| 92 | +# --------------------------------------------------------------------------- |
| 93 | + |
| 94 | + |
| 95 | +def test_mcpm004_all_description(): |
| 96 | + app = Dash(__name__) |
| 97 | + app.layout = html.Div( |
| 98 | + [ |
| 99 | + html.Div(id={"type": "item", "index": 0}), |
| 100 | + html.Div(id="result"), |
| 101 | + ] |
| 102 | + ) |
| 103 | + |
| 104 | + @app.callback( |
| 105 | + Output("result", "children"), |
| 106 | + Input({"type": "item", "index": ALL}, "children"), |
| 107 | + ) |
| 108 | + def combine(values): |
| 109 | + return str(values) |
| 110 | + |
| 111 | + tool = _user_tool(_tools_list(app)) |
| 112 | + desc = _desc_for(tool) |
| 113 | + assert "Pattern-matching input (ALL)" in desc |
| 114 | + assert 'type="item"' in desc |
| 115 | + |
| 116 | + |
| 117 | +def test_mcpm005_match_description(): |
| 118 | + app = Dash(__name__) |
| 119 | + app.layout = html.Div( |
| 120 | + [ |
| 121 | + html.Div(id={"type": "item", "index": 0}), |
| 122 | + html.Div(id="result"), |
| 123 | + ] |
| 124 | + ) |
| 125 | + |
| 126 | + @app.callback( |
| 127 | + Output("result", "children"), |
| 128 | + Input({"type": "item", "index": MATCH}, "children"), |
| 129 | + ) |
| 130 | + def echo(value): |
| 131 | + return value |
| 132 | + |
| 133 | + tool = _user_tool(_tools_list(app)) |
| 134 | + desc = _desc_for(tool) |
| 135 | + assert "Pattern-matching input (MATCH)" in desc |
| 136 | + assert 'type="item"' in desc |
0 commit comments