Skip to content

Commit 84e6778

Browse files
committed
test: use patch decorators on session tests to prevent python 3.14 branch misreporting
1 parent 41a7fca commit 84e6778

1 file changed

Lines changed: 70 additions & 63 deletions

File tree

‎tests/client/test_session_group.py‎

Lines changed: 70 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -424,83 +424,90 @@ def auth_flow(self, request: httpx2.Request):
424424

425425

426426
@pytest.mark.anyio
427-
async def test_establish_session_sse_passes_auth():
427+
@mock.patch("mcp.client.session_group.sse_client")
428+
@mock.patch("mcp.client.session_group.mcp.ClientSession")
429+
async def test_establish_session_sse_passes_auth(
430+
mock_ClientSession_class: mock.MagicMock,
431+
mock_sse_client: mock.MagicMock,
432+
):
428433
"""_establish_session should pass auth to sse_client for SseServerParameters."""
429434
mock_auth = mock.Mock(spec=httpx2.Auth)
430435
server_params = SseServerParameters(url="http://test.com/sse", auth=mock_auth)
431436

432-
with mock.patch("mcp.client.session_group.mcp.ClientSession") as mock_ClientSession_class:
433-
with mock.patch("mcp.client.session_group.sse_client") as mock_sse_client:
434-
mock_client_cm = mock.AsyncMock()
435-
mock_read = mock.AsyncMock()
436-
mock_write = mock.AsyncMock()
437-
mock_client_cm.__aenter__.return_value = (mock_read, mock_write)
438-
mock_client_cm.__aexit__ = mock.AsyncMock(return_value=None)
439-
mock_sse_client.return_value = mock_client_cm
437+
mock_client_cm = mock.AsyncMock()
438+
mock_read = mock.AsyncMock()
439+
mock_write = mock.AsyncMock()
440+
mock_client_cm.__aenter__.return_value = (mock_read, mock_write)
441+
mock_client_cm.__aexit__ = mock.AsyncMock(return_value=None)
442+
mock_sse_client.return_value = mock_client_cm
440443

441-
mock_session_cm = mock.AsyncMock()
442-
mock_ClientSession_class.return_value = mock_session_cm
443-
mock_session = mock.AsyncMock()
444-
mock_session_cm.__aenter__.return_value = mock_session
445-
mock_session_cm.__aexit__ = mock.AsyncMock(return_value=None)
444+
mock_session_cm = mock.AsyncMock()
445+
mock_ClientSession_class.return_value = mock_session_cm
446+
mock_session = mock.AsyncMock()
447+
mock_session_cm.__aenter__.return_value = mock_session
448+
mock_session_cm.__aexit__ = mock.AsyncMock(return_value=None)
446449

447-
mock_result = mock.AsyncMock()
448-
mock_result.server_info = types.Implementation(name="test", version="1")
449-
mock_session.initialize.return_value = mock_result
450+
mock_result = mock.AsyncMock()
451+
mock_result.server_info = types.Implementation(name="test", version="1")
452+
mock_session.initialize.return_value = mock_result
450453

451-
group = ClientSessionGroup()
452-
async with contextlib.AsyncExitStack() as stack:
453-
group._exit_stack = stack
454-
await group._establish_session(server_params, ClientSessionParameters())
455-
456-
mock_sse_client.assert_called_once_with(
457-
url="http://test.com/sse",
458-
headers=None,
459-
timeout=5.0,
460-
sse_read_timeout=300.0,
461-
auth=mock_auth,
462-
)
454+
group = ClientSessionGroup()
455+
async with contextlib.AsyncExitStack() as stack:
456+
group._exit_stack = stack
457+
await group._establish_session(server_params, ClientSessionParameters())
458+
459+
mock_sse_client.assert_called_once_with(
460+
url="http://test.com/sse",
461+
headers=None,
462+
timeout=5.0,
463+
sse_read_timeout=300.0,
464+
auth=mock_auth,
465+
)
463466

464467

465468
@pytest.mark.anyio
466-
async def test_establish_session_streamable_http_passes_auth():
469+
@mock.patch("mcp.client.session_group.create_mcp_http_client")
470+
@mock.patch("mcp.client.session_group.streamable_http_client")
471+
@mock.patch("mcp.client.session_group.mcp.ClientSession")
472+
async def test_establish_session_streamable_http_passes_auth(
473+
mock_ClientSession_class: mock.MagicMock,
474+
mock_streamable_client: mock.MagicMock,
475+
mock_create_client: mock.MagicMock,
476+
):
467477
"""_establish_session should pass auth to create_mcp_http_client for StreamableHttpParameters."""
468478
mock_auth = mock.Mock(spec=httpx2.Auth)
469479
server_params = StreamableHttpParameters(url="http://test.com/stream", auth=mock_auth)
470480

471-
with mock.patch("mcp.client.session_group.mcp.ClientSession") as mock_ClientSession_class:
472-
with mock.patch("mcp.client.session_group.streamable_http_client") as mock_streamable_client:
473-
with mock.patch("mcp.client.session_group.create_mcp_http_client") as mock_create_client:
474-
mock_httpx_client = mock.AsyncMock(spec=httpx2.AsyncClient)
475-
mock_httpx_client.__aenter__ = mock.AsyncMock(return_value=mock_httpx_client)
476-
mock_httpx_client.__aexit__ = mock.AsyncMock(return_value=None)
477-
mock_create_client.return_value = mock_httpx_client
478-
479-
mock_client_cm = mock.AsyncMock()
480-
mock_read = mock.AsyncMock()
481-
mock_write = mock.AsyncMock()
482-
mock_client_cm.__aenter__.return_value = (mock_read, mock_write)
483-
mock_client_cm.__aexit__ = mock.AsyncMock(return_value=None)
484-
mock_streamable_client.return_value = mock_client_cm
485-
486-
mock_session_cm = mock.AsyncMock()
487-
mock_ClientSession_class.return_value = mock_session_cm
488-
mock_session = mock.AsyncMock()
489-
mock_session_cm.__aenter__.return_value = mock_session
490-
mock_session_cm.__aexit__ = mock.AsyncMock(return_value=None)
491-
492-
mock_result = mock.AsyncMock()
493-
mock_result.server_info = types.Implementation(name="test", version="1")
494-
mock_session.initialize.return_value = mock_result
495-
496-
group = ClientSessionGroup()
497-
async with contextlib.AsyncExitStack() as stack:
498-
group._exit_stack = stack
499-
await group._establish_session(server_params, ClientSessionParameters())
500-
501-
mock_create_client.assert_called_once()
502-
call_kwargs = mock_create_client.call_args.kwargs
503-
assert call_kwargs["auth"] is mock_auth
481+
mock_httpx_client = mock.AsyncMock(spec=httpx2.AsyncClient)
482+
mock_httpx_client.__aenter__ = mock.AsyncMock(return_value=mock_httpx_client)
483+
mock_httpx_client.__aexit__ = mock.AsyncMock(return_value=None)
484+
mock_create_client.return_value = mock_httpx_client
485+
486+
mock_client_cm = mock.AsyncMock()
487+
mock_read = mock.AsyncMock()
488+
mock_write = mock.AsyncMock()
489+
mock_client_cm.__aenter__.return_value = (mock_read, mock_write)
490+
mock_client_cm.__aexit__ = mock.AsyncMock(return_value=None)
491+
mock_streamable_client.return_value = mock_client_cm
492+
493+
mock_session_cm = mock.AsyncMock()
494+
mock_ClientSession_class.return_value = mock_session_cm
495+
mock_session = mock.AsyncMock()
496+
mock_session_cm.__aenter__.return_value = mock_session
497+
mock_session_cm.__aexit__ = mock.AsyncMock(return_value=None)
498+
499+
mock_result = mock.AsyncMock()
500+
mock_result.server_info = types.Implementation(name="test", version="1")
501+
mock_session.initialize.return_value = mock_result
502+
503+
group = ClientSessionGroup()
504+
async with contextlib.AsyncExitStack() as stack:
505+
group._exit_stack = stack
506+
await group._establish_session(server_params, ClientSessionParameters())
507+
508+
mock_create_client.assert_called_once()
509+
call_kwargs = mock_create_client.call_args.kwargs
510+
assert call_kwargs["auth"] is mock_auth
504511

505512

506513
def test_server_parameters_auth_model_config_and_serialization():

0 commit comments

Comments
 (0)