Skip to content

Commit bb4508b

Browse files
committed
test: add more coverage for route disablement
Signed-off-by: Ion Koutsouris <[email protected]>
1 parent 964fd4d commit bb4508b

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

tests/agent_to_fastapi/integration_tests/test_api_integration.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
with try_import() as imports_successful:
1010
from fastapi import FastAPI
11+
from fastapi.routing import APIRoute
1112
from httpx import ASGITransport, AsyncClient
1213

1314
from pydantic_ai.fastapi.agent_router import AgentAPIRouter
@@ -66,6 +67,38 @@ async def test_models_list_and_get(
6667
assert detail['error']['type'] == 'not_found_error'
6768

6869

70+
@pytest.mark.asyncio
71+
async def test_routers_disabled(
72+
registry_with_openai_clients: AgentRegistry,
73+
) -> None:
74+
"""Verify whether disabling apis actually effectively not adds APIRoutes to the app."""
75+
registry = registry_with_openai_clients
76+
77+
router = AgentAPIRouter(agent_registry=registry, disable_completions_api=True, disable_response_api=True)
78+
79+
app = FastAPI()
80+
app.include_router(router)
81+
82+
transport = ASGITransport(app=app)
83+
84+
api_routes: list[APIRoute] = list(filter(lambda x: isinstance(x, APIRoute), app.routes)) # type: ignore
85+
assert {item.path for item in api_routes} == {'/v1/models', '/v1/models/{model_id}'}
86+
87+
async with AsyncClient(transport=transport, base_url='http://testserver') as client:
88+
payload = {
89+
'model': 'test-model',
90+
'messages': [{'role': 'user', 'content': 'hello'}],
91+
}
92+
93+
response = await client.post('/v1/chat/completions', json=payload)
94+
assert response.is_error
95+
assert response.status_code == 404
96+
97+
response = await client.post('/v1/responses', json=payload)
98+
assert response.is_error
99+
assert response.status_code == 404
100+
101+
69102
@pytest.mark.asyncio
70103
async def test_route_not_implemented(registry_with_openai_clients: AgentRegistry) -> None:
71104
"""Isolated test to assert registry raises KeyError for models that only implement the other route."""

0 commit comments

Comments
 (0)