codeant-ai-for-open-source[bot] commented on code in PR #41855: URL: https://github.com/apache/superset/pull/41855#discussion_r3565560758
########## tests/unit_tests/mcp_service/dashboard/tool/test_list_dashboards_deleted_state.py: ########## @@ -0,0 +1,132 @@ +# 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 list_dashboards ``deleted_state`` trash listing. + +Mirrors the list_charts deleted_state tests: DAO custom filter pass-through, +session-scoped visibility bypass around the DAO call, and ``deleted_at`` +forced into loaded columns and the serialized response. +""" + +from collections.abc import Iterator +from datetime import datetime +from unittest.mock import MagicMock, Mock, patch +from uuid import UUID + +import pytest +from fastmcp import Client + +from superset.mcp_service.app import mcp +from superset.utils import json + +_DAO_LIST = "superset.daos.dashboard.DashboardDAO.list" Review Comment: ✅ **Customized review instruction saved!** **Instruction:** > Do not flag missing type annotations for constants and locals with inferable types in mcp_service; this pattern is intentional and mypy already passes. **Applied to:** - `superset/mcp_service/**` --- 💡 *To manage or update this instruction, visit: [CodeAnt AI Settings](https://app.codeant.ai/org/settings/learnings)* ########## tests/unit_tests/mcp_service/dashboard/tool/test_list_dashboards_deleted_state.py: ########## @@ -0,0 +1,132 @@ +# 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 list_dashboards ``deleted_state`` trash listing. + +Mirrors the list_charts deleted_state tests: DAO custom filter pass-through, +session-scoped visibility bypass around the DAO call, and ``deleted_at`` +forced into loaded columns and the serialized response. +""" + +from collections.abc import Iterator +from datetime import datetime +from unittest.mock import MagicMock, Mock, patch +from uuid import UUID + +import pytest +from fastmcp import Client + +from superset.mcp_service.app import mcp +from superset.utils import json + +_DAO_LIST = "superset.daos.dashboard.DashboardDAO.list" +_BYPASS = "superset.mcp_service.mcp_core.skip_visibility_filter" + + [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 + + +def _trashed_dashboard_row() -> Mock: + row = Mock( + spec=[ + "id", + "dashboard_title", + "slug", + "published", + "changed_on", + "created_on", + "uuid", + "deleted_at", + ] + ) + row.id = 1 + row.dashboard_title = "Trashed Dashboard" + row.slug = None + row.published = False + row.changed_on = datetime(2026, 6, 1) + row.created_on = datetime(2026, 5, 1) + row.uuid = UUID("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee") + row.deleted_at = datetime(2026, 7, 1) + return row + + +@patch(_DAO_LIST) [email protected] +async def test_list_dashboards_deleted_state_only_passes_custom_filter( + mock_list: Mock, mcp_server: object +) -> None: + mock_list.return_value = ([], 0) + + async with Client(mcp_server) as client: + await client.call_tool( + "list_dashboards", {"request": {"deleted_state": "only"}} + ) + + kwargs = mock_list.call_args.kwargs + assert "deleted_state" in (kwargs.get("custom_filters") or {}) + assert "deleted_at" in kwargs["columns"] + + +@patch(_BYPASS) +@patch(_DAO_LIST) [email protected] +async def test_list_dashboards_deleted_state_wraps_visibility_bypass( + mock_list: Mock, mock_bypass: MagicMock, mcp_server: object +) -> None: + from superset.models.dashboard import Dashboard + + mock_list.return_value = ([], 0) + mock_bypass.return_value.__enter__ = Mock(return_value=None) + mock_bypass.return_value.__exit__ = Mock(return_value=False) + + async with Client(mcp_server) as client: + await client.call_tool( + "list_dashboards", {"request": {"deleted_state": "include"}} + ) + + mock_bypass.assert_called_once() + assert Dashboard in mock_bypass.call_args.args + + +@patch(_DAO_LIST) [email protected] +async def test_list_dashboards_deleted_state_serializes_deleted_at( + mock_list: Mock, mcp_server: object +) -> None: + mock_list.return_value = ([_trashed_dashboard_row()], 1) + + async with Client(mcp_server) as client: + result = await client.call_tool( + "list_dashboards", {"request": {"deleted_state": "only"}} + ) + + data = json.loads(result.content[0].text) Review Comment: ✅ **Customized review instruction saved!** **Instruction:** > Do not flag unannotated locals or constants when their type is trivially inferable and the codebase's type checks already pass. **Applied to:** - `**/*.py` --- 💡 *To manage or update this instruction, visit: [CodeAnt AI Settings](https://app.codeant.ai/org/settings/learnings)* ########## tests/unit_tests/mcp_service/test_mcp_core.py: ########## @@ -312,3 +312,19 @@ def test_explicit_title_column_overrides_dao_attribute() -> None: ) def test_slugify_handles_edge_cases(identifier: str, expected_slug: str) -> None: assert _slugify(identifier) == expected_slug + + +def test_deleted_state_bound_filter_delegates_bound_value() -> None: + """The adapter passed to DAO custom_filters must forward the bound + deleted_state value — BaseDAO.list invokes custom filters with + ``apply(query, None)``, which the FAB filter would treat as 'live only'.""" + from unittest.mock import Mock + + from superset.mcp_service.mcp_core import DeletedStateBoundFilter + + inner = Mock() + inner.apply.return_value = "filtered-query" + bound = DeletedStateBoundFilter(inner, "only", model=Mock) Review Comment: ✅ **Customized review instruction saved!** **Instruction:** > Do not flag Python local variables or constants with inferable types for missing type annotations when the codebase already follows that pattern and mypy passes. **Applied to:** - `**/*.py` --- 💡 *To manage or update this instruction, visit: [CodeAnt AI Settings](https://app.codeant.ai/org/settings/learnings)* ########## superset/mcp_service/mcp_core.py: ########## @@ -326,17 +381,40 @@ def run_tool( self._validate_order_column(order_column) + deleted_state_bound = self._build_deleted_state_filter(deleted_state) Review Comment: ✅ **Customized review instruction saved!** **Instruction:** > Do not flag unannotated locals or constants when their types are clearly inferable; this codebase intentionally leaves such variables unannotated and mypy passes. **Applied to:** - `superset/mcp_service/**` --- 💡 *To manage or update this instruction, visit: [CodeAnt AI Settings](https://app.codeant.ai/org/settings/learnings)* ########## superset/mcp_service/mcp_core.py: ########## @@ -326,17 +381,40 @@ def run_tool( self._validate_order_column(order_column) + deleted_state_bound = self._build_deleted_state_filter(deleted_state) + if deleted_state_bound is not None: + # Trashed rows must be distinguishable from live ones (matters in + # "include" mode), so force deleted_at into the loaded columns + # and the serialization allowlist. + for column_list in (columns_requested, columns_to_load): + if "deleted_at" not in column_list: + column_list.append("deleted_at") + # Query the DAO items: List[Any] - items, total_count = self._call_dao_list( - filters=filters, - order_column=order_column or "changed_on", - order_direction=str(order_direction or "desc"), - page=page, - page_size=page_size, - search=search, - columns_to_load=columns_to_load, - ) + dao_kwargs = { + "filters": filters, + "order_column": order_column or "changed_on", + "order_direction": str(order_direction or "desc"), + "page": page, + "page_size": page_size, + "search": search, + "columns_to_load": columns_to_load, Review Comment: ✅ **Customized review instruction saved!** **Instruction:** > Do not require explicit type annotations for inferable local variables or constants in Python code when the existing type is clear and mypy passes. **Applied to:** - `**/*.py` --- 💡 *To manage or update this instruction, visit: [CodeAnt AI Settings](https://app.codeant.ai/org/settings/learnings)* -- 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]
