gkneighb commented on code in PR #41497: URL: https://github.com/apache/superset/pull/41497#discussion_r3540831986
########## tests/unit_tests/mcp_service/theme/tool/test_create_theme.py: ########## @@ -0,0 +1,171 @@ +# 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 create_theme MCP tool.""" + +import importlib +from unittest.mock import MagicMock, Mock, patch + +import pytest +from fastmcp import Client +from marshmallow import ValidationError + +from superset.mcp_service.app import mcp +from superset.utils import json + +# Resolve the module object directly so patch.object targets the module, not +# the function re-exported through __init__.py. +create_theme_module = importlib.import_module( + "superset.mcp_service.theme.tool.create_theme" +) + + +def _make_mock_theme( + theme_id: int = 7, + theme_name: str = "Corporate Blue", + uuid: str = "22222222-2222-2222-2222-222222222222", +) -> MagicMock: + theme = MagicMock() + theme.id = theme_id + theme.theme_name = theme_name + theme.uuid = uuid + return theme + + [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: Annotations added in 33ae92251e. ########## tests/unit_tests/mcp_service/theme/tool/test_create_theme.py: ########## @@ -0,0 +1,171 @@ +# 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 create_theme MCP tool.""" + +import importlib +from unittest.mock import MagicMock, Mock, patch + +import pytest +from fastmcp import Client +from marshmallow import ValidationError + +from superset.mcp_service.app import mcp +from superset.utils import json + +# Resolve the module object directly so patch.object targets the module, not +# the function re-exported through __init__.py. +create_theme_module = importlib.import_module( + "superset.mcp_service.theme.tool.create_theme" +) + + +def _make_mock_theme( + theme_id: int = 7, + theme_name: str = "Corporate Blue", + uuid: str = "22222222-2222-2222-2222-222222222222", +) -> MagicMock: + theme = MagicMock() + theme.id = theme_id + theme.theme_name = theme_name + theme.uuid = uuid + return theme + + [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](create_theme_module.db.session, "commit") +@patch("superset.daos.theme.ThemeDAO.create") [email protected](create_theme_module, "_sanitize_and_validate_theme_config") [email protected] +async def test_create_theme_success_with_dict( + mock_sanitize, mock_create, mock_commit, mcp_server +): Review Comment: Annotations added in 33ae92251e. ########## tests/unit_tests/mcp_service/theme/tool/test_create_theme.py: ########## @@ -0,0 +1,171 @@ +# 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 create_theme MCP tool.""" + +import importlib +from unittest.mock import MagicMock, Mock, patch + +import pytest +from fastmcp import Client +from marshmallow import ValidationError + +from superset.mcp_service.app import mcp +from superset.utils import json + +# Resolve the module object directly so patch.object targets the module, not +# the function re-exported through __init__.py. +create_theme_module = importlib.import_module( + "superset.mcp_service.theme.tool.create_theme" +) + + +def _make_mock_theme( + theme_id: int = 7, + theme_name: str = "Corporate Blue", + uuid: str = "22222222-2222-2222-2222-222222222222", +) -> MagicMock: + theme = MagicMock() + theme.id = theme_id + theme.theme_name = theme_name + theme.uuid = uuid + return theme + + [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](create_theme_module.db.session, "commit") +@patch("superset.daos.theme.ThemeDAO.create") [email protected](create_theme_module, "_sanitize_and_validate_theme_config") [email protected] +async def test_create_theme_success_with_dict( + mock_sanitize, mock_create, mock_commit, mcp_server +): + """Happy path: dict json_data is sanitized, persisted, and id/uuid returned.""" + config = {"token": {"colorPrimary": "#1d4ed8"}} + mock_sanitize.return_value = config + mock_create.return_value = _make_mock_theme() + + async with Client(mcp_server) as client: + result = await client.call_tool( + "create_theme", + { + "request": { + "theme_name": "Corporate Blue", + "json_data": config, + } + }, + ) + data = json.loads(result.content[0].text) + + assert data["success"] is True + assert data["id"] == 7 + assert data["uuid"] == "22222222-2222-2222-2222-222222222222" + assert data["theme_name"] == "Corporate Blue" + mock_sanitize.assert_called_once_with(config) + # json_data persisted as a serialized string + create_kwargs = mock_create.call_args.kwargs["attributes"] + assert isinstance(create_kwargs["json_data"], str) + assert json.loads(create_kwargs["json_data"]) == config + mock_commit.assert_called_once() + + [email protected](create_theme_module.db.session, "commit") +@patch("superset.daos.theme.ThemeDAO.create") [email protected](create_theme_module, "_sanitize_and_validate_theme_config") [email protected] +async def test_create_theme_success_with_json_string( + mock_sanitize, mock_create, mock_commit, mcp_server +): Review Comment: Annotations added in 33ae92251e. ########## tests/unit_tests/mcp_service/theme/tool/test_create_theme.py: ########## @@ -0,0 +1,171 @@ +# 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 create_theme MCP tool.""" + +import importlib +from unittest.mock import MagicMock, Mock, patch + +import pytest +from fastmcp import Client +from marshmallow import ValidationError + +from superset.mcp_service.app import mcp +from superset.utils import json + +# Resolve the module object directly so patch.object targets the module, not +# the function re-exported through __init__.py. +create_theme_module = importlib.import_module( + "superset.mcp_service.theme.tool.create_theme" +) + + +def _make_mock_theme( + theme_id: int = 7, + theme_name: str = "Corporate Blue", + uuid: str = "22222222-2222-2222-2222-222222222222", +) -> MagicMock: + theme = MagicMock() + theme.id = theme_id + theme.theme_name = theme_name + theme.uuid = uuid + return theme + + [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](create_theme_module.db.session, "commit") +@patch("superset.daos.theme.ThemeDAO.create") [email protected](create_theme_module, "_sanitize_and_validate_theme_config") [email protected] +async def test_create_theme_success_with_dict( + mock_sanitize, mock_create, mock_commit, mcp_server +): + """Happy path: dict json_data is sanitized, persisted, and id/uuid returned.""" + config = {"token": {"colorPrimary": "#1d4ed8"}} + mock_sanitize.return_value = config + mock_create.return_value = _make_mock_theme() + + async with Client(mcp_server) as client: + result = await client.call_tool( + "create_theme", + { + "request": { + "theme_name": "Corporate Blue", + "json_data": config, + } + }, + ) + data = json.loads(result.content[0].text) + + assert data["success"] is True + assert data["id"] == 7 + assert data["uuid"] == "22222222-2222-2222-2222-222222222222" + assert data["theme_name"] == "Corporate Blue" + mock_sanitize.assert_called_once_with(config) + # json_data persisted as a serialized string + create_kwargs = mock_create.call_args.kwargs["attributes"] + assert isinstance(create_kwargs["json_data"], str) + assert json.loads(create_kwargs["json_data"]) == config + mock_commit.assert_called_once() + + [email protected](create_theme_module.db.session, "commit") +@patch("superset.daos.theme.ThemeDAO.create") [email protected](create_theme_module, "_sanitize_and_validate_theme_config") [email protected] +async def test_create_theme_success_with_json_string( + mock_sanitize, mock_create, mock_commit, mcp_server +): + """json_data supplied as a JSON string is parsed and accepted.""" + config = {"token": {"colorPrimary": "#abcdef"}} + mock_sanitize.return_value = config + mock_create.return_value = _make_mock_theme(theme_id=9) + + async with Client(mcp_server) as client: + result = await client.call_tool( + "create_theme", + { + "request": { + "theme_name": "From String", + "json_data": json.dumps(config), + } + }, + ) + data = json.loads(result.content[0].text) + + assert data["success"] is True + assert data["id"] == 9 + mock_sanitize.assert_called_once_with(config) + + +@patch("superset.daos.theme.ThemeDAO.create") [email protected](create_theme_module, "_sanitize_and_validate_theme_config") [email protected] +async def test_create_theme_invalid_config(mock_sanitize, mock_create, mcp_server): Review Comment: Annotations added in 33ae92251e. ########## tests/unit_tests/mcp_service/theme/tool/test_list_themes.py: ########## @@ -0,0 +1,125 @@ +# 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 list_themes MCP tool.""" + +from unittest.mock import MagicMock, Mock, patch + +import pytest +from fastmcp import Client +from pydantic import ValidationError + +from superset.mcp_service.app import mcp +from superset.mcp_service.theme.schemas import ListThemesRequest, ThemeFilter +from superset.utils import json + + +def create_mock_theme( + theme_id: int = 1, + theme_name: str = "Corporate Blue", + json_data: str = '{"token": {"colorPrimary": "#1d4ed8"}}', + uuid: str = "11111111-1111-1111-1111-111111111111", +) -> MagicMock: + theme = MagicMock() + theme.id = theme_id + theme.theme_name = theme_name + theme.json_data = json_data + theme.uuid = uuid + theme.is_system = False + theme.is_system_default = False + theme.is_system_dark = False + theme.changed_on = None + theme.created_on = None + return theme + + [email protected] +def mcp_server(): + return mcp + Review Comment: Annotations added in 33ae92251e. ########## tests/unit_tests/mcp_service/theme/tool/test_list_themes.py: ########## @@ -0,0 +1,125 @@ +# 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 list_themes MCP tool.""" + +from unittest.mock import MagicMock, Mock, patch + +import pytest +from fastmcp import Client +from pydantic import ValidationError + +from superset.mcp_service.app import mcp +from superset.mcp_service.theme.schemas import ListThemesRequest, ThemeFilter +from superset.utils import json + + +def create_mock_theme( + theme_id: int = 1, + theme_name: str = "Corporate Blue", + json_data: str = '{"token": {"colorPrimary": "#1d4ed8"}}', + uuid: str = "11111111-1111-1111-1111-111111111111", +) -> MagicMock: + theme = MagicMock() + theme.id = theme_id + theme.theme_name = theme_name + theme.json_data = json_data + theme.uuid = uuid + theme.is_system = False + theme.is_system_default = False + theme.is_system_dark = False + theme.changed_on = None + theme.created_on = None + return theme + + [email protected] +def mcp_server(): + 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: Annotations added in 33ae92251e. ########## tests/unit_tests/mcp_service/theme/tool/test_list_themes.py: ########## @@ -0,0 +1,125 @@ +# 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 list_themes MCP tool.""" + +from unittest.mock import MagicMock, Mock, patch + +import pytest +from fastmcp import Client +from pydantic import ValidationError + +from superset.mcp_service.app import mcp +from superset.mcp_service.theme.schemas import ListThemesRequest, ThemeFilter +from superset.utils import json + + +def create_mock_theme( + theme_id: int = 1, + theme_name: str = "Corporate Blue", + json_data: str = '{"token": {"colorPrimary": "#1d4ed8"}}', + uuid: str = "11111111-1111-1111-1111-111111111111", +) -> MagicMock: + theme = MagicMock() + theme.id = theme_id + theme.theme_name = theme_name + theme.json_data = json_data + theme.uuid = uuid + theme.is_system = False + theme.is_system_default = False + theme.is_system_dark = False + theme.changed_on = None + theme.created_on = None + return theme + + [email protected] +def mcp_server(): + 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 + + +class TestThemeFilterSchema: + """Tests for ThemeFilter schema — filterable columns.""" + + def test_invalid_filter_column_rejected(self): + with pytest.raises(ValidationError): + ThemeFilter(col="not_a_real_column", opr="eq", value="x") + Review Comment: Annotations added in 33ae92251e. ########## tests/unit_tests/mcp_service/theme/tool/test_list_themes.py: ########## @@ -0,0 +1,125 @@ +# 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 list_themes MCP tool.""" + +from unittest.mock import MagicMock, Mock, patch + +import pytest +from fastmcp import Client +from pydantic import ValidationError + +from superset.mcp_service.app import mcp +from superset.mcp_service.theme.schemas import ListThemesRequest, ThemeFilter +from superset.utils import json + + +def create_mock_theme( + theme_id: int = 1, + theme_name: str = "Corporate Blue", + json_data: str = '{"token": {"colorPrimary": "#1d4ed8"}}', + uuid: str = "11111111-1111-1111-1111-111111111111", +) -> MagicMock: + theme = MagicMock() + theme.id = theme_id + theme.theme_name = theme_name + theme.json_data = json_data + theme.uuid = uuid + theme.is_system = False + theme.is_system_default = False + theme.is_system_dark = False + theme.changed_on = None + theme.created_on = None + return theme + + [email protected] +def mcp_server(): + 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 + + +class TestThemeFilterSchema: + """Tests for ThemeFilter schema — filterable columns.""" + + def test_invalid_filter_column_rejected(self): + with pytest.raises(ValidationError): + ThemeFilter(col="not_a_real_column", opr="eq", value="x") + + def test_valid_theme_name_filter(self): + f = ThemeFilter(col="theme_name", opr="ct", value="blue") + assert f.col == "theme_name" + + +@patch("superset.daos.theme.ThemeDAO.list") [email protected] +async def test_list_themes_basic(mock_list, mcp_server): + """Basic theme listing returns the mocked theme.""" + theme = create_mock_theme() + mock_list.return_value = ([theme], 1) + async with Client(mcp_server) as client: + request = ListThemesRequest(page=1, page_size=10) + result = await client.call_tool( + "list_themes", {"request": request.model_dump()} + ) + assert result.content is not None + data = json.loads(result.content[0].text) + assert data["themes"] is not None + assert len(data["themes"]) == 1 + assert data["themes"][0]["id"] == 1 + assert "Corporate Blue" in data["themes"][0]["theme_name"] + Review Comment: Annotations added in 33ae92251e. ########## tests/unit_tests/mcp_service/theme/tool/test_list_themes.py: ########## @@ -0,0 +1,125 @@ +# 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 list_themes MCP tool.""" + +from unittest.mock import MagicMock, Mock, patch + +import pytest +from fastmcp import Client +from pydantic import ValidationError + +from superset.mcp_service.app import mcp +from superset.mcp_service.theme.schemas import ListThemesRequest, ThemeFilter +from superset.utils import json + + +def create_mock_theme( + theme_id: int = 1, + theme_name: str = "Corporate Blue", + json_data: str = '{"token": {"colorPrimary": "#1d4ed8"}}', + uuid: str = "11111111-1111-1111-1111-111111111111", +) -> MagicMock: + theme = MagicMock() + theme.id = theme_id + theme.theme_name = theme_name + theme.json_data = json_data + theme.uuid = uuid + theme.is_system = False + theme.is_system_default = False + theme.is_system_dark = False + theme.changed_on = None + theme.created_on = None + return theme + + [email protected] +def mcp_server(): + 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 + + +class TestThemeFilterSchema: + """Tests for ThemeFilter schema — filterable columns.""" + + def test_invalid_filter_column_rejected(self): + with pytest.raises(ValidationError): + ThemeFilter(col="not_a_real_column", opr="eq", value="x") + + def test_valid_theme_name_filter(self): + f = ThemeFilter(col="theme_name", opr="ct", value="blue") + assert f.col == "theme_name" + + +@patch("superset.daos.theme.ThemeDAO.list") [email protected] +async def test_list_themes_basic(mock_list, mcp_server): + """Basic theme listing returns the mocked theme.""" + theme = create_mock_theme() + mock_list.return_value = ([theme], 1) + async with Client(mcp_server) as client: + request = ListThemesRequest(page=1, page_size=10) + result = await client.call_tool( + "list_themes", {"request": request.model_dump()} + ) + assert result.content is not None + data = json.loads(result.content[0].text) + assert data["themes"] is not None + assert len(data["themes"]) == 1 + assert data["themes"][0]["id"] == 1 + assert "Corporate Blue" in data["themes"][0]["theme_name"] + + +@patch("superset.daos.theme.ThemeDAO.list") [email protected] +async def test_list_themes_without_request(mock_list, mcp_server): + """Listing with no request payload uses defaults.""" + theme = create_mock_theme() + mock_list.return_value = ([theme], 1) + async with Client(mcp_server) as client: + result = await client.call_tool("list_themes", {}) + data = json.loads(result.content[0].text) + assert data["themes"] is not None + assert len(data["themes"]) == 1 + Review Comment: Annotations added in 33ae92251e. -- 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]
