1414
1515from unittest import mock
1616
17- from google .adk .auth .auth_credential import AuthCredentialTypes , ServiceAccount , ServiceAccountCredential
17+ from google .genai .types import FunctionDeclaration
18+ import pytest
19+
20+ from google .adk .auth .auth_credential import AuthCredentialTypes
21+ from google .adk .auth .auth_credential import ServiceAccount
22+ from google .adk .auth .auth_credential import ServiceAccountCredential
1823from google .adk .tools .google_api_tool .google_api_tool import GoogleApiTool
1924from google .adk .tools .openapi_tool import RestApiTool
2025from google .adk .tools .tool_context import ToolContext
21- from google .genai .types import FunctionDeclaration
22- import pytest
2326
2427
2528@pytest .fixture
2629def mock_rest_api_tool ():
27- """Fixture for a mock RestApiTool."""
28- mock_tool = mock .MagicMock (spec = RestApiTool )
29- mock_tool .name = "test_tool"
30- mock_tool .description = "Test Tool Description"
31- mock_tool .is_long_running = False
32- mock_tool ._get_declaration .return_value = FunctionDeclaration (
33- name = "test_function" ,
34- description = "Test function description"
35- )
36- mock_tool . run_async . return_value = { "result" : "success" }
37- return mock_tool
30+ """Fixture for a mock RestApiTool."""
31+ mock_tool = mock .MagicMock (spec = RestApiTool )
32+ mock_tool .name = "test_tool"
33+ mock_tool .description = "Test Tool Description"
34+ mock_tool .is_long_running = False
35+ mock_tool ._get_declaration .return_value = FunctionDeclaration (
36+ name = "test_function" , description = "Test function description"
37+ )
38+ mock_tool . run_async . return_value = { "result" : "success" }
39+ return mock_tool
40+
3841
3942@pytest .fixture
4043def mock_tool_context ():
41- """Fixture for a mock ToolContext."""
42- return mock .MagicMock (spec = ToolContext )
44+ """Fixture for a mock ToolContext."""
45+ return mock .MagicMock (spec = ToolContext )
46+
4347
4448class TestGoogleApiTool :
45- """Test suite for the GoogleApiTool class."""
46-
47- def test_init (self , mock_rest_api_tool ):
48- """Test GoogleApiTool initialization."""
49- tool = GoogleApiTool (mock_rest_api_tool )
50-
51- assert tool .name == "test_tool"
52- assert tool .description == "Test Tool Description"
53- assert tool .is_long_running == False
54- assert tool .rest_api_tool == mock_rest_api_tool
55-
56- def test_get_declaration (self , mock_rest_api_tool ):
57- """Test _get_declaration method."""
58- tool = GoogleApiTool (mock_rest_api_tool )
59-
60- declaration = tool ._get_declaration ()
61-
62- assert isinstance (declaration , FunctionDeclaration )
63- assert declaration .name == "test_function"
64- assert declaration .description == "Test function description"
65- mock_rest_api_tool ._get_declaration .assert_called_once ()
66-
67- @pytest .mark .asyncio
68- async def test_run_async (self , mock_rest_api_tool , mock_tool_context ):
69- """Test run_async method."""
70- tool = GoogleApiTool (mock_rest_api_tool )
71- args = {"param1" : "value1" }
72-
73- result = await tool .run_async (args = args , tool_context = mock_tool_context )
74-
75- assert result == {"result" : "success" }
76- mock_rest_api_tool .run_async .assert_called_once_with (
77- args = args , tool_context = mock_tool_context
78- )
79-
80- def test_configure_auth (self , mock_rest_api_tool ):
81- """Test configure_auth method."""
82- tool = GoogleApiTool (mock_rest_api_tool )
83- client_id = "test_client_id"
84- client_secret = "test_client_secret"
85-
86- tool .configure_auth (client_id = client_id , client_secret = client_secret )
87-
88- # Check that auth_credential was set correctly on the rest_api_tool
89- assert mock_rest_api_tool .auth_credential is not None
90- assert mock_rest_api_tool .auth_credential .auth_type == AuthCredentialTypes .OPEN_ID_CONNECT
91- assert mock_rest_api_tool .auth_credential .oauth2 .client_id == client_id
92- assert mock_rest_api_tool .auth_credential .oauth2 .client_secret == client_secret
93-
94- @mock .patch ("google.adk.tools.google_api_tool.google_api_tool.service_account_scheme_credential" )
95- def test_configure_sa_auth (self , mock_service_account_scheme_credential , mock_rest_api_tool ):
96- """Test configure_sa_auth method."""
97- # Setup mock return values
98- mock_auth_scheme = mock .MagicMock ()
99- mock_auth_credential = mock .MagicMock ()
100- mock_service_account_scheme_credential .return_value = (mock_auth_scheme , mock_auth_credential )
101-
102- service_account = ServiceAccount (
103- service_account_credential = ServiceAccountCredential (
104- type = "service_account" ,
105- project_id = "project_id" ,
106- private_key_id = "private_key_id" ,
107- private_key = "private_key" ,
108- client_email = "client_email" ,
109- client_id = "client_id" ,
110- auth_uri = "auth_uri" ,
111- token_uri = "token_uri" ,
112- auth_provider_x509_cert_url = "auth_provider_x509_cert_url" ,
113- client_x509_cert_url = "client_x509_cert_url" ,
114- universe_domain = "universe_domain" ,
115- ),
116- scopes = ["scope1" , "scope2" ],
117- )
118-
119- # Create tool and call method
120- tool = GoogleApiTool (mock_rest_api_tool )
121- tool .configure_sa_auth (service_account = service_account )
122-
123- # Verify service_account_scheme_credential was called correctly
124- mock_service_account_scheme_credential .assert_called_once_with (service_account )
125-
126- # Verify auth_scheme and auth_credential were set correctly on the rest_api_tool
127- assert mock_rest_api_tool .auth_scheme == mock_auth_scheme
128- assert mock_rest_api_tool .auth_credential == mock_auth_credential
49+ """Test suite for the GoogleApiTool class."""
50+
51+ def test_init (self , mock_rest_api_tool ):
52+ """Test GoogleApiTool initialization."""
53+ tool = GoogleApiTool (mock_rest_api_tool )
54+
55+ assert tool .name == "test_tool"
56+ assert tool .description == "Test Tool Description"
57+ assert tool .is_long_running == False
58+ assert tool .rest_api_tool == mock_rest_api_tool
59+
60+ def test_get_declaration (self , mock_rest_api_tool ):
61+ """Test _get_declaration method."""
62+ tool = GoogleApiTool (mock_rest_api_tool )
63+
64+ declaration = tool ._get_declaration ()
65+
66+ assert isinstance (declaration , FunctionDeclaration )
67+ assert declaration .name == "test_function"
68+ assert declaration .description == "Test function description"
69+ mock_rest_api_tool ._get_declaration .assert_called_once ()
70+
71+ @pytest .mark .asyncio
72+ async def test_run_async (self , mock_rest_api_tool , mock_tool_context ):
73+ """Test run_async method."""
74+ tool = GoogleApiTool (mock_rest_api_tool )
75+ args = {"param1" : "value1" }
76+
77+ result = await tool .run_async (args = args , tool_context = mock_tool_context )
78+
79+ assert result == {"result" : "success" }
80+ mock_rest_api_tool .run_async .assert_called_once_with (
81+ args = args , tool_context = mock_tool_context
82+ )
83+
84+ def test_configure_auth (self , mock_rest_api_tool ):
85+ """Test configure_auth method."""
86+ tool = GoogleApiTool (mock_rest_api_tool )
87+ client_id = "test_client_id"
88+ client_secret = "test_client_secret"
89+
90+ tool .configure_auth (client_id = client_id , client_secret = client_secret )
91+
92+ # Check that auth_credential was set correctly on the rest_api_tool
93+ assert mock_rest_api_tool .auth_credential is not None
94+ assert (
95+ mock_rest_api_tool .auth_credential .auth_type
96+ == AuthCredentialTypes .OPEN_ID_CONNECT
97+ )
98+ assert mock_rest_api_tool .auth_credential .oauth2 .client_id == client_id
99+ assert (
100+ mock_rest_api_tool .auth_credential .oauth2 .client_secret == client_secret
101+ )
102+
103+ @mock .patch (
104+ "google.adk.tools.google_api_tool.google_api_tool.service_account_scheme_credential"
105+ )
106+ def test_configure_sa_auth (
107+ self , mock_service_account_scheme_credential , mock_rest_api_tool
108+ ):
109+ """Test configure_sa_auth method."""
110+ # Setup mock return values
111+ mock_auth_scheme = mock .MagicMock ()
112+ mock_auth_credential = mock .MagicMock ()
113+ mock_service_account_scheme_credential .return_value = (
114+ mock_auth_scheme ,
115+ mock_auth_credential ,
116+ )
117+
118+ service_account = ServiceAccount (
119+ service_account_credential = ServiceAccountCredential (
120+ type = "service_account" ,
121+ project_id = "project_id" ,
122+ private_key_id = "private_key_id" ,
123+ private_key = "private_key" ,
124+ client_email = "client_email" ,
125+ client_id = "client_id" ,
126+ auth_uri = "auth_uri" ,
127+ token_uri = "token_uri" ,
128+ auth_provider_x509_cert_url = "auth_provider_x509_cert_url" ,
129+ client_x509_cert_url = "client_x509_cert_url" ,
130+ universe_domain = "universe_domain" ,
131+ ),
132+ scopes = ["scope1" , "scope2" ],
133+ )
134+
135+ # Create tool and call method
136+ tool = GoogleApiTool (mock_rest_api_tool )
137+ tool .configure_sa_auth (service_account = service_account )
138+
139+ # Verify service_account_scheme_credential was called correctly
140+ mock_service_account_scheme_credential .assert_called_once_with (
141+ service_account
142+ )
143+
144+ # Verify auth_scheme and auth_credential were set correctly on the rest_api_tool
145+ assert mock_rest_api_tool .auth_scheme == mock_auth_scheme
146+ assert mock_rest_api_tool .auth_credential == mock_auth_credential
0 commit comments