o
    Yiv                     @   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ZvertexaiprojectlocationN)
api_clienthas_aiohttpr   closer   _httpx_client	is_closedclient r   e/home/kim/smarthome/.venv/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)Z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.defaultZ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$   ZClientResponser-   strheadersjsonr'   text)r-   responser   r   r   _aiohttp_async_responsel   s   r4   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)Zconnection_keyZos_error   r	   r
   r   GETzhttps://example.com)methodurlr0   datatimeout)r$   ClientConnectorErrorclient_reqrepConnectionKeyOSErrorr4   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   asynciorL   r(   rL   r   rK   r   test_aclose_aiohttp_sessionv   s   !rP   c                    r5   )r6   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 r7   )r$   r@   rA   rB   rC   r4   rD   r,   r   r   r   rE   r   rF   rG   rH   rI   rK   r   r   rL      s@   	

	("z1test_aiohttp_session_context_manager.<locals>.runNrM   rO   r   rK   r   $test_aiohttp_session_context_manager   s    rQ   ) __doc__rN   Zunittestr   Zgoogle.oauth2r   Zpytestr$   ZAIOHTTP_NOT_INSTALLEDImportErrorZ	MagicMock r   r   r   markZskipifZrequires_aiohttpr   r   r   r    Zfixturer(   r,   intr4   r*   objectr%   rP   rQ   r   r   r   r   <module>   sB   



'