o
    iv                     @   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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vertexaiprojectlocationN)
api_clienthas_aiohttpr   closer   _httpx_client	is_closedclient r   _/home/ubuntu/.local/lib/python3.10/site-packages/google/genai/tests/client/test_client_close.pytest_close_httpx_client'   s   r   c                  C   sV   dt _tdddd} 	 | jjjrJ W d   n1 sw   Y  | jjjs)J dS r   )r   r   r   r   r   r   r   r   r   r   !test_httpx_client_context_manager3   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   N)r   r   r   aioacloser   _async_httpx_clientr   async_clientr   r   r   test_aclose_httpx_clientA   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_managerN   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_defaultd   s
   r/   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%   ClientResponser0   strheadersjsonr(   text)r0   responser   r   r   _aiohttp_async_responsel   s   r8   r'   )r,   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 ? tdddd	j	} | j
tjd
di d d dI d H }| j
jd us<J | j
jjrCJ |  I d H  | j
jjsQJ W d    d S 1 s\w   Y  d S N	localhostP   FT)connection_keyos_error   r	   r
   r   GETzhttps://example.com)methodurlr4   datatimeout)r%   ClientConnectorErrorclient_reqrepConnectionKeyOSErrorr8   side_effectr/   r   r   r   _async_request_oncer   HttpRequest_aiohttp_sessionclosedr   r   _r*   r   r   run{   s>   	
	"z(test_aclose_aiohttp_session.<locals>.runNr   r   asynciorR   r*   rR   r   rQ   r   test_aclose_aiohttp_sessionv   s   !rV   c                    r9   )r:   Tc                     s   t jt jddddd d d tdtdf _t R tdddd	j	4 I d H +} | j
tjd
di d d dI d H }| j
jd usAJ | j
jjrHJ W d   I d H  n1 I d H sXw   Y  | j
jjsdJ W d    d S 1 sow   Y  d S r;   )r%   rF   rG   rH   rI   r8   rJ   r/   r   r   r   rK   r   rL   rM   rN   rO   rQ   r   r   rR      s@   	

	("z1test_aiohttp_session_context_manager.<locals>.runNrS   rU   r   rQ   r   $test_aiohttp_session_context_manager   s    rW   ) __doc__rT   unittestr   google.oauth2r   pytestr%   AIOHTTP_NOT_INSTALLEDImportError	MagicMock r   r   r   markskipifrequires_aiohttpr   r   r    r!   fixturer*   r/   intr8   r-   objectr&   rV   rW   r   r   r   r   <module>   sB   



'