o
    pkjV                     @   s  d Z ddlZddlZddl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 ejd	d
ddg ddZdd Zdd Zdd Zejdd Zejdddd Zejdd ZdededefddZejd d! Z
ejd"d# Zejd$d% Zejd&d' ZdS )(z Conftest for google.genai tests.    N)mock   )_common)_replay_api_client)client)types   g      ?g      $@g       @)i  i  i  i  i  i  )attemptsinitial_delay	max_delayexp_basehttp_status_codesc                 C   s   dt | jt jv S )z=True only for the curated cross-SDK suite under tests/shared.shared)osfspathpathsplitsep)request r   Y/home/djax/ivt_ai_plugin/venv/lib/python3.10/site-packages/google/genai/tests/conftest.py_is_shared_integration_test.   s   r   c                 C   sf   | du r
t jtdS t| tr#| ds| dr| S i | dtiS t| ddr+| S | jdtidS )zFAdds the shared retry options, leaving any caller-supplied ones alone.N)retry_optionsr   retryOptions)update)r   HttpOptions_SHARED_TEST_RETRY_OPTIONS
isinstancedictgetgetattr
model_copy)http_optionsr   r   r   _with_shared_test_retry_options3   s   
r#   c                 C   s(   | j ddddd | j dddd	d d S )
N--modestoreautoae  Replay mode.
  One of:
  * auto: Replay if replay files exist, otherwise record.
  * record: Always call the API and record.
  * replay: Always replay, fail if replay files do not exist.
  * api: Always call the API and do not record.
  * tap: Always replay, fail if replay files do not exist. Also sets default values for the API key and replay directory.
)actiondefaulthelp	--private
store_trueFzRun private tests.)	addoption)parserr   r   r   pytest_addoptionB   s   
r.   c                   C      dS )NFr   r   r   r   r   
use_vertexY      r0   T)autousec                 C   s   | j d}|dkrPd }t| dr t| jdr | jjjd}n
d| jv r*| d}t	j
d}t	j
d}|du rC|rCtd	 d S |d
u rR|rTtd d S d S d S d S )Nr$   apinodecallspecr0   (GOOGLE_GENAI_RUN_VERTEX_ONLY_IN_API_MODE(GOOGLE_GENAI_RUN_GEMINI_ONLY_IN_API_MODETzBSkipping Vertex AI tests in API mode (GEMINI ONLY config enabled).FzCSkipping Gemini API tests in API mode (VERTEX ONLY config enabled).)config	getoptionhasattrr4   r5   paramsr   fixturenamesgetfixturevaluer   environpytestskip)r   moder0   run_vertex_onlyrun_gemini_onlyr   r   r   skip_based_on_api_mode_env_vars^   s   

rD   c                   C   r/   )Ntestr   r   r   r   r   replays_prefixr   r1   rF   returnc                 C   sL   t jddd }|dd dd d | rdnd	 }d
||gS )NPYTEST_CURRENT_TESTz:: r   [.vertexmldev/)r   r>   r   r   join)r0   rF   test_name_ending	test_namer   r   r   _get_replay_idw   s   
rS   c                 c   s   |j d}|dvrtd| |jj}tjtj|jd }|	|r/td| ddD ]}||v r?td| d	q1t
| |}|d
kr|d}dtjd< tjtjtdtjd< dtjd< dtjd< tjtjtjtd}	|	tjd< |j d}
d }| rd|v rtjddkrd}|dkrt|rt|}tj||| ||
|d}tjtjd&}||_d| i}|r||d< tjdi |}|V  |j  W d    d S 1 sw   Y  d S )Nr$   )r&   recordreplayr3   tapzInvalid mode: r   z
      zl:
      Do not include the test filename in the test function name.
      keep the test function name short.)rN   ml_devrM   z	
        z:
        You should never ever include api types in the file name (mldev or vertex).
        All tests should run against all apis.
        Assert an exception if the test is not supported in an API.rV   rU   zdummy-api-keyGOOGLE_API_KEYzcredentials.jsonGOOGLE_APPLICATION_CREDENTIALSz
project-idGOOGLE_CLOUD_PROJECTlocationGOOGLE_CLOUD_LOCATIONz8../../../../../google/cloud/aiplatform/sdk/genai/replaysGOOGLE_GENAI_REPLAYS_DIRECTORYr*   tuningsglobalzus-central1r3   )rA   	replay_idvertexair"   privater[   _get_api_clientra   r   )r8   r9   
ValueErrorfunction__name__r   r   splitextbasename
startswithrS   r>   rP   dirname__file__abspathr   r   r#   r   ReplayApiClientr   patchobjectgoogle_genai_client_moduleClientreturn_value_api_clientclose)r0   rF   r"   r   rA   test_function_nametest_filenamewordr`   replays_root_directoryrb   location_overridereplay_clientpatch_methodkwargsgoogle_genai_clientr   r   r   r      sx   










	"r   c                  c   s@    t jjtddd} | V  W d    d S 1 sw   Y  d S )Ntimestamped_unique_name20240101000000_bd656)rr   )r   rn   ro   r   )unique_name_mockr   r   r   mock_timestamped_unique_name   s   "r   c                  c   Z    dd l } tjtjtdd}| j|}|V  W d    d S 1 s&w   Y  d S )Nr   dataz
google.jpg	PIL.Imager   r   rP   rj   rk   ImageopenPIL
image_pathimgr   r   r   
image_jpeg      "r   c                  c   r   )Nr   r   z
google.pngr   r   r   r   r   	image_png   r   r   )__doc__datetimer   unittestr   uuidr?    r   r   r   rp   r   HttpRetryOptionsr   r   r#   r.   fixturer0   rD   rF   boolstrrS   r   r   r   r   r   r   r   <module>   sF   		





O
	
