o
    i                     @   s   d dl Z d dlZd dlmZmZ d dlZddlmZ ededdd Zeded	ej	j d
d Z
eded	ej	j dd ZdS )    N)	MagicMockpatch   )BaseApiClientz-genai.api_client.BaseApiClient._build_requestz'genai.api_client.BaseApiClient._requestc                 C   s   t dd}d}d}ddi}t }||_dd }t }||j_|| _g }	t }
||||D ]}|	| t|	d	ks=J q.t }|	|||d  | j	|d
d |	g dksYJ ||
 dksaJ d S )Ntest_api_keyapi_keyGET	test/pathkeyvaluec                  s   s(    g d} | D ]
}t d |V  qd S )Nz{"chunk": 1}z{"chunk": 2}z{"chunk": 3}皙?)timesleep)chunkschunk r   c/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/google/genai/_test_api_client.pydelayed_segments"   s   
z<test_request_streamed_non_blocking.<locals>.delayed_segments   T)streamr   333333?)
r   r   return_valuesegmentsside_effectr   request_streamedappendlenassert_called_once_with)mock_requestmock_build_request
api_clienthttp_methodpathrequest_dictmock_http_requestr   mock_responser   
start_timer   end_timer   r   r   "test_request_streamed_non_blocking   s,   

r*   z-genai.api_client.BaseApiClient._async_requestc                    s  t dd}d}d}ddi}t }||_G dd d  fd	d
}|| _||||}||||}	||||}
t }t||	|
I d H }t }||||d  |j	dksZJ | j|dd | j	dkshJ |g dkspJ d||   kr}dk sJ  J d S )Nr   r   r	   r
   r   r   c                   @   s   e Zd Zdd ZdS )z(test_async_request.<locals>.MockResponsec                 S   
   || _ d S N)text)selfr-   r   r   r   __init__I      
z1test_async_request.<locals>.MockResponse.__init__N)__name__
__module____qualname__r/   r   r   r   r   MockResponseG   s    r4   c                    s   t dI d H   dS )Nr   r   )asyncior   http_requestr   r4   r   r   delayed_responseL   s   z,test_async_request.<locals>.delayed_responser   Fr6   )r   r   r   r   g333333?)
	ApiClientr   r   r   async_requestr   r5   gatherassert_called_with
call_count)mock_async_requestr!   r"   r#   r$   r%   r&   r9   async_coroutine1async_coroutine2async_coroutine3r(   resultsr)   r   r8   r   test_async_request;   s4   

$rD   c                    s   t dd}d}d}ddi}t }||_G dd d  fd	d
}|| _g }t }	||||I d H 2 z3 d H W }
||
 t|dksGJ q46 t }||||d  | j|dd |g dksdJ ||	 dkslJ d S )Nr   r   r	   r
   r   r   c                   @   s   e Zd Zdd Zdd ZdS )z>test_async_request_streamed_non_blocking.<locals>.MockResponsec                 S   r+   r,   )	_segments)r.   r   r   r   r   r/   v   r0   zGtest_async_request_streamed_non_blocking.<locals>.MockResponse.__init__c                 s   s"    | j D ]
}td |V  qd S )Nr   )rE   r   r   )r.   segmentr   r   r   r   {   s
   

zGtest_async_request_streamed_non_blocking.<locals>.MockResponse.segmentsN)r1   r2   r3   r/   r   r   r   r   r   r4   t   s    r4   c                    s    g dS )Nr   r   r6   r8   r   r   r9      s   zBtest_async_request_streamed_non_blocking.<locals>.delayed_responser   Tr6   r   r   )	r:   r   r   r   r   async_request_streamedr   r   r   )r?   r!   r"   r#   r$   r%   r&   r9   r   r(   r   r)   r   r8   r   (test_async_request_streamed_non_blockingf   s6   

rH   )r5   r   unittest.mockr   r   pytestr"   r   r*   markrD   rH   r   r   r   r   <module>   s    "(