
    5P@i>                       U d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	 ddlm
Z
 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddgiZddgiZddgiZdddgiZdaded<   daded<   g dZd0dZ G d dej:                        Zej>                  jA                  ejB                  dejB                  e"   i       G d de             Z#ej>                  jA                  ejB                  dejB                  e"   i       G d  d!e             Z$ej>                  jA                  ejB                  dejB                  e"   i       G d" d#e             Z%ej>                  jA                  ejB                  dejB                  e"   i       G d$ d%e             Z& ejN                   ejP                         d&g'      Z) ejN                   ejP                         d(g'      Z*d1d)Z+ej>                  jA                  ejB                  dejB                  e"   i       G d* d+e             Z,ej>                  jA                  ejB                  dejB                  e"   i       G d, d-e             Z-ej>                  jA                  ejB                  dejB                  e"   i       G d. d/e             Z.e"dk(  r ej^                          yy)2zUnittests for cmd_line_parser.    )annotationsN)mock)absltest)argument_parser)cmd_line_parser)model_registry)parsed_args_lib)post_process_utils)llm_function)llmfn_output_row)llmfn_outputsmodelwordonetwothreenot_wordhelloworldz!llmfn_outputs.LLMFnOutputs | None_OUTPUT_VAR_ONE_OUTPUT_VAR_TWOapplebanana
cantaloupec                   |j                  t        j                  t        j                  ddi t	        j
                  t        j                  j                  dt        j                  j                  | it              g      g             y )Nr   )dataresult_type)
prompt_num	input_numprompt_varsoutput_rows)outputs)
write_outputsr   LLMFnOutputsLLMFnOutputEntryr   LLMFnOutputRowColumnNames
RESULT_NUMTEXT_RESULTstrtext_resultsinks     {/home/www/lebenam.kofcorporation.com/venv/lib/python3.12/site-packages/google/generativeai/notebook/cmd_line_parser_test.py_set_output_sinkr1   *   sy    ""..  "(77 - 9 9 D Da - 9 9 E E" ),!		
    c                       e Zd Z fdZ xZS )CmdLineParserTestBasec                *    t         |           d ad ay N)supersetUpr   r   )self	__class__s    r0   r8   zCmdLineParserTestBase.setUpB   s    
 r2   )__name__
__module____qualname__r8   __classcell__)r:   s   @r0   r4   r4   A   s     r2   r4   __main__c                  "    e Zd ZdZd Zd Zd Zy)CmdLineParserCommonTestz/For tests that are not specific to any command.c                T   t        j                         }| j                  t        j                        5  |j                  d       d d d        | j                  t        j                        5  |j                  d       d d d        | j                  t        j                        5  |j                  d       d d d        | j                  t        j                        5  |j                  d       d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   [xY w# 1 sw Y   y xY w)Nz-hz--helpzrun -hz
run --help)r   CmdLineParserassertRaisesr   ParserNormalExit
parse_liner9   parsers     r0   test_parse_args_helpz,CmdLineParserCommonTest.test_parse_args_helpQ   s     ..0??@ 	$d#	$??@ 	(h'	(??@ 	(h'	(??@ 	,l+	, 	,	$ 	$	( 	(	( 	(	, 	,s/   C:-D&DD:DDDD'c                   t        j                         }|j                  d      \  }}| j                  t        j
                  j                  |j                         | j                  t        j                  j                  |j                         | j                  g |j                         | j                  t        j                         |j                         y )N )r   rC   rF   assertEqualr	   CommandNameRUN_CMDcmdr   	ModelName
TEXT_MODEL
model_typeinputs	model_libModelArguments
model_argsr9   rH   results_s       r0   test_parse_args_emptyz-CmdLineParserCommonTest.test_parse_args_empty\   s     ..0&&r*
44<<gkkJ11<<g>P>PQW^^,$$&	
r2   c                   t        j                         }|j                  d      \  }}| j                  |j                  d       | j                  ddig|j                  d   j                                |j                  d      \  }}| j                  |j                  d       | j                  ddig|j                  d   j                                | j                  dd	ig|j                  d   j                                y )
Nz--inputs _INPUT_VAR_ONE   r   r   r   z(--inputs _INPUT_VAR_TWO _INPUT_VAR_THREE   r   r   )r   rC   rF   	assertLenrS   rL   to_normalized_inputsrW   s       r0   test_parse_args_no_reusez0CmdLineParserCommonTest.test_parse_args_no_reuseg   s     ..0&&'@A
w~~q)65/*GNN1,=,R,R,TU &&'QR
w~~q)65/*GNN1,=,R,R,TU67+,gnnQ.?.T.T.VWr2   N)r;   r<   r=   __doc__rI   rZ   r`    r2   r0   rA   rA   M   s    9	,	

Xr2   rA   c                  *    e Zd Zd Zd Zd Zd Zd Zy)CmdLineParserModelFlagsTestc                L   t        j                         }|j                  d      \  }}| j                  t        j
                  j                  |j                         |j                  d      \  }}| j                  t        j
                  j                  |j                         y )N--model_type=echoz--model_type=text)	r   rC   rF   rL   r   rP   
ECHO_MODELrR   rQ   rW   s       r0   test_parse_args_sets_model_typez;CmdLineParserModelFlagsTest.test_parse_args_sets_model_typew   s~     ..0&&':;
11<<g>P>PQ&&':;
11<<g>P>PQr2   c                    t        j                         }|j                  d      \  }}| j                  t	        j
                  d      |j                         y )Nz--model=/ml/testz/ml/testr   )r   rC   rF   rL   rT   rU   rV   rW   s       r0   test_parse_args_sets_modelz6CmdLineParserModelFlagsTest.test_parse_args_sets_model   sF     ..0&&'9:
11
CWEWEWXr2   c                   t        j                         }|j                  d      \  }}| j                  t	        j
                  d      |j                         |j                  d      \  }}| j                  t	        j
                  d      |j                         | j                  t        j                  d      5  |j                  d       d d d        y # 1 sw Y   y xY w)Nz--temperature=0r   )temperaturez--temperature=0.5g      ?z`Error with value "-1.0", got ValueError: Value should be greater than or equal to zero, got -1.0z--temperature=-1
r   rC   rF   rL   rT   rU   rV   assertRaisesRegexr   ParserErrorrW   s       r0    test_parse_args_sets_temperaturez<CmdLineParserModelFlagsTest.test_parse_args_sets_temperature   s     ..0&&'89
11a@'BTBTU&&':;
11cBGDVDVW##''3
 	2 01	2 	2 	2s   =CC!c                T   t        j                         }|j                  d      \  }}| j                  t	        j
                  d      |j                         |j                  d      \  }}| j                  t	        j
                  d      |j                         | j                  t        j                  d      5  |j                  d       d d d        | j                  t        j                  d      5  |j                  d	       d d d        y # 1 sw Y   DxY w# 1 sw Y   y xY w)
Nz--candidate_count=1r\   )candidate_countz--candidate_count=8   zRError with value "0", got ValueError: Value should be in the range \[1, 8\], got 0z--candidate_count=0zRError with value "9", got ValueError: Value should be in the range \[1, 8\], got 9z--candidate_count=9rm   rW   s       r0   $test_parse_args_sets_candidate_countz@CmdLineParserModelFlagsTest.test_parse_args_sets_candidate_count   s	    ..0&&'<=
11!DgFXFXY&&'<=
11!DgFXFXY ##''*
 	5 34	5 ##''*
 	5 34	5 	5	5 	5	5 	5s   =D7DDD'c                    t        j                         }|j                  d      \  }}| j                  |j                         |j                  d      \  }}| j                  |j                         y )NrK   z--unique)r   rC   rF   assertFalseunique
assertTruerW   s       r0   test_parse_args_sets_uniquez7CmdLineParserModelFlagsTest.test_parse_args_sets_unique   sZ     ..0&&r*
(&&z2
'r2   N)r;   r<   r=   rh   rj   rp   rt   ry   rb   r2   r0   rd   rd   u   s    RY
2"56(r2   rd   c                  "    e Zd ZdZd Zd Zd Zy)CmdLineParserRunTestFor the "run" command.c                $   t        j                         }|j                  d      \  }}| j                  t        j
                  j                  |j                         | j                  t        j                  j                  |j                         y )Nrf   )r   rC   rF   rL   r	   rM   rN   rO   r   rP   rg   rR   rW   s       r0   test_parse_args_run_is_defaultz3CmdLineParserRunTest.test_parse_args_run_is_default   sg     ..0&&':;
44<<gkkJ11<<g>P>PQr2   c                J   t        j                         }|j                  d      \  }}| j                  t        j
                  j                  |j                         | j                  t        j                  j                  |j                         | j                  |j                  d       | j                  ddig|j                  d   j                                | j                  ddig|j                  d   j                                | j                  |j                  d       t!        d|j                  d   	       | j#                  t$        t&        j(                         | j                  dt$        d   j*                  d   t&        j,                  j.                            t!        d|j                  d   	       | j#                  t0        t&        j(                         | j                  dt0        d   j*                  d   t&        j,                  j.                            t!        d
|j                  d   	       t2        }| j#                  |t&        j(                         | j                  d
|d   j*                  d   t&        j,                  j.                            y )Nz}run --model_type=echo --inputs _INPUT_VAR_ONE _INPUT_VAR_TWO --outputs _OUTPUT_VAR_ONE _OUTPUT_VAR_TWO _UNDECLARED_OUTPUT_VARr]   r   r   r   r   r\      r-   
undeclared)r   rC   rF   rL   r	   rM   rN   rO   r   rP   rg   rR   r^   rS   r_   r$   r1   assertIsInstancer   r   r&   r#   r)   r+   r   _UNDECLARED_OUTPUT_VAR)r9   rH   rX   rY   undeclared_vars        r0    test_parse_input_and_output_argsz5CmdLineParserRunTest.test_parse_input_and_output_args   s	    ..0&&F

 	44<<gkkJ11<<g>P>PQw~~q)65/*GNN1,=,R,R,TU65/*GNN1,=,R,R,TUw* 	U1CDo}/I/IJA**1-m.G.G.S.ST	
 	U1CDo}/I/IJA**1-m.G.G.S.ST	
 	\8JK/nm.H.HI1))!,]-F-F-R-RS	
r2   c                    t        j                         }| j                  t        j                  d      5  |j                  dt        dh             d d d        y # 1 sw Y   y xY w)Nsargument --inputs/-i: Error with value "_NOT_WORD_INPUT_VAR", got ValueError: Placeholder "word" not found in inputz run --inputs _NOT_WORD_INPUT_VARr   placeholdersr   rC   rn   r   ro   rF   	frozensetrG   s     r0   test_placeholder_errorz+CmdLineParserRunTest.test_placeholder_error   sd     ..0##''E
 
	 2&x0  
	 
	 
	   AA%N)r;   r<   r=   ra   r~   r   r   rb   r2   r0   r{   r{      s     R$
Lr2   r{   c                  "    e Zd ZdZd Zd Zd Zy)CmdLineParserCompileTestzFor the "compile" command.c                    t        j                         }| j                  t        j                  d      5  |j                  d       d d d        y # 1 sw Y   y xY w)Nz7the following arguments are required: compile_save_namecompiler   rC   rn   r   ro   rF   rG   s     r0   test_parse_args_needs_save_namez8CmdLineParserCompileTest.test_parse_args_needs_save_name   sO     ..0##''E
 	) i(		) 	) 	)   AAc                    t        j                         }| j                  t        j                  d      5  |j                  d       d d d        y # 1 sw Y   y xY w)NzInvalid Python variable namezcompile 1234r   rG   s     r0   test_parse_args_bad_save_namez6CmdLineParserCompileTest.test_parse_args_bad_save_name  sJ     ..0##O$?$?A_` 	.n-	. 	. 	.r   c                    t        j                         }|j                  d      \  }}| j                  d|j                         y )Nzcompile my_fnmy_fn)r   rC   rF   rL   compile_save_namerW   s       r0   test_parse_args_has_save_namez6CmdLineParserCompileTest.test_parse_args_has_save_name  s:     ..0&&7
'";";<r2   N)r;   r<   r=   ra   r   r   r   rb   r2   r0   r   r      s    $).
=r2   r   zdummy lhs prompt {word})r   promptszdummy rhs prompt {word}c                    | |k(  S r6   rb   )lhsrhss     r0   _test_compare_fnr     s    #:r2   c                  "    e Zd ZdZd Zd Zd Zy)CmdLineParserCompareTestzFor the "compare" command.c                   t        j                         }|j                  d      \  }}| j                  dt        f|j
                         | j                  dt        f|j                         | j                  |j                         y )Nz9compare _test_lhs_fn _test_rhs_fn --inputs _INPUT_VAR_ONE_test_lhs_fn_test_rhs_fn)
r   rC   rF   rL   r   lhs_name_and_fnr   rhs_name_and_fnassertEmpty
compare_fnrW   s       r0   test_comparez%CmdLineParserCompareTest.test_compare#  so     ..0&&'bc
.,79P9PQ.,79P9PQ++,r2   c                "   t        j                         }|j                  d      \  }}| j                  dt        f|j
                         | j                  dt        f|j                         | j                  dt        fg|j                         y )NzWcompare _test_lhs_fn _test_rhs_fn --inputs _INPUT_VAR_ONE --compare_fn _test_compare_fnr   r   r   )
r   rC   rF   rL   r   r   r   r   r   r   rW   s       r0   #test_compare_with_custom_compare_fnz<CmdLineParserCompareTest.test_compare_with_custom_compare_fn*  s     ..0&& 

 	.,79P9PQ.,79P9PQ-/?@A7CUCUVr2   c                    t        j                         }| j                  t        j                  d      5  |j                  d       d d d        y # 1 sw Y   y xY w)Nr   z>compare _test_lhs_fn _test_rhs_fn --inputs _NOT_WORD_INPUT_VARr   rG   s     r0   r   z/CmdLineParserCompareTest.test_placeholder_error4  sW     ..0##''E
 	` ^_	` 	` 	`r   N)r;   r<   r=   ra   r   r   r   rb   r2   r0   r   r     s    $-W	`r2   r   c                      e Zd ZdZd Zd Zy)CmdLineParserEvalTestzFor the "eval" command.c                    t        j                         }|j                  d      \  }}| j                  g d|j                         y )Nz=eval --ground_truth _GROUND_TRUTH_VAR --inputs _INPUT_VAR_ONEr   )r   rC   rF   rL   ground_truthrW   s       r0   	test_evalzCmdLineParserEvalTest.test_evalE  s@     ..0&&K

 	:G<P<PQr2   c                    t        j                         }| j                  t        j                  d      5  |j                  dt        dh             d d d        y # 1 sw Y   y xY w)Nr   zBeval --ground_truth _GROUND_TRUTH_VAR --inputs _NOT_WORD_INPUT_VARr   r   r   rG   s     r0   r   z,CmdLineParserEvalTest.test_placeholder_errorL  sd     ..0##''E
 
	 T&x0  
	 
	 
	r   N)r;   r<   r=   ra   r   r   rb   r2   r0   r   r   A  s    !Rr2   r   c                  "    e Zd ZdZd Zd Zd Zy)CmdLineParserPostProcessingTestr|   c                    t        j                         }|j                  d      \  }}| j                  |d       | j	                  dg|d          | j	                  dg|d          y )Nz| add_length | to_upperr]   
add_lengthr   to_upperr\   )r   rC   rF   r^   rL   )r9   rH   rY   post_process_exprss       r0   test_parse_tokensz1CmdLineParserPostProcessingTest.test_parse_tokens`  sf     ..0 & 1 12K L)1-,);A)>?*'9!'<=r2   c                   t        j                         }| j                  t        j                  d      5  |j                  d       d d d        | j                  t        j                  d      5  |j                  d       d d d        | j                  t        j                  d      5  |j                  d       d d d        y # 1 sw Y   ~xY w# 1 sw Y   PxY w# 1 sw Y   y xY w)Nz,Cannot have empty post-processing expressionz| | to_upperz| z| add_length |r   rC   rn   r
   PostProcessParseErrorrF   rG   s     r0   test_illformed_expressionz9CmdLineParserPostProcessingTest.test_illformed_expressiong  s     ..0##44:
 	. n-		. ##44:
 	$ d#		$ ##44:
 	0 ./		0 	0	. 	.	$ 	$	0 	0s#   C/C)CCCC%c                    t        j                         }| j                  t        j                  d      5  |j                  d       d d d        y # 1 sw Y   y xY w)Nz3Post-processing expression should be a single tokenz| add_length to_upperr   rG   s     r0   3test_cannot_parse_multiple_tokens_in_one_expressionzSCmdLineParserPostProcessingTest.test_cannot_parse_multiple_tokens_in_one_expression{  sP     ..0##44A
 	7 56		7 	7 	7r   N)r;   r<   r=   ra   r   r   r   rb   r2   r0   r   r   \  s     >0(7r2   r   )r.   r,   r/   zllmfn_outputs.LLMFnOutputsSinkreturnNone)r   r,   r   r,   r   bool)0ra   
__future__r   sysunittestr   absl.testingr   google.generativeai.notebookr   r   r   r	   r
    google.generativeai.notebook.libr   r   r   r   rT   _INPUT_VAR_ONE_INPUT_VAR_TWO_INPUT_VAR_THREE_NOT_WORD_INPUT_VARr   __annotations__r   _GROUND_TRUTH_VARr1   TestCaser4   patchdictmodulesr;   rA   rd   r{   r   LLMFunctionImpl	EchoModelr   r   r   r   r   r   mainrb   r2   r0   <module>r      s   % " 
  ! 8 8 7 8 ; 9 = : ? 5'"5'"WI& !GW#56 592 9592 95 .H--  z3;;x+@AB#X3 #X C#XN z3;;x+@AB@("7 @( C@(H z3;;x+@AB;0 ; C;~ z3;;x+@AB=4 = C=, ,|++
)


*C)D ,|++
)


*C)D

 z3;;x+@AB`4 ` C`B z3;;x+@AB1  C4 z3;;x+@AB$7&; $7 C$7N zHMMO r2   