o
    _i_                      @   s  d dl Z d dlZd dlZd dlZd dlmZmZ d dlmZm	Z	m
Z
 d dl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	ZG d
d dejZdededededeeef f
ddZdeeef dedefddZdeeef dedee fddZddddddedeeeef  dee deee  dee f
ddZdee fddZdee fd d!Z d"ed#efd$d%Z!d&ed#efd'd(Z"d)d* Z#d+d, Z$dS )-    N)AnyOptional)	BaseModelFieldSerializeAsAny   )_common)_replay_api_client)types)HttpOptionsz#config.getoption('--mode') == 'api'c                   @   s(   e Zd ZU dZeddZee ed< dS )TestTableItemFz0The parameters to the test. Use pydantic models.)description
parametersN)	__name__
__module____qualname____test__r   r   r   r   __annotations__ r   r   Y/home/air/demo/back/venv/lib/python3.10/site-packages/google/genai/tests/pytest_helper.pyr   !   s
   
 r   
use_vertexreplays_prefixtest_methodtest_table_itemglobals_for_filec              
   C   s  |j r|j n|j}|rdnd}| d| d| }| j| t|j }zTd|v rS|d}	|	d d }
|	d }| }|
D ]}t||}q>t||}|di | n
|| }|| |j |j	rh| jj
shJ d|jrs| jj
rsJ d| j  W d S  ty } zL|j	r| jj
s|j	t|vrtd	|j	 d
t| d	|n |jr| jj
r|jt|vrtd	|j d
t| d	|n	|W Y d }~d S W Y d }~d S d }~ww )Nvertexmldev/.Fz&Should have raised exception in MLDev.z'Should have raised exception in Vertex.'z
' not in 'r   )override_replay_idname_api_clientinitialize_replay_sessionvarsr   copysplitgetattrexception_if_mldevvertexaiexception_if_vertexclose	ExceptionstrAssertionError)clientr   r   r   r   r   	replay_idapi_typeparameters_dictmethod_name_partsmodule_path_partsmethod_namecurrent_objectpartmethodcustom_methoder   r   r   base_test_function+   s^   


r<   c                    s    fddS )Nc                    s   t | || S N)r<   )r0   r   r   r   r   r   r   r   <lambda>e   s    z,create_test_for_table_item.<locals>.<lambda>r   r>   r   r>   r   create_test_for_table_item`   s   r@   
test_tablec                 C   sD   |D ]}|j r|jdsJ d|j dt| ||| |j< qd S )Ntest_union_z
      test: a  
      When has_union is true, it must be a test that explicitly test the
      non-canonical type in the union.
      For all other tests, use transformers to convert data to the canonical
      type of the field so it can be tested in other languages.
      If this test is truly about testing the non-canonical type of the union,
      rename the test to start with 'test_union_'. E.g.
      test_union_contents_is_string())	has_unionr"   
startswithr@   )r   r   rA   r   r   r   r   create_test_for_tableo   s   
	rE   )r   r   rA   http_optionsfilerF   c                 C   sX  |  tjtd dd dd}d}tjdddurd	}|r't||| |r|stjd
d}|du r:tdtj	||}tj	|d}	t
|jd	d	d t|d jtsaJ |	 dtjd|t|d jj |d}
tjtj|	d	d t|	d}||
jd	d	dd W d   n1 sw   Y  tjdd	||fd||fgS )zDGenerates parameterization for tests, run for both Vertex and MLDev.testsz.py z/test_r   FUNITTEST_ON_FORGENTGOOGLE_GENAI_REPLAYS_DIRECTORYz>GOOGLE_GENAI_REPLAYS_DIRECTORY environment variable is not setz_test_table.json)parentsexist_okr   z  parameters must be a BaseModel.zAuto-generated. Do not edit.)commentr   parameter_namesrA   )rM   wr   )exclude_noneby_aliasindentz(use_vertex, replays_prefix, http_options)replaceospathdirname__file__environgetrE   
ValueErrorjoinpathlibPathmkdir
isinstancer   r   r
   TestTableFilelistmodel_fieldskeysmakedirsopenwritemodel_dump_jsonpytestmarkparametrize)rG   r   r   rA   rF   replays_directoryis_tap_modereplays_root_directoryabs_replay_directorytest_table_file_pathtest_table_filefr   r   r   setup   s^   

rs   exception_typec                 C   s   | j jrt S t|S r=   )r#   r*   
contextlibnullcontextri   raisesr0   rt   r   r   r   r)      s   
r)   c                 C   s   | j jr	t|S t S r=   )r#   r*   ri   rw   ru   rv   rx   r   r   r   r+      s   
r+   	snake_strreturnc                 C   s   t ddd | S )z*Converts a snake_case string to CamelCase.z_([a-zA-Z])c                 S   s   |  d S )N   )groupupper)matchr   r   r   r?      s    z snake_to_camel.<locals>.<lambda>)resub)ry   r   r   r   snake_to_camel   s   r   	camel_strc                 C   s   t dd|  dS )z*Converts a CamelCase string to snake_case.z([A-Z])z_\1_)r   r   lowerlstrip)r   r   r   r   camel_to_snake   s   r   c                 C   s   |  t||  t|dS )z_Returns the value of the key in the object, converting to camelCase or snake_case if necessary.N)rZ   r   r   )objkeyr   r   r   get_value_ignore_key_case   s   r   c                 C   sd   t | tr$i }|  D ]\}}t |trt|}n|}t|||< q|S t | tr0dd | D S | S )z8Converts all keys in a dictionary or list to snake_case.c                 S   s   g | ]}t |qS r   )camel_to_snake_all_keys).0itemr   r   r   
<listcomp>   s    z+camel_to_snake_all_keys.<locals>.<listcomp>)r`   dictitemsr.   r   r   rb   )datanew_dictr   valuenew_keyr   r   r   r      s   



r   )%ru   jsonrU   r]   typingr   r   pydanticr   r   r   ri   r   rI   r   r	   r
   r#   r   is_api_moder   boolr.   r   r<   r@   rb   rE   rs   typer-   r)   r+   r   r   r   r   r   r   r   r   <module>   sx   


5





@