U
    hm;                     @  s  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< dddgZddddd d!ZG d"d# d#ejZej ej!d$ej!e" iG d%d& d&eZ#ej ej!d$ej!e" iG d'd( d(eZ$ej ej!d$ej!e" iG d)d* d*eZ%ej ej!d$ej!e" iG d+d, d,eZ&ej'e( d-gd.Z)ej'e( d/gd.Z*ddd0d1d2d3Z+ej ej!d$ej!e" iG d4d5 d5eZ,ej ej!d$ej!e" iG d6d7 d7eZ-ej ej!d$ej!e" iG d8d9 d9eZ.e"d$kre/  dS ):z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threeZnot_wordZhelloZworldz!llmfn_outputs.LLMFnOutputs | None_OUTPUT_VAR_ONE_OUTPUT_VAR_TWOapplebanana
cantaloupestrzllmfn_outputs.LLMFnOutputsSinkNone)text_resultsinkreturnc                 C  sB   | tjtjddi tjtjjdtjj| it	dgdgd d S )Nr   )dataZresult_type)Z
prompt_numZ	input_numZprompt_varsoutput_rows)outputs)
Zwrite_outputsr   LLMFnOutputsZLLMFnOutputEntryr   ZLLMFnOutputRowColumnNamesZ
RESULT_NUMTEXT_RESULTr   r   r    r$   U/tmp/pip-unpacked-wheel-doshhd5e/google/generativeai/notebook/cmd_line_parser_test.py_set_output_sink*   s&     r&   c                      s   e Zd Z fddZ  ZS )CmdLineParserTestBasec                   s   t    d ad ad S N)supersetUpr   r   )self	__class__r$   r%   r*   G   s    
zCmdLineParserTestBase.setUp)__name__
__module____qualname__r*   __classcell__r$   r$   r,   r%   r'   E   s   r'   __main__c                   @  s(   e Zd ZdZdd Zdd Zdd ZdS )	CmdLineParserCommonTestz/For tests that are not specific to any command.c              	   C  s   t  }| tj |d W 5 Q R X | tj |d W 5 Q R X | tj |d W 5 Q R X | tj |d W 5 Q R X d S )Nz-hz--helpzrun -hz
run --help)r   CmdLineParserassertRaisesr   ZParserNormalExit
parse_liner+   parserr$   r$   r%   test_parse_args_helpV   s    z,CmdLineParserCommonTest.test_parse_args_helpc                 C  s^   t  }|d\}}| tjj|j | tj	j
|j | g |j | t |j d S )N )r   r4   r6   assertEqualr   CommandNameRUN_CMDcmdr   	ModelName
TEXT_MODEL
model_typeinputs	model_libModelArguments
model_argsr+   r8   results_r$   r$   r%   test_parse_args_emptya   s    z-CmdLineParserCommonTest.test_parse_args_emptyc                 C  s   t  }|d\}}| |jd | ddig|jd   |d\}}| |jd | ddig|jd   | dd	ig|jd   d S )
Nz--inputs _INPUT_VAR_ONE   r   r   r   z(--inputs _INPUT_VAR_TWO _INPUT_VAR_THREE   r   r   )r   r4   r6   	assertLenrB   r;   to_normalized_inputsrF   r$   r$   r%   test_parse_args_no_reusel   s"       z0CmdLineParserCommonTest.test_parse_args_no_reuseN)r.   r/   r0   __doc__r9   rI   rN   r$   r$   r$   r%   r3   R   s   r3   c                   @  s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )CmdLineParserModelFlagsTestc                 C  sL   t  }|d\}}| tjj|j |d\}}| tjj|j d S )N--model_type=echoz--model_type=text)	r   r4   r6   r;   r   r?   
ECHO_MODELrA   r@   rF   r$   r$   r%   test_parse_args_sets_model_type   s
    z;CmdLineParserModelFlagsTest.test_parse_args_sets_model_typec                 C  s0   t  }|d\}}| tjdd|j d S )Nz--model=/ml/testz/ml/testr   )r   r4   r6   r;   rC   rD   rE   rF   r$   r$   r%   test_parse_args_sets_model   s    
 z6CmdLineParserModelFlagsTest.test_parse_args_sets_modelc              	   C  sx   t  }|d\}}| tjdd|j |d\}}| tjdd|j | tj	d |d W 5 Q R X d S )Nz--temperature=0r   )Z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   r4   r6   r;   rC   rD   rE   assertRaisesRegexr   ParserErrorrF   r$   r$   r%    test_parse_args_sets_temperature   s     
 
 z<CmdLineParserModelFlagsTest.test_parse_args_sets_temperaturec              	   C  s   t  }|d\}}| tjdd|j |d\}}| tjdd|j | tj	d |d W 5 Q R X | tj	d |d	 W 5 Q R X d S )
Nz--candidate_count=1rJ   )Z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=9rU   rF   r$   r$   r%   $test_parse_args_sets_candidate_count   s*    
 
 z@CmdLineParserModelFlagsTest.test_parse_args_sets_candidate_countc                 C  s@   t  }|d\}}| |j |d\}}| |j d S )Nr:   z--unique)r   r4   r6   ZassertFalseunique
assertTruerF   r$   r$   r%   test_parse_args_sets_unique   s
    z7CmdLineParserModelFlagsTest.test_parse_args_sets_uniqueN)r.   r/   r0   rS   rT   rX   rZ   r]   r$   r$   r$   r%   rP      s
   rP   c                   @  s(   e Zd ZdZdd Zdd Zdd ZdS )	CmdLineParserRunTestFor the "run" command.c                 C  s>   t  }|d\}}| tjj|j | tj	j
|j d S )NrQ   )r   r4   r6   r;   r   r<   r=   r>   r   r?   rR   rA   rF   r$   r$   r%   test_parse_args_run_is_default   s    z3CmdLineParserRunTest.test_parse_args_run_is_defaultc                 C  sP  t  }|d\}}| tjj|j | tj	j
|j | |jd | ddig|jd   | ddig|jd   | |jd td|jd d	 | ttj | dtd jd tjj  td|jd d	 | ttj | dtd jd tjj  td
|jd d	 t}| |tj | d
|d jd tjj  d S )Nz}run --model_type=echo --inputs _INPUT_VAR_ONE _INPUT_VAR_TWO --outputs _OUTPUT_VAR_ONE _OUTPUT_VAR_TWO _UNDECLARED_OUTPUT_VARrK   r   r   r   r   rJ      r#   Z
undeclared)r   r4   r6   r;   r   r<   r=   r>   r   r?   rR   rA   rL   rB   rM   r   r&   ZassertIsInstancer   r   r    r   r!   r"   r   Z_UNDECLARED_OUTPUT_VAR)r+   r8   rG   rH   Zundeclared_varr$   r$   r%    test_parse_input_and_output_args   sN      z5CmdLineParserRunTest.test_parse_input_and_output_argsc              	   C  s:   t  }| tjd |jdtdhd W 5 Q R X d S )Nsargument --inputs/-i: Error with value "_NOT_WORD_INPUT_VAR", got ValueError: Placeholder "word" not found in inputz run --inputs _NOT_WORD_INPUT_VARr   Zplaceholdersr   r4   rV   r   rW   r6   	frozensetr7   r$   r$   r%   test_placeholder_error  s     z+CmdLineParserRunTest.test_placeholder_errorN)r.   r/   r0   rO   r`   rb   rg   r$   r$   r$   r%   r^      s   .r^   c                   @  s(   e Zd ZdZdd Zdd Zdd ZdS )	CmdLineParserCompileTestzFor the "compile" command.c              	   C  s0   t  }| tjd |d W 5 Q R X d S )Nz7the following arguments are required: compile_save_namecompiler   r4   rV   r   rW   r6   r7   r$   r$   r%   test_parse_args_needs_save_name  s    z8CmdLineParserCompileTest.test_parse_args_needs_save_namec              	   C  s0   t  }| tjd |d W 5 Q R X d S )NzInvalid Python variable namezcompile 1234rj   r7   r$   r$   r%   test_parse_args_bad_save_name#  s     z6CmdLineParserCompileTest.test_parse_args_bad_save_namec                 C  s(   t  }|d\}}| d|j d S )Nzcompile my_fnZmy_fn)r   r4   r6   r;   Zcompile_save_namerF   r$   r$   r%   test_parse_args_has_save_name*  s    z6CmdLineParserCompileTest.test_parse_args_has_save_nameN)r.   r/   r0   rO   rk   rl   rm   r$   r$   r$   r%   rh     s   rh   zdummy lhs prompt {word})r   Zpromptszdummy rhs prompt {word}bool)lhsrhsr   c                 C  s   | |kS r(   r$   )ro   rp   r$   r$   r%   _test_compare_fn9  s    rq   c                   @  s(   e Zd ZdZdd Zdd Zdd ZdS )	CmdLineParserCompareTestzFor the "compare" command.c                 C  sJ   t  }|d\}}| dtf|j | dtf|j | |j	 d S )Nz9compare _test_lhs_fn _test_rhs_fn --inputs _INPUT_VAR_ONE_test_lhs_fn_test_rhs_fn)
r   r4   r6   r;   rs   lhs_name_and_fnrt   rhs_name_and_fnZassertEmpty
compare_fnrF   r$   r$   r%   test_compareB  s    z%CmdLineParserCompareTest.test_comparec                 C  sR   t  }|d\}}| dtf|j | dtf|j | dtfg|j	 d S )NzWcompare _test_lhs_fn _test_rhs_fn --inputs _INPUT_VAR_ONE --compare_fn _test_compare_fnrs   rt   rq   )
r   r4   r6   r;   rs   ru   rt   rv   rq   rw   rF   r$   r$   r%   #test_compare_with_custom_compare_fnK  s     z<CmdLineParserCompareTest.test_compare_with_custom_compare_fnc              	   C  s0   t  }| tjd |d W 5 Q R X d S )Nrc   z>compare _test_lhs_fn _test_rhs_fn --inputs _NOT_WORD_INPUT_VARrj   r7   r$   r$   r%   rg   W  s    z/CmdLineParserCompareTest.test_placeholder_errorN)r.   r/   r0   rO   rx   ry   rg   r$   r$   r$   r%   rr   >  s   	rr   c                   @  s    e Zd ZdZdd Zdd ZdS )CmdLineParserEvalTestzFor the "eval" command.c                 C  s.   t  }|d\}}| dddg|j d S )Nz=eval --ground_truth _GROUND_TRUTH_VAR --inputs _INPUT_VAR_ONEr   r   r   )r   r4   r6   r;   Zground_truthrF   r$   r$   r%   	test_evalj  s
    zCmdLineParserEvalTest.test_evalc              	   C  s:   t  }| tjd |jdtdhd W 5 Q R X d S )Nrc   zBeval --ground_truth _GROUND_TRUTH_VAR --inputs _NOT_WORD_INPUT_VARr   rd   re   r7   r$   r$   r%   rg   q  s    z,CmdLineParserEvalTest.test_placeholder_errorN)r.   r/   r0   rO   r{   rg   r$   r$   r$   r%   rz   f  s   rz   c                   @  s(   e Zd ZdZdd Zdd Zdd ZdS )	CmdLineParserPostProcessingTestr_   c                 C  sJ   t  }|d\}}| |d | dg|d  | dg|d  d S )Nz| add_length | to_upperrK   Z
add_lengthr   Zto_upperrJ   )r   r4   r6   rL   r;   )r+   r8   rH   Zpost_process_exprsr$   r$   r%   test_parse_tokens  s
    z1CmdLineParserPostProcessingTest.test_parse_tokensc              	   C  sx   t  }| tjd |d W 5 Q R X | tjd |d W 5 Q R X | tjd |d W 5 Q R X d S )Nz,Cannot have empty post-processing expressionz| | to_upperz| z| add_length |r   r4   rV   r	   ZPostProcessParseErrorr6   r7   r$   r$   r%   test_illformed_expression  s     z9CmdLineParserPostProcessingTest.test_illformed_expressionc              	   C  s0   t  }| tjd |d W 5 Q R X d S )Nz3Post-processing expression should be a single tokenz| add_length to_upperr~   r7   r$   r$   r%   3test_cannot_parse_multiple_tokens_in_one_expression  s    zSCmdLineParserPostProcessingTest.test_cannot_parse_multiple_tokens_in_one_expressionN)r.   r/   r0   rO   r}   r   r   r$   r$   r$   r%   r|     s   r|   )0rO   
__future__r   sysZunittestr   Zabsl.testingr   Zgoogle.generativeai.notebookr   r   r   r   r	   Z google.generativeai.notebook.libr
   r   r   r   rC   Z_INPUT_VAR_ONEZ_INPUT_VAR_TWOZ_INPUT_VAR_THREEZ_NOT_WORD_INPUT_VARr   __annotations__r   Z_GROUND_TRUTH_VARr&   ZTestCaser'   patchdictmodulesr.   r3   rP   r^   rh   ZLLMFunctionImplZ	EchoModelrs   rt   rq   rr   rz   r|   mainr$   r$   r$   r%   <module>   s^   



-OF  ''
