o
    Yi_                      @   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__Z__test__r   r   r   r   __annotations__ r   r   Z/home/kim/smarthome/.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 )NZvertexZmldev/.Fz&Should have raised exception in MLDev.z'Should have raised exception in Vertex.'z
' not in 'r   )Zoverride_replay_idname_api_clientZinitialize_replay_sessionvarsr   copysplitgetattrexception_if_mldevvertexaiexception_if_vertexclose	ExceptionstrAssertionError)clientr   r   r   r   r   Z	replay_idZapi_typeZparameters_dictZmethod_name_partsZmodule_path_partsmethod_nameZcurrent_objectpartmethodZcustom_methoder   r   r   base_test_function+   s^   


r0   c                    s    fddS )Nc                    s   t | || S N)r0   )r+   r   r   r   r   r   r   r   <lambda>e   s    z,create_test_for_table_item.<locals>.<lambda>r   r2   r   r2   r   create_test_for_table_item`   s   r4   
test_tablec                 C   sD   |D ]}|j r|jdsJ d|j dt| ||| |j< qd S )NZ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())Z	has_unionr   
startswithr4   )r   r   r5   r   r   r   r   create_test_for_tableo   s   
	r7   )r   r   r5   http_optionsfiler8   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.Ztestsz.py z/test_r   FZUNITTEST_ON_FORGENTZ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   Zparameter_namesr5   )r<   wr   )Zexclude_noneZby_aliasindentz(use_vertex, replays_prefix, http_options)replaceospathdirname__file__environgetr7   
ValueErrorjoinpathlibPathmkdir
isinstancer   r   r
   ZTestTableFilelistZmodel_fieldskeysmakedirsopenwriteZmodel_dump_jsonpytestmarkZparametrize)r9   r   r   r5   r8   Zreplays_directoryZis_tap_modeZreplays_root_directoryZabs_replay_directoryZtest_table_file_pathZtest_table_filefr   r   r   setup   s^   

rU   exception_typec                 C   s   | j jrt S t|S r1   )r   r%   
contextlibnullcontextrR   raisesr+   rV   r   r   r   r$      s   
r$   c                 C   s   | j jr	t|S t S r1   )r   r%   rR   rY   rW   rX   rZ   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   r3      s    z snake_to_camel.<locals>.<lambda>)resub)r[   r   r   r   snake_to_camel   s   rc   	camel_strc                 C   s   t dd|  dS )z*Converts a CamelCase string to snake_case.z([A-Z])z_\1_)ra   rb   lowerlstrip)rd   r   r   r   camel_to_snake   s   rh   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)rF   rc   rh   )objkeyr   r   r   get_value_ignore_key_case   s   rk   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>)rL   dictitemsr)   rh   rl   rM   )dataZnew_dictrj   valueZnew_keyr   r   r   rl      s   



rl   )%rW   jsonrA   rI   typingr   r   Zpydanticr   r   r   rR   ra   r:   r   r	   r
   r   r   Zis_api_moder   boolr)   rp   r0   r4   rM   r7   rU   typer(   r$   r&   rc   rh   rk   rl   r   r   r   r   <module>   sx   


5





@