codeant-ai-for-open-source[bot] commented on code in PR #40961: URL: https://github.com/apache/superset/pull/40961#discussion_r3493780827
########## superset/mcp_service/dashboard/tool/get_dashboard_datasets.py: ########## @@ -0,0 +1,156 @@ +# 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. + +""" +Get dashboard datasets FastMCP tool + +Returns the datasets used by a dashboard's charts, including columns and +metrics. This is the prerequisite context an agent needs before configuring +native filters on a dashboard (e.g. picking filter target columns). +""" + +import logging +from datetime import datetime, timezone + +from fastmcp import Context +from sqlalchemy.orm import subqueryload +from superset_core.mcp.decorators import tool, ToolAnnotations + +from superset.extensions import event_logger +from superset.mcp_service.dashboard.schemas import ( + dashboard_datasets_serializer, + DashboardDatasets, + DashboardError, + GetDashboardDatasetsRequest, +) +from superset.mcp_service.mcp_core import ModelGetInfoCore +from superset.mcp_service.privacy import ( + DATA_MODEL_METADATA_ERROR_TYPE, + requires_data_model_metadata_access, + user_can_view_data_model_metadata, +) + +logger = logging.getLogger(__name__) Review Comment: ✅ **Customized review instruction saved!** **Instruction:** > Do not require explicit type annotations for module-level logger variables in dashboard tool modules; keep the existing unannotated `logger = logging.getLogger(__name__)` pattern for consistency. **Applied to:** - `superset/mcp_service/dashboard/tool/**` --- 💡 *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_get_dashboard_datasets.py: ########## @@ -0,0 +1,387 @@ +# 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 MCP get_dashboard_datasets tool.""" + +from unittest.mock import Mock, patch + +import pytest +from fastmcp import Client + +from superset.mcp_service.app import mcp +from superset.mcp_service.utils.sanitization import ( + LLM_CONTEXT_CLOSE_DELIMITER, + LLM_CONTEXT_OPEN_DELIMITER, +) +from superset.utils import json + + +def _wrapped(value: str) -> str: + return f"{LLM_CONTEXT_OPEN_DELIMITER}\n{value}\n{LLM_CONTEXT_CLOSE_DELIMITER}" + + +def _build_column_mock( + name: str, + *, + verbose_name: str | None = None, + type_: str | None = "VARCHAR", + is_dttm: bool = False, +) -> Mock: + column = Mock() + column.column_name = name + column.verbose_name = verbose_name + column.type = type_ + column.is_dttm = is_dttm + return column + + +def _build_metric_mock( + name: str, + *, + verbose_name: str | None = None, + expression: str | None = None, +) -> Mock: + metric = Mock() + metric.metric_name = name + metric.verbose_name = verbose_name + metric.expression = expression + return metric + + +def _build_database_mock( + *, database_id: int = 7, name: str = "examples", backend: str = "postgresql" +) -> Mock: + database = Mock() + database.id = database_id + database.database_name = name + database.backend = backend + return database + + +def _build_datasource_mock( + *, + dataset_id: int, + uuid: str | None = None, + table_name: str = "my_table", + schema: str | None = "public", + database: Mock | None = None, + columns: list[Mock] | None = None, + metrics: list[Mock] | None = None, +) -> Mock: + datasource = Mock() + datasource.id = dataset_id + datasource.uuid = uuid + datasource.table_name = table_name + datasource.schema = schema + datasource.database = database + datasource.columns = columns or [] + datasource.metrics = metrics or [] + return datasource + + +def _build_slice_mock(datasource: Mock) -> Mock: + slc = Mock() + slc.datasource_id = datasource.id + slc.datasource = datasource + return slc + + +def _build_dashboard_mock( + *, + dashboard_id: int = 1, + title: str = "Test Dashboard", + uuid: str | None = "dashboard-uuid-1", + slices: list[Mock] | None = None, +) -> Mock: + dashboard = Mock() + dashboard.id = dashboard_id + dashboard.dashboard_title = title + dashboard.uuid = uuid + dashboard.slices = slices or [] + return dashboard + + [email protected] +def mcp_server(): + return mcp Review Comment: ✅ **Customized review instruction saved!** **Instruction:** > Keep test helpers and pytest fixtures in the mcp_service dashboard tool tests unannotated to match the existing local convention; do not flag missing type annotations in these test files when mypy already passes. **Applied to:** - `tests/unit_tests/mcp_service/dashboard/tool/**` --- 💡 *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_get_dashboard_datasets.py: ########## @@ -0,0 +1,355 @@ +# 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 MCP get_dashboard_datasets tool.""" + +from unittest.mock import Mock, patch + +import pytest +from fastmcp import Client + +from superset.mcp_service.app import mcp +from superset.mcp_service.utils.sanitization import ( + LLM_CONTEXT_CLOSE_DELIMITER, + LLM_CONTEXT_OPEN_DELIMITER, +) +from superset.utils import json + + +def _wrapped(value: str) -> str: + return f"{LLM_CONTEXT_OPEN_DELIMITER}\n{value}\n{LLM_CONTEXT_CLOSE_DELIMITER}" + + +def _build_column_mock( + name: str, + *, + verbose_name: str | None = None, + type_: str | None = "VARCHAR", + is_dttm: bool = False, +) -> Mock: + column = Mock() + column.column_name = name + column.verbose_name = verbose_name + column.type = type_ + column.is_dttm = is_dttm + return column + + +def _build_metric_mock( + name: str, + *, + verbose_name: str | None = None, + expression: str | None = None, +) -> Mock: + metric = Mock() + metric.metric_name = name + metric.verbose_name = verbose_name + metric.expression = expression + return metric + + +def _build_database_mock( + *, database_id: int = 7, name: str = "examples", backend: str = "postgresql" +) -> Mock: + database = Mock() + database.id = database_id + database.database_name = name + database.backend = backend + return database + + +def _build_datasource_mock( + *, + dataset_id: int, + uuid: str | None = None, + table_name: str = "my_table", + schema: str | None = "public", + database: Mock | None = None, + columns: list[Mock] | None = None, + metrics: list[Mock] | None = None, +) -> Mock: + datasource = Mock() + datasource.id = dataset_id + datasource.uuid = uuid + datasource.table_name = table_name + datasource.schema = schema + datasource.database = database + datasource.columns = columns or [] + datasource.metrics = metrics or [] + return datasource + + +def _build_slice_mock(datasource: Mock) -> Mock: + slc = Mock() + slc.datasource_id = datasource.id + slc.datasource = datasource + return slc + + +def _build_dashboard_mock( + *, + dashboard_id: int = 1, + title: str = "Test Dashboard", + uuid: str | None = "dashboard-uuid-1", + slices: list[Mock] | None = None, +) -> Mock: + dashboard = Mock() + dashboard.id = dashboard_id + dashboard.dashboard_title = title + dashboard.uuid = uuid + dashboard.slices = slices or [] + return dashboard + + [email protected] +def mcp_server(): + return mcp + + [email protected](autouse=True) Review Comment: ✅ **Customized review instruction saved!** **Instruction:** > Do not flag missing type annotations for pytest fixtures and helper functions in mcp_service test files, where the established convention is to keep them unannotated and mypy already passes. **Applied to:** - `tests/unit_tests/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_get_dashboard_datasets.py: ########## @@ -0,0 +1,355 @@ +# 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 MCP get_dashboard_datasets tool.""" + +from unittest.mock import Mock, patch + +import pytest +from fastmcp import Client + +from superset.mcp_service.app import mcp +from superset.mcp_service.utils.sanitization import ( + LLM_CONTEXT_CLOSE_DELIMITER, + LLM_CONTEXT_OPEN_DELIMITER, +) +from superset.utils import json + + +def _wrapped(value: str) -> str: + return f"{LLM_CONTEXT_OPEN_DELIMITER}\n{value}\n{LLM_CONTEXT_CLOSE_DELIMITER}" + + +def _build_column_mock( + name: str, + *, + verbose_name: str | None = None, + type_: str | None = "VARCHAR", + is_dttm: bool = False, +) -> Mock: + column = Mock() + column.column_name = name + column.verbose_name = verbose_name + column.type = type_ + column.is_dttm = is_dttm + return column + + +def _build_metric_mock( + name: str, + *, + verbose_name: str | None = None, + expression: str | None = None, +) -> Mock: + metric = Mock() + metric.metric_name = name + metric.verbose_name = verbose_name + metric.expression = expression + return metric + + +def _build_database_mock( + *, database_id: int = 7, name: str = "examples", backend: str = "postgresql" +) -> Mock: + database = Mock() + database.id = database_id + database.database_name = name + database.backend = backend + return database + + +def _build_datasource_mock( + *, + dataset_id: int, + uuid: str | None = None, + table_name: str = "my_table", + schema: str | None = "public", + database: Mock | None = None, + columns: list[Mock] | None = None, + metrics: list[Mock] | None = None, +) -> Mock: + datasource = Mock() + datasource.id = dataset_id + datasource.uuid = uuid + datasource.table_name = table_name + datasource.schema = schema + datasource.database = database + datasource.columns = columns or [] + datasource.metrics = metrics or [] + return datasource + + +def _build_slice_mock(datasource: Mock) -> Mock: + slc = Mock() + slc.datasource_id = datasource.id + slc.datasource = datasource + return slc + + +def _build_dashboard_mock( + *, + dashboard_id: int = 1, + title: str = "Test Dashboard", + uuid: str | None = "dashboard-uuid-1", + slices: list[Mock] | None = None, +) -> Mock: + dashboard = Mock() + dashboard.id = dashboard_id + dashboard.dashboard_title = title + dashboard.uuid = uuid + dashboard.slices = slices or [] + return dashboard + + [email protected] Review Comment: ✅ **Customized review instruction saved!** **Instruction:** > Do not require explicit return type annotations for pytest fixtures and helper functions in test files when the surrounding test code follows an untyped convention. **Applied to:** - `**/test/**` - `**/tests/**` - `**/*test*.py` --- 💡 *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_get_dashboard_datasets.py: ########## @@ -0,0 +1,387 @@ +# 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 MCP get_dashboard_datasets tool.""" + +from unittest.mock import Mock, patch + +import pytest +from fastmcp import Client + +from superset.mcp_service.app import mcp +from superset.mcp_service.utils.sanitization import ( + LLM_CONTEXT_CLOSE_DELIMITER, + LLM_CONTEXT_OPEN_DELIMITER, +) +from superset.utils import json + + +def _wrapped(value: str) -> str: + return f"{LLM_CONTEXT_OPEN_DELIMITER}\n{value}\n{LLM_CONTEXT_CLOSE_DELIMITER}" + + +def _build_column_mock( + name: str, + *, + verbose_name: str | None = None, + type_: str | None = "VARCHAR", + is_dttm: bool = False, +) -> Mock: + column = Mock() + column.column_name = name + column.verbose_name = verbose_name + column.type = type_ + column.is_dttm = is_dttm + return column + + +def _build_metric_mock( + name: str, + *, + verbose_name: str | None = None, + expression: str | None = None, +) -> Mock: + metric = Mock() + metric.metric_name = name + metric.verbose_name = verbose_name + metric.expression = expression + return metric + + +def _build_database_mock( + *, database_id: int = 7, name: str = "examples", backend: str = "postgresql" +) -> Mock: + database = Mock() + database.id = database_id + database.database_name = name + database.backend = backend + return database + + +def _build_datasource_mock( + *, + dataset_id: int, + uuid: str | None = None, + table_name: str = "my_table", + schema: str | None = "public", + database: Mock | None = None, + columns: list[Mock] | None = None, + metrics: list[Mock] | None = None, +) -> Mock: + datasource = Mock() + datasource.id = dataset_id + datasource.uuid = uuid + datasource.table_name = table_name + datasource.schema = schema + datasource.database = database + datasource.columns = columns or [] + datasource.metrics = metrics or [] + return datasource + + +def _build_slice_mock(datasource: Mock) -> Mock: + slc = Mock() + slc.datasource_id = datasource.id + slc.datasource = datasource + return slc + + +def _build_dashboard_mock( + *, + dashboard_id: int = 1, + title: str = "Test Dashboard", + uuid: str | None = "dashboard-uuid-1", + slices: list[Mock] | None = None, +) -> Mock: + dashboard = Mock() + dashboard.id = dashboard_id + dashboard.dashboard_title = title + dashboard.uuid = uuid + dashboard.slices = slices or [] + return dashboard + + [email protected] +def mcp_server(): + return mcp + + [email protected](autouse=True) +def mock_auth(): + 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 + + [email protected](autouse=True) +def mock_dataset_access(): + with patch( + "superset.mcp_service.auth.has_dataset_access", return_value=True + ) as mock_access: + yield mock_access Review Comment: ✅ **Customized review instruction saved!** **Instruction:** > Do not flag missing return type annotations on pytest fixtures and helper functions in test files; keep them unannotated when that matches the existing test-file convention. **Applied to:** - `**/tests/**` - `**/*test*.py` --- 💡 *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_get_dashboard_datasets.py: ########## @@ -0,0 +1,387 @@ +# 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 MCP get_dashboard_datasets tool.""" + +from unittest.mock import Mock, patch + +import pytest +from fastmcp import Client + +from superset.mcp_service.app import mcp +from superset.mcp_service.utils.sanitization import ( + LLM_CONTEXT_CLOSE_DELIMITER, + LLM_CONTEXT_OPEN_DELIMITER, +) +from superset.utils import json + + +def _wrapped(value: str) -> str: + return f"{LLM_CONTEXT_OPEN_DELIMITER}\n{value}\n{LLM_CONTEXT_CLOSE_DELIMITER}" + + +def _build_column_mock( + name: str, + *, + verbose_name: str | None = None, + type_: str | None = "VARCHAR", + is_dttm: bool = False, +) -> Mock: + column = Mock() + column.column_name = name + column.verbose_name = verbose_name + column.type = type_ + column.is_dttm = is_dttm + return column + + +def _build_metric_mock( + name: str, + *, + verbose_name: str | None = None, + expression: str | None = None, +) -> Mock: + metric = Mock() + metric.metric_name = name + metric.verbose_name = verbose_name + metric.expression = expression + return metric + + +def _build_database_mock( + *, database_id: int = 7, name: str = "examples", backend: str = "postgresql" +) -> Mock: + database = Mock() + database.id = database_id + database.database_name = name + database.backend = backend + return database + + +def _build_datasource_mock( + *, + dataset_id: int, + uuid: str | None = None, + table_name: str = "my_table", + schema: str | None = "public", + database: Mock | None = None, + columns: list[Mock] | None = None, + metrics: list[Mock] | None = None, +) -> Mock: + datasource = Mock() + datasource.id = dataset_id + datasource.uuid = uuid + datasource.table_name = table_name + datasource.schema = schema + datasource.database = database + datasource.columns = columns or [] + datasource.metrics = metrics or [] + return datasource + + +def _build_slice_mock(datasource: Mock) -> Mock: + slc = Mock() + slc.datasource_id = datasource.id + slc.datasource = datasource + return slc + + +def _build_dashboard_mock( + *, + dashboard_id: int = 1, + title: str = "Test Dashboard", + uuid: str | None = "dashboard-uuid-1", + slices: list[Mock] | None = None, +) -> Mock: + dashboard = Mock() + dashboard.id = dashboard_id + dashboard.dashboard_title = title + dashboard.uuid = uuid + dashboard.slices = slices or [] + return dashboard + + [email protected] +def mcp_server(): + return mcp + + [email protected](autouse=True) +def mock_auth(): + 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 + + [email protected](autouse=True) +def mock_dataset_access(): + with patch( + "superset.mcp_service.auth.has_dataset_access", return_value=True + ) as mock_access: + yield mock_access + + [email protected](autouse=True) +def allow_data_model_metadata(): + """Keep tests in the metadata-allowed path unless a test overrides it.""" + with patch( + "superset.mcp_service.dashboard.tool.get_dashboard_datasets." + "user_can_view_data_model_metadata", + return_value=True, + ) as mock_allow: + yield mock_allow + + +@patch("superset.daos.dashboard.DashboardDAO.find_by_id") [email protected] +async def test_get_dashboard_datasets_multiple_datasets(mock_find, mcp_server): Review Comment: ✅ **Customized review instruction saved!** **Instruction:** > Keep pytest fixtures and helper functions in these test files unannotated when that matches the existing sibling test convention and mypy still passes. **Applied to:** - `tests/unit_tests/mcp_service/dashboard/tool/**` --- 💡 *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_get_dashboard_datasets.py: ########## @@ -0,0 +1,387 @@ +# 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 MCP get_dashboard_datasets tool.""" + +from unittest.mock import Mock, patch + +import pytest +from fastmcp import Client + +from superset.mcp_service.app import mcp +from superset.mcp_service.utils.sanitization import ( + LLM_CONTEXT_CLOSE_DELIMITER, + LLM_CONTEXT_OPEN_DELIMITER, +) +from superset.utils import json + + +def _wrapped(value: str) -> str: + return f"{LLM_CONTEXT_OPEN_DELIMITER}\n{value}\n{LLM_CONTEXT_CLOSE_DELIMITER}" + + +def _build_column_mock( + name: str, + *, + verbose_name: str | None = None, + type_: str | None = "VARCHAR", + is_dttm: bool = False, +) -> Mock: + column = Mock() + column.column_name = name + column.verbose_name = verbose_name + column.type = type_ + column.is_dttm = is_dttm + return column + + +def _build_metric_mock( + name: str, + *, + verbose_name: str | None = None, + expression: str | None = None, +) -> Mock: + metric = Mock() + metric.metric_name = name + metric.verbose_name = verbose_name + metric.expression = expression + return metric + + +def _build_database_mock( + *, database_id: int = 7, name: str = "examples", backend: str = "postgresql" +) -> Mock: + database = Mock() + database.id = database_id + database.database_name = name + database.backend = backend + return database + + +def _build_datasource_mock( + *, + dataset_id: int, + uuid: str | None = None, + table_name: str = "my_table", + schema: str | None = "public", + database: Mock | None = None, + columns: list[Mock] | None = None, + metrics: list[Mock] | None = None, +) -> Mock: + datasource = Mock() + datasource.id = dataset_id + datasource.uuid = uuid + datasource.table_name = table_name + datasource.schema = schema + datasource.database = database + datasource.columns = columns or [] + datasource.metrics = metrics or [] + return datasource + + +def _build_slice_mock(datasource: Mock) -> Mock: + slc = Mock() + slc.datasource_id = datasource.id + slc.datasource = datasource + return slc + + +def _build_dashboard_mock( + *, + dashboard_id: int = 1, + title: str = "Test Dashboard", + uuid: str | None = "dashboard-uuid-1", + slices: list[Mock] | None = None, +) -> Mock: + dashboard = Mock() + dashboard.id = dashboard_id + dashboard.dashboard_title = title + dashboard.uuid = uuid + dashboard.slices = slices or [] + return dashboard + + [email protected] +def mcp_server(): + return mcp + + [email protected](autouse=True) +def mock_auth(): + 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 Review Comment: ✅ **Customized review instruction saved!** **Instruction:** > Do not require type annotations for pytest fixtures and helper functions in test files; keep test helpers and fixtures unannotated when they follow the existing test convention. **Applied to:** - `**/test/**` - `**/tests/**` - `**/*test*.py` --- 💡 *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_get_dashboard_datasets.py: ########## @@ -0,0 +1,387 @@ +# 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 MCP get_dashboard_datasets tool.""" + +from unittest.mock import Mock, patch + +import pytest +from fastmcp import Client + +from superset.mcp_service.app import mcp +from superset.mcp_service.utils.sanitization import ( + LLM_CONTEXT_CLOSE_DELIMITER, + LLM_CONTEXT_OPEN_DELIMITER, +) +from superset.utils import json + + +def _wrapped(value: str) -> str: + return f"{LLM_CONTEXT_OPEN_DELIMITER}\n{value}\n{LLM_CONTEXT_CLOSE_DELIMITER}" + + +def _build_column_mock( + name: str, + *, + verbose_name: str | None = None, + type_: str | None = "VARCHAR", + is_dttm: bool = False, +) -> Mock: + column = Mock() + column.column_name = name + column.verbose_name = verbose_name + column.type = type_ + column.is_dttm = is_dttm + return column + + +def _build_metric_mock( + name: str, + *, + verbose_name: str | None = None, + expression: str | None = None, +) -> Mock: + metric = Mock() + metric.metric_name = name + metric.verbose_name = verbose_name + metric.expression = expression + return metric + + +def _build_database_mock( + *, database_id: int = 7, name: str = "examples", backend: str = "postgresql" +) -> Mock: + database = Mock() + database.id = database_id + database.database_name = name + database.backend = backend + return database + + +def _build_datasource_mock( + *, + dataset_id: int, + uuid: str | None = None, + table_name: str = "my_table", + schema: str | None = "public", + database: Mock | None = None, + columns: list[Mock] | None = None, + metrics: list[Mock] | None = None, +) -> Mock: + datasource = Mock() + datasource.id = dataset_id + datasource.uuid = uuid + datasource.table_name = table_name + datasource.schema = schema + datasource.database = database + datasource.columns = columns or [] + datasource.metrics = metrics or [] + return datasource + + +def _build_slice_mock(datasource: Mock) -> Mock: + slc = Mock() + slc.datasource_id = datasource.id + slc.datasource = datasource + return slc + + +def _build_dashboard_mock( + *, + dashboard_id: int = 1, + title: str = "Test Dashboard", + uuid: str | None = "dashboard-uuid-1", + slices: list[Mock] | None = None, +) -> Mock: + dashboard = Mock() + dashboard.id = dashboard_id + dashboard.dashboard_title = title + dashboard.uuid = uuid + dashboard.slices = slices or [] + return dashboard + + [email protected] +def mcp_server(): + return mcp + + [email protected](autouse=True) +def mock_auth(): + 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 + + [email protected](autouse=True) +def mock_dataset_access(): + with patch( + "superset.mcp_service.auth.has_dataset_access", return_value=True + ) as mock_access: + yield mock_access + + [email protected](autouse=True) +def allow_data_model_metadata(): + """Keep tests in the metadata-allowed path unless a test overrides it.""" + with patch( + "superset.mcp_service.dashboard.tool.get_dashboard_datasets." + "user_can_view_data_model_metadata", + return_value=True, + ) as mock_allow: + yield mock_allow + + +@patch("superset.daos.dashboard.DashboardDAO.find_by_id") [email protected] +async def test_get_dashboard_datasets_multiple_datasets(mock_find, mcp_server): + sales = _build_datasource_mock( + dataset_id=10, + uuid="dataset-uuid-10", + table_name="sales", + schema="public", + database=_build_database_mock(), + columns=[ + _build_column_mock("region", verbose_name="Region"), + _build_column_mock("order_date", type_="TIMESTAMP", is_dttm=True), + ], + metrics=[ + _build_metric_mock( + "total_revenue", + verbose_name="Total Revenue", + expression="SUM(revenue)", + ) + ], + ) + customers = _build_datasource_mock( + dataset_id=20, + uuid="dataset-uuid-20", + table_name="customers", + schema="crm", + database=_build_database_mock(database_id=8, name="crm_db", backend="mysql"), + columns=[_build_column_mock("customer_name")], + metrics=[], + ) + mock_find.return_value = _build_dashboard_mock( + slices=[ + _build_slice_mock(sales), + _build_slice_mock(sales), + _build_slice_mock(customers), + ] + ) + + async with Client(mcp_server) as client: + result = await client.call_tool( + "get_dashboard_datasets", {"request": {"identifier": 1}} + ) + data = json.loads(result.content[0].text) + + assert data["id"] == 1 + assert data["dashboard_title"] == _wrapped("Test Dashboard") + assert data["uuid"] == "dashboard-uuid-1" + assert data["dataset_count"] == 2 + assert data["inaccessible_dataset_count"] == 0 + assert len(data["datasets"]) == 2 + + datasets_by_id = {d["id"]: d for d in data["datasets"]} + sales_data = datasets_by_id[10] + assert sales_data["uuid"] == "dataset-uuid-10" + assert sales_data["table_name"] == "sales" + assert sales_data["schema"] == "public" + assert sales_data["database"] == { + "id": 7, + "name": "examples", + "backend": "postgresql", + } + assert sales_data["chart_count"] == 2 + assert sales_data["columns"] == [ + { + "column_name": "region", + "verbose_name": _wrapped("Region"), + "type": "VARCHAR", + "is_dttm": False, + }, + { + "column_name": "order_date", + "verbose_name": None, + "type": "TIMESTAMP", + "is_dttm": True, + }, + ] + assert sales_data["metrics"] == [ + { + "metric_name": "total_revenue", + "verbose_name": _wrapped("Total Revenue"), + "expression": _wrapped("SUM(revenue)"), + } + ] + assert sales_data["total_column_count"] == 2 + assert sales_data["total_metric_count"] == 1 + assert sales_data["columns_truncated"] is False + assert sales_data["metrics_truncated"] is False + + customers_data = datasets_by_id[20] + assert customers_data["table_name"] == "customers" + assert customers_data["schema"] == "crm" + assert customers_data["chart_count"] == 1 + assert customers_data["metrics"] == [] + + +@patch("superset.daos.dashboard.DashboardDAO.find_by_id") [email protected] +async def test_get_dashboard_datasets_by_slug(mock_find, mcp_server): + datasource = _build_datasource_mock( + dataset_id=10, + table_name="sales", + database=_build_database_mock(), + columns=[_build_column_mock("region")], + ) + dashboard = _build_dashboard_mock(slices=[_build_slice_mock(datasource)]) + + def find_by_id(identifier, id_column=None, query_options=None): + if id_column == "slug" and identifier == "sales-dash": + return dashboard + return None Review Comment: ✅ **Customized review instruction saved!** **Instruction:** > Do not flag missing type annotations on test helper functions and fixtures in test files when the project convention is to leave them untyped and mypy passes. **Applied to:** - `**/test/**` - `**/tests/**` - `**/*test*.py` --- 💡 *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_get_dashboard_datasets.py: ########## @@ -0,0 +1,355 @@ +# 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 MCP get_dashboard_datasets tool.""" + +from unittest.mock import Mock, patch + +import pytest +from fastmcp import Client + +from superset.mcp_service.app import mcp +from superset.mcp_service.utils.sanitization import ( + LLM_CONTEXT_CLOSE_DELIMITER, + LLM_CONTEXT_OPEN_DELIMITER, +) +from superset.utils import json + + +def _wrapped(value: str) -> str: + return f"{LLM_CONTEXT_OPEN_DELIMITER}\n{value}\n{LLM_CONTEXT_CLOSE_DELIMITER}" + + +def _build_column_mock( + name: str, + *, + verbose_name: str | None = None, + type_: str | None = "VARCHAR", + is_dttm: bool = False, +) -> Mock: + column = Mock() + column.column_name = name + column.verbose_name = verbose_name + column.type = type_ + column.is_dttm = is_dttm + return column + + +def _build_metric_mock( + name: str, + *, + verbose_name: str | None = None, + expression: str | None = None, +) -> Mock: + metric = Mock() + metric.metric_name = name + metric.verbose_name = verbose_name + metric.expression = expression + return metric + + +def _build_database_mock( + *, database_id: int = 7, name: str = "examples", backend: str = "postgresql" +) -> Mock: + database = Mock() + database.id = database_id + database.database_name = name + database.backend = backend + return database + + +def _build_datasource_mock( + *, + dataset_id: int, + uuid: str | None = None, + table_name: str = "my_table", + schema: str | None = "public", + database: Mock | None = None, + columns: list[Mock] | None = None, + metrics: list[Mock] | None = None, +) -> Mock: + datasource = Mock() + datasource.id = dataset_id + datasource.uuid = uuid + datasource.table_name = table_name + datasource.schema = schema + datasource.database = database + datasource.columns = columns or [] + datasource.metrics = metrics or [] + return datasource + + +def _build_slice_mock(datasource: Mock) -> Mock: + slc = Mock() + slc.datasource_id = datasource.id + slc.datasource = datasource + return slc + + +def _build_dashboard_mock( + *, + dashboard_id: int = 1, + title: str = "Test Dashboard", + uuid: str | None = "dashboard-uuid-1", + slices: list[Mock] | None = None, +) -> Mock: + dashboard = Mock() + dashboard.id = dashboard_id + dashboard.dashboard_title = title + dashboard.uuid = uuid + dashboard.slices = slices or [] + return dashboard + + [email protected] +def mcp_server(): + return mcp + + [email protected](autouse=True) +def mock_auth(): + 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 + + [email protected](autouse=True) +def mock_dataset_access(): + with patch( + "superset.mcp_service.auth.has_dataset_access", return_value=True + ) as mock_access: + yield mock_access + + +@patch("superset.daos.dashboard.DashboardDAO.find_by_id") [email protected] +async def test_get_dashboard_datasets_multiple_datasets(mock_find, mcp_server): + sales = _build_datasource_mock( + dataset_id=10, + uuid="dataset-uuid-10", + table_name="sales", + schema="public", + database=_build_database_mock(), + columns=[ + _build_column_mock("region", verbose_name="Region"), + _build_column_mock("order_date", type_="TIMESTAMP", is_dttm=True), + ], + metrics=[ + _build_metric_mock( + "total_revenue", + verbose_name="Total Revenue", + expression="SUM(revenue)", + ) + ], + ) + customers = _build_datasource_mock( + dataset_id=20, + uuid="dataset-uuid-20", + table_name="customers", + schema="crm", + database=_build_database_mock(database_id=8, name="crm_db", backend="mysql"), + columns=[_build_column_mock("customer_name")], + metrics=[], + ) + mock_find.return_value = _build_dashboard_mock( + slices=[ + _build_slice_mock(sales), + _build_slice_mock(sales), + _build_slice_mock(customers), + ] + ) + + async with Client(mcp_server) as client: + result = await client.call_tool( + "get_dashboard_datasets", {"request": {"identifier": 1}} + ) + data = json.loads(result.content[0].text) + + assert data["id"] == 1 + assert data["dashboard_title"] == _wrapped("Test Dashboard") + assert data["uuid"] == "dashboard-uuid-1" + assert data["dataset_count"] == 2 + assert data["inaccessible_dataset_count"] == 0 + assert len(data["datasets"]) == 2 + + datasets_by_id = {d["id"]: d for d in data["datasets"]} + sales_data = datasets_by_id[10] + assert sales_data["uuid"] == "dataset-uuid-10" + assert sales_data["table_name"] == "sales" + assert sales_data["schema"] == "public" + assert sales_data["database"] == { + "id": 7, + "name": "examples", + "backend": "postgresql", + } + assert sales_data["chart_count"] == 2 + assert sales_data["columns"] == [ + { + "column_name": "region", + "verbose_name": _wrapped("Region"), + "type": "VARCHAR", + "is_dttm": False, + }, + { + "column_name": "order_date", + "verbose_name": None, + "type": "TIMESTAMP", + "is_dttm": True, + }, + ] + assert sales_data["metrics"] == [ + { + "metric_name": "total_revenue", + "verbose_name": _wrapped("Total Revenue"), + "expression": _wrapped("SUM(revenue)"), + } + ] + assert sales_data["total_column_count"] == 2 + assert sales_data["total_metric_count"] == 1 + assert sales_data["columns_truncated"] is False + assert sales_data["metrics_truncated"] is False + + customers_data = datasets_by_id[20] + assert customers_data["table_name"] == "customers" + assert customers_data["schema"] == "crm" + assert customers_data["chart_count"] == 1 + assert customers_data["metrics"] == [] + + +@patch("superset.daos.dashboard.DashboardDAO.find_by_id") [email protected] +async def test_get_dashboard_datasets_by_slug(mock_find, mcp_server): + datasource = _build_datasource_mock( + dataset_id=10, + table_name="sales", + database=_build_database_mock(), + columns=[_build_column_mock("region")], + ) + dashboard = _build_dashboard_mock(slices=[_build_slice_mock(datasource)]) + + def find_by_id(identifier, id_column=None, query_options=None): Review Comment: ✅ **Customized review instruction saved!** **Instruction:** > Do not flag missing type annotations on pytest fixtures or test helper functions in mcp_service test files; these tests intentionally follow the existing untyped convention and already pass mypy. **Applied to:** - `tests/unit_tests/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_get_dashboard_datasets.py: ########## @@ -0,0 +1,355 @@ +# 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 MCP get_dashboard_datasets tool.""" + +from unittest.mock import Mock, patch + +import pytest +from fastmcp import Client + +from superset.mcp_service.app import mcp +from superset.mcp_service.utils.sanitization import ( + LLM_CONTEXT_CLOSE_DELIMITER, + LLM_CONTEXT_OPEN_DELIMITER, +) +from superset.utils import json + + +def _wrapped(value: str) -> str: + return f"{LLM_CONTEXT_OPEN_DELIMITER}\n{value}\n{LLM_CONTEXT_CLOSE_DELIMITER}" + + +def _build_column_mock( + name: str, + *, + verbose_name: str | None = None, + type_: str | None = "VARCHAR", + is_dttm: bool = False, +) -> Mock: + column = Mock() + column.column_name = name + column.verbose_name = verbose_name + column.type = type_ + column.is_dttm = is_dttm + return column + + +def _build_metric_mock( + name: str, + *, + verbose_name: str | None = None, + expression: str | None = None, +) -> Mock: + metric = Mock() + metric.metric_name = name + metric.verbose_name = verbose_name + metric.expression = expression + return metric + + +def _build_database_mock( + *, database_id: int = 7, name: str = "examples", backend: str = "postgresql" +) -> Mock: + database = Mock() + database.id = database_id + database.database_name = name + database.backend = backend + return database + + +def _build_datasource_mock( + *, + dataset_id: int, + uuid: str | None = None, + table_name: str = "my_table", + schema: str | None = "public", + database: Mock | None = None, + columns: list[Mock] | None = None, + metrics: list[Mock] | None = None, +) -> Mock: + datasource = Mock() + datasource.id = dataset_id + datasource.uuid = uuid + datasource.table_name = table_name + datasource.schema = schema + datasource.database = database + datasource.columns = columns or [] + datasource.metrics = metrics or [] + return datasource + + +def _build_slice_mock(datasource: Mock) -> Mock: + slc = Mock() + slc.datasource_id = datasource.id + slc.datasource = datasource + return slc + + +def _build_dashboard_mock( + *, + dashboard_id: int = 1, + title: str = "Test Dashboard", + uuid: str | None = "dashboard-uuid-1", + slices: list[Mock] | None = None, +) -> Mock: + dashboard = Mock() + dashboard.id = dashboard_id + dashboard.dashboard_title = title + dashboard.uuid = uuid + dashboard.slices = slices or [] + return dashboard + + [email protected] +def mcp_server(): + return mcp + + [email protected](autouse=True) +def mock_auth(): + 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 + + [email protected](autouse=True) +def mock_dataset_access(): + with patch( + "superset.mcp_service.auth.has_dataset_access", return_value=True + ) as mock_access: + yield mock_access + + +@patch("superset.daos.dashboard.DashboardDAO.find_by_id") [email protected] +async def test_get_dashboard_datasets_multiple_datasets(mock_find, mcp_server): Review Comment: ✅ **Customized review instruction saved!** **Instruction:** > Do not require type annotations on pytest test functions, fixtures, or helper functions in mcp_service test files when the existing convention is unannotated and mypy passes. **Applied to:** - `tests/unit_tests/mcp_service/**` --- 💡 *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]
