codeant-ai-for-open-source[bot] commented on code in PR #38403: URL: https://github.com/apache/superset/pull/38403#discussion_r3016382600
########## tests/unit_tests/mcp_service/chart/test_big_number_chart.py: ########## @@ -0,0 +1,514 @@ +# 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 Big Number chart type support in MCP service.""" + +import pytest +from pydantic import ValidationError + +from superset.mcp_service.chart.chart_utils import ( + _resolve_viz_type, + analyze_chart_capabilities, + analyze_chart_semantics, + generate_chart_name, + map_big_number_config, + map_config_to_form_data, +) +from superset.mcp_service.chart.schemas import ( + BigNumberChartConfig, + ColumnRef, + FilterConfig, +) +from superset.mcp_service.chart.validation.schema_validator import ( + SchemaValidator, +) + + +class TestBigNumberChartConfig: + """Test BigNumberChartConfig Pydantic schema.""" + + def test_minimal_config(self) -> None: + config = BigNumberChartConfig( + chart_type="big_number", + metric=ColumnRef(name="revenue", aggregate="SUM"), + ) + assert config.chart_type == "big_number" + assert config.metric.name == "revenue" + assert config.metric.aggregate == "SUM" + assert config.show_trendline is False + + def test_with_trendline(self) -> None: + config = BigNumberChartConfig( + chart_type="big_number", + metric=ColumnRef(name="revenue", aggregate="SUM"), + temporal_column="ds", + show_trendline=True, + ) + assert config.show_trendline is True + assert config.temporal_column == "ds" + + def test_trendline_without_temporal_column_fails(self) -> None: + with pytest.raises(ValidationError, match="requires 'temporal_column'"): + BigNumberChartConfig( + chart_type="big_number", + metric=ColumnRef(name="revenue", aggregate="SUM"), + show_trendline=True, + ) + + def test_metric_without_aggregate_fails(self) -> None: + with pytest.raises(ValidationError, match="saved dataset metric"): + BigNumberChartConfig( + chart_type="big_number", + metric=ColumnRef(name="revenue"), + ) + + def test_saved_metric_accepted(self) -> None: + config = BigNumberChartConfig( + chart_type="big_number", + metric=ColumnRef(name="total_sales", saved_metric=True), + ) + assert config.metric.saved_metric is True + assert config.metric.is_metric is True Review Comment: **Suggestion:** This test only validates `BigNumberChartConfig` directly, but the MCP request path goes through `SchemaValidator` pre-validation, which is where saved-metric requests can still be rejected. Replace this with a request-level validation test so saved metric support is verified in the real integration path and runtime request failures are caught. [logic error] <details> <summary><b>Severity Level:</b> Major ⚠️</summary> ```mdx - ❌ generate_chart rejects big_number requests using saved metrics. - ⚠️ MCP clients cannot reuse dataset saved metrics for KPIs. - ⚠️ BigNumberChartConfig schema and pre-validation disagree on metrics. ``` </details> ```suggestion data = { "dataset_id": 1, "config": { "chart_type": "big_number", "metric": {"name": "total_sales", "saved_metric": True}, }, } is_valid, request, error = SchemaValidator.validate_request(data) assert is_valid is True assert request is not None assert error is None ``` <details> <summary><b>Steps of Reproduction ✅ </b></summary> ```mdx 1. In an MCP-enabled Superset environment, call the `generate_chart` tool (entrypoint at `superset/mcp_service/chart/tool/generate_chart.py`, `async def generate_chart(...)` shown in lines 6–66 of the tool output) with a request payload where `config.chart_type` is `"big_number"` and `config.metric` is a saved metric without an aggregate, e.g.: `{"dataset_id": 1, "config": {"chart_type": "big_number", "metric": {"name": "total_sales", "saved_metric": true}}}`. 2. The MCP tooling constructs a `GenerateChartRequest` (schema defined in `superset/mcp_service/chart/schemas.py` lines 12–24 of the tool output) where `config` is a `BigNumberChartConfig` and `metric` is a `ColumnRef` with `saved_metric=True`; `BigNumberChartConfig.validate_metric_aggregate` (same file, lines 51–60 of the BigNumber section) accepts this because `ColumnRef.is_metric` returns True when `saved_metric` is set. 3. Inside `generate_chart`, the code calls `ValidationPipeline.validate_request_with_warnings(request.model_dump())` (see `generate_chart.py` lines 85–96), which in turn calls `SchemaValidator.validate_request` (`superset/mcp_service/chart/validation/schema_validator.py` lines 39–61). That method dispatches to `_pre_validate_big_number_config` via `_pre_validate_chart_type` (same file lines 145–181). 4. `_pre_validate_big_number_config` (in `schema_validator.py` lines 1–65 of the focused snippet) reads `metric = config.get("metric", {})` and then unconditionally enforces `if not metric.get("aggregate"):` returning a `ChartGenerationError` with `error_code="MISSING_BIG_NUMBER_AGGREGATE"` (lines 35–47), because it does not check `saved_metric`. As a result, the MCP `generate_chart` tool sees `validation_result.is_valid` as False (generate_chart.py lines 110–118) and returns an error response instead of creating the Big Number chart, even though the Pydantic `BigNumberChartConfig` and `ColumnRef` schemas accept saved metrics. The current test `test_saved_metric_accepted` in `tests/unit_tests/mcp_service/chart/test_big_number_chart.py` only instantiates `BigNumberChartConfig` directly (lines 20–26 of the test file snippet) and never exercises this request/validation path, so this runtime failure is not covered. ``` </details> <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** tests/unit_tests/mcp_service/chart/test_big_number_chart.py **Line:** 80:85 **Comment:** *Logic Error: This test only validates `BigNumberChartConfig` directly, but the MCP request path goes through `SchemaValidator` pre-validation, which is where saved-metric requests can still be rejected. Replace this with a request-level validation test so saved metric support is verified in the real integration path and runtime request failures are caught. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F38403&comment_hash=133ee67f92434b5800446f6d45a1fca77fc31bf51e95af47e74ecfa588496ac7&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F38403&comment_hash=133ee67f92434b5800446f6d45a1fca77fc31bf51e95af47e74ecfa588496ac7&reaction=dislike'>👎</a> -- 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]
