Spaces:
Running
Running
Jeremiah Lowin commited on
Commit ·
16d79ff
1
Parent(s): 70727d0
Remove mock transport
Browse files- tests/client/test_client.py +10 -24
tests/client/test_client.py
CHANGED
|
@@ -1,15 +1,10 @@
|
|
| 1 |
-
import contextlib
|
| 2 |
-
from collections.abc import AsyncIterator
|
| 3 |
from typing import cast
|
| 4 |
|
| 5 |
import pytest
|
| 6 |
-
from mcp import ClientSession
|
| 7 |
-
from mcp.shared.memory import create_client_server_memory_streams
|
| 8 |
from pydantic import AnyUrl
|
| 9 |
-
from typing_extensions import Unpack
|
| 10 |
|
| 11 |
from fastmcp.client import Client
|
| 12 |
-
from fastmcp.client.transports import
|
| 13 |
from fastmcp.server.server import FastMCP
|
| 14 |
|
| 15 |
|
|
@@ -168,40 +163,31 @@ async def test_client_connection(fastmcp_server):
|
|
| 168 |
async def test_client_nested_context_manager(fastmcp_server):
|
| 169 |
"""Test that the client connects and disconnects once in nested context manager."""
|
| 170 |
|
| 171 |
-
|
| 172 |
-
def __init__(self):
|
| 173 |
-
self._connected = False
|
| 174 |
-
|
| 175 |
-
@contextlib.asynccontextmanager
|
| 176 |
-
async def connect_session(
|
| 177 |
-
self,
|
| 178 |
-
**session_kwargs: Unpack[SessionKwargs],
|
| 179 |
-
) -> AsyncIterator[ClientSession]:
|
| 180 |
-
assert not self._connected, "Transport is connected multiple times"
|
| 181 |
-
self._connected = True
|
| 182 |
-
async with create_client_server_memory_streams() as (
|
| 183 |
-
_,
|
| 184 |
-
server_streams,
|
| 185 |
-
):
|
| 186 |
-
yield ClientSession(*server_streams)
|
| 187 |
-
|
| 188 |
-
client = Client(transport=MockTransport())
|
| 189 |
|
| 190 |
# Before connection
|
| 191 |
assert not client.is_connected()
|
|
|
|
| 192 |
|
| 193 |
# During connection
|
| 194 |
async with client:
|
| 195 |
assert client.is_connected()
|
|
|
|
|
|
|
| 196 |
|
|
|
|
| 197 |
async with client:
|
| 198 |
assert client.is_connected()
|
|
|
|
| 199 |
|
|
|
|
| 200 |
async with client:
|
| 201 |
assert client.is_connected()
|
|
|
|
| 202 |
|
| 203 |
# After connection
|
| 204 |
assert not client.is_connected()
|
|
|
|
| 205 |
|
| 206 |
|
| 207 |
async def test_resource_template(fastmcp_server):
|
|
|
|
|
|
|
|
|
|
| 1 |
from typing import cast
|
| 2 |
|
| 3 |
import pytest
|
|
|
|
|
|
|
| 4 |
from pydantic import AnyUrl
|
|
|
|
| 5 |
|
| 6 |
from fastmcp.client import Client
|
| 7 |
+
from fastmcp.client.transports import FastMCPTransport
|
| 8 |
from fastmcp.server.server import FastMCP
|
| 9 |
|
| 10 |
|
|
|
|
| 163 |
async def test_client_nested_context_manager(fastmcp_server):
|
| 164 |
"""Test that the client connects and disconnects once in nested context manager."""
|
| 165 |
|
| 166 |
+
client = Client(fastmcp_server)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
# Before connection
|
| 169 |
assert not client.is_connected()
|
| 170 |
+
assert client._session is None
|
| 171 |
|
| 172 |
# During connection
|
| 173 |
async with client:
|
| 174 |
assert client.is_connected()
|
| 175 |
+
assert client._session is not None
|
| 176 |
+
session = client._session
|
| 177 |
|
| 178 |
+
# Re-use the same session
|
| 179 |
async with client:
|
| 180 |
assert client.is_connected()
|
| 181 |
+
assert client._session is session
|
| 182 |
|
| 183 |
+
# Re-use the same session
|
| 184 |
async with client:
|
| 185 |
assert client.is_connected()
|
| 186 |
+
assert client._session is session
|
| 187 |
|
| 188 |
# After connection
|
| 189 |
assert not client.is_connected()
|
| 190 |
+
assert client._session is None
|
| 191 |
|
| 192 |
|
| 193 |
async def test_resource_template(fastmcp_server):
|