aminghadersohi commented on code in PR #38403: URL: https://github.com/apache/superset/pull/38403#discussion_r2886227933
########## tests/unit_tests/mcp_service/chart/test_big_number_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 Big Number chart type support in MCP service.""" + +import pytest + +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 + assert config.header_only is True + + 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.header_only is False + assert config.temporal_column == "ds" + + def test_trendline_without_temporal_column_fails(self) -> None: + with pytest.raises(ValueError, 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: Review Comment: The prior fix commit (6023b7fc) already changed the test assertions from `ValueError` to `ValidationError`. Tests now correctly catch Pydantic's `ValidationError` for invalid config. ########## superset/mcp_service/chart/chart_utils.py: ########## @@ -567,6 +570,53 @@ def map_xy_config( return form_data +def map_big_number_config(config: BigNumberChartConfig) -> Dict[str, Any]: + """Map big number chart config to Superset form_data.""" + # Determine viz_type: big_number (with trendline) or big_number_total + if config.show_trendline and config.temporal_column: + viz_type = "big_number" + else: + viz_type = "big_number_total" + + metric = create_metric_object(config.metric) + form_data: Dict[str, Any] = { + "viz_type": viz_type, + "metric": metric, + } + + if config.subheader: + form_data["subheader"] = config.subheader + + if config.y_axis_format: + form_data["y_axis_format"] = config.y_axis_format + + # Trendline-specific fields + if viz_type == "big_number": + form_data["x_axis"] = config.temporal_column + form_data["start_y_axis_at_zero"] = config.start_y_axis_at_zero + + if config.time_grain: + form_data["time_grain_sqla"] = config.time_grain + + if config.compare_lag is not None: + form_data["compare_lag"] = config.compare_lag + Review Comment: Fixed in the latest push. Removed `form_data["x_axis"]` entirely for big number charts — only `granularity_sqla` is set for the trendline temporal column, which is what big_number/big_number_total visualizations actually use. -- 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]
