aminghadersohi commented on code in PR #37200: URL: https://github.com/apache/superset/pull/37200#discussion_r2699878823
########## tests/unit_tests/mcp_service/test_middleware.py: ########## @@ -0,0 +1,336 @@ +# 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 MCP service middleware. +""" + +from unittest.mock import AsyncMock, MagicMock, patch + +import pytest +from fastmcp.exceptions import ToolError + +from superset.mcp_service.middleware import ( + create_response_size_guard_middleware, + ResponseSizeGuardMiddleware, +) + + +class TestResponseSizeGuardMiddleware: + """Test ResponseSizeGuardMiddleware class.""" + + def test_init_default_values(self): + """Should initialize with default values.""" + middleware = ResponseSizeGuardMiddleware() + assert middleware.token_limit == 25000 + assert middleware.warn_threshold_pct == 80 + assert middleware.warn_threshold == 20000 + assert middleware.excluded_tools == set() + + def test_init_custom_values(self): + """Should initialize with custom values.""" + middleware = ResponseSizeGuardMiddleware( + token_limit=10000, + warn_threshold_pct=70, + excluded_tools=["health_check", "get_chart_preview"], + ) + assert middleware.token_limit == 10000 + assert middleware.warn_threshold_pct == 70 + assert middleware.warn_threshold == 7000 + assert middleware.excluded_tools == {"health_check", "get_chart_preview"} + + @pytest.mark.asyncio + async def test_allows_small_response(self): + """Should allow responses under token limit.""" + middleware = ResponseSizeGuardMiddleware(token_limit=25000) + + # Create mock context + context = MagicMock() + context.message.name = "list_charts" + context.message.params = {} + + # Create mock call_next that returns small response + small_response = {"charts": [{"id": 1, "name": "test"}]} + call_next = AsyncMock(return_value=small_response) + + with ( + patch("superset.mcp_service.middleware.get_user_id", return_value=1), + patch("superset.mcp_service.middleware.event_logger"), Review Comment: ✅ **No change needed - Valid Python 3.10+ syntax** The parenthesized context manager syntax: ```python with ( patch("..."), patch("..."), ): ``` is **valid Python 3.10+ syntax** introduced by [PEP 617](https://peps.python.org/pep-0617/). Superset requires Python 3.10+ (`requires-python = ">=3.10"` in pyproject.toml), and the `ruff` formatter actively prefers this style over line continuation or nested with blocks. This is NOT a tuple - it's the new grammar for multi-line context managers. -- 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]
