Skip to content

Commit fd33610

Browse files
lupuleticcopybara-github
authored andcommitted
test: add tests for max_query_result_rows in BigQuery tool config
Merge #2167, during which: - Let the already added `max_query_result_rows` field cover for `max_downloaded_rows` field added in the PR - Revert `max_rows` parameter added to execute_sql function, the tool config control should serve most practical use cases - Keep the relevant tests for tool config 🤖 Generated with [Claude Code](https://claude.ai/code) COPYBARA_INTEGRATE_REVIEW=#2167 from lupuletic:feature/configurable-max-downloaded-rows 23c5690 PiperOrigin-RevId: 830701093
1 parent 2b0f953 commit fd33610

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

tests/unittests/tools/bigquery/test_bigquery_query_tool.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,3 +1773,56 @@ def test_ml_tool_job_labels(tool_call, expected_label):
17731773
assert mock_kwargs["job_config"].labels == {
17741774
"adk-bigquery-tool": expected_label
17751775
}
1776+
1777+
1778+
def test_execute_sql_max_rows_config():
1779+
"""Test execute_sql tool respects max_query_result_rows from config."""
1780+
project = "my_project"
1781+
query = "SELECT 123 AS num"
1782+
statement_type = "SELECT"
1783+
query_result = [{"num": i} for i in range(20)] # 20 rows
1784+
credentials = mock.create_autospec(Credentials, instance=True)
1785+
tool_config = BigQueryToolConfig(max_query_result_rows=10)
1786+
tool_context = mock.create_autospec(ToolContext, instance=True)
1787+
1788+
with mock.patch("google.cloud.bigquery.Client", autospec=False) as Client:
1789+
bq_client = Client.return_value
1790+
query_job = mock.create_autospec(bigquery.QueryJob)
1791+
query_job.statement_type = statement_type
1792+
bq_client.query.return_value = query_job
1793+
bq_client.query_and_wait.return_value = query_result[:10]
1794+
1795+
result = execute_sql(project, query, credentials, tool_config, tool_context)
1796+
1797+
# Check that max_results was called with config value
1798+
bq_client.query_and_wait.assert_called_once()
1799+
call_args = bq_client.query_and_wait.call_args
1800+
assert call_args.kwargs["max_results"] == 10
1801+
1802+
# Check truncation flag is set
1803+
assert result["status"] == "SUCCESS"
1804+
assert result["result_is_likely_truncated"] is True
1805+
1806+
1807+
def test_execute_sql_no_truncation():
1808+
"""Test execute_sql tool when results are not truncated."""
1809+
project = "my_project"
1810+
query = "SELECT 123 AS num"
1811+
statement_type = "SELECT"
1812+
query_result = [{"num": i} for i in range(3)] # Only 3 rows
1813+
credentials = mock.create_autospec(Credentials, instance=True)
1814+
tool_config = BigQueryToolConfig(max_query_result_rows=10)
1815+
tool_context = mock.create_autospec(ToolContext, instance=True)
1816+
1817+
with mock.patch("google.cloud.bigquery.Client", autospec=False) as Client:
1818+
bq_client = Client.return_value
1819+
query_job = mock.create_autospec(bigquery.QueryJob)
1820+
query_job.statement_type = statement_type
1821+
bq_client.query.return_value = query_job
1822+
bq_client.query_and_wait.return_value = query_result
1823+
1824+
result = execute_sql(project, query, credentials, tool_config, tool_context)
1825+
1826+
# Check no truncation flag when fewer rows than limit
1827+
assert result["status"] == "SUCCESS"
1828+
assert "result_is_likely_truncated" not in result

tests/unittests/tools/bigquery/test_bigquery_tool_config.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,17 @@ def test_bigquery_tool_config_invalid_application_name():
4242
match="Application name should not contain spaces.",
4343
):
4444
BigQueryToolConfig(application_name="my agent")
45+
46+
47+
def test_bigquery_tool_config_max_query_result_rows_default():
48+
"""Test BigQueryToolConfig max_query_result_rows default value."""
49+
with pytest.warns(UserWarning):
50+
config = BigQueryToolConfig()
51+
assert config.max_query_result_rows == 50
52+
53+
54+
def test_bigquery_tool_config_max_query_result_rows_custom():
55+
"""Test BigQueryToolConfig max_query_result_rows custom value."""
56+
with pytest.warns(UserWarning):
57+
config = BigQueryToolConfig(max_query_result_rows=100)
58+
assert config.max_query_result_rows == 100

0 commit comments

Comments
 (0)