codeant-ai-for-open-source[bot] commented on code in PR #40960: URL: https://github.com/apache/superset/pull/40960#discussion_r3476981994
########## superset/mcp_service/dashboard/tool/manage_native_filters.py: ########## @@ -0,0 +1,450 @@ +# 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. + +""" +MCP tool: manage_native_filters + +Adds, updates, removes, and reorders native filters on a dashboard by +translating high-level operations into the ``deleted`` / ``modified`` / +``reordered`` payload consumed by ``UpdateDashboardNativeFiltersCommand``. +""" + +import copy +import logging +from typing import Any + +from fastmcp import Context +from superset_core.mcp.decorators import tool, ToolAnnotations + +from superset.extensions import event_logger +from superset.mcp_service.dashboard.constants import generate_id +from superset.mcp_service.dashboard.schemas import ( + FilterSelectSpec, + FilterTimeSpec, + ManageNativeFiltersRequest, + ManageNativeFiltersResponse, + NativeFilterSummary, + NativeFilterUpdateSpec, +) +from superset.mcp_service.utils.url_utils import get_superset_base_url +from superset.utils import json + +logger = logging.getLogger(__name__) + +# Control values that map to filter_select controlValues keys. +_SELECT_CONTROL_FIELDS: dict[str, str] = { + "multi_select": "multiSelect", + "default_to_first_item": "defaultToFirstItem", + "enable_empty_filter": "enableEmptyFilter", + "sort_ascending": "sortAscending", + "search_all_options": "searchAllOptions", +} + + +class _FilterValidationError(Exception): + """Raised internally when a filter operation fails validation.""" + + +def _empty_data_mask() -> dict[str, Any]: + return {"filterState": {"value": None}, "extraFormData": {}} Review Comment: **Suggestion:** Add a concise docstring to this new helper function to describe the structure and purpose of the empty native filter data mask it returns. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This is a newly added Python function in a new source file, and it has no docstring. The custom rule explicitly requires new functions to be documented inline, so the suggestion identifies a real violation. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=793d25eccf1e47d78d129b47b20dea51&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=793d25eccf1e47d78d129b47b20dea51&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:** superset/mcp_service/dashboard/tool/manage_native_filters.py **Line:** 62:63 **Comment:** *Custom Rule: Add a concise docstring to this new helper function to describe the structure and purpose of the empty native filter data mask it returns. 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%2F40960&comment_hash=bc5ea17dbd067f48de3ea8dc48f0248d084fd15c283e50f0ebb3a20c6599fdf5&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40960&comment_hash=bc5ea17dbd067f48de3ea8dc48f0248d084fd15c283e50f0ebb3a20c6599fdf5&reaction=dislike'>👎</a> ########## superset/mcp_service/dashboard/tool/manage_native_filters.py: ########## @@ -0,0 +1,450 @@ +# 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. + +""" +MCP tool: manage_native_filters + +Adds, updates, removes, and reorders native filters on a dashboard by +translating high-level operations into the ``deleted`` / ``modified`` / +``reordered`` payload consumed by ``UpdateDashboardNativeFiltersCommand``. +""" + +import copy +import logging +from typing import Any + +from fastmcp import Context +from superset_core.mcp.decorators import tool, ToolAnnotations + +from superset.extensions import event_logger +from superset.mcp_service.dashboard.constants import generate_id +from superset.mcp_service.dashboard.schemas import ( + FilterSelectSpec, + FilterTimeSpec, + ManageNativeFiltersRequest, + ManageNativeFiltersResponse, + NativeFilterSummary, + NativeFilterUpdateSpec, +) +from superset.mcp_service.utils.url_utils import get_superset_base_url +from superset.utils import json + +logger = logging.getLogger(__name__) + +# Control values that map to filter_select controlValues keys. +_SELECT_CONTROL_FIELDS: dict[str, str] = { + "multi_select": "multiSelect", + "default_to_first_item": "defaultToFirstItem", + "enable_empty_filter": "enableEmptyFilter", + "sort_ascending": "sortAscending", + "search_all_options": "searchAllOptions", +} + + +class _FilterValidationError(Exception): + """Raised internally when a filter operation fails validation.""" + + +def _empty_data_mask() -> dict[str, Any]: + return {"filterState": {"value": None}, "extraFormData": {}} + + +def _time_data_mask(default_time_range: str | None) -> dict[str, Any]: + if not default_time_range: + return _empty_data_mask() + return { + "filterState": {"value": default_time_range}, + "extraFormData": {"time_range": default_time_range}, + } Review Comment: **Suggestion:** Add a docstring to this new function explaining how `default_time_range` affects the generated data mask and when it falls back to the empty mask. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This newly added function lacks a docstring in the final file state. Since the rule requires new Python functions to include docstrings, the suggestion correctly flags a genuine violation. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=ccc3c719fd394dcf878b243dca2dc0f0&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=ccc3c719fd394dcf878b243dca2dc0f0&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:** superset/mcp_service/dashboard/tool/manage_native_filters.py **Line:** 66:72 **Comment:** *Custom Rule: Add a docstring to this new function explaining how `default_time_range` affects the generated data mask and when it falls back to the empty mask. 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%2F40960&comment_hash=ce4d10ec921f3eaeb4f7519e3db39621cdfe9602d3b2cc38e824b5993688f4dd&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40960&comment_hash=ce4d10ec921f3eaeb4f7519e3db39621cdfe9602d3b2cc38e824b5993688f4dd&reaction=dislike'>👎</a> ########## tests/unit_tests/mcp_service/dashboard/tool/test_manage_native_filters.py: ########## @@ -0,0 +1,588 @@ +# 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 manage_native_filters MCP tool. + +Follows the pattern from 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: +- Adding a filter_select filter (full config shape, scope translation) +- Adding a filter_time filter (with default time range) +- Updating a filter (merge produces a FULL config, not a delta) +- Removing a filter +- Reordering filters (including incomplete-reorder validation) +- Invalid dataset / column errors +- Dashboard not found +- Permission denied (DashboardForbiddenError) +""" + +import logging +from typing import Any +from unittest.mock import Mock, patch + +import pytest +from fastmcp import Client + +from superset.commands.dashboard.exceptions import DashboardForbiddenError +from superset.mcp_service.app import mcp +from superset.utils import json + +logging.basicConfig(level=logging.DEBUG) +logger = logging.getLogger(__name__) + +DAO_FIND_BY_ID = "superset.daos.dashboard.DashboardDAO.find_by_id" +DATASET_FIND_BY_ID = "superset.daos.dataset.DatasetDAO.find_by_id" +COMMAND_PATH = "superset.commands.dashboard.update.UpdateDashboardNativeFiltersCommand" + + [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(): Review Comment: **Suggestion:** Add an explicit return type annotation to this fixture function so it complies with the full typing requirement for new Python code. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This is new Python code and the fixture function has no return type annotation. The custom rule requires newly added Python functions to be fully typed, so this is a real violation. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=e9db8e700aa240bcb224d7cd2fa1a7e9&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=e9db8e700aa240bcb224d7cd2fa1a7e9&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_manage_native_filters.py **Line:** 62:63 **Comment:** *Custom Rule: Add an explicit return type annotation to this fixture function so it complies with the full typing requirement for new Python code. 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%2F40960&comment_hash=3dc7be556860629e09173908d974399298f5d3f551e7ab528019273313c613e3&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40960&comment_hash=3dc7be556860629e09173908d974399298f5d3f551e7ab528019273313c613e3&reaction=dislike'>👎</a> ########## superset/mcp_service/dashboard/tool/manage_native_filters.py: ########## @@ -0,0 +1,450 @@ +# 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. + +""" +MCP tool: manage_native_filters + +Adds, updates, removes, and reorders native filters on a dashboard by +translating high-level operations into the ``deleted`` / ``modified`` / +``reordered`` payload consumed by ``UpdateDashboardNativeFiltersCommand``. +""" + +import copy +import logging +from typing import Any + +from fastmcp import Context +from superset_core.mcp.decorators import tool, ToolAnnotations + +from superset.extensions import event_logger +from superset.mcp_service.dashboard.constants import generate_id +from superset.mcp_service.dashboard.schemas import ( + FilterSelectSpec, + FilterTimeSpec, + ManageNativeFiltersRequest, + ManageNativeFiltersResponse, + NativeFilterSummary, + NativeFilterUpdateSpec, +) +from superset.mcp_service.utils.url_utils import get_superset_base_url +from superset.utils import json + +logger = logging.getLogger(__name__) + +# Control values that map to filter_select controlValues keys. +_SELECT_CONTROL_FIELDS: dict[str, str] = { + "multi_select": "multiSelect", + "default_to_first_item": "defaultToFirstItem", + "enable_empty_filter": "enableEmptyFilter", + "sort_ascending": "sortAscending", + "search_all_options": "searchAllOptions", +} + + +class _FilterValidationError(Exception): + """Raised internally when a filter operation fails validation.""" + + +def _empty_data_mask() -> dict[str, Any]: + return {"filterState": {"value": None}, "extraFormData": {}} + + +def _time_data_mask(default_time_range: str | None) -> dict[str, Any]: + if not default_time_range: + return _empty_data_mask() + return { + "filterState": {"value": default_time_range}, + "extraFormData": {"time_range": default_time_range}, + } + + +def _validate_dataset_column(dataset_id: int, column: str) -> None: + """Validate that the dataset exists and contains the given column.""" + from superset.daos.dataset import DatasetDAO + + dataset = DatasetDAO.find_by_id(dataset_id) + if not dataset: + raise _FilterValidationError( + f"Dataset with ID {dataset_id} not found." + " Use list_datasets to get valid dataset IDs." + ) + column_names = [c.column_name for c in dataset.columns] + if column not in column_names: + raise _FilterValidationError( + f"Column '{column}' not found in dataset {dataset_id}. " + f"Available columns: {', '.join(sorted(column_names))}." + ) + + +def _build_scope( + scope_chart_ids: list[int] | None, + dashboard_chart_ids: list[int], +) -> dict[str, Any]: + """Translate scope_chart_ids into the frontend scope structure. + + The frontend expresses scope as an exclusion list, so charts NOT in + ``scope_chart_ids`` are excluded. When ``scope_chart_ids`` is None + the filter applies to all charts (empty exclusion list). + """ + if scope_chart_ids is None: + return {"rootPath": ["ROOT_ID"], "excluded": []} + unknown = sorted(set(scope_chart_ids) - set(dashboard_chart_ids)) + if unknown: + raise _FilterValidationError( + f"scope_chart_ids contains chart IDs not on the dashboard: " + f"{unknown}. Charts on this dashboard: {sorted(dashboard_chart_ids)}." + ) + excluded = sorted(set(dashboard_chart_ids) - set(scope_chart_ids)) + return {"rootPath": ["ROOT_ID"], "excluded": excluded} + + +def _build_new_filter_config( + spec: FilterSelectSpec | FilterTimeSpec, + dashboard_chart_ids: list[int], +) -> dict[str, Any]: + """Build a full native filter config dict for a new filter.""" + scope = _build_scope(spec.scope_chart_ids, dashboard_chart_ids) + filter_id = generate_id("NATIVE_FILTER") + + if isinstance(spec, FilterSelectSpec): + _validate_dataset_column(spec.dataset_id, spec.column) + control_values: dict[str, Any] = { + "multiSelect": spec.multi_select, + "defaultToFirstItem": spec.default_to_first_item, + "enableEmptyFilter": spec.enable_empty_filter, + "searchAllOptions": spec.search_all_options, + } + if spec.sort_ascending is not None: + control_values["sortAscending"] = spec.sort_ascending + return { + "id": filter_id, + "type": "NATIVE_FILTER", + "filterType": "filter_select", + "name": spec.name, + "description": spec.description, + "scope": scope, + "targets": [ + {"datasetId": spec.dataset_id, "column": {"name": spec.column}} + ], + "controlValues": control_values, + "defaultDataMask": _empty_data_mask(), + "cascadeParentIds": [], + } + + # filter_time: no dataset target, empty controlValues + return { + "id": filter_id, + "type": "NATIVE_FILTER", + "filterType": "filter_time", + "name": spec.name, + "description": spec.description, + "scope": scope, + "targets": [{}], + "controlValues": {}, + "defaultDataMask": _time_data_mask(spec.default_time_range), + "cascadeParentIds": [], + } + + +def _validate_update_type_compat( + spec: NativeFilterUpdateSpec, filter_type: str | None +) -> None: + """Reject update fields that do not apply to the filter's type.""" + select_fields_set = [ + field + for field in (*_SELECT_CONTROL_FIELDS, "dataset_id", "column") + if getattr(spec, field) is not None + ] + if filter_type != "filter_select" and select_fields_set: + raise _FilterValidationError( + f"Filter '{spec.id}' has type '{filter_type}'; fields " + f"{select_fields_set} only apply to filter_select filters." + ) + if filter_type != "filter_time" and spec.default_time_range is not None: + raise _FilterValidationError( + f"Filter '{spec.id}' has type '{filter_type}'; default_time_range " + "only applies to filter_time filters." + ) + + +def _merge_target(spec: NativeFilterUpdateSpec, merged: dict[str, Any]) -> None: + """Merge dataset_id / column changes into the filter's first target.""" + targets = merged.get("targets") or [{}] + target = dict(targets[0]) if targets else {} + dataset_id = ( + spec.dataset_id if spec.dataset_id is not None else target.get("datasetId") + ) + column = ( + spec.column + if spec.column is not None + else (target.get("column") or {}).get("name") + ) + if dataset_id is None or not column: + raise _FilterValidationError( + f"Filter '{spec.id}' is missing a dataset or column target; " + "provide both dataset_id and column to set the target." + ) + _validate_dataset_column(dataset_id, column) + target["datasetId"] = dataset_id + target["column"] = {"name": column} + merged["targets"] = [target] + + +def _merge_filter_update( + spec: NativeFilterUpdateSpec, + existing: dict[str, Any], + dashboard_chart_ids: list[int], +) -> dict[str, Any]: + """Merge a partial update into an existing filter config. + + Returns a FULL filter config (the backend command substitutes whole + entries, it does not merge deltas). + """ + merged = copy.deepcopy(existing) + _validate_update_type_compat(spec, merged.get("filterType")) + + if spec.name is not None: + merged["name"] = spec.name + if spec.description is not None: + merged["description"] = spec.description + if spec.scope_chart_ids is not None: + merged["scope"] = _build_scope(spec.scope_chart_ids, dashboard_chart_ids) + if spec.dataset_id is not None or spec.column is not None: + _merge_target(spec, merged) + + control_values = dict(merged.get("controlValues") or {}) + for field, control_key in _SELECT_CONTROL_FIELDS.items(): + value = getattr(spec, field) + if value is not None: + control_values[control_key] = value + merged["controlValues"] = control_values + + if spec.default_time_range is not None: + merged["defaultDataMask"] = _time_data_mask(spec.default_time_range) + + return merged + + +def _filter_summary(conf: dict[str, Any]) -> NativeFilterSummary: + return NativeFilterSummary( + id=conf.get("id"), + name=conf.get("name"), + filter_type=conf.get("filterType"), + targets=[t for t in (conf.get("targets") or []) if t], + ) Review Comment: **Suggestion:** Add a short docstring to this new function describing what fields are included in the returned summary object and why target entries are filtered. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This is a new function introduced in the PR and it does not have a docstring. That matches the custom rule requiring new Python functions and classes to be documented inline. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=b0aac88a5adb427981f2c9b297ece4c8&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=b0aac88a5adb427981f2c9b297ece4c8&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:** superset/mcp_service/dashboard/tool/manage_native_filters.py **Line:** 242:248 **Comment:** *Custom Rule: Add a short docstring to this new function describing what fields are included in the returned summary object and why target entries are filtered. 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%2F40960&comment_hash=0ddad4cf3c3561fecfbb37f2c73f3e22094ce2434a01b2142b0ac124baf9c919&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40960&comment_hash=0ddad4cf3c3561fecfbb37f2c73f3e22094ce2434a01b2142b0ac124baf9c919&reaction=dislike'>👎</a> ########## tests/unit_tests/mcp_service/dashboard/tool/test_manage_native_filters.py: ########## @@ -0,0 +1,588 @@ +# 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 manage_native_filters MCP tool. + +Follows the pattern from 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: +- Adding a filter_select filter (full config shape, scope translation) +- Adding a filter_time filter (with default time range) +- Updating a filter (merge produces a FULL config, not a delta) +- Removing a filter +- Reordering filters (including incomplete-reorder validation) +- Invalid dataset / column errors +- Dashboard not found +- Permission denied (DashboardForbiddenError) +""" + +import logging +from typing import Any +from unittest.mock import Mock, patch + +import pytest +from fastmcp import Client + +from superset.commands.dashboard.exceptions import DashboardForbiddenError +from superset.mcp_service.app import mcp +from superset.utils import json + +logging.basicConfig(level=logging.DEBUG) +logger = logging.getLogger(__name__) + +DAO_FIND_BY_ID = "superset.daos.dashboard.DashboardDAO.find_by_id" +DATASET_FIND_BY_ID = "superset.daos.dataset.DatasetDAO.find_by_id" +COMMAND_PATH = "superset.commands.dashboard.update.UpdateDashboardNativeFiltersCommand" + + [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 +# --------------------------------------------------------------------------- + + +EXISTING_SELECT_FILTER = { + "id": "NATIVE_FILTER-existing1", + "type": "NATIVE_FILTER", + "filterType": "filter_select", + "name": "Region", + "description": "", + "scope": {"rootPath": ["ROOT_ID"], "excluded": []}, + "targets": [{"datasetId": 5, "column": {"name": "region"}}], + "controlValues": { + "multiSelect": True, + "defaultToFirstItem": False, + "enableEmptyFilter": False, + "searchAllOptions": False, + }, + "defaultDataMask": {"filterState": {"value": None}, "extraFormData": {}}, + "cascadeParentIds": [], +} + +EXISTING_TIME_FILTER = { + "id": "NATIVE_FILTER-existing2", + "type": "NATIVE_FILTER", + "filterType": "filter_time", + "name": "Time Range", + "description": "", + "scope": {"rootPath": ["ROOT_ID"], "excluded": []}, + "targets": [{}], + "controlValues": {}, + "defaultDataMask": {"filterState": {"value": None}, "extraFormData": {}}, + "cascadeParentIds": [], +} + + +def _mock_dashboard( + id: int = 1, + filters: list[dict[str, Any]] | None = None, + chart_ids: list[int] | None = None, +) -> Mock: + dashboard = Mock() + dashboard.id = id + dashboard.dashboard_title = "Test Dashboard" + dashboard.json_metadata = json.dumps({"native_filter_configuration": filters or []}) + slices = [] + for chart_id in chart_ids or [10, 11]: + slc = Mock() + slc.id = chart_id + slices.append(slc) + dashboard.slices = slices + return dashboard + + +def _mock_dataset(columns: list[str] | None = None) -> Mock: + dataset = Mock() + dataset.id = 5 + cols = [] + for name in columns or ["region", "country", "ds"]: + col = Mock() + col.column_name = name + cols.append(col) + dataset.columns = cols + return dataset + + +def _mock_command(captured: dict[str, Any]): Review Comment: **Suggestion:** Add a return type annotation to this helper so the function signature is fully typed. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> The helper is newly added and lacks a return type annotation. Since new Python code should be fully typed, this matches the rule violation. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=6881a4579da4454c9ad65ef28243f592&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=6881a4579da4454c9ad65ef28243f592&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_manage_native_filters.py **Line:** 140:140 **Comment:** *Custom Rule: Add a return type annotation to this helper so the function signature is 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%2F40960&comment_hash=fade10e1a1e11a3350464853e719143fed71f9aa0c0d3f3664a937c2e1f9b28a&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40960&comment_hash=fade10e1a1e11a3350464853e719143fed71f9aa0c0d3f3664a937c2e1f9b28a&reaction=dislike'>👎</a> ########## tests/unit_tests/mcp_service/dashboard/tool/test_manage_native_filters.py: ########## @@ -0,0 +1,588 @@ +# 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 manage_native_filters MCP tool. + +Follows the pattern from 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: +- Adding a filter_select filter (full config shape, scope translation) +- Adding a filter_time filter (with default time range) +- Updating a filter (merge produces a FULL config, not a delta) +- Removing a filter +- Reordering filters (including incomplete-reorder validation) +- Invalid dataset / column errors +- Dashboard not found +- Permission denied (DashboardForbiddenError) +""" + +import logging +from typing import Any +from unittest.mock import Mock, patch + +import pytest +from fastmcp import Client + +from superset.commands.dashboard.exceptions import DashboardForbiddenError +from superset.mcp_service.app import mcp +from superset.utils import json + +logging.basicConfig(level=logging.DEBUG) +logger = logging.getLogger(__name__) + +DAO_FIND_BY_ID = "superset.daos.dashboard.DashboardDAO.find_by_id" +DATASET_FIND_BY_ID = "superset.daos.dataset.DatasetDAO.find_by_id" +COMMAND_PATH = "superset.commands.dashboard.update.UpdateDashboardNativeFiltersCommand" + + [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 +# --------------------------------------------------------------------------- + + +EXISTING_SELECT_FILTER = { + "id": "NATIVE_FILTER-existing1", + "type": "NATIVE_FILTER", + "filterType": "filter_select", + "name": "Region", + "description": "", + "scope": {"rootPath": ["ROOT_ID"], "excluded": []}, + "targets": [{"datasetId": 5, "column": {"name": "region"}}], + "controlValues": { + "multiSelect": True, + "defaultToFirstItem": False, + "enableEmptyFilter": False, + "searchAllOptions": False, + }, + "defaultDataMask": {"filterState": {"value": None}, "extraFormData": {}}, + "cascadeParentIds": [], +} + +EXISTING_TIME_FILTER = { + "id": "NATIVE_FILTER-existing2", + "type": "NATIVE_FILTER", + "filterType": "filter_time", + "name": "Time Range", + "description": "", + "scope": {"rootPath": ["ROOT_ID"], "excluded": []}, + "targets": [{}], + "controlValues": {}, + "defaultDataMask": {"filterState": {"value": None}, "extraFormData": {}}, + "cascadeParentIds": [], +} + + +def _mock_dashboard( + id: int = 1, + filters: list[dict[str, Any]] | None = None, + chart_ids: list[int] | None = None, +) -> Mock: + dashboard = Mock() + dashboard.id = id + dashboard.dashboard_title = "Test Dashboard" + dashboard.json_metadata = json.dumps({"native_filter_configuration": filters or []}) + slices = [] + for chart_id in chart_ids or [10, 11]: + slc = Mock() + slc.id = chart_id + slices.append(slc) + dashboard.slices = slices + return dashboard + + +def _mock_dataset(columns: list[str] | None = None) -> Mock: + dataset = Mock() + dataset.id = 5 + cols = [] + for name in columns or ["region", "country", "ds"]: + col = Mock() + col.column_name = name + cols.append(col) + dataset.columns = cols + return dataset + + +def _mock_command(captured: dict[str, Any]): + """Build a mock UpdateDashboardNativeFiltersCommand class. + + Captures constructor args and returns the modified configuration + the way the real DAO would (existing filters with substitutions, + new filters appended, deletions removed). + """ + + def command_factory(dashboard_id, payload): Review Comment: **Suggestion:** Add parameter and return type annotations to this nested factory function to satisfy the requirement that new functions are fully typed. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This nested factory function is part of the newly added Python code and has neither parameter types nor a return type. That is a direct violation of the typing requirement for new Python code. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=fb0fabe969154011bc31dae58850e57e&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=fb0fabe969154011bc31dae58850e57e&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_manage_native_filters.py **Line:** 148:148 **Comment:** *Custom Rule: Add parameter and return type annotations to this nested factory function to satisfy the requirement that new 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%2F40960&comment_hash=2e2a15ed51ce159cf663b36f570af4b97ac53a90fde40744c9f9ec15990fa4ce&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40960&comment_hash=2e2a15ed51ce159cf663b36f570af4b97ac53a90fde40744c9f9ec15990fa4ce&reaction=dislike'>👎</a> ########## tests/unit_tests/mcp_service/dashboard/tool/test_manage_native_filters.py: ########## @@ -0,0 +1,588 @@ +# 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 manage_native_filters MCP tool. + +Follows the pattern from 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: +- Adding a filter_select filter (full config shape, scope translation) +- Adding a filter_time filter (with default time range) +- Updating a filter (merge produces a FULL config, not a delta) +- Removing a filter +- Reordering filters (including incomplete-reorder validation) +- Invalid dataset / column errors +- Dashboard not found +- Permission denied (DashboardForbiddenError) +""" + +import logging +from typing import Any +from unittest.mock import Mock, patch + +import pytest +from fastmcp import Client + +from superset.commands.dashboard.exceptions import DashboardForbiddenError +from superset.mcp_service.app import mcp +from superset.utils import json + +logging.basicConfig(level=logging.DEBUG) +logger = logging.getLogger(__name__) + +DAO_FIND_BY_ID = "superset.daos.dashboard.DashboardDAO.find_by_id" +DATASET_FIND_BY_ID = "superset.daos.dataset.DatasetDAO.find_by_id" +COMMAND_PATH = "superset.commands.dashboard.update.UpdateDashboardNativeFiltersCommand" + + [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 +# --------------------------------------------------------------------------- + + +EXISTING_SELECT_FILTER = { + "id": "NATIVE_FILTER-existing1", + "type": "NATIVE_FILTER", + "filterType": "filter_select", + "name": "Region", + "description": "", + "scope": {"rootPath": ["ROOT_ID"], "excluded": []}, + "targets": [{"datasetId": 5, "column": {"name": "region"}}], + "controlValues": { + "multiSelect": True, + "defaultToFirstItem": False, + "enableEmptyFilter": False, + "searchAllOptions": False, + }, + "defaultDataMask": {"filterState": {"value": None}, "extraFormData": {}}, + "cascadeParentIds": [], +} + +EXISTING_TIME_FILTER = { + "id": "NATIVE_FILTER-existing2", + "type": "NATIVE_FILTER", + "filterType": "filter_time", + "name": "Time Range", + "description": "", + "scope": {"rootPath": ["ROOT_ID"], "excluded": []}, + "targets": [{}], + "controlValues": {}, + "defaultDataMask": {"filterState": {"value": None}, "extraFormData": {}}, + "cascadeParentIds": [], +} + + +def _mock_dashboard( + id: int = 1, + filters: list[dict[str, Any]] | None = None, + chart_ids: list[int] | None = None, +) -> Mock: + dashboard = Mock() + dashboard.id = id + dashboard.dashboard_title = "Test Dashboard" + dashboard.json_metadata = json.dumps({"native_filter_configuration": filters or []}) + slices = [] + for chart_id in chart_ids or [10, 11]: + slc = Mock() + slc.id = chart_id + slices.append(slc) + dashboard.slices = slices + return dashboard + + +def _mock_dataset(columns: list[str] | None = None) -> Mock: + dataset = Mock() + dataset.id = 5 + cols = [] + for name in columns or ["region", "country", "ds"]: + col = Mock() + col.column_name = name + cols.append(col) + dataset.columns = cols + return dataset + + +def _mock_command(captured: dict[str, Any]): + """Build a mock UpdateDashboardNativeFiltersCommand class. + + Captures constructor args and returns the modified configuration + the way the real DAO would (existing filters with substitutions, + new filters appended, deletions removed). + """ + + def command_factory(dashboard_id, payload): + captured["dashboard_id"] = dashboard_id + captured["payload"] = payload + + command = Mock() + + def run(): + current = captured.get("current_config", []) + deleted = payload.get("deleted", []) + modified = payload.get("modified", []) + result = [] + for conf in current: + if conf["id"] in deleted: + continue + replacement = next((m for m in modified if m["id"] == conf["id"]), None) + result.append(replacement if replacement else conf) + for m in modified: + if m["id"] not in [c["id"] for c in result]: + result.append(m) + if reordered := list(payload.get("reordered", [])): + for m in modified: + if m["id"] not in reordered: + reordered.append(m["id"]) + by_id = {c["id"]: c for c in result} + result = [by_id[fid] for fid in reordered if fid in by_id] + captured["result"] = result + return result + + command.run = run + return command + + return command_factory + + +async def _call(mcp_server, request: dict[str, Any]) -> dict[str, Any]: Review Comment: **Suggestion:** Type the `mcp_server` parameter explicitly so this async helper has fully annotated inputs and outputs. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> The helper already has a return type annotation and one typed parameter, but `mcp_server` is untyped. Because the rule requires fully typed new Python functions, this is a genuine violation. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=f7a9f4f7e1244ecd99943bf5c8caa54c&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=f7a9f4f7e1244ecd99943bf5c8caa54c&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_manage_native_filters.py **Line:** 182:182 **Comment:** *Custom Rule: Type the `mcp_server` parameter explicitly so this async helper has fully annotated inputs and outputs. 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%2F40960&comment_hash=8dd076e6bc97b0b8d5e4bf5fb11d8536cc6b30368ddc6b25f4eb75e9cf9cb920&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40960&comment_hash=8dd076e6bc97b0b8d5e4bf5fb11d8536cc6b30368ddc6b25f4eb75e9cf9cb920&reaction=dislike'>👎</a> ########## tests/unit_tests/mcp_service/dashboard/tool/test_manage_native_filters.py: ########## @@ -0,0 +1,588 @@ +# 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 manage_native_filters MCP tool. + +Follows the pattern from 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: +- Adding a filter_select filter (full config shape, scope translation) +- Adding a filter_time filter (with default time range) +- Updating a filter (merge produces a FULL config, not a delta) +- Removing a filter +- Reordering filters (including incomplete-reorder validation) +- Invalid dataset / column errors +- Dashboard not found +- Permission denied (DashboardForbiddenError) +""" + +import logging +from typing import Any +from unittest.mock import Mock, patch + +import pytest +from fastmcp import Client + +from superset.commands.dashboard.exceptions import DashboardForbiddenError +from superset.mcp_service.app import mcp +from superset.utils import json + +logging.basicConfig(level=logging.DEBUG) +logger = logging.getLogger(__name__) + +DAO_FIND_BY_ID = "superset.daos.dashboard.DashboardDAO.find_by_id" +DATASET_FIND_BY_ID = "superset.daos.dataset.DatasetDAO.find_by_id" +COMMAND_PATH = "superset.commands.dashboard.update.UpdateDashboardNativeFiltersCommand" + + [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 +# --------------------------------------------------------------------------- + + +EXISTING_SELECT_FILTER = { + "id": "NATIVE_FILTER-existing1", + "type": "NATIVE_FILTER", + "filterType": "filter_select", + "name": "Region", + "description": "", + "scope": {"rootPath": ["ROOT_ID"], "excluded": []}, + "targets": [{"datasetId": 5, "column": {"name": "region"}}], + "controlValues": { + "multiSelect": True, + "defaultToFirstItem": False, + "enableEmptyFilter": False, + "searchAllOptions": False, + }, + "defaultDataMask": {"filterState": {"value": None}, "extraFormData": {}}, + "cascadeParentIds": [], +} + +EXISTING_TIME_FILTER = { + "id": "NATIVE_FILTER-existing2", + "type": "NATIVE_FILTER", + "filterType": "filter_time", + "name": "Time Range", + "description": "", + "scope": {"rootPath": ["ROOT_ID"], "excluded": []}, + "targets": [{}], + "controlValues": {}, + "defaultDataMask": {"filterState": {"value": None}, "extraFormData": {}}, + "cascadeParentIds": [], +} + + +def _mock_dashboard( + id: int = 1, + filters: list[dict[str, Any]] | None = None, + chart_ids: list[int] | None = None, +) -> Mock: + dashboard = Mock() + dashboard.id = id + dashboard.dashboard_title = "Test Dashboard" + dashboard.json_metadata = json.dumps({"native_filter_configuration": filters or []}) + slices = [] + for chart_id in chart_ids or [10, 11]: + slc = Mock() + slc.id = chart_id + slices.append(slc) + dashboard.slices = slices + return dashboard + + +def _mock_dataset(columns: list[str] | None = None) -> Mock: + dataset = Mock() + dataset.id = 5 + cols = [] + for name in columns or ["region", "country", "ds"]: + col = Mock() + col.column_name = name + cols.append(col) + dataset.columns = cols + return dataset + + +def _mock_command(captured: dict[str, Any]): + """Build a mock UpdateDashboardNativeFiltersCommand class. + + Captures constructor args and returns the modified configuration + the way the real DAO would (existing filters with substitutions, + new filters appended, deletions removed). + """ + + def command_factory(dashboard_id, payload): + captured["dashboard_id"] = dashboard_id + captured["payload"] = payload + + command = Mock() + + def run(): + current = captured.get("current_config", []) + deleted = payload.get("deleted", []) + modified = payload.get("modified", []) + result = [] + for conf in current: + if conf["id"] in deleted: + continue + replacement = next((m for m in modified if m["id"] == conf["id"]), None) + result.append(replacement if replacement else conf) + for m in modified: + if m["id"] not in [c["id"] for c in result]: + result.append(m) + if reordered := list(payload.get("reordered", [])): + for m in modified: + if m["id"] not in reordered: + reordered.append(m["id"]) + by_id = {c["id"]: c for c in result} + result = [by_id[fid] for fid in reordered if fid in by_id] + captured["result"] = result + return result + + command.run = run + return command + + return command_factory + + +async def _call(mcp_server, request: dict[str, Any]) -> dict[str, Any]: + async with Client(mcp_server) as client: + result = await client.call_tool("manage_native_filters", {"request": request}) + return json.loads(result.content[0].text) + + +# --------------------------------------------------------------------------- +# Add +# --------------------------------------------------------------------------- + + [email protected] +async def test_add_filter_select(mcp_server): Review Comment: **Suggestion:** Add an inline docstring to this newly added test function to comply with the rule that new functions should be documented. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This is a newly added test function and it does not include a docstring. The rule explicitly requires new functions and classes to be documented inline, so this is a real violation. </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=2acb7c5a5a1f4b909514cd09082517e8&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=2acb7c5a5a1f4b909514cd09082517e8&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_manage_native_filters.py **Line:** 193:194 **Comment:** *Custom Rule: Add an inline docstring to this newly added test function to comply with the rule that new functions should be documented. 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%2F40960&comment_hash=a8a8e01ad8853e7cacb902bf5071b77bafee77b449f9d33bbbbf1d4fd3dac9f7&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40960&comment_hash=a8a8e01ad8853e7cacb902bf5071b77bafee77b449f9d33bbbbf1d4fd3dac9f7&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]
