bito-code-review[bot] commented on code in PR #43572: URL: https://github.com/apache/superset/pull/43572#discussion_r4048670383
########## tests/unit_tests/mcp_service/chart/test_bubble_chart.py: ########## @@ -0,0 +1,471 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +"""Tests for the bubble chart type plugin. + +Schema validation, form_data mapping (matching the frontend Bubble buildQuery +contract for viz_type ``bubble_v2`` — an ``entity`` dimension plus three +separate metric keys ``x``/``y``/``size`` and an optional ``series``), and +registry integration. +""" + +import pytest +from pydantic import TypeAdapter, ValidationError + +from superset.mcp_service.chart.chart_utils import map_bubble_config +from superset.mcp_service.chart.schemas import BubbleChartConfig, ChartConfig + + +def _base(**overrides): + cfg = { Review Comment: <div> <div id="suggestion"> <div id="issue"><b>Missing type hints on helper</b></div> <div id="fix"> The `_base` helper used by every test in this file has no parameter/return annotations and no docstring, while the sibling helper `_config` in `tests/unit_tests/mcp_service/chart/test_gantt_chart.py` is fully annotated. BITO.md rules 7819/12490 mandate explicit type hints and docstrings on new test helpers. Annotate it and add a short docstring. </div> </div> <small><i>Code Review Run #b7102a</i></small> </div> --- Should Bito avoid suggestions like this for future reviews? (<a href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>) - [ ] Yes, avoid them ########## tests/unit_tests/mcp_service/chart/tool/test_generate_chart.py: ########## @@ -973,3 +976,37 @@ def test_response_form_data_preserves_sql_metric_strings(self) -> None: assert m["sqlExpression"] == _SQL_EXPR assert m["label"] == "Win Rate" assert m["optionName"] == "metric_sql_abcd1234" + + +class TestGenerateBubbleWithSqlExpressionMetric: + """A SQL-expression metric must survive the response-building analyzers. + + Bubble carries a metric in ``x``, and a SQL-expression ColumnRef has no + name, so the semantics analyzer joined None into its data story. The + analyzers run while the response is assembled — in save mode that is + after CreateChartCommand has already committed the chart, so the caller + got an exception for a chart that exists. + """ + + @pytest.mark.asyncio + async def test_saved_bubble_with_sql_expression_x_is_reported(self) -> None: Review Comment: <div> <div id="suggestion"> <div id="issue"><b>Missing Test Docstring</b></div> <div id="fix"> New test methods in this file carry their own docstrings (e.g. `test_detached_chart_is_reported_as_created`), and BITO rule 12148 requires a docstring on every newly added test function. `test_saved_bubble_with_sql_expression_x_is_reported` relies only on the class docstring. Add a one-line method docstring describing the scenario (SQL-expression x metric survives the semantics analyzer in save mode). </div> </div> <small><i>Code Review Run #b7102a</i></small> </div> --- Should Bito avoid suggestions like this for future reviews? (<a href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>) - [ ] Yes, avoid them ########## tests/unit_tests/mcp_service/chart/test_bubble_chart.py: ########## @@ -0,0 +1,471 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +"""Tests for the bubble chart type plugin. + +Schema validation, form_data mapping (matching the frontend Bubble buildQuery +contract for viz_type ``bubble_v2`` — an ``entity`` dimension plus three +separate metric keys ``x``/``y``/``size`` and an optional ``series``), and +registry integration. +""" + +import pytest +from pydantic import TypeAdapter, ValidationError + +from superset.mcp_service.chart.chart_utils import map_bubble_config +from superset.mcp_service.chart.schemas import BubbleChartConfig, ChartConfig + + +def _base(**overrides): + cfg = { + "chart_type": "bubble_v2", + "entity": {"name": "country"}, + "x": {"name": "gdp", "aggregate": "AVG"}, + "y": {"name": "life_expectancy", "aggregate": "AVG"}, + "size": {"name": "population", "aggregate": "SUM"}, + } + cfg.update(overrides) + return cfg + + +class TestBubbleChartConfigSchema: + """BubbleChartConfig schema validation.""" + + def test_basic_bubble_config(self) -> None: + config = BubbleChartConfig(**_base()) + assert config.entity.name == "country" + assert config.x.name == "gdp" + assert config.series is None # series grouping is optional + assert config.row_limit == 10000 # shared control default + + @pytest.mark.parametrize("missing", ["entity", "x", "y", "size"]) + def test_bubble_missing_required(self, missing: str) -> None: + cfg = _base() + del cfg[missing] + with pytest.raises(ValidationError): + BubbleChartConfig(**cfg) + + def test_bubble_rejects_extra_fields(self) -> None: + with pytest.raises(ValidationError): + BubbleChartConfig(**_base(bogus=1)) + + def test_bubble_entity_rejects_saved_metric(self) -> None: + with pytest.raises(ValidationError): + BubbleChartConfig(**_base(entity={"name": "c", "saved_metric": True})) + + def test_bubble_series_rejects_saved_metric(self) -> None: + with pytest.raises(ValidationError): + BubbleChartConfig(**_base(series={"name": "c", "saved_metric": True})) + + def test_bubble_entity_rejects_aggregate(self) -> None: + """An aggregate makes entity metric-like; entity is a dimension.""" + with pytest.raises(ValidationError): + BubbleChartConfig(**_base(entity={"name": "country", "aggregate": "SUM"})) + + def test_bubble_series_rejects_aggregate(self) -> None: + with pytest.raises(ValidationError): + BubbleChartConfig( + **_base(series={"name": "continent", "aggregate": "COUNT"}) + ) + + def test_bubble_x_accepts_saved_metric(self) -> None: + """A saved metric is a valid x/y/size value.""" + config = BubbleChartConfig( + **_base(x={"name": "gdp_index", "saved_metric": True}) + ) + assert config.x.saved_metric is True + + def test_chart_config_union_dispatches_bubble(self) -> None: + config = TypeAdapter(ChartConfig).validate_python(_base()) + assert isinstance(config, BubbleChartConfig) + + +class TestMapBubbleConfig: + """form_data mapping must match the frontend Bubble buildQuery.""" + + def test_basic_bubble_form_data(self) -> None: + config = BubbleChartConfig(**_base()) + form_data = map_bubble_config(config) + assert form_data["viz_type"] == "bubble_v2" + assert form_data["entity"] == "country" + # x/y/size are three separate metric keys (not a metrics array) + assert form_data["x"]["label"] == "AVG(gdp)" + assert form_data["y"]["label"] == "AVG(life_expectancy)" + assert form_data["size"]["label"] == "SUM(population)" + assert form_data["row_limit"] == 10000 + assert "series" not in form_data # omitted when not set + + def test_bubble_form_data_with_series_and_filters(self) -> None: + config = BubbleChartConfig( + **_base( + series={"name": "continent"}, + filters=[{"column": "year", "op": "=", "value": 2026}], + ) + ) + form_data = map_bubble_config(config) + assert form_data["series"] == "continent" + assert form_data["adhoc_filters"], "filters must map to adhoc_filters" + + def test_bubble_saved_metric_maps_to_name_string(self) -> None: + config = BubbleChartConfig( + **_base(size={"name": "headcount", "saved_metric": True}) + ) + assert map_bubble_config(config)["size"] == "headcount" + + +class TestBubbleMetricsResolution: + """The MCP query path must fold x/y/size into metrics for bubble_v2. + + The mapper emits viz_type 'bubble_v2', so resolve_metrics must recognize + it (not just the legacy 'bubble' key) or the query drops all three metrics. + """ + + def test_bubble_v2_metrics_resolved(self) -> None: + from superset.mcp_service.chart.chart_helpers import resolve_metrics Review Comment: <div> <div id="suggestion"> <div id="issue"><b>Function-local imports</b></div> <div id="fix"> These tests import `resolve_metrics` inside the function body; the same pattern repeats at lines 150, 157 and 162 for `registry` and `display_name_for_viz_type`. No circular dependency justifies it — the module already imports `map_bubble_config` from the same package at top level. Move these imports to module level. </div> </div> <small><i>Code Review Run #b7102a</i></small> </div> --- Should Bito avoid suggestions like this for future reviews? (<a href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>) - [ ] Yes, avoid them ########## tests/unit_tests/mcp_service/chart/test_bubble_chart.py: ########## @@ -0,0 +1,471 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +"""Tests for the bubble chart type plugin. + +Schema validation, form_data mapping (matching the frontend Bubble buildQuery +contract for viz_type ``bubble_v2`` — an ``entity`` dimension plus three +separate metric keys ``x``/``y``/``size`` and an optional ``series``), and +registry integration. +""" + +import pytest +from pydantic import TypeAdapter, ValidationError + +from superset.mcp_service.chart.chart_utils import map_bubble_config +from superset.mcp_service.chart.schemas import BubbleChartConfig, ChartConfig + + +def _base(**overrides): + cfg = { + "chart_type": "bubble_v2", + "entity": {"name": "country"}, + "x": {"name": "gdp", "aggregate": "AVG"}, + "y": {"name": "life_expectancy", "aggregate": "AVG"}, + "size": {"name": "population", "aggregate": "SUM"}, + } + cfg.update(overrides) + return cfg + + +class TestBubbleChartConfigSchema: + """BubbleChartConfig schema validation.""" + + def test_basic_bubble_config(self) -> None: + config = BubbleChartConfig(**_base()) + assert config.entity.name == "country" + assert config.x.name == "gdp" + assert config.series is None # series grouping is optional + assert config.row_limit == 10000 # shared control default + + @pytest.mark.parametrize("missing", ["entity", "x", "y", "size"]) + def test_bubble_missing_required(self, missing: str) -> None: + cfg = _base() + del cfg[missing] + with pytest.raises(ValidationError): + BubbleChartConfig(**cfg) + + def test_bubble_rejects_extra_fields(self) -> None: + with pytest.raises(ValidationError): + BubbleChartConfig(**_base(bogus=1)) + + def test_bubble_entity_rejects_saved_metric(self) -> None: + with pytest.raises(ValidationError): + BubbleChartConfig(**_base(entity={"name": "c", "saved_metric": True})) + + def test_bubble_series_rejects_saved_metric(self) -> None: + with pytest.raises(ValidationError): + BubbleChartConfig(**_base(series={"name": "c", "saved_metric": True})) + + def test_bubble_entity_rejects_aggregate(self) -> None: + """An aggregate makes entity metric-like; entity is a dimension.""" + with pytest.raises(ValidationError): + BubbleChartConfig(**_base(entity={"name": "country", "aggregate": "SUM"})) + + def test_bubble_series_rejects_aggregate(self) -> None: + with pytest.raises(ValidationError): + BubbleChartConfig( + **_base(series={"name": "continent", "aggregate": "COUNT"}) + ) + + def test_bubble_x_accepts_saved_metric(self) -> None: + """A saved metric is a valid x/y/size value.""" + config = BubbleChartConfig( + **_base(x={"name": "gdp_index", "saved_metric": True}) + ) + assert config.x.saved_metric is True + + def test_chart_config_union_dispatches_bubble(self) -> None: + config = TypeAdapter(ChartConfig).validate_python(_base()) + assert isinstance(config, BubbleChartConfig) + + +class TestMapBubbleConfig: + """form_data mapping must match the frontend Bubble buildQuery.""" + + def test_basic_bubble_form_data(self) -> None: + config = BubbleChartConfig(**_base()) + form_data = map_bubble_config(config) + assert form_data["viz_type"] == "bubble_v2" + assert form_data["entity"] == "country" + # x/y/size are three separate metric keys (not a metrics array) + assert form_data["x"]["label"] == "AVG(gdp)" + assert form_data["y"]["label"] == "AVG(life_expectancy)" + assert form_data["size"]["label"] == "SUM(population)" + assert form_data["row_limit"] == 10000 + assert "series" not in form_data # omitted when not set + + def test_bubble_form_data_with_series_and_filters(self) -> None: + config = BubbleChartConfig( + **_base( + series={"name": "continent"}, + filters=[{"column": "year", "op": "=", "value": 2026}], + ) + ) + form_data = map_bubble_config(config) + assert form_data["series"] == "continent" + assert form_data["adhoc_filters"], "filters must map to adhoc_filters" Review Comment: <div> <div id="suggestion"> <div id="issue"><b>Weak filter assertion</b></div> <div id="fix"> This only checks `adhoc_filters` is non-empty; `_add_adhoc_filters` builds a specific {clause, expressionType, subject, operator, comparator} dict from `config.filters`, and none of that mapping is verified. A regression in `map_filter_operator` or the subject mapping would still pass. Assert the full filter dict. </div> </div> <small><i>Code Review Run #b7102a</i></small> </div> --- Should Bito avoid suggestions like this for future reviews? (<a href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>) - [ ] Yes, avoid them -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
