-
Notifications
You must be signed in to change notification settings - Fork 423
Expand file tree
/
Copy pathtest_fastapi_app.py
More file actions
100 lines (82 loc) · 3.51 KB
/
test_fastapi_app.py
File metadata and controls
100 lines (82 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from typing import Any
from unittest.mock import MagicMock
import pytest
from a2a.server.apps.jsonrpc import fastapi_app
from a2a.server.apps.jsonrpc.fastapi_app import A2AFastAPIApplication
from a2a.server.request_handlers.request_handler import (
RequestHandler, # For mock spec
)
from a2a.types import AgentCard # For mock spec
# --- A2AFastAPIApplication Tests ---
class TestA2AFastAPIApplicationOptionalDeps:
# Running tests in this class requires the optional dependency fastapi to be
# present in the test environment.
@pytest.fixture(scope='class', autouse=True)
def ensure_pkg_fastapi_is_present(self):
try:
import fastapi as _fastapi # noqa: F401
except ImportError:
pytest.fail(
f'Running tests in {self.__class__.__name__} requires'
' the optional dependency fastapi to be present in the test'
' environment. Run `uv sync --dev ...` before running the test'
' suite.'
)
@pytest.fixture(scope='class')
def mock_app_params(self) -> dict:
# Mock http_handler
mock_handler = MagicMock(spec=RequestHandler)
# Mock agent_card with essential attributes accessed in __init__
mock_agent_card = MagicMock(spec=AgentCard)
# Ensure 'url' attribute exists on the mock_agent_card, as it's accessed
# in __init__
mock_agent_card.url = 'http://example.com'
# Ensure 'supports_authenticated_extended_card' attribute exists
mock_agent_card.supports_authenticated_extended_card = False
return {'agent_card': mock_agent_card, 'http_handler': mock_handler}
@pytest.fixture(scope='class')
def mark_pkg_fastapi_not_installed(self):
pkg_fastapi_installed_flag = fastapi_app._package_fastapi_installed
fastapi_app._package_fastapi_installed = False
yield
fastapi_app._package_fastapi_installed = pkg_fastapi_installed_flag
def test_create_a2a_fastapi_app_with_present_deps_succeeds(
self, mock_app_params: dict
):
try:
_app = A2AFastAPIApplication(**mock_app_params)
except ImportError:
pytest.fail(
'With the fastapi package present, creating a'
' A2AFastAPIApplication instance should not raise ImportError'
)
def test_build_a2a_fastapi_app_with_dependencies_succeeds(
self, mock_app_params: dict
):
from fastapi import Depends
def mock_dependency():
return 'mock'
app = A2AFastAPIApplication(**mock_app_params)
fastapi_app = app.build(dependencies=[Depends(mock_dependency)])
from fastapi.routing import APIRoute
# Check that routes have the dependency
for route in fastapi_app.routes:
if getattr(route, 'path', '') in ['/v1/message:send', '/v1/card']:
assert isinstance(route, APIRoute)
assert len(route.dependencies) == 1
assert route.dependencies[0].dependency == mock_dependency
def test_create_a2a_fastapi_app_with_missing_deps_raises_importerror(
self,
mock_app_params: dict,
mark_pkg_fastapi_not_installed: Any,
):
with pytest.raises(
ImportError,
match=(
'The `fastapi` package is required to use the'
' `A2AFastAPIApplication`'
),
):
_app = A2AFastAPIApplication(**mock_app_params)
if __name__ == '__main__':
pytest.main([__file__])