o
    i                     @   s  d Z ddlZddlmZ ddlmZ ddlZzddlZdZW n e	y-   dZe
 ZY nw ddlmZ dd	lmZ ejjed
dZdd Zdd Zejjdd Zejjdd Zejdd Zdd ZdefddZeejjejddddd Zeejjejddddd  ZdS )!z3Tests for closing the clients and context managers.    N)mock)credentialsFT   )_api_client)Clientz(aiohttp is not installed, skipping test.)reasonc                  C   s<   dt _tdddt jddidd} |   | jjjsJ d	S 
z@Tests that the httpx client is closed when the client is closed.FTtest_projectglobalmax_redirects
   )client_argsvertexaiprojectlocationhttp_optionsN)
api_clienthas_aiohttpr   HttpOptionscloser   _httpx_client	is_closedclient r   c/home/livre-enfant/venv/lib/python3.10/site-packages/google/genai/tests/client/test_client_close.pytest_close_httpx_client'   s   r   c                  C   sd   dt _tdddt jddidd} 	 | jjjrJ W d	   n1 s$w   Y  | jjjs0J d	S r   )r   r   r   r   r   r   r   r   r   r   r   !test_httpx_client_context_manager4   s   	r   c                     s8   dt _tddddj} |  I dH  | jjjsJ dS zFTests that the httpx async client is closed when the client is closed.FTr	   r
   )r   r   r   N)r   r   r   aioacloser   _async_httpx_clientr   async_clientr   r   r   test_aclose_httpx_clientC   s   r%   c               	      sn   dt _tddddj4 I dH } 	 | jjjrJ W d  I dH  n1 I dH s)w   Y  | jjjs5J dS r   )r   r   r   r    r   r"   r   r#   r   r   r   'test_async_httpx_client_context_managerP   s   
(r&   c                  c   s"    t jtjjd} | | _| V  d S )Nspec)r   MockaiohttpClientSessionrequestreturn_value)mock_aiohttp_responser   r   r   mock_request_   s   
r/   c                   C   s   t jdtddfddS )Nzgoogle.auth.defaultmagic_tokenr	   T)r-   autospec)r   patchr   Credentialsr   r   r   r   _patch_auth_defaultf   s
   r4   statusc                    s8   t jtjd}| |_dt| i|_i |j_d|j	_|S )z&Has to return a coroutine hence async.r'   zstatus-codetest)
r   r)   r*   ClientResponser5   strheadersjsonr-   text)r5   responser   r   r   _aiohttp_async_responsen   s   r=   r,   )r1   c                    "   dt _ fdd}t|  dS )CTests that the aiohttp session is closed when the client is closed.Tc               
      s   t jt jddddd d d tdtdf _t F tdddt	j
d	did
dj} | jt	jddi d d dI d H }| jjd usCJ | jjjrJJ |  I d H  | jjjsXJ W d    d S 1 scw   Y  d S N	localhostP   FT)connection_keyos_error   r	   r
   	trust_env)async_client_argsr   GETzhttps://example.com)methodurlr9   datatimeout)r*   ClientConnectorErrorclient_reqrepConnectionKeyOSErrorr=   side_effectr4   r   r   r   r    r   _async_request_onceHttpRequest_aiohttp_sessionclosedr!   r$   _r/   r   r   run}   sD   	

	"z(test_aclose_aiohttp_session.<locals>.runNr   r   asynciorY   r/   rY   r   rX   r   test_aclose_aiohttp_sessionx   s   $r]   c                    r>   )r?   Tc                     s   t jt jddddd d d tdtdf _t Y tdddt	j
d	did
dj4 I d H +} | jt	jddi d d dI d H }| jjd usHJ | jjjrOJ W d   I d H  n1 I d H s_w   Y  | jjjskJ W d    d S 1 svw   Y  d S r@   )r*   rM   rN   rO   rP   r=   rQ   r4   r   r   r   r    r   rR   rS   rT   rU   rV   rX   r   r   rY      sF   	

	("z1test_aiohttp_session_context_manager.<locals>.runNrZ   r\   r   rX   r   $test_aiohttp_session_context_manager   s   #r^   ) __doc__r[   unittestr   google.oauth2r   pytestr*   AIOHTTP_NOT_INSTALLEDImportError	MagicMock r   r   r   markskipifrequires_aiohttpr   r   r%   r&   fixturer/   r4   intr=   r2   objectr+   r]   r^   r   r   r   r   <module>   sB   



*