codeant-ai-for-open-source[bot] commented on code in PR #40957: URL: https://github.com/apache/superset/pull/40957#discussion_r3417008494
########## tests/unit_tests/mcp_service/dashboard/tool/test_update_dashboard.py: ########## @@ -0,0 +1,620 @@ +# 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 the update_dashboard MCP tool. + +Follows the same pattern used in test_add_chart_to_existing_dashboard.py: +- Tests 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 +- No fields provided -> error +- Successful direct-field updates (title, publish/certification, roles/tags) +- json_metadata merge preserves existing keys (the set_dash_metadata gotcha) +- Command failure -> error response +- Schema-level validation (title sanitization, filter_bar_orientation literal) +""" + +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.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_dashboard( + id: int = 1, + title: str = "Sales Dashboard", + 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.uuid = f"dashboard-uuid-{id}" + dashboard.slices = [] + dashboard.owners = [] + dashboard.tags = [] + dashboard.roles = [] + dashboard.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 + + +async def _call_update(mcp_server: object, request: dict[str, Any]) -> dict[str, Any]: + async with Client(mcp_server) as client: + result = await client.call_tool("update_dashboard", {"request": request}) + return result.structured_content Review Comment: **Suggestion:** Add a docstring to this new helper function to document its behavior and satisfy the requirement that newly added functions are documented inline. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This is a newly added function and it does not include a docstring. The stated rule explicitly requires newly added Python functions and classes to be documented inline, so this is a genuine violation. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=2c475d9667e5461e922cda8b0e69519b&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=2c475d9667e5461e922cda8b0e69519b&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_update_dashboard.py **Line:** 106:109 **Comment:** *Custom Rule: Add a docstring to this new helper function to document its behavior and satisfy the requirement that newly added functions are documented inline. 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%2F40957&comment_hash=ff1c91d6c87fc404393f3023060ca42416b14999be2874558d95e8e7e6808698&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40957&comment_hash=ff1c91d6c87fc404393f3023060ca42416b14999be2874558d95e8e7e6808698&reaction=dislike'>👎</a> ########## tests/unit_tests/mcp_service/dashboard/tool/test_update_dashboard.py: ########## @@ -0,0 +1,620 @@ +# 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 the update_dashboard MCP tool. + +Follows the same pattern used in test_add_chart_to_existing_dashboard.py: +- Tests 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 +- No fields provided -> error +- Successful direct-field updates (title, publish/certification, roles/tags) +- json_metadata merge preserves existing keys (the set_dash_metadata gotcha) +- Command failure -> error response +- Schema-level validation (title sanitization, filter_bar_orientation literal) +""" + +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.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 new fixture function so it complies with the requirement that newly added Python functions are fully typed. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This is a newly added Python function and it omits a return type annotation. The rule requires newly added Python code to be fully typed, so the absence of `-> ...` is a real violation. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=d8ea020431e24f65bd2ec72efb65b608&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=d8ea020431e24f65bd2ec72efb65b608&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_update_dashboard.py **Line:** 61:61 **Comment:** *Custom Rule: Add an explicit return type annotation to this new fixture function so it complies with the requirement that newly added Python functions are fully typed. 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%2F40957&comment_hash=04eecab311edc7f4cd6a4e83a903df694c68901b1223a06ef69fecbd83c34736&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40957&comment_hash=04eecab311edc7f4cd6a4e83a903df694c68901b1223a06ef69fecbd83c34736&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]
