codeant-ai-for-open-source[bot] commented on code in PR #41497: URL: https://github.com/apache/superset/pull/41497#discussion_r3545892327
########## tests/unit_tests/mcp_service/theme/tool/test_get_theme_info.py: ########## @@ -0,0 +1,133 @@ +# 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 get_theme_info MCP tool.""" + +from collections.abc import Iterator +from unittest.mock import MagicMock, Mock, patch + +import pytest +from fastmcp import Client + +from superset.mcp_service.app import mcp +from superset.utils import json + + +def create_mock_theme( + theme_id: int = 1, + theme_name: str = "Corporate Blue", + uuid: str = "11111111-1111-1111-1111-111111111111", +) -> MagicMock: + theme = MagicMock() + theme.id = theme_id + theme.theme_name = theme_name + theme.json_data = '{"token": {"colorPrimary": "#1d4ed8"}}' + theme.uuid = uuid + theme.is_system = False + theme.is_system_default = False + theme.is_system_dark = False + theme.changed_on = None + theme.created_on = None + return theme + + [email protected] +def mcp_server() -> object: + return mcp + + [email protected](autouse=True) +def mock_auth() -> Iterator[Mock]: + 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 + + +@patch("superset.daos.theme.ThemeDAO.find_by_id") [email protected] +async def test_get_theme_info_by_id_success( + mock_find: MagicMock, mcp_server: object +) -> None: + """Returns ThemeInfo when the theme is found by numeric ID.""" + mock_find.return_value = create_mock_theme() + async with Client(mcp_server) as client: + result = await client.call_tool( + "get_theme_info", {"request": {"identifier": 1}} + ) + data = json.loads(result.content[0].text) + assert data["id"] == 1 + assert "Corporate Blue" in data["theme_name"] + assert "colorPrimary" in data["json_data"] + + +@patch("superset.daos.theme.ThemeDAO.find_by_id") [email protected] +async def test_get_theme_info_by_uuid_success( + mock_find: MagicMock, mcp_server: object +) -> None: + """Returns ThemeInfo when the theme is found by UUID string.""" + uuid = "11111111-1111-1111-1111-111111111111" + mock_find.return_value = create_mock_theme(uuid=uuid) + async with Client(mcp_server) as client: + result = await client.call_tool( + "get_theme_info", {"request": {"identifier": uuid}} + ) + data = json.loads(result.content[0].text) + assert data["id"] == 1 + # Confirm UUID lookup path was used (id_column="uuid"). + assert any( + call.kwargs.get("id_column") == "uuid" for call in mock_find.call_args_list + ) + + +@patch("superset.daos.theme.ThemeDAO.find_by_id", return_value=None) [email protected] +async def test_get_theme_info_not_found( + mock_find: MagicMock, mcp_server: object +) -> None: + """Returns a ThemeError when the theme is not found.""" + async with Client(mcp_server) as client: + result = await client.call_tool( + "get_theme_info", {"request": {"identifier": 999}} + ) + data = json.loads(result.content[0].text) + assert data["error_type"] == "not_found" + assert "999" in data["error"] + + +@patch("superset.daos.theme.ThemeDAO.find_by_id") [email protected] +async def test_get_theme_info_wraps_json_data( + mock_find: MagicMock, mcp_server: object +) -> None: + """json_data token values are user-controlled text; the whole JSON string + must come back wrapped as an untrusted block, like theme_name.""" + theme = create_mock_theme() Review Comment: **Suggestion:** Add an explicit type annotation for this local variable holding the mocked theme object. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> The variable is a local assignment to a helper return value and is not annotated. Under the stated rule, this is a missing type hint on an annotatable variable. </details> <details> <summary><b>Rule source 📖 </b></summary> .cursor/rules/dev-standard.mdc (line 28) </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=38512513f6c247dc986ab22fb03eb37a&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=38512513f6c247dc986ab22fb03eb37a&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/theme/tool/test_get_theme_info.py **Line:** 122:122 **Comment:** *Custom Rule: Add an explicit type annotation for this local variable holding the mocked theme 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%2F41497&comment_hash=d47a06dab0e08b1c30147daac79d65d1111285b1b3f5ca1fa97a47fb63b2210c&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41497&comment_hash=d47a06dab0e08b1c30147daac79d65d1111285b1b3f5ca1fa97a47fb63b2210c&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]
