aminghadersohi commented on code in PR #40958:
URL: https://github.com/apache/superset/pull/40958#discussion_r3477191780


##########
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:
   Fixed in af9ac74: added `-> Generator[Mock, None, None]` return type 
annotation.



##########
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:
   Fixed in af9ac74: added a one-line docstring.



##########
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:
   Fixed in af9ac74: added a one-line docstring.



-- 
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]

Reply via email to