Skip to content

Commit bea0761

Browse files
yinghsienwucopybara-github
authored andcommitted
chore: add unit test coverage to webhooks and agents modules
FUTURE_COPYBARA_INTEGRATE_REVIEW=#2473 from googleapis:release-please--branches--main 806b12a PiperOrigin-RevId: 917512901
1 parent 161f8e4 commit bea0761

3 files changed

Lines changed: 433 additions & 29 deletions

File tree

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
17+
"""Tests for Agents API URL paths."""
18+
19+
from unittest import mock
20+
import google.auth
21+
from httpx import Client as HTTPClient
22+
from httpx import Request, Response
23+
import pytest
24+
from ..._api_client import AsyncHttpxClient
25+
from .. import pytest_helper
26+
27+
28+
@mock.patch.object(google.auth, 'default', autospec=True)
29+
def test_urls(mock_auth_default, client):
30+
agent_id = 'test-agent-id'
31+
32+
mock_creds = mock.Mock()
33+
mock_creds.token = 'test-token'
34+
mock_creds.expired = False
35+
mock_creds.quota_project_id = 'test-quota-project'
36+
mock_auth_default.return_value = (mock_creds, 'test-project')
37+
38+
if client._api_client.vertexai:
39+
if client._api_client.location == 'global':
40+
expected_base_url = (
41+
f'https://aiplatform.googleapis.com/v1beta1'
42+
f'/projects/{client._api_client.project}/locations/global'
43+
)
44+
else:
45+
expected_base_url = (
46+
f'https://{client._api_client.location}-aiplatform.googleapis.com'
47+
f'/v1beta1/projects/{client._api_client.project}'
48+
f'/locations/{client._api_client.location}'
49+
)
50+
else:
51+
expected_base_url = 'https://generativelanguage.googleapis.com/v1beta'
52+
53+
with mock.patch.object(HTTPClient, 'send') as mock_send:
54+
# 1. Create Agent
55+
mock_send.return_value = Response(200, json={}, request=Request('POST', ''))
56+
client.agents.create(
57+
id=agent_id,
58+
description='Test agent',
59+
)
60+
mock_send.assert_called_once()
61+
request = mock_send.call_args[0][0]
62+
assert str(request.url) == f'{expected_base_url}/agents'
63+
64+
# 2. Get Agent
65+
mock_send.reset_mock()
66+
mock_send.return_value = Response(200, json={}, request=Request('GET', ''))
67+
client.agents.get(id=agent_id)
68+
mock_send.assert_called_once()
69+
request = mock_send.call_args[0][0]
70+
assert str(request.url) == f'{expected_base_url}/agents/{agent_id}'
71+
72+
# 3. List Agents
73+
mock_send.reset_mock()
74+
mock_send.return_value = Response(200, json={}, request=Request('GET', ''))
75+
client.agents.list()
76+
mock_send.assert_called_once()
77+
request = mock_send.call_args[0][0]
78+
assert str(request.url) == f'{expected_base_url}/agents'
79+
80+
# 4. Delete Agent
81+
mock_send.reset_mock()
82+
mock_send.return_value = Response(
83+
200, json={}, request=Request('DELETE', '')
84+
)
85+
client.agents.delete(id=agent_id)
86+
mock_send.assert_called_once()
87+
request = mock_send.call_args[0][0]
88+
assert str(request.url) == f'{expected_base_url}/agents/{agent_id}'
89+
90+
91+
@pytest.mark.asyncio
92+
@mock.patch.object(google.auth, 'default', autospec=True)
93+
async def test_async_urls(mock_auth_default, client):
94+
agent_id = 'test-agent-id'
95+
96+
mock_creds = mock.Mock()
97+
mock_creds.token = 'test-token'
98+
mock_creds.expired = False
99+
mock_creds.quota_project_id = 'test-quota-project'
100+
mock_auth_default.return_value = (mock_creds, 'test-project')
101+
102+
if client._api_client.vertexai:
103+
if client._api_client.location == 'global':
104+
expected_base_url = (
105+
f'https://aiplatform.googleapis.com/v1beta1'
106+
f'/projects/{client._api_client.project}/locations/global'
107+
)
108+
else:
109+
expected_base_url = (
110+
f'https://{client._api_client.location}-aiplatform.googleapis.com'
111+
f'/v1beta1/projects/{client._api_client.project}'
112+
f'/locations/{client._api_client.location}'
113+
)
114+
else:
115+
expected_base_url = 'https://generativelanguage.googleapis.com/v1beta'
116+
117+
with mock.patch.object(AsyncHttpxClient, 'send') as mock_send:
118+
# 1. Create Agent
119+
mock_send.return_value = Response(200, json={}, request=Request('POST', ''))
120+
await client.aio.agents.create(
121+
id=agent_id,
122+
description='Test agent',
123+
)
124+
mock_send.assert_called_once()
125+
request = mock_send.call_args[0][0]
126+
assert str(request.url) == f'{expected_base_url}/agents'
127+
128+
# 2. Get Agent
129+
mock_send.reset_mock()
130+
mock_send.return_value = Response(200, json={}, request=Request('GET', ''))
131+
await client.aio.agents.get(id=agent_id)
132+
mock_send.assert_called_once()
133+
request = mock_send.call_args[0][0]
134+
assert str(request.url) == f'{expected_base_url}/agents/{agent_id}'
135+
136+
# 3. List Agents
137+
mock_send.reset_mock()
138+
mock_send.return_value = Response(200, json={}, request=Request('GET', ''))
139+
await client.aio.agents.list()
140+
mock_send.assert_called_once()
141+
request = mock_send.call_args[0][0]
142+
assert str(request.url) == f'{expected_base_url}/agents'
143+
144+
# 4. Delete Agent
145+
mock_send.reset_mock()
146+
mock_send.return_value = Response(
147+
200, json={}, request=Request('DELETE', '')
148+
)
149+
await client.aio.agents.delete(id=agent_id)
150+
mock_send.assert_called_once()
151+
request = mock_send.call_args[0][0]
152+
assert str(request.url) == f'{expected_base_url}/agents/{agent_id}'
153+
154+
155+
pytestmark = pytest_helper.setup(
156+
file=__file__,
157+
globals_for_file=globals(),
158+
test_table=[],
159+
)

google/genai/tests/interactions/test_paths.py

Lines changed: 63 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
# Copyright 2025 Google LLC
1+
# Copyright 2026 Google LLC
32
#
43
# Licensed under the Apache License, Version 2.0 (the "License");
54
# you may not use this file except in compliance with the License.
@@ -18,85 +17,120 @@
1817
"""Tests for Interactions API URL paths."""
1918

2019
from unittest import mock
21-
import pytest
20+
import google.auth
21+
from httpx import Client as HTTPClient
2222
from httpx import Request, Response
23+
import pytest
2324
from ..._api_client import AsyncHttpxClient
24-
from httpx import Client as HTTPClient
2525
from .. import pytest_helper
26-
import google.auth
2726

28-
@mock.patch.object(google.auth, "default", autospec=True)
29-
def test_interactions_paths(mock_auth_default, client):
30-
interaction_id = "test-interaction-id"
3127

28+
@mock.patch.object(google.auth, 'default', autospec=True)
29+
def test_interactions_paths(mock_auth_default, client):
30+
interaction_id = 'test-interaction-id'
3231
mock_creds = mock.Mock()
33-
mock_creds.token = "test-token"
32+
mock_creds.token = 'test-token'
3433
mock_creds.expired = False
35-
mock_creds.quota_project_id = "test-quota-project"
36-
mock_auth_default.return_value = (mock_creds, "test-project")
34+
mock_creds.quota_project_id = 'test-quota-project'
35+
mock_auth_default.return_value = (mock_creds, 'test-project')
3736

3837
if client._api_client.vertexai:
39-
expected_base_url = f'https://{client._api_client.location}-aiplatform.googleapis.com/v1beta1/projects/{client._api_client.project}/locations/{client._api_client.location}'
38+
if client._api_client.location == 'global':
39+
expected_base_url = (
40+
'https://aiplatform.googleapis.com'
41+
f'/v1beta1/projects/{client._api_client.project}'
42+
'/locations/global'
43+
)
44+
else:
45+
expected_base_url = (
46+
f'https://{client._api_client.location}-aiplatform.googleapis.com'
47+
f'/v1beta1/projects/{client._api_client.project}'
48+
f'/locations/{client._api_client.location}'
49+
)
4050
else:
41-
expected_base_url = "https://generativelanguage.googleapis.com/v1beta"
51+
expected_base_url = 'https://generativelanguage.googleapis.com/v1beta'
4252

43-
with mock.patch.object(HTTPClient, "send") as mock_send:
53+
with mock.patch.object(HTTPClient, 'send') as mock_send:
4454
mock_send.return_value = Response(200, request=Request('GET', ''))
4555
client.interactions.get(id=interaction_id)
4656
mock_send.assert_called_once()
4757
request = mock_send.call_args[0][0]
48-
assert str(request.url) == f'{expected_base_url}/interactions/{interaction_id}'
58+
assert str(request.url) == (
59+
f'{expected_base_url}/interactions/{interaction_id}'
60+
)
4961

5062
mock_send.reset_mock()
5163
mock_send.return_value = Response(200, request=Request('POST', ''))
5264
client.interactions.cancel(id=interaction_id)
5365
mock_send.assert_called_once()
5466
request = mock_send.call_args[0][0]
55-
assert str(request.url) == f'{expected_base_url}/interactions/{interaction_id}/cancel'
67+
assert str(request.url) == (
68+
f'{expected_base_url}/interactions/{interaction_id}/cancel'
69+
)
5670

5771
mock_send.reset_mock()
5872
mock_send.return_value = Response(200, request=Request('DELETE', ''))
5973
client.interactions.delete(id=interaction_id)
6074
mock_send.assert_called_once()
6175
request = mock_send.call_args[0][0]
62-
assert str(request.url) == f'{expected_base_url}/interactions/{interaction_id}'
76+
assert str(request.url) == (
77+
f'{expected_base_url}/interactions/{interaction_id}'
78+
)
79+
6380

6481
@pytest.mark.asyncio
65-
@mock.patch.object(google.auth, "default", autospec=True)
82+
@mock.patch.object(google.auth, 'default', autospec=True)
6683
async def test_async_interactions_paths(mock_auth_default, client):
67-
interaction_id = "test-interaction-id"
68-
84+
interaction_id = 'test-interaction-id'
6985
mock_creds = mock.Mock()
70-
mock_creds.token = "test-token"
86+
mock_creds.token = 'test-token'
7187
mock_creds.expired = False
72-
mock_creds.quota_project_id = "test-quota-project"
73-
mock_auth_default.return_value = (mock_creds, "test-project")
88+
mock_creds.quota_project_id = 'test-quota-project'
89+
mock_auth_default.return_value = (mock_creds, 'test-project')
7490

7591
if client._api_client.vertexai:
76-
expected_base_url = f'https://{client._api_client.location}-aiplatform.googleapis.com/v1beta1/projects/{client._api_client.project}/locations/{client._api_client.location}'
92+
if client._api_client.location == 'global':
93+
expected_base_url = (
94+
'https://aiplatform.googleapis.com'
95+
f'/v1beta1/projects/{client._api_client.project}'
96+
'/locations/global'
97+
)
98+
else:
99+
expected_base_url = (
100+
f'https://{client._api_client.location}-aiplatform.googleapis.com'
101+
f'/v1beta1/projects/{client._api_client.project}'
102+
f'/locations/{client._api_client.location}'
103+
)
77104
else:
78-
expected_base_url = "https://generativelanguage.googleapis.com/v1beta"
105+
expected_base_url = 'https://generativelanguage.googleapis.com/v1beta'
79106

80-
with mock.patch.object(AsyncHttpxClient, "send") as mock_send:
107+
with mock.patch.object(AsyncHttpxClient, 'send') as mock_send:
81108
mock_send.return_value = Response(200, request=Request('GET', ''))
82109
await client.aio.interactions.get(id=interaction_id)
83110
mock_send.assert_called_once()
84111
request = mock_send.call_args[0][0]
85-
assert str(request.url) == f'{expected_base_url}/interactions/{interaction_id}'
112+
assert str(request.url) == (
113+
f'{expected_base_url}/interactions/{interaction_id}'
114+
)
86115

87116
mock_send.reset_mock()
88117
mock_send.return_value = Response(200, request=Request('POST', ''))
89118
await client.aio.interactions.cancel(id=interaction_id)
90119
mock_send.assert_called_once()
91120
request = mock_send.call_args[0][0]
92-
assert str(request.url) == f'{expected_base_url}/interactions/{interaction_id}/cancel'
121+
assert str(request.url) == (
122+
f'{expected_base_url}/interactions/{interaction_id}/cancel'
123+
)
93124

94125
mock_send.reset_mock()
95126
mock_send.return_value = Response(200, request=Request('DELETE', ''))
96127
await client.aio.interactions.delete(id=interaction_id)
97128
mock_send.assert_called_once()
98129
request = mock_send.call_args[0][0]
99-
assert str(request.url) == f'{expected_base_url}/interactions/{interaction_id}'
130+
assert str(request.url) == (
131+
f'{expected_base_url}/interactions/{interaction_id}'
132+
)
133+
100134

101135
pytestmark = pytest_helper.setup(
102136
file=__file__,

0 commit comments

Comments
 (0)