@@ -973,3 +973,86 @@ def test_execute_sql_result_dtype(
973973 # Test the tool worked without invoking default auth
974974 result = execute_sql (project , query , credentials , tool_config , tool_context )
975975 assert result == {"status" : "SUCCESS" , "rows" : tool_result_rows }
976+
977+
978+ def test_execute_sql_max_rows_config ():
979+ """Test execute_sql tool respects max_downloaded_rows from config."""
980+ project = "my_project"
981+ query = "SELECT 123 AS num"
982+ statement_type = "SELECT"
983+ query_result = [{"num" : i } for i in range (20 )] # 20 rows
984+ credentials = mock .create_autospec (Credentials , instance = True )
985+ tool_config = BigQueryToolConfig (max_downloaded_rows = 10 )
986+ tool_context = mock .create_autospec (ToolContext , instance = True )
987+
988+ with mock .patch ("google.cloud.bigquery.Client" , autospec = False ) as Client :
989+ bq_client = Client .return_value
990+ query_job = mock .create_autospec (bigquery .QueryJob )
991+ query_job .statement_type = statement_type
992+ bq_client .query .return_value = query_job
993+ bq_client .query_and_wait .return_value = query_result [:10 ]
994+
995+ result = execute_sql (project , query , credentials , tool_config , tool_context )
996+
997+ # Check that max_results was called with config value
998+ bq_client .query_and_wait .assert_called_once ()
999+ call_args = bq_client .query_and_wait .call_args
1000+ assert call_args .kwargs ["max_results" ] == 10
1001+
1002+ # Check truncation flag is set
1003+ assert result ["status" ] == "SUCCESS"
1004+ assert result ["result_is_likely_truncated" ] is True
1005+
1006+
1007+ def test_execute_sql_max_rows_parameter ():
1008+ """Test execute_sql tool respects max_rows parameter."""
1009+ project = "my_project"
1010+ query = "SELECT 123 AS num"
1011+ statement_type = "SELECT"
1012+ query_result = [{"num" : i } for i in range (20 )] # 20 rows
1013+ credentials = mock .create_autospec (Credentials , instance = True )
1014+ tool_config = BigQueryToolConfig (max_downloaded_rows = 10 )
1015+ tool_context = mock .create_autospec (ToolContext , instance = True )
1016+
1017+ with mock .patch ("google.cloud.bigquery.Client" , autospec = False ) as Client :
1018+ bq_client = Client .return_value
1019+ query_job = mock .create_autospec (bigquery .QueryJob )
1020+ query_job .statement_type = statement_type
1021+ bq_client .query .return_value = query_job
1022+ bq_client .query_and_wait .return_value = query_result [:5 ]
1023+
1024+ # Parameter should override config
1025+ result = execute_sql (project , query , credentials , tool_config , tool_context , max_rows = 5 )
1026+
1027+ # Check that max_results was called with parameter value
1028+ bq_client .query_and_wait .assert_called_once ()
1029+ call_args = bq_client .query_and_wait .call_args
1030+ assert call_args .kwargs ["max_results" ] == 5
1031+
1032+ # Check truncation flag is set
1033+ assert result ["status" ] == "SUCCESS"
1034+ assert result ["result_is_likely_truncated" ] is True
1035+
1036+
1037+ def test_execute_sql_no_truncation ():
1038+ """Test execute_sql tool when results are not truncated."""
1039+ project = "my_project"
1040+ query = "SELECT 123 AS num"
1041+ statement_type = "SELECT"
1042+ query_result = [{"num" : i } for i in range (3 )] # Only 3 rows
1043+ credentials = mock .create_autospec (Credentials , instance = True )
1044+ tool_config = BigQueryToolConfig (max_downloaded_rows = 10 )
1045+ tool_context = mock .create_autospec (ToolContext , instance = True )
1046+
1047+ with mock .patch ("google.cloud.bigquery.Client" , autospec = False ) as Client :
1048+ bq_client = Client .return_value
1049+ query_job = mock .create_autospec (bigquery .QueryJob )
1050+ query_job .statement_type = statement_type
1051+ bq_client .query .return_value = query_job
1052+ bq_client .query_and_wait .return_value = query_result
1053+
1054+ result = execute_sql (project , query , credentials , tool_config , tool_context )
1055+
1056+ # Check no truncation flag when fewer rows than limit
1057+ assert result ["status" ] == "SUCCESS"
1058+ assert "result_is_likely_truncated" not in result
0 commit comments