chore: Make UT of a2a consistent about how tests should be skipped when python verison < 3.10

PiperOrigin-RevId: 801040421
This commit is contained in:
Xiang (Sean) Zhou
2025-08-29 14:59:27 -07:00
committed by Copybara-Service
parent 2eddc5e4d3
commit 98b0426cd2
11 changed files with 197 additions and 323 deletions
+28 -33
View File
@@ -21,22 +21,35 @@ from unittest.mock import patch
import pytest
# Skip entire module if Python < 3.10
if sys.version_info < (3, 10):
pytest.skip("A2A requires Python 3.10+", allow_module_level=True)
# Skip all tests in this module if Python version is less than 3.10
pytestmark = pytest.mark.skipif(
sys.version_info < (3, 10), reason="A2A requires Python 3.10+"
)
# Normal imports after the skip
from a2a.types import DataPart as A2ADataPart
from a2a.types import Message as A2AMessage
from a2a.types import MessageSendConfiguration
from a2a.types import MessageSendParams
from a2a.types import Part as A2APart
from a2a.types import Role
from a2a.types import SendMessageRequest
from a2a.types import Task as A2ATask
from a2a.types import TaskState
from a2a.types import TaskStatus
from a2a.types import TextPart as A2ATextPart
# Import dependencies with version checking
try:
from a2a.types import DataPart as A2ADataPart
from a2a.types import Message as A2AMessage
from a2a.types import MessageSendConfiguration
from a2a.types import MessageSendParams
from a2a.types import Part as A2APart
from a2a.types import Role
from a2a.types import SendMessageRequest
from a2a.types import Task as A2ATask
from a2a.types import TaskState
from a2a.types import TaskStatus
from a2a.types import TextPart as A2ATextPart
from google.adk.a2a.logs.log_utils import build_a2a_request_log
from google.adk.a2a.logs.log_utils import build_a2a_response_log
from google.adk.a2a.logs.log_utils import build_message_part_log
except ImportError as e:
if sys.version_info < (3, 10):
# Imports are not needed since tests will be skipped due to pytestmark.
# The imported names are only used within test methods, not at module level,
# so no NameError occurs during module compilation.
pass
else:
raise e
class TestBuildMessagePartLog:
@@ -44,7 +57,6 @@ class TestBuildMessagePartLog:
def test_text_part_short_text(self):
"""Test TextPart with short text."""
from google.adk.a2a.logs.log_utils import build_message_part_log
# Create real A2A objects
text_part = A2ATextPart(text="Hello, world!")
@@ -56,7 +68,6 @@ class TestBuildMessagePartLog:
def test_text_part_long_text(self):
"""Test TextPart with long text that gets truncated."""
from google.adk.a2a.logs.log_utils import build_message_part_log
long_text = "x" * 150 # Long text that should be truncated
text_part = A2ATextPart(text=long_text)
@@ -69,7 +80,6 @@ class TestBuildMessagePartLog:
def test_data_part_simple_data(self):
"""Test DataPart with simple data."""
from google.adk.a2a.logs.log_utils import build_message_part_log
data_part = A2ADataPart(data={"key1": "value1", "key2": 42})
part = A2APart(root=data_part)
@@ -82,7 +92,6 @@ class TestBuildMessagePartLog:
def test_data_part_large_values(self):
"""Test DataPart with large values that get summarized."""
from google.adk.a2a.logs.log_utils import build_message_part_log
large_dict = {f"key{i}": f"value{i}" for i in range(50)}
large_list = list(range(100))
@@ -109,7 +118,6 @@ class TestBuildMessagePartLog:
def test_other_part_type(self):
"""Test handling of other part types (not Text or Data)."""
from google.adk.a2a.logs.log_utils import build_message_part_log
# Create a mock part that will fall through to the else case
mock_root = Mock()
@@ -132,7 +140,6 @@ class TestBuildA2ARequestLog:
def test_request_with_parts_and_config(self):
"""Test request logging with message parts and configuration."""
from google.adk.a2a.logs.log_utils import build_a2a_request_log
# Create mock request with all components
req = SendMessageRequest(
@@ -184,7 +191,6 @@ class TestBuildA2ARequestLog:
def test_request_without_parts(self):
"""Test request logging without message parts."""
from google.adk.a2a.logs.log_utils import build_a2a_request_log
req = Mock()
req.id = "req-123"
@@ -210,7 +216,6 @@ class TestBuildA2ARequestLog:
def test_request_with_empty_parts_list(self):
"""Test request logging with empty parts list."""
from google.adk.a2a.logs.log_utils import build_a2a_request_log
req = Mock()
req.id = "req-123"
@@ -237,7 +242,6 @@ class TestBuildA2AResponseLog:
def test_error_response(self):
"""Test error response logging."""
from google.adk.a2a.logs.log_utils import build_a2a_response_log
resp = Mock()
resp.root.error.code = 500
@@ -257,7 +261,6 @@ class TestBuildA2AResponseLog:
def test_error_response_no_data(self):
"""Test error response logging without error data."""
from google.adk.a2a.logs.log_utils import build_a2a_response_log
resp = Mock()
resp.root.error.code = 404
@@ -276,7 +279,6 @@ class TestBuildA2AResponseLog:
def test_success_response_with_task(self):
"""Test success response logging with Task result."""
# Use module-level imported types consistently
from google.adk.a2a.logs.log_utils import build_a2a_response_log
task_status = TaskStatus(state=TaskState.working)
task = A2ATask(id="task-123", context_id="ctx-456", status=task_status)
@@ -305,7 +307,6 @@ class TestBuildA2AResponseLog:
def test_success_response_with_task_and_status_message(self):
"""Test success response with Task that has status message."""
from google.adk.a2a.logs.log_utils import build_a2a_response_log
# Create status message using module-level imported types
status_message = A2AMessage(
@@ -348,7 +349,6 @@ class TestBuildA2AResponseLog:
def test_success_response_with_message(self):
"""Test success response logging with Message result."""
from google.adk.a2a.logs.log_utils import build_a2a_response_log
# Use module-level imported types consistently
message = A2AMessage(
@@ -384,7 +384,6 @@ class TestBuildA2AResponseLog:
def test_success_response_with_message_no_parts(self):
"""Test success response with Message that has no parts."""
from google.adk.a2a.logs.log_utils import build_a2a_response_log
# Use mock for this case since we want to test empty parts handling
message = Mock()
@@ -411,7 +410,6 @@ class TestBuildA2AResponseLog:
def test_success_response_with_other_result_type(self):
"""Test success response with result type that's not Task or Message."""
from google.adk.a2a.logs.log_utils import build_a2a_response_log
other_result = Mock()
other_result.__class__.__name__ = "OtherResult"
@@ -434,7 +432,6 @@ class TestBuildA2AResponseLog:
def test_success_response_without_model_dump_json(self):
"""Test success response with result that doesn't have model_dump_json."""
from google.adk.a2a.logs.log_utils import build_a2a_response_log
other_result = Mock()
other_result.__class__.__name__ = "SimpleResult"
@@ -456,7 +453,6 @@ class TestBuildA2AResponseLog:
def test_build_message_part_log_with_metadata(self):
"""Test build_message_part_log with metadata in the part."""
from google.adk.a2a.logs.log_utils import build_message_part_log
mock_root = Mock()
mock_root.__class__.__name__ = "MockPartWithMetadata"
@@ -475,7 +471,6 @@ class TestBuildA2AResponseLog:
def test_build_a2a_request_log_with_message_metadata(self):
"""Test request logging with message metadata."""
from google.adk.a2a.logs.log_utils import build_a2a_request_log
req = Mock()
req.id = "req-with-metadata"