codeant-ai-for-open-source[bot] commented on code in PR #40958: URL: https://github.com/apache/superset/pull/40958#discussion_r3476976524
########## tests/unit_tests/mcp_service/dashboard/tool/test_remove_chart_from_dashboard.py: ########## @@ -0,0 +1,632 @@ +# 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 remove_chart_from_dashboard MCP tool. + +Follows the same pattern used in test_add_chart_to_existing_dashboard.py: +- Tool flows run through the async MCP Client (not direct function calls) +- Patches applied at source locations (superset.daos.dashboard.*, etc.) +- auth is mocked via the autouse mock_auth fixture + +Covers: +- Dashboard not found +- Permission denied (user does not own the dashboard) -> permission_denied=True +- Chart not present in the dashboard +- Simple grid removal (chart directly inside a ROW) with empty-row pruning +- Chart inside a COLUMN (sibling survives; lone chart prunes COLUMN + ROW) +- Tabbed layout where the chart appears under multiple tabs +- json_metadata cleanup (expanded_slices, timed_refresh_immune_slices, + filter_scopes) +""" + +import logging +from typing import Any +from unittest.mock import Mock, patch + +import pytest +from fastmcp import Client + +from superset.mcp_service.app import mcp +from superset.mcp_service.dashboard.tool.remove_chart_from_dashboard import ( + _clean_json_metadata, + _remove_chart_from_layout, +) +from superset.utils import json + +logging.basicConfig(level=logging.DEBUG) +logger = logging.getLogger(__name__) + + +# --------------------------------------------------------------------------- +# Fixtures +# --------------------------------------------------------------------------- + + [email protected] +def mcp_server() -> object: + """Return the FastMCP app instance for use in MCP client tests.""" + return mcp + + [email protected](autouse=True) Review Comment: **Suggestion:** Add an explicit return type annotation to this fixture so the new function is fully typed (for example, annotate it as an iterator/generator of the mocked user object). [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This is a newly added Python fixture function and it has no return type annotation. The custom rule requires new Python code to be fully typed, so this is a real type-hint violation. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=4257e3711bc7465a8b3a841b26c353ff&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=4257e3711bc7465a8b3a841b26c353ff&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <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/dashboard/tool/test_remove_chart_from_dashboard.py **Line:** 66:66 **Comment:** *Custom Rule: Add an explicit return type annotation to this fixture so the new function is fully typed (for example, annotate it as an iterator/generator of the mocked user object). 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. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40958&comment_hash=942c7972f8e93ec2594de801f750d21145c4660a545780f5a8d92a94248135cb&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40958&comment_hash=942c7972f8e93ec2594de801f750d21145c4660a545780f5a8d92a94248135cb&reaction=dislike'>👎</a> ########## tests/unit_tests/mcp_service/dashboard/tool/test_remove_chart_from_dashboard.py: ########## @@ -0,0 +1,632 @@ +# 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 remove_chart_from_dashboard MCP tool. + +Follows the same pattern used in test_add_chart_to_existing_dashboard.py: +- Tool flows run through the async MCP Client (not direct function calls) +- Patches applied at source locations (superset.daos.dashboard.*, etc.) +- auth is mocked via the autouse mock_auth fixture + +Covers: +- Dashboard not found +- Permission denied (user does not own the dashboard) -> permission_denied=True +- Chart not present in the dashboard +- Simple grid removal (chart directly inside a ROW) with empty-row pruning +- Chart inside a COLUMN (sibling survives; lone chart prunes COLUMN + ROW) +- Tabbed layout where the chart appears under multiple tabs +- json_metadata cleanup (expanded_slices, timed_refresh_immune_slices, + filter_scopes) +""" + +import logging +from typing import Any +from unittest.mock import Mock, patch + +import pytest +from fastmcp import Client + +from superset.mcp_service.app import mcp +from superset.mcp_service.dashboard.tool.remove_chart_from_dashboard import ( + _clean_json_metadata, + _remove_chart_from_layout, +) +from superset.utils import json + +logging.basicConfig(level=logging.DEBUG) +logger = logging.getLogger(__name__) + + +# --------------------------------------------------------------------------- +# Fixtures +# --------------------------------------------------------------------------- + + [email protected] +def mcp_server() -> object: + """Return the FastMCP app instance for use in MCP client tests.""" + return mcp + + [email protected](autouse=True) +def mock_auth(): + """Mock authentication for all tests.""" + with patch("superset.mcp_service.auth.get_user_from_request") as mock_get_user: + mock_user = Mock() + mock_user.id = 1 + mock_user.username = "admin" + mock_get_user.return_value = mock_user + yield mock_get_user + + +# --------------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------------- + + +def _mock_chart(id: int = 10, slice_name: str = "Test Chart") -> Mock: + """Create a minimal mock Slice object with the given ID and name.""" + chart = Mock() + chart.id = id + chart.slice_name = slice_name + chart.uuid = f"chart-uuid-{id}" + chart.tags = [] + chart.owners = [] + chart.viz_type = "table" + chart.datasource_name = None + chart.description = None + return chart + + +def _mock_dashboard( + id: int = 1, + title: str = "Sales Dashboard", + slices: list[Mock] | None = None, + position_json: str = "{}", + json_metadata: str | None = None, +) -> Mock: + """Create a minimal mock Dashboard object.""" + dashboard = Mock() + dashboard.id = id + dashboard.dashboard_title = title + dashboard.slug = f"test-dashboard-{id}" + dashboard.description = None + dashboard.published = True + dashboard.created_on = None + dashboard.changed_on = None + dashboard.created_by_name = "test_user" + dashboard.changed_by_name = "test_user" + dashboard.uuid = f"dashboard-uuid-{id}" + dashboard.slices = slices or [] + dashboard.owners = [] + dashboard.tags = [] + dashboard.roles = [] + dashboard.position_json = position_json + dashboard.json_metadata = json_metadata + dashboard.css = None + dashboard.certified_by = None + dashboard.certification_details = None + dashboard.is_managed_externally = False + dashboard.external_url = None + return dashboard + + +def _chart_node(key: str, chart_id: int, parents: list[str]) -> dict[str, Any]: Review Comment: **Suggestion:** Add a short docstring describing what this helper returns and how the input arguments are used. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This is a newly added Python helper function and it does not include a docstring. The custom rule explicitly requires new functions and classes to be documented inline, so the omission is a real violation. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=488683a792794cc4a61303ad1f463da1&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=488683a792794cc4a61303ad1f463da1&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <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/dashboard/tool/test_remove_chart_from_dashboard.py **Line:** 129:129 **Comment:** *Custom Rule: Add a short docstring describing what this helper returns and how the input arguments are used. 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. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40958&comment_hash=59883414b6569f83e9d85fdf5c55fca2d05c83c0467a9b1665e115a481a77d6d&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40958&comment_hash=59883414b6569f83e9d85fdf5c55fca2d05c83c0467a9b1665e115a481a77d6d&reaction=dislike'>👎</a> ########## tests/unit_tests/mcp_service/dashboard/tool/test_remove_chart_from_dashboard.py: ########## @@ -0,0 +1,632 @@ +# 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 remove_chart_from_dashboard MCP tool. + +Follows the same pattern used in test_add_chart_to_existing_dashboard.py: +- Tool flows run through the async MCP Client (not direct function calls) +- Patches applied at source locations (superset.daos.dashboard.*, etc.) +- auth is mocked via the autouse mock_auth fixture + +Covers: +- Dashboard not found +- Permission denied (user does not own the dashboard) -> permission_denied=True +- Chart not present in the dashboard +- Simple grid removal (chart directly inside a ROW) with empty-row pruning +- Chart inside a COLUMN (sibling survives; lone chart prunes COLUMN + ROW) +- Tabbed layout where the chart appears under multiple tabs +- json_metadata cleanup (expanded_slices, timed_refresh_immune_slices, + filter_scopes) +""" + +import logging +from typing import Any +from unittest.mock import Mock, patch + +import pytest +from fastmcp import Client + +from superset.mcp_service.app import mcp +from superset.mcp_service.dashboard.tool.remove_chart_from_dashboard import ( + _clean_json_metadata, + _remove_chart_from_layout, +) +from superset.utils import json + +logging.basicConfig(level=logging.DEBUG) +logger = logging.getLogger(__name__) + + +# --------------------------------------------------------------------------- +# Fixtures +# --------------------------------------------------------------------------- + + [email protected] +def mcp_server() -> object: + """Return the FastMCP app instance for use in MCP client tests.""" + return mcp + + [email protected](autouse=True) +def mock_auth(): + """Mock authentication for all tests.""" + with patch("superset.mcp_service.auth.get_user_from_request") as mock_get_user: + mock_user = Mock() + mock_user.id = 1 + mock_user.username = "admin" + mock_get_user.return_value = mock_user + yield mock_get_user + + +# --------------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------------- + + +def _mock_chart(id: int = 10, slice_name: str = "Test Chart") -> Mock: + """Create a minimal mock Slice object with the given ID and name.""" + chart = Mock() + chart.id = id + chart.slice_name = slice_name + chart.uuid = f"chart-uuid-{id}" + chart.tags = [] + chart.owners = [] + chart.viz_type = "table" + chart.datasource_name = None + chart.description = None + return chart + + +def _mock_dashboard( + id: int = 1, + title: str = "Sales Dashboard", + slices: list[Mock] | None = None, + position_json: str = "{}", + json_metadata: str | None = None, +) -> Mock: + """Create a minimal mock Dashboard object.""" + dashboard = Mock() + dashboard.id = id + dashboard.dashboard_title = title + dashboard.slug = f"test-dashboard-{id}" + dashboard.description = None + dashboard.published = True + dashboard.created_on = None + dashboard.changed_on = None + dashboard.created_by_name = "test_user" + dashboard.changed_by_name = "test_user" + dashboard.uuid = f"dashboard-uuid-{id}" + dashboard.slices = slices or [] + dashboard.owners = [] + dashboard.tags = [] + dashboard.roles = [] + dashboard.position_json = position_json + dashboard.json_metadata = json_metadata + dashboard.css = None + dashboard.certified_by = None + dashboard.certification_details = None + dashboard.is_managed_externally = False + dashboard.external_url = None + return dashboard + + +def _chart_node(key: str, chart_id: int, parents: list[str]) -> dict[str, Any]: + return { + "children": [], + "id": key, + "meta": {"chartId": chart_id, "height": 50, "width": 4}, + "parents": parents, + "type": "CHART", + } + + +def _simple_grid_layout() -> dict[str, Any]: + """ROOT > GRID > [ROW-1 > CHART-10, ROW-2 > CHART-20].""" + return { + "DASHBOARD_VERSION_KEY": "v2", + "ROOT_ID": {"children": ["GRID_ID"], "id": "ROOT_ID", "type": "ROOT"}, + "GRID_ID": { + "children": ["ROW-1", "ROW-2"], + "id": "GRID_ID", + "parents": ["ROOT_ID"], + "type": "GRID", + }, + "ROW-1": { + "children": ["CHART-aaa"], + "id": "ROW-1", + "meta": {}, + "parents": ["ROOT_ID", "GRID_ID"], + "type": "ROW", + }, + "CHART-aaa": _chart_node("CHART-aaa", 10, ["ROOT_ID", "GRID_ID", "ROW-1"]), + "ROW-2": { + "children": ["CHART-bbb"], + "id": "ROW-2", + "meta": {}, + "parents": ["ROOT_ID", "GRID_ID"], + "type": "ROW", + }, + "CHART-bbb": _chart_node("CHART-bbb", 20, ["ROOT_ID", "GRID_ID", "ROW-2"]), + } + + +def _column_layout(column_children: list[tuple[str, int]]) -> dict[str, Any]: + """ROOT > GRID > ROW-1 > COLUMN-1 > [charts].""" + layout = { + "DASHBOARD_VERSION_KEY": "v2", + "ROOT_ID": {"children": ["GRID_ID"], "id": "ROOT_ID", "type": "ROOT"}, + "GRID_ID": { + "children": ["ROW-1"], + "id": "GRID_ID", + "parents": ["ROOT_ID"], + "type": "GRID", + }, + "ROW-1": { + "children": ["COLUMN-1"], + "id": "ROW-1", + "meta": {}, + "parents": ["ROOT_ID", "GRID_ID"], + "type": "ROW", + }, + "COLUMN-1": { + "children": [key for key, _ in column_children], + "id": "COLUMN-1", + "meta": {}, + "parents": ["ROOT_ID", "GRID_ID", "ROW-1"], + "type": "COLUMN", + }, + } + for key, chart_id in column_children: + layout[key] = _chart_node( + key, chart_id, ["ROOT_ID", "GRID_ID", "ROW-1", "COLUMN-1"] + ) + return layout + + +def _tabbed_layout() -> dict[str, Any]: + """ROOT > TABS > [TAB-1 > ROW-1 > CHART(10), TAB-2 > ROW-2 > CHART(10)].""" + return { + "DASHBOARD_VERSION_KEY": "v2", + "ROOT_ID": {"children": ["TABS-1"], "id": "ROOT_ID", "type": "ROOT"}, + "TABS-1": { + "children": ["TAB-1", "TAB-2"], + "id": "TABS-1", + "meta": {}, + "parents": ["ROOT_ID"], + "type": "TABS", + }, + "TAB-1": { + "children": ["ROW-1"], + "id": "TAB-1", + "meta": {"text": "First"}, + "parents": ["ROOT_ID", "TABS-1"], + "type": "TAB", + }, + "ROW-1": { + "children": ["CHART-aaa"], + "id": "ROW-1", + "meta": {}, + "parents": ["ROOT_ID", "TABS-1", "TAB-1"], + "type": "ROW", + }, + "CHART-aaa": _chart_node( + "CHART-aaa", 10, ["ROOT_ID", "TABS-1", "TAB-1", "ROW-1"] + ), + "TAB-2": { + "children": ["ROW-2"], + "id": "TAB-2", + "meta": {"text": "Second"}, + "parents": ["ROOT_ID", "TABS-1"], + "type": "TAB", + }, + "ROW-2": { + "children": ["CHART-ccc", "CHART-bbb"], + "id": "ROW-2", + "meta": {}, + "parents": ["ROOT_ID", "TABS-1", "TAB-2"], + "type": "ROW", + }, + "CHART-ccc": _chart_node( + "CHART-ccc", 10, ["ROOT_ID", "TABS-1", "TAB-2", "ROW-2"] + ), + "CHART-bbb": _chart_node( + "CHART-bbb", 20, ["ROOT_ID", "TABS-1", "TAB-2", "ROW-2"] + ), + } + + +async def _call_remove( + mcp_server: object, dashboard_id: int = 1, chart_id: int = 10 +) -> dict[str, Any]: Review Comment: **Suggestion:** Add a docstring to describe this async helper's behavior and its structured response payload. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This newly added async helper has type hints, but it does not have a docstring. The custom rule requires new Python functions to include docstrings, so this is a genuine violation. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=009d8a5e405740f9837be52f005a1a45&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=009d8a5e405740f9837be52f005a1a45&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <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/dashboard/tool/test_remove_chart_from_dashboard.py **Line:** 254:256 **Comment:** *Custom Rule: Add a docstring to describe this async helper's behavior and its structured response payload. 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. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40958&comment_hash=1ccad3913efb98791db6513cce429e4b06b704b39b7c33c7f58cdd4357acd2bb&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40958&comment_hash=1ccad3913efb98791db6513cce429e4b06b704b39b7c33c7f58cdd4357acd2bb&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]
