@@ -5822,3 +5822,264 @@ async def test_async_generate_user_scenarios(self):
58225822 assert len (eval_dataset .eval_dataset_df ) == 2
58235823
58245824 self .mock_api_client .async_request .assert_called_once ()
5825+
5826+
5827+ class TestCreateEvaluationSetFromDataFrame :
5828+ """Unit tests for the _create_evaluation_set_from_dataframe function."""
5829+
5830+ def setup_method (self ):
5831+ self .mock_api_client = mock .Mock (spec = client .Client )
5832+ self .mock_api_client .project = "test-project"
5833+ self .mock_api_client .location = "us-central1"
5834+
5835+ @mock .patch .object (_evals_common , "evals" )
5836+ @mock .patch .object (_evals_common , "_gcs_utils" )
5837+ def test_create_evaluation_set_with_intermediate_events (
5838+ self , mock_gcs_utils , mock_evals_module
5839+ ):
5840+ intermediate_events = [
5841+ {
5842+ "content" : {"parts" : [{"text" : "thought 1" }]},
5843+ "timestamp" : "2024-01-01T00:00:00Z" ,
5844+ },
5845+ {
5846+ "content" : {"parts" : [{"functionCall" : {"name" : "foo" }}]},
5847+ "timestamp" : "2024-01-01T00:00:01Z" ,
5848+ },
5849+ ]
5850+
5851+ eval_df = pd .DataFrame (
5852+ [
5853+ {
5854+ "prompt" : "test prompt" ,
5855+ "response" : "test response" ,
5856+ "intermediate_events" : intermediate_events ,
5857+ }
5858+ ]
5859+ )
5860+
5861+ mock_gcs_instance = mock_gcs_utils .GcsUtils .return_value
5862+ mock_gcs_instance .upload_json_to_prefix .return_value = (
5863+ "gs://bucket/path/request.json"
5864+ )
5865+
5866+ mock_evals_instance = mock_evals_module .Evals .return_value
5867+ mock_eval_item = mock .Mock ()
5868+ mock_eval_item .name = "eval_item_1"
5869+ mock_evals_instance .create_evaluation_item .return_value = mock_eval_item
5870+
5871+ mock_eval_set = mock .Mock ()
5872+ mock_evals_instance .create_evaluation_set .return_value = mock_eval_set
5873+
5874+ result = _evals_common ._create_evaluation_set_from_dataframe (
5875+ api_client = self .mock_api_client ,
5876+ gcs_dest_prefix = "gs://bucket/prefix" ,
5877+ eval_df = eval_df ,
5878+ candidate_name = "test-candidate" ,
5879+ )
5880+
5881+ assert result == mock_eval_set
5882+
5883+ mock_gcs_instance .upload_json_to_prefix .assert_called_once ()
5884+ call_args = mock_gcs_instance .upload_json_to_prefix .call_args
5885+ uploaded_data = call_args .kwargs ["data" ]
5886+
5887+ candidate_responses = uploaded_data ["candidate_responses" ]
5888+ assert len (candidate_responses ) == 1
5889+ candidate_response = candidate_responses [0 ]
5890+ assert candidate_response ["candidate" ] == "test-candidate"
5891+ assert candidate_response ["text" ] == "test response"
5892+
5893+ expected_events = [
5894+ {"parts" : [{"text" : "thought 1" }]},
5895+ {"parts" : [{"function_call" : {"name" : "foo" }}]},
5896+ ]
5897+ assert candidate_response ["events" ] == expected_events
5898+
5899+ @mock .patch .object (_evals_common , "evals" )
5900+ @mock .patch .object (_evals_common , "_gcs_utils" )
5901+ def test_create_evaluation_set_with_user_scenario (
5902+ self , mock_gcs_utils , mock_evals_module
5903+ ):
5904+ eval_df = pd .DataFrame (
5905+ [
5906+ {
5907+ "starting_prompt" : "test starting prompt" ,
5908+ "conversation_plan" : "test conversation plan" ,
5909+ }
5910+ ]
5911+ )
5912+
5913+ mock_gcs_instance = mock_gcs_utils .GcsUtils .return_value
5914+ mock_gcs_instance .upload_json_to_prefix .return_value = (
5915+ "gs://bucket/path/request.json"
5916+ )
5917+
5918+ mock_evals_instance = mock_evals_module .Evals .return_value
5919+ mock_eval_item = mock .Mock ()
5920+ mock_eval_item .name = "eval_item_1"
5921+ mock_evals_instance .create_evaluation_item .return_value = mock_eval_item
5922+
5923+ mock_eval_set = mock .Mock ()
5924+ mock_evals_instance .create_evaluation_set .return_value = mock_eval_set
5925+
5926+ result = _evals_common ._create_evaluation_set_from_dataframe (
5927+ api_client = self .mock_api_client ,
5928+ gcs_dest_prefix = "gs://bucket/prefix" ,
5929+ eval_df = eval_df ,
5930+ candidate_name = "test-candidate" ,
5931+ )
5932+
5933+ assert result == mock_eval_set
5934+
5935+ mock_gcs_instance .upload_json_to_prefix .assert_called_once ()
5936+ call_args = mock_gcs_instance .upload_json_to_prefix .call_args
5937+ uploaded_data = call_args .kwargs ["data" ]
5938+
5939+ assert uploaded_data .get ("candidate_responses" ) is None
5940+ assert uploaded_data ["prompt" ]["user_scenario" ] == {
5941+ "starting_prompt" : "test starting prompt" ,
5942+ "conversation_plan" : "test conversation plan" ,
5943+ }
5944+
5945+ @mock .patch .object (_evals_common , "evals" )
5946+ @mock .patch .object (_evals_common , "_gcs_utils" )
5947+ def test_create_evaluation_set_with_agent_data (
5948+ self , mock_gcs_utils , mock_evals_module
5949+ ):
5950+ agent_data = {"turns" : [{"turn_id" : "turn1" , "events" : []}]}
5951+ eval_df = pd .DataFrame (
5952+ [
5953+ {
5954+ "prompt" : "test prompt" ,
5955+ "agent_data" : agent_data ,
5956+ }
5957+ ]
5958+ )
5959+
5960+ mock_gcs_instance = mock_gcs_utils .GcsUtils .return_value
5961+ mock_gcs_instance .upload_json_to_prefix .return_value = (
5962+ "gs://bucket/path/request.json"
5963+ )
5964+
5965+ mock_evals_instance = mock_evals_module .Evals .return_value
5966+ mock_eval_item = mock .Mock ()
5967+ mock_eval_item .name = "eval_item_1"
5968+ mock_evals_instance .create_evaluation_item .return_value = mock_eval_item
5969+
5970+ mock_eval_set = mock .Mock ()
5971+ mock_evals_instance .create_evaluation_set .return_value = mock_eval_set
5972+
5973+ result = _evals_common ._create_evaluation_set_from_dataframe (
5974+ api_client = self .mock_api_client ,
5975+ gcs_dest_prefix = "gs://bucket/prefix" ,
5976+ eval_df = eval_df ,
5977+ candidate_name = "test-candidate" ,
5978+ )
5979+
5980+ assert result == mock_eval_set
5981+
5982+ mock_gcs_instance .upload_json_to_prefix .assert_called_once ()
5983+ call_args = mock_gcs_instance .upload_json_to_prefix .call_args
5984+ uploaded_data = call_args .kwargs ["data" ]
5985+
5986+ assert uploaded_data ["prompt" ]["text" ] == "test prompt"
5987+ candidate_responses = uploaded_data ["candidate_responses" ]
5988+ assert len (candidate_responses ) == 1
5989+ candidate_response = candidate_responses [0 ]
5990+ assert candidate_response ["candidate" ] == "test-candidate"
5991+ assert candidate_response ["agent_data" ] == agent_data
5992+
5993+ @mock .patch .object (_evals_common , "evals" )
5994+ @mock .patch .object (_evals_common , "_gcs_utils" )
5995+ def test_create_evaluation_set_with_user_scenario (
5996+ self , mock_gcs_utils , mock_evals_module
5997+ ):
5998+ eval_df = pd .DataFrame (
5999+ [
6000+ {
6001+ "starting_prompt" : "test starting prompt" ,
6002+ "conversation_plan" : "test conversation plan" ,
6003+ }
6004+ ]
6005+ )
6006+
6007+ mock_gcs_instance = mock_gcs_utils .GcsUtils .return_value
6008+ mock_gcs_instance .upload_json_to_prefix .return_value = (
6009+ "gs://bucket/path/request.json"
6010+ )
6011+
6012+ mock_evals_instance = mock_evals_module .Evals .return_value
6013+ mock_eval_item = mock .Mock ()
6014+ mock_eval_item .name = "eval_item_1"
6015+ mock_evals_instance .create_evaluation_item .return_value = mock_eval_item
6016+
6017+ mock_eval_set = mock .Mock ()
6018+ mock_evals_instance .create_evaluation_set .return_value = mock_eval_set
6019+
6020+ result = _evals_common ._create_evaluation_set_from_dataframe (
6021+ api_client = self .mock_api_client ,
6022+ gcs_dest_prefix = "gs://bucket/prefix" ,
6023+ eval_df = eval_df ,
6024+ candidate_name = "test-candidate" ,
6025+ )
6026+
6027+ assert result == mock_eval_set
6028+
6029+ mock_gcs_instance .upload_json_to_prefix .assert_called_once ()
6030+ call_args = mock_gcs_instance .upload_json_to_prefix .call_args
6031+ uploaded_data = call_args .kwargs ["data" ]
6032+
6033+ assert uploaded_data .get ("candidate_responses" ) is None
6034+ assert uploaded_data ["prompt" ]["user_scenario" ] == {
6035+ "starting_prompt" : "test starting prompt" ,
6036+ "conversation_plan" : "test conversation plan" ,
6037+ }
6038+
6039+ @mock .patch .object (_evals_common , "evals" )
6040+ @mock .patch .object (_evals_common , "_gcs_utils" )
6041+ def test_create_evaluation_set_with_agent_data (
6042+ self , mock_gcs_utils , mock_evals_module
6043+ ):
6044+ agent_data = {"turns" : [{"turn_id" : "turn1" , "events" : []}]}
6045+ eval_df = pd .DataFrame (
6046+ [
6047+ {
6048+ "prompt" : "test prompt" ,
6049+ "agent_data" : agent_data ,
6050+ }
6051+ ]
6052+ )
6053+
6054+ mock_gcs_instance = mock_gcs_utils .GcsUtils .return_value
6055+ mock_gcs_instance .upload_json_to_prefix .return_value = (
6056+ "gs://bucket/path/request.json"
6057+ )
6058+
6059+ mock_evals_instance = mock_evals_module .Evals .return_value
6060+ mock_eval_item = mock .Mock ()
6061+ mock_eval_item .name = "eval_item_1"
6062+ mock_evals_instance .create_evaluation_item .return_value = mock_eval_item
6063+
6064+ mock_eval_set = mock .Mock ()
6065+ mock_evals_instance .create_evaluation_set .return_value = mock_eval_set
6066+
6067+ result = _evals_common ._create_evaluation_set_from_dataframe (
6068+ api_client = self .mock_api_client ,
6069+ gcs_dest_prefix = "gs://bucket/prefix" ,
6070+ eval_df = eval_df ,
6071+ candidate_name = "test-candidate" ,
6072+ )
6073+
6074+ assert result == mock_eval_set
6075+
6076+ mock_gcs_instance .upload_json_to_prefix .assert_called_once ()
6077+ call_args = mock_gcs_instance .upload_json_to_prefix .call_args
6078+ uploaded_data = call_args .kwargs ["data" ]
6079+
6080+ assert uploaded_data ["prompt" ]["text" ] == "test prompt"
6081+ candidate_responses = uploaded_data ["candidate_responses" ]
6082+ assert len (candidate_responses ) == 1
6083+ candidate_response = candidate_responses [0 ]
6084+ assert candidate_response ["candidate" ] == "test-candidate"
6085+ assert candidate_response ["agent_data" ] == agent_data
0 commit comments