Skip to content

Commit b7d571b

Browse files
google-genai-botcopybara-github
authored andcommitted
chore: make readability improvements in ADK BQ tool tests
PiperOrigin-RevId: 832110063
1 parent 5adbf95 commit b7d571b

File tree

5 files changed

+116
-116
lines changed

5 files changed

+116
-116
lines changed

tests/unittests/tools/bigquery/test_bigquery_client.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
import google.adk
2121
from google.adk.tools.bigquery.client import get_bigquery_client
22+
import google.auth
2223
from google.auth.exceptions import DefaultCredentialsError
24+
from google.cloud.bigquery import client as bigquery_client
2325
from google.oauth2.credentials import Credentials
2426

2527

@@ -41,7 +43,9 @@ def test_bigquery_client_project_set_explicit():
4143
# Let's simulate that no environment variables are set, so that any project
4244
# set in there does not interfere with this test
4345
with mock.patch.dict(os.environ, {}, clear=True):
44-
with mock.patch("google.auth.default", autospec=True) as mock_default_auth:
46+
with mock.patch.object(
47+
google.auth, "default", autospec=True
48+
) as mock_default_auth:
4549
# Simulate exception from default auth
4650
mock_default_auth.side_effect = DefaultCredentialsError(
4751
"Your default credentials were not found"
@@ -66,7 +70,9 @@ def test_bigquery_client_project_set_with_default_auth():
6670
# Let's simulate that no environment variables are set, so that any project
6771
# set in there does not interfere with this test
6872
with mock.patch.dict(os.environ, {}, clear=True):
69-
with mock.patch("google.auth.default", autospec=True) as mock_default_auth:
73+
with mock.patch.object(
74+
google.auth, "default", autospec=True
75+
) as mock_default_auth:
7076
# Simulate credentials
7177
mock_creds = mock.create_autospec(Credentials, instance=True)
7278

@@ -90,7 +96,9 @@ def test_bigquery_client_project_set_with_env():
9096
with mock.patch.dict(
9197
os.environ, {"GOOGLE_CLOUD_PROJECT": "test-gcp-project"}, clear=True
9298
):
93-
with mock.patch("google.auth.default", autospec=True) as mock_default_auth:
99+
with mock.patch.object(
100+
google.auth, "default", autospec=True
101+
) as mock_default_auth:
94102
# Simulate exception from default auth
95103
mock_default_auth.side_effect = DefaultCredentialsError(
96104
"Your default credentials were not found"
@@ -112,8 +120,8 @@ def test_bigquery_client_project_set_with_env():
112120

113121
def test_bigquery_client_user_agent_default():
114122
"""Test BigQuery client default user agent."""
115-
with mock.patch(
116-
"google.cloud.bigquery.client.Connection", autospec=True
123+
with mock.patch.object(
124+
bigquery_client, "Connection", autospec=True
117125
) as mock_connection:
118126
# Trigger the BigQuery client creation
119127
get_bigquery_client(
@@ -134,8 +142,8 @@ def test_bigquery_client_user_agent_default():
134142

135143
def test_bigquery_client_user_agent_custom():
136144
"""Test BigQuery client custom user agent."""
137-
with mock.patch(
138-
"google.cloud.bigquery.client.Connection", autospec=True
145+
with mock.patch.object(
146+
bigquery_client, "Connection", autospec=True
139147
) as mock_connection:
140148
# Trigger the BigQuery client creation
141149
get_bigquery_client(
@@ -158,8 +166,8 @@ def test_bigquery_client_user_agent_custom():
158166

159167
def test_bigquery_client_user_agent_custom_list():
160168
"""Test BigQuery client custom user agent."""
161-
with mock.patch(
162-
"google.cloud.bigquery.client.Connection", autospec=True
169+
with mock.patch.object(
170+
bigquery_client, "Connection", autospec=True
163171
) as mock_connection:
164172
# Trigger the BigQuery client creation
165173
get_bigquery_client(

tests/unittests/tools/bigquery/test_bigquery_credentials.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ def test_valid_credentials_object_auth_credentials(self):
3636
to pass them directly without needing to provide client ID/secret.
3737
"""
3838
# Create a mock auth credentials object
39-
# auth_creds = google.auth.credentials.Credentials()
4039
auth_creds = mock.create_autospec(
4140
google.auth.credentials.Credentials, instance=True
4241
)

tests/unittests/tools/bigquery/test_bigquery_data_insights_tool.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
pytest.param("test_data/ask_data_insights_penguins_highest_mass.yaml"),
2727
],
2828
)
29-
@mock.patch(
30-
"google.adk.tools.bigquery.data_insights_tool.requests.Session.post"
31-
)
29+
@mock.patch.object(data_insights_tool.requests.Session, "post")
3230
def test_ask_data_insights_pipeline_from_file(mock_post, case_file_path):
3331
"""Runs a full integration test for the ask_data_insights pipeline using data from a specific file."""
3432
# 1. Construct the full, absolute path to the data file
@@ -65,7 +63,7 @@ def test_ask_data_insights_pipeline_from_file(mock_post, case_file_path):
6563
assert result == expected_final_list
6664

6765

68-
@mock.patch("google.adk.tools.bigquery.data_insights_tool._get_stream")
66+
@mock.patch.object(data_insights_tool, "_get_stream")
6967
def test_ask_data_insights_success(mock_get_stream):
7068
"""Tests the success path of ask_data_insights using decorators."""
7169
# 1. Configure the behavior of the mocked functions
@@ -92,7 +90,7 @@ def test_ask_data_insights_success(mock_get_stream):
9290
mock_get_stream.assert_called_once()
9391

9492

95-
@mock.patch("google.adk.tools.bigquery.data_insights_tool._get_stream")
93+
@mock.patch.object(data_insights_tool, "_get_stream")
9694
def test_ask_data_insights_handles_exception(mock_get_stream):
9795
"""Tests the exception path of ask_data_insights using decorators."""
9896
# 1. Configure one of the mocks to raise an error

tests/unittests/tools/bigquery/test_bigquery_metadata_tool.py

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@
1717
import os
1818
from unittest import mock
1919

20+
from google.adk.tools.bigquery import client as bq_client_lib
2021
from google.adk.tools.bigquery import metadata_tool
2122
from google.adk.tools.bigquery.config import BigQueryToolConfig
23+
import google.auth
2224
from google.auth.exceptions import DefaultCredentialsError
2325
from google.cloud import bigquery
2426
from google.oauth2.credentials import Credentials
2527

2628

2729
@mock.patch.dict(os.environ, {}, clear=True)
28-
@mock.patch("google.cloud.bigquery.Client.list_datasets", autospec=True)
29-
@mock.patch("google.auth.default", autospec=True)
30+
@mock.patch.object(bigquery.Client, "list_datasets", autospec=True)
31+
@mock.patch.object(google.auth, "default", autospec=True)
3032
def test_list_dataset_ids_no_default_auth(
3133
mock_default_auth, mock_list_datasets
3234
):
@@ -53,8 +55,8 @@ def test_list_dataset_ids_no_default_auth(
5355

5456

5557
@mock.patch.dict(os.environ, {}, clear=True)
56-
@mock.patch("google.cloud.bigquery.Client.get_dataset", autospec=True)
57-
@mock.patch("google.auth.default", autospec=True)
58+
@mock.patch.object(bigquery.Client, "get_dataset", autospec=True)
59+
@mock.patch.object(google.auth, "default", autospec=True)
5860
def test_get_dataset_info_no_default_auth(mock_default_auth, mock_get_dataset):
5961
"""Test get_dataset_info tool invocation involves no default auth."""
6062
mock_credentials = mock.create_autospec(Credentials, instance=True)
@@ -80,8 +82,8 @@ def test_get_dataset_info_no_default_auth(mock_default_auth, mock_get_dataset):
8082

8183

8284
@mock.patch.dict(os.environ, {}, clear=True)
83-
@mock.patch("google.cloud.bigquery.Client.list_tables", autospec=True)
84-
@mock.patch("google.auth.default", autospec=True)
85+
@mock.patch.object(bigquery.Client, "list_tables", autospec=True)
86+
@mock.patch.object(google.auth, "default", autospec=True)
8587
def test_list_table_ids_no_default_auth(mock_default_auth, mock_list_tables):
8688
"""Test list_table_ids tool invocation involves no default auth."""
8789
project = "my_project_id"
@@ -108,8 +110,8 @@ def test_list_table_ids_no_default_auth(mock_default_auth, mock_list_tables):
108110

109111

110112
@mock.patch.dict(os.environ, {}, clear=True)
111-
@mock.patch("google.cloud.bigquery.Client.get_table", autospec=True)
112-
@mock.patch("google.auth.default", autospec=True)
113+
@mock.patch.object(bigquery.Client, "get_table", autospec=True)
114+
@mock.patch.object(google.auth, "default", autospec=True)
113115
def test_get_table_info_no_default_auth(mock_default_auth, mock_get_table):
114116
"""Test get_table_info tool invocation involves no default auth."""
115117
mock_credentials = mock.create_autospec(Credentials, instance=True)
@@ -137,8 +139,8 @@ def test_get_table_info_no_default_auth(mock_default_auth, mock_get_table):
137139

138140

139141
@mock.patch.dict(os.environ, {}, clear=True)
140-
@mock.patch("google.cloud.bigquery.Client.get_job", autospec=True)
141-
@mock.patch("google.auth.default", autospec=True)
142+
@mock.patch.object(bigquery.Client, "get_job", autospec=True)
143+
@mock.patch.object(google.auth, "default", autospec=True)
142144
def test_get_job_info_no_default_auth(mock_default_auth, mock_get_job):
143145
"""Test get_job_info tool invocation involves no default auth."""
144146
mock_credentials = mock.create_autospec(Credentials, instance=True)
@@ -166,9 +168,7 @@ def test_get_job_info_no_default_auth(mock_default_auth, mock_get_job):
166168
mock_default_auth.assert_not_called()
167169

168170

169-
@mock.patch(
170-
"google.adk.tools.bigquery.client.get_bigquery_client", autospec=True
171-
)
171+
@mock.patch.object(bq_client_lib, "get_bigquery_client", autospec=True)
172172
def test_list_dataset_ids_bq_client_creation(mock_get_bigquery_client):
173173
"""Test BigQuery client creation params during list_dataset_ids tool invocation."""
174174
bq_project = "my_project_id"
@@ -189,9 +189,7 @@ def test_list_dataset_ids_bq_client_creation(mock_get_bigquery_client):
189189
]
190190

191191

192-
@mock.patch(
193-
"google.adk.tools.bigquery.client.get_bigquery_client", autospec=True
194-
)
192+
@mock.patch.object(bq_client_lib, "get_bigquery_client", autospec=True)
195193
def test_get_dataset_info_bq_client_creation(mock_get_bigquery_client):
196194
"""Test BigQuery client creation params during get_dataset_info tool invocation."""
197195
bq_project = "my_project_id"
@@ -215,9 +213,7 @@ def test_get_dataset_info_bq_client_creation(mock_get_bigquery_client):
215213
]
216214

217215

218-
@mock.patch(
219-
"google.adk.tools.bigquery.client.get_bigquery_client", autospec=True
220-
)
216+
@mock.patch.object(bq_client_lib, "get_bigquery_client", autospec=True)
221217
def test_list_table_ids_bq_client_creation(mock_get_bigquery_client):
222218
"""Test BigQuery client creation params during list_table_ids tool invocation."""
223219
bq_project = "my_project_id"
@@ -241,9 +237,7 @@ def test_list_table_ids_bq_client_creation(mock_get_bigquery_client):
241237
]
242238

243239

244-
@mock.patch(
245-
"google.adk.tools.bigquery.client.get_bigquery_client", autospec=True
246-
)
240+
@mock.patch.object(bq_client_lib, "get_bigquery_client", autospec=True)
247241
def test_get_table_info_bq_client_creation(mock_get_bigquery_client):
248242
"""Test BigQuery client creation params during get_table_info tool invocation."""
249243
bq_project = "my_project_id"
@@ -268,9 +262,7 @@ def test_get_table_info_bq_client_creation(mock_get_bigquery_client):
268262
]
269263

270264

271-
@mock.patch(
272-
"google.adk.tools.bigquery.client.get_bigquery_client", autospec=True
273-
)
265+
@mock.patch.object(bq_client_lib, "get_bigquery_client", autospec=True)
274266
def test_get_job_info_bq_client_creation(mock_get_bigquery_client):
275267
"""Test BigQuery client creation params during get_table_info tool invocation."""
276268
bq_project = "my_project_id"

0 commit comments

Comments
 (0)