aminghadersohi commented on code in PR #39916: URL: https://github.com/apache/superset/pull/39916#discussion_r3198822843
########## tests/unit_tests/mcp_service/chart/test_ascii_charts.py: ########## @@ -0,0 +1,106 @@ +# 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. + +"""Unit tests for ascii_charts.py NaN/null value handling.""" + +from superset.mcp_service.chart.ascii_charts import generate_ascii_chart + + +def test_bar_chart_with_null_values_does_not_raise() -> None: + """Bar chart renderer must not crash when dataset rows contain NaN values.""" + data = [ + {"category": "A", "value": 10.0}, + {"category": "B", "value": float("nan")}, + {"category": "C", "value": 30.0}, + ] + result = generate_ascii_chart(data, "bar") + assert isinstance(result, str) + assert len(result) > 0 + + +def test_bar_chart_with_all_null_values_returns_fallback() -> None: + """Bar chart with no valid numeric rows should return a fallback message.""" + data = [ + {"category": "A", "value": float("nan")}, + {"category": "B", "value": float("nan")}, + ] + result = generate_ascii_chart(data, "bar") + assert isinstance(result, str) + assert len(result) > 0 + + +def test_line_chart_with_null_values_does_not_raise() -> None: + """Line chart renderer must not crash when dataset rows contain NaN values.""" + data = [ + {"date": "2024-01", "sales": 100.0}, + {"date": "2024-02", "sales": float("nan")}, + {"date": "2024-03", "sales": 300.0}, + ] + result = generate_ascii_chart(data, "line") + assert isinstance(result, str) + assert len(result) > 0 + + +def test_horizontal_bar_chart_nan_rows_are_skipped() -> None: + """NaN rows must be silently skipped; valid rows render normally.""" + # Use labels longer than 8 chars to force horizontal layout, where full + # label text is preserved (vertical layout truncates to 3 chars). + data = [ + {"label": "Alpha Category", "amount": 50.0}, + {"label": "Beta Category", "amount": float("nan")}, + {"label": "Gamma Category", "amount": 150.0}, + ] + result = generate_ascii_chart(data, "bar") Review Comment: Addressed. — agor claude on Amin's behalf ########## superset/mcp_service/chart/ascii_charts.py: ########## @@ -79,7 +79,11 @@ def _generate_ascii_bar_chart(data: list[Any], width: int, height: int) -> str: label_val = None for _key, val in row.items(): - if isinstance(val, (int, float)) and numeric_val is None: + if ( + isinstance(val, (int, float)) + and not _is_nan_value(val) + and numeric_val is None + ): Review Comment: Addressed. — agor claude on Amin's behalf ########## superset/mcp_service/chart/ascii_charts.py: ########## @@ -274,7 +284,11 @@ def _extract_time_series_data(data: list[Any]) -> tuple[list[float], list[str]]: label_val = None for key, val in row.items(): - if isinstance(val, (int, float)) and numeric_val is None: + if ( + isinstance(val, (int, float)) + and not _is_nan_value(val) + and numeric_val is None + ): Review Comment: Addressed. — agor claude on Amin's behalf -- 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]
