a
    zeV                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlmZ d dl mZmZ d dlm	Z	 d dl
mZmZmZmZmZmZmZmZmZ dd ZG dd	 d	ZG d
d dZdd Zdd Zdd ZG dd deZe dZe dZedd ZG dd dej Z!G dd dej Z"G dd dej Z#G dd dej Z$G d d! d!ej Z%G d"d# d#ej Z&G d$d% d%e	Z'G d&d' d'ej Z(G d(d) d)ej Z)G d*d+ d+ej Z*G d,d- d-ej Z+dS ).    N)contextmanager)runiscoroutinefunction)IsolatedAsyncioTestCase)	ANYcall	AsyncMockpatch	MagicMockMockcreate_autospecsentinel	_CallListc                   C   s   t d  d S N)asyncioZset_event_loop_policy r   r   6/usr/lib/python3.9/unittest/test/testmock/testasync.pytearDownModule   s    r   c                   @   s<   e Zd Zdd Zdd Zdd Zedd Zed	d
 Z	dS )
AsyncClassc                 C   s   d S r   r   selfr   r   r   __init__       zAsyncClass.__init__c                    s   d S r   r   r   r   r   r   async_method   r   zAsyncClass.async_methodc                 C   s   d S r   r   r   r   r   r   normal_method   r   zAsyncClass.normal_methodc                    s   d S r   r   )clsr   r   r   async_class_method   s    zAsyncClass.async_class_methodc                      s   d S r   r   r   r   r   r   async_static_method   s    zAsyncClass.async_static_methodN)
__name__
__module____qualname__r   r   r   classmethodr   staticmethodr   r   r   r   r   r      s   
r   c                   @   s   e Zd Zdd ZdS )AwaitableClassc                 c   s
   d V  d S r   r   r   r   r   r   	__await__   r   zAwaitableClass.__await__N)r   r   r    r$   r   r   r   r   r#      s   r#   c                      s   d S r   r   r   r   r   r   
async_func!   r   r%   c                   s   d S r   r   )abcr   r   r   async_func_args#   r   r)   c                   C   s   d S r   r   r   r   r   r   normal_func%   r   r*   c                   @   s   e Zd Zdd ZdS )NormalClassc                 C   s   d S r   r   r   r   r   r   r&   (   r   zNormalClass.aN)r   r   r    r&   r   r   r   r   r+   '   s   r+   z.AsyncClassz.NormalClassc                 c   s>   |  td d V  t  W d    n1 s00    Y  d S )Nzwas never awaited$)ZassertWarnsRegexRuntimeWarninggcZcollect)testr   r   r   assertNeverAwaited/   s    r/   c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )AsyncPatchDecoratorTestc                    s"   t td fdd}|  d S )Nr   c                    s     t|  d S r   )
assertTruer   mock_methodr   r   r   
test_async:   s    zLAsyncPatchDecoratorTest.test_is_coroutine_function_patch.<locals>.test_asyncr	   objectr   r   r4   r   r   r    test_is_coroutine_function_patch9   s    
z8AsyncPatchDecoratorTest.test_is_coroutine_function_patchc                    sB   t td fdd}t t d fdd}|  |  d S )Nr   c                    s"   |  }  t| t| d S r   r1   inspectisawaitabler   r3   mr   r   r   r4   @   s    z?AsyncPatchDecoratorTest.test_is_async_patch.<locals>.test_asyncz.async_methodc                    s"   |  }  t| t| d S r   r9   r<   r   r   r   test_no_parent_attributeF   s    zMAsyncPatchDecoratorTest.test_is_async_patch.<locals>.test_no_parent_attribute)r	   r6   r   async_foo_name)r   r4   r>   r   r   r   test_is_async_patch?   s    
z+AsyncPatchDecoratorTest.test_is_async_patchc                    s"   t td fdd}|  d S )Nr   c                    s     | t d S r   assertIsInstancer   r2   r   r   r   r4   P   s    zCAsyncPatchDecoratorTest.test_is_AsyncMock_patch.<locals>.test_asyncr5   r7   r   r   r   test_is_AsyncMock_patchO   s    
z/AsyncPatchDecoratorTest.test_is_AsyncMock_patchc                    s"   t td fdd}|  d S )Nr   c                    s     | t d S r   rA   r2   r   r   r   r4   W   s    zPAsyncPatchDecoratorTest.test_is_AsyncMock_patch_staticmethod.<locals>.test_asyncr5   r7   r   r   r   $test_is_AsyncMock_patch_staticmethodV   s    
z<AsyncPatchDecoratorTest.test_is_AsyncMock_patch_staticmethodc                    s"   t td fdd}|  d S )Nr   c                    s     | t d S r   rA   r2   r   r   r   r4   ^   s    zOAsyncPatchDecoratorTest.test_is_AsyncMock_patch_classmethod.<locals>.test_asyncr5   r7   r   r   r   #test_is_AsyncMock_patch_classmethod]   s    
z;AsyncPatchDecoratorTest.test_is_AsyncMock_patch_classmethodc                    sN   t t dddt t ddd fdd}t|   tt d S )N.async_func   return_value.async_func_args   c                    sh     | jd   |jd  tt  tt   t I d H d   tddddI d H d d S )Nr)   r%   rG   rK      r(   )assertEqualZ
_mock_namerB   r%   r   r)   )Zfunc_args_mockZ	func_mockr   r   r   r4   e   s    z@AsyncPatchDecoratorTest.test_async_def_patch.<locals>.test_async)r	   r   r   r1   r:   r   r%   r7   r   r   r   test_async_def_patchd   s
    

z,AsyncPatchDecoratorTest.test_async_def_patchN)	r   r   r    r8   r@   rC   rD   rE   rO   r   r   r   r   r0   8   s   r0   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
AsyncPatchCMTestc                    s    fdd}|  d S )Nc                     s>   t td}  t|  W d    n1 s00    Y  d S Nr   )r	   r6   r   r1   r   r2   r   r   r   r4   w   s    z>AsyncPatchCMTest.test_is_async_function_cm.<locals>.test_asyncr   r7   r   r   r   test_is_async_function_cmv   s    z*AsyncPatchCMTest.test_is_async_function_cmc                    s    fdd}|  d S )Nc                     sN   t td.} |  } t| t| W d    n1 s@0    Y  d S rQ   )r	   r6   r   r1   r:   r;   r   r<   r   r   r   r4   ~   s    z5AsyncPatchCMTest.test_is_async_cm.<locals>.test_asyncr   r7   r   r   r   test_is_async_cm}   s    z!AsyncPatchCMTest.test_is_async_cmc                    s    fdd}|  d S )Nc                     s<   t td}  | t W d    n1 s.0    Y  d S rQ   )r	   r6   r   rB   r   r2   r   r   r   r4      s    z9AsyncPatchCMTest.test_is_AsyncMock_cm.<locals>.test_asyncr   r7   r   r   r   test_is_AsyncMock_cm   s    z%AsyncPatchCMTest.test_is_AsyncMock_cmc                    s    fdd}t |  d S )Nc                      sR   t t dt   tt W d    n1 s40    Y   tt d S )NrF   )r	   r   r   rB   r%   r1   r:   r   r   r   r   r   r4      s    *z6AsyncPatchCMTest.test_async_def_cm.<locals>.test_asyncr   r7   r   r   r   test_async_def_cm   s    z"AsyncPatchCMTest.test_async_def_cmN)r   r   r    rR   rS   rT   rV   r   r   r   r   rP   u   s   	rP   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )AsyncMockTestc                 C   s   t  }| t| d S r   )r   r1   r   r   mockr   r   r    test_iscoroutinefunction_default   s    z.AsyncMockTest.test_iscoroutinefunction_defaultc                 C   s2   dd }t |}| t| | t| d S )Nc                      s   d S r   r   r   r   r   r   foo   r   z<AsyncMockTest.test_iscoroutinefunction_function.<locals>.foor   r1   r   r:   r   r[   rY   r   r   r   !test_iscoroutinefunction_function   s    z/AsyncMockTest.test_iscoroutinefunction_functionc                 C   s8   t  }| }| t| t| | dt| d S )Nassert_awaited)r   r1   r:   r;   r   ZassertIndirr   rY   r=   r   r   r   test_isawaitable   s
    zAsyncMockTest.test_isawaitablec                 C   s2   dd }t |}| t| | t| d S )Nc                   S   s   d S r   r   r   r   r   r   r[      r   zCAsyncMockTest.test_iscoroutinefunction_normal_function.<locals>.foor\   r]   r   r   r   (test_iscoroutinefunction_normal_function   s    z6AsyncMockTest.test_iscoroutinefunction_normal_functionc                 C   sD   t  }t | t  }|  |  t|}| |t j d S r   )r   Znew_event_loopZset_event_loopFuturestopcloser   rB   )r   ZloopfutrY   r   r   r   test_future_isfuture   s    
z"AsyncMockTest.test_future_isfutureN)r   r   r    rZ   r^   rb   rc   rh   r   r   r   r   rW      s
   rW   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )AsyncAutospecTestc                    s@   t tdd fdd}t tdd fdd}|  |  d S )NTZautospecc                    s     | jt   | t d S r   rB   r   r   r
   r2   r   r   r   r4      s    z=AsyncAutospecTest.test_is_AsyncMock_patch.<locals>.test_asyncc                    s     | jt d S r   )rB   r   r
   r2   r   r   r   test_normal_method   s    zEAsyncAutospecTest.test_is_AsyncMock_patch.<locals>.test_normal_methodr	   r?   )r   r4   rl   r   r   r   rC      s    

z)AsyncAutospecTest.test_is_AsyncMock_patchc                 C   s:   |  t ttdd W d    n1 s,0    Y  d S )NT)instance)assertRaisesRuntimeErrorr   r%   r   r   r   r   test_create_autospec_instance   s    z/AsyncAutospecTest.test_create_autospec_instancec                 C   s    t t d}| t |t d S Nspec)r   r#   rB   r   )r   Zawaitable_mockr   r   r   $test_create_autospec_awaitable_class   s    z6AsyncAutospecTest.test_create_autospec_awaitable_classc                    s&  t t}|dddd  fdd}| |jd | |j | |jg  |  t|  | 	t
| | 	t  | |jd | |jtdddd | |jtddddg |  |jdddd |jdddd |  | t |jdd W d    n1 s0    Y  d S )	NrG   rK   rL   rM   c                      s    I d H  d S r   r   r   	awaitabler   r   main   s    z4AsyncAutospecTest.test_create_autospec.<locals>.mainr   )e)r   r)   rN   await_countassertIsNone
await_argsawait_args_listassert_not_awaitedr   r1   r   r   iscoroutiner   assert_awaited_onceassert_awaited_once_withassert_awaited_withr_   ro   AssertionErrorassert_any_await)r   rt   rx   r   rv   r   test_create_autospec   s&    
z&AsyncAutospecTest.test_create_autospecc                    s    fdd}t |  d S )Nc                     sZ  t t ddd} | dddd} | jt  t|   t|  t	
|  | jd  | jg   | j |   |I d H  W d    n1 s0    Y   | jd  | jtdddd  | jtddddg |   | jdddd | jdddd |   |    | jd  | j  | jg  d S )	NrJ   Trj   rG   rK   rL   rM   r   )r	   r   rB   rY   r   r1   r   r   r   r:   r;   rN   rz   r}   r{   r|   r~   r   r   r   r   r_   
reset_mock)r3   rw   r   r   r   r4      s,    (z>AsyncAutospecTest.test_patch_with_autospec.<locals>.test_asyncrU   r7   r   r   r   test_patch_with_autospec   s    z*AsyncAutospecTest.test_patch_with_autospecN)r   r   r    rC   rq   ru   r   r   r   r   r   r   ri      s
   ri   c                   @   sd   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd ZdS )AsyncSpecTestc              	      sV    fdd}t tfD ]<} d|  || W d    q1 sF0    Y  qd S )Nc                    s(   | t } |jt  |jt d S r   )r   rB   r   r   r   r
   )	mock_typerY   r   r   r   
inner_test  s    zCAsyncSpecTest.test_spec_normal_methods_on_class.<locals>.inner_testztest method types with r   r
   subTestr   r   r   r   r   r   !test_spec_normal_methods_on_class  s    z/AsyncSpecTest.test_spec_normal_methods_on_classc                 C   s(   t t}| |jt | |jt  d S r   )r   r   rB   r   r   r   rX   r   r   r   +test_spec_normal_methods_on_class_with_mock  s    z9AsyncSpecTest.test_spec_normal_methods_on_class_with_mockc              	      sX    fdd}t ttfD ]<} d|  || W d    q1 sH0    Y  qd S )Nc                    sj   | t d} ||  t "  t|  W d    n1 sF0    Y  | td} ||  d S rr   r%   rB   r/   r1   r:   r;   r*   r   
async_mockZ	sync_mockr   r   r   r     s    

0
z8AsyncSpecTest.test_spec_mock_type_kw.<locals>.inner_testztest spec kwarg with r   r
   r   r   r   r   r   r   test_spec_mock_type_kw  s    	z$AsyncSpecTest.test_spec_mock_type_kwc              	      sX    fdd}t ttfD ]<} d|  || W d    q1 sH0    Y  qd S )Nc                    sf   | t } ||  t "  t|  W d    n1 sD0    Y  | t} ||  d S r   r   r   r   r   r   r   +  s    
0z@AsyncSpecTest.test_spec_mock_type_positional.<locals>.inner_testztest spec positional with r   r   r   r   r   test_spec_mock_type_positional*  s    	z,AsyncSpecTest.test_spec_mock_type_positionalc                 C   s8   t td}| |t  | }| t| t| d S rr   r   r*   rB   r1   r:   r;   r   ra   r   r   r    test_spec_as_normal_kw_AsyncMock8  s
    
z.AsyncSpecTest.test_spec_as_normal_kw_AsyncMockc                 C   s6   t t}| |t  | }| t| t| d S r   r   ra   r   r   r   (test_spec_as_normal_positional_AsyncMock?  s
    z6AsyncSpecTest.test_spec_as_normal_positional_AsyncMockc                    s&   t jtddd fdd}|  d S )Nr   Trs   c                    s     | t d S r   rA   r2   r   r   r   r4   G  s    z6AsyncSpecTest.test_spec_async_mock.<locals>.test_asyncr5   r7   r   r   r   test_spec_async_mockF  s    z"AsyncSpecTest.test_spec_async_mockc                    s"   t tdd fdd}|  d S )NTrs   c                    s     | t   | jt d S r   )rB   r
   r   r   r2   r   r   r   r4   N  s    zIAsyncSpecTest.test_spec_parent_not_async_attribute_is.<locals>.test_asyncrm   r7   r   r   r   'test_spec_parent_not_async_attribute_isM  s    
z5AsyncSpecTest.test_spec_parent_not_async_attribute_isc                    s(   t jtdtjd fdd}|  d S )Nr   rs   c                    s0     | t  t|   t|  d S r   )rB   r
   assertFalser:   r   r;   r2   r   r   r   test_async_attributeV  s    zFAsyncSpecTest.test_target_async_spec_not.<locals>.test_async_attribute)r	   r6   r   r+   r&   )r   r   r   r   r   test_target_async_spec_notU  s    z(AsyncSpecTest.test_target_async_spec_notc                    s&   t jtdtd fdd}|  d S )Nr&   rs   c                    s     | t d S r   rA   )Zmock_async_funcr   r   r    test_attribute_not_async_spec_is_  s    zUAsyncSpecTest.test_target_not_async_spec_is.<locals>.test_attribute_not_async_spec_is)r	   r6   r+   r%   )r   r   r   r   r   test_target_not_async_spec_is^  s    z+AsyncSpecTest.test_target_not_async_spec_isc                    s"   t ttd fdd}|  d S )Nrs   c                    s     | jt   | t d S r   rk   )ZMockNormalClassr   r   r    test_async_attributes_coroutinese  s    zRAsyncSpecTest.test_spec_async_attributes.<locals>.test_async_attributes_coroutines)r	   normal_foo_namer   )r   r   r   r   r   test_spec_async_attributesd  s    
z(AsyncSpecTest.test_spec_async_attributesN)r   r   r    r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   
	r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
AsyncSpecSetTestc                    s&   t jtddd fdd}|  d S )Nr   TZspec_setc                    s     | t d S r   rA   )r   r   r   r   r4   o  s    z<AsyncSpecSetTest.test_is_AsyncMock_patch.<locals>.test_asyncr5   r7   r   r   r   rC   n  s    z(AsyncSpecSetTest.test_is_AsyncMock_patchc                 C   s*   t tjd}| t| | |t  d S Nr   )r   r   r   r1   r   rB   rX   r   r   r   test_is_async_AsyncMockt  s    z(AsyncSpecSetTest.test_is_async_AsyncMockc                 C   sV   t td}| t|j | t|j | |jt | |jt  | |t  d S r   )	r
   r   r1   r   r   r   r   rB   r   rX   r   r   r   test_is_child_AsyncMocky  s    
z(AsyncSpecSetTest.test_is_child_AsyncMockc                 C   sR   t  }t dd d|_t|d}| |t  W d    n1 sD0    Y  d S )Nc                 S   s   | S r   r   xr   r   r   <lambda>  r   z=AsyncSpecSetTest.test_magicmock_lambda_spec.<locals>.<lambda>rs   	mock_func)r
   r   r	   r6   rB   )r   Zmock_objcmr   r   r   test_magicmock_lambda_spec  s    z+AsyncSpecSetTest.test_magicmock_lambda_specN)r   r   r    rC   r   r   r   r   r   r   r   r   m  s   r   c                   @   st   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )AsyncArgumentsc                    s2   dd }t |dd}|dI d H }| |d d S )Nc                    s   d S r   r   )r   varr   r   r   addition  r   z6AsyncArguments.test_add_return_value.<locals>.addition
   rH      r   rN   )r   r   rY   outputr   r   r   test_add_return_value  s    z$AsyncArguments.test_add_return_valuec                    sT   dd }t |tdd}| t |dI d H  W d    n1 sF0    Y  d S )Nc                    s   d S r   r   r   r   r   r   r     r   z?AsyncArguments.test_add_side_effect_exception.<locals>.additionerrZside_effectr   )r   	Exceptionro   )r   r   rY   r   r   r   test_add_side_effect_exception  s    z-AsyncArguments.test_add_side_effect_exceptionc                    s0   dd }t |d}|dI d H }| |d d S )Nc                    s   | d S NrG   r   r   r   r   r   r     s    z?AsyncArguments.test_add_side_effect_coroutine.<locals>.additionr   r      r   r   r   rY   resultr   r   r   test_add_side_effect_coroutine  s    
z-AsyncArguments.test_add_side_effect_coroutinec                    s0   dd }t |d}|dI d H }| |d d S )Nc                 S   s   | d S r   r   r   r   r   r   r     s    zEAsyncArguments.test_add_side_effect_normal_function.<locals>.additionr   r   r   r   r   r   r   r   $test_add_side_effect_normal_function  s    
z3AsyncArguments.test_add_side_effect_normal_functionc                    sj   g d}t |d}|D ]}| | I d H | q| t}| I d H  W d    n1 s\0    Y  d S )NrG   rK   rL   r   )r   rN   ro   StopAsyncIteration)r   valsrY   itemry   r   r   r   test_add_side_effect_iterable  s    
z,AsyncArguments.test_add_side_effect_iterablec                    st   G dd dt }d|dg}t|d}| | I d H d | |}| I d H  W d    n1 sf0    Y  d S )Nc                   @   s   e Zd ZdS )zOAsyncArguments.test_add_side_effect_exception_iterable.<locals>.SampleExceptionN)r   r   r    r   r   r   r   SampleException  s   r   rG   r[   r   )r   r   rN   ro   )r   r   r   rY   ry   r   r   r   'test_add_side_effect_exception_iterable  s    
z6AsyncArguments.test_add_side_effect_exception_iterablec                    s0   t dd}t |d}| I d H }| || d S )Nr   rH   )r   assertIs)r   valuerY   r   r   r   r   test_return_value_AsyncMock  s    

z*AsyncArguments.test_return_value_AsyncMockc                    s:   t  }|d  t|d}| I d H }| |t j d S )NrH   )r   rd   
set_resultr   rB   r   rg   rY   r   r   r   r   test_return_value_awaitable  s
    

z*AsyncArguments.test_return_value_awaitablec                    sr   t  }|d  t|gd}| I d H }| |t j | t | I d H  W d    n1 sd0    Y  d S )Nr   )r   rd   r   r   rB   ro   r   r   r   r   r   !test_side_effect_awaitable_values  s    
z0AsyncArguments.test_side_effect_awaitable_valuesc                    s0   t dd}t |d}| I d H }| |d d S )Nr   rH   r   r   )r   ZeffectrY   r   r   r   r   test_side_effect_is_AsyncMock  s    

z,AsyncArguments.test_side_effect_is_AsyncMockc                    sR   t  d  fdd}t|d}| I d H }| | |  |   d S )NFc                      s   d S NTr   r   Zranr   r   r   inner  s    z2AsyncArguments.test_wraps_coroutine.<locals>.innerwraps)r   rd   r   rN   r_   r1   r   r   rY   r   r   r   r   test_wraps_coroutine  s    
z#AsyncArguments.test_wraps_coroutinec                    sN   dd  fdd}t |d}| I d H }| | |  |   d S )NrG   Fc                      s   d S r   r   r   r   r   r   r     s    z8AsyncArguments.test_wraps_normal_function.<locals>.innerr   )r   rN   r_   r1   r   r   r   r   test_wraps_normal_function  s    
z)AsyncArguments.test_wraps_normal_functionc                    sx   t  }|d}|d}|I d H  |I d H  |tdtdg | |jtdtdg | |jtdtdg d S )NrK   rG   )r   assert_has_awaitsr   rN   r}   call_args_list)r   r   Zmock2Zmock1r   r   r   test_await_args_list_order  s    

z)AsyncArguments.test_await_args_list_orderN)r   r   r    r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   	r   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )AsyncMagicMethodsc                 C   sB   t  }| |jt | |jt | |jt | |jt  d S r   )r
   rB   
__aenter__r   	__aexit__	__anext__	__aiter__r   Zm_mockr   r   r   +test_async_magic_methods_return_async_mocks  s
    z=AsyncMagicMethods.test_async_magic_methods_return_async_mocksc                 C   sB   t  }| |jt | |jt | |jt | |jt d S r   )r   rB   	__enter__r
   __exit____next____len__r   a_mockr   r   r   *test_sync_magic_methods_return_magic_mocks  s
    z<AsyncMagicMethods.test_sync_magic_methods_return_magic_mocksc                 C   s:   t  }| t|d | t|d | t|d d S )Nr   r   r   )r
   r1   hasattrr   r   r   r   &test_magicmock_has_async_magic_methods  s    z8AsyncMagicMethods.test_magicmock_has_async_magic_methodsc                 C   sJ   t  }| t|d | t|d | t|d | t|d d S )Nr   r   r   r   )r   r1   r   r   r   r   r   %test_asyncmock_has_sync_magic_methods  s
    z7AsyncMagicMethods.test_asyncmock_has_sync_magic_methodsc                 C   sF   t  }| |jt | |jt | t|j | t|j d S r   )r
   rB   r   r   r   r1   r   r   r   r   r   &test_magic_methods_are_async_functions   s
    z8AsyncMagicMethods.test_magic_methods_are_async_functionsN)r   r   r    r   r   r   r   r   r   r   r   r   r     s
   	r   c                   @   s^   e Zd ZG dd dZG dd dZG dd dZdd Zd	d
 Zdd Zdd Z	dd Z
dS )AsyncContextManagerTestc                   @   s   e Zd Zdd Zdd ZdS )z/AsyncContextManagerTest.WithAsyncContextManagerc                    s   d S r   r   r   argskwargsr   r   r   r   +  r   z:AsyncContextManagerTest.WithAsyncContextManager.__aenter__c                    s   d S r   r   r   r   r   r   r   -  r   z9AsyncContextManagerTest.WithAsyncContextManager.__aexit__N)r   r   r    r   r   r   r   r   r   WithAsyncContextManager*  s   r   c                   @   s   e Zd Zdd Zdd ZdS )z.AsyncContextManagerTest.WithSyncContextManagerc                 O   s   d S r   r   r   r   r   r   r   0  r   z8AsyncContextManagerTest.WithSyncContextManager.__enter__c                 O   s   d S r   r   r   r   r   r   r   2  r   z7AsyncContextManagerTest.WithSyncContextManager.__exit__N)r   r   r    r   r   r   r   r   r   WithSyncContextManager/  s   r   c                   @   s   e Zd Zdd Zdd ZdS )z&AsyncContextManagerTest.ProductionCodec                 C   s
   d | _ d S r   )sessionr   r   r   r   r   6  s    z/AsyncContextManagerTest.ProductionCode.__init__c              	      sV   | j d4 I d H (}| I d H }|W  d   I d H  S 1 I d H sH0    Y  d S )Nzhttps://python.org)r   postjson)r   responsevalr   r   r   rx   9  s    z+AsyncContextManagerTest.ProductionCode.mainN)r   r   r    r   rx   r   r   r   r   ProductionCode4  s   r   c              	      sV    fdd}t tfD ]<} d|  || W d    q1 sF0    Y  qd S )Nc                    sj      }tdd|_| dd}tdd}tddid|_||j_||jj_t|	 } 
|ddi d S )NZsessionmocknameZmagic_cmr   r   {   rH   )r   r
   r   r   r   r   rI   r   r   rx   rN   )r   Zpcr   r   r   r   r   r   r   ?  s    


zKAsyncContextManagerTest.test_set_return_value_of_aenter.<locals>.inner_testz%test set return value of aenter with r   r   r   r   r   test_set_return_value_of_aenter>  s    z7AsyncContextManagerTest.test_set_return_value_of_aenterc              	      sV    fdd}t tfD ]<} d|  || W d    q1 sF0    Y  qd S )Nc                    sv   d   }| | fdd}t| }  jj jj j  j  | d S )NFc               	      s@   4 I d H } d W d   I d H  q<1 I d H s20    Y  | S r   r   r   calledZcm_mockr   r   use_context_managerT  s    .zqAsyncContextManagerTest.test_mock_supports_async_context_manager.<locals>.inner_test.<locals>.use_context_manager)r   r   r1   r   r   r   r_   ZassertIsNot)r   r   r   Z	cm_resultr   r   r   r   O  s    



zTAsyncContextManagerTest.test_mock_supports_async_context_manager.<locals>.inner_testz!test context manager magics with r   r   r   r   r   (test_mock_supports_async_context_managerN  s    z@AsyncContextManagerTest.test_mock_supports_async_context_managerc                    s@   |   }t| t }| j_ fdd}| t| | d S )Nc               	      s@    4 I d H } | W  d   I d H  S 1 I d H s20    Y  d S r   r   r   mock_instancer   r   r   o  s    z^AsyncContextManagerTest.test_mock_customize_async_context_manager.<locals>.use_context_manager)r   r
   r6   r   rI   r   r   )r   rn   Zexpected_resultr   r   r  r   )test_mock_customize_async_context_managerh  s    zAAsyncContextManagerTest.test_mock_customize_async_context_managerc                    sj   d d fdd}fdd}|   }t||_|_fdd}t|  |   |  d S )NFc                     s   d d S r   r   r   )enter_calledr   r   enter_coroutiney  s    ziAsyncContextManagerTest.test_mock_customize_async_context_manager_with_coroutine.<locals>.enter_coroutinec                     s   d d S r   r   r  )exit_calledr   r   exit_coroutine}  s    zhAsyncContextManagerTest.test_mock_customize_async_context_manager_with_coroutine.<locals>.exit_coroutinec                	      s<    4 I d H  W d   I d H  q81 I d H s.0    Y  d S r   r   r   r  r   r   r     s    zmAsyncContextManagerTest.test_mock_customize_async_context_manager_with_coroutine.<locals>.use_context_manager)r   r
   r   r   r   r1   )r   r  r	  rn   r   r   )r  r  r  r   8test_mock_customize_async_context_manager_with_coroutineu  s    

zPAsyncContextManagerTest.test_mock_customize_async_context_manager_with_coroutinec                 C   sR   dd }|   }t|}| t t|| W d    n1 sD0    Y  d S )Nc              	      sB   | 4 I d H  t  W d   I d H  q>1 I d H s40    Y  d S r   )	TypeError)Zcontext_managerr   r   r   raise_in  s    zYAsyncContextManagerTest.test_context_manager_raise_exception_by_default.<locals>.raise_in)r   r
   ro   r  r   )r   r  rn   r  r   r   r   /test_context_manager_raise_exception_by_default  s
    zGAsyncContextManagerTest.test_context_manager_raise_exception_by_defaultN)r   r   r    r   r   r   r   r  r  r
  r  r   r   r   r   r   (  s   
r   c                   @   s4   e Zd ZG dd deZdd Zdd Zdd Zd	S )
AsyncIteratorTestc                   @   s$   e Zd Zdd Zdd Zdd ZdS )z#AsyncIteratorTest.WithAsyncIteratorc                 C   s   g d| _ d S )N)r[   Z	NormalFoobaz)itemsr   r   r   r   r     s    z,AsyncIteratorTest.WithAsyncIterator.__init__c                 C   s   d S r   r   r   r   r   r   r     r   z-AsyncIteratorTest.WithAsyncIterator.__aiter__c                    s   d S r   r   r   r   r   r   r     r   z-AsyncIteratorTest.WithAsyncIterator.__anext__N)r   r   r    r   r   r   r   r   r   r   WithAsyncIterator  s   r  c                    s@   t dd g d j_ fdd}t| }| |g d d S )NZtesterr   r   c                      s   dd  2 I d H S )Nc                    s   g | z3 d H W }|q6 S r   r   ).0ir   r   r   
<listcomp>  r   zOAsyncIteratorTest.test_aiter_set_return_value.<locals>.main.<locals>.<listcomp>r   r   Z	mock_iterr   r   rx     s    z;AsyncIteratorTest.test_aiter_set_return_value.<locals>.main)r   r   rI   r   rN   )r   rx   r   r   r  r   test_aiter_set_return_value  s
    

z-AsyncIteratorTest.test_aiter_set_return_valuec              	      sV    fdd}t tfD ]<} d|  || W d    q1 sF0    Y  qd S )Nc                    sT      }| |} t|j  t|j  t|j  t|j d S r   )r  r   r   r   r1   r   )r   rn   r  r   r   r   r     s    zIAsyncIteratorTest.test_mock_aiter_and_anext_asyncmock.<locals>.inner_testz%test aiter and anext corourtine with r   r   r   r   r   #test_mock_aiter_and_anext_asyncmock  s    z5AsyncIteratorTest.test_mock_aiter_and_anext_asyncmockc              	      s   dd g d fdd} fdd} fdd	}t tfD ]}d
|  || W d    n1 sx0    Y  d|  || W d    n1 s0    Y  d|  || W d    qF1 s0    Y  qFd S )Nc                    s&   g }| 2 z3 d H W }| | q6 |S r   )append)iteratorZaccumulatorr   r   r   r   iterate  s    z6AsyncIteratorTest.test_mock_async_for.<locals>.iterate)ZFOOZBARZBAZc                    s$   |   }t |g  d S r   )r  rN   r   r   r  )r  r   r   r   test_default  s    z;AsyncIteratorTest.test_mock_async_for.<locals>.test_defaultc                    s4   |   } d d  |j_t|  d S r   )r  r   rI   rN   r   r  Zexpectedr  r   r   r   test_set_return_value  s    zDAsyncIteratorTest.test_mock_async_for.<locals>.test_set_return_valuec                    s8   |   }t d d  |j_t|  d S r   )r  iterr   rI   rN   r   r  r  r   r   test_set_return_value_iter  s    zIAsyncIteratorTest.test_mock_async_for.<locals>.test_set_return_value_iterzdefault value with zset return_value with zset return_value iterator with r   )r   r  r  r   r   r   r  r   test_mock_async_for  s    &&z%AsyncIteratorTest.test_mock_async_forN)r   r   r    r6   r  r  r  r!  r   r   r   r   r    s   r  c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3S )4AsyncMockAssertc                 C   s   t  | _d S r   )r   rY   r   r   r   r   setUp  s    zAsyncMockAssert.setUpc                    s   | j |i |I d H  d S r   )rY   r   r   r   r   _runnable_test  s    zAsyncMockAssert._runnable_testc                    s
   |I d H S r   r   )r   	coroutiner   r   r   _await_coroutine  s    z AsyncMockAssert._await_coroutinec                 C   s   t t}t|  |  W d    n1 s.0    Y  | t|j |j  |j  |j  | 	t
 |  W d    n1 s0    Y  | 	t
 |j  W d    n1 s0    Y  d S r   )r   r   r/   r   r1   r   assert_calledassert_called_onceassert_called_once_withro   r   r_   rX   r   r   r   "test_assert_called_but_not_awaited  s    
&


&z2AsyncMockAssert.test_assert_called_but_not_awaitedc                 C   s   t t}| }|j  |j  |j  | t |j  W d    n1 sX0    Y  t	| 
| |j  |j  |j  |j  d S r   )r   r   r   r'  r(  r)  ro   r   r_   r   r&  r   r   )r   rY   Zmock_coroutiner   r   r   test_assert_called_then_awaited  s    


(


z/AsyncMockAssert.test_assert_called_then_awaitedc                 C   s   |  t | j  W d    n1 s*0    Y  |  t | j  W d    n1 s^0    Y  t|   | j  | j  d S r   )	ro   r   rY   r_   r'  r   r$  r(  r   r   r   r   r   +test_assert_called_and_awaited_at_same_time  s    ((
z;AsyncMockAssert.test_assert_called_and_awaited_at_same_timec                 C   s   t t}| }t|  |  W d    n1 s60    Y  | t |j  W d    n1 sj0    Y  |j  t| 	| |j  |j
  d S r   )r   r   r   r/   ro   r   r_   r'  r   r&  r   r   rY   r%  r   r   r   )test_assert_called_twice_and_awaited_once  s    
&(

z9AsyncMockAssert.test_assert_called_twice_and_awaited_oncec                 C   sn   t t}| }|j  t| | | t t| | W d    n1 sV0    Y  |j  d S r   )	r   r   r   r(  r   r&  ro   rp   r_   r-  r   r   r   )test_assert_called_once_and_awaited_twice  s    
,z9AsyncMockAssert.test_assert_called_once_and_awaited_twicec                 C   s  |  t | j  W d    n1 s*0    Y  |  t | j  W d    n1 s^0    Y  |  t  t| | j W d    n1 s0    Y  |  t | j  W d    n1 s0    Y  |  t | j  W d    n1 s0    Y  d S r   )ro   r   rY   r_   r'  r  r   r&  r   r   r   r   "test_assert_awaited_but_not_called)  s    ((.(z2AsyncMockAssert.test_assert_awaited_but_not_calledc                 C   s   t dg}t|  | d W d    n1 s20    Y  | j| | t | j| W d    n1 st0    Y  d S Nr[   )r   r/   rY   Zassert_has_callsro   r   r   )r   Zkallsr   r   r    test_assert_has_calls_not_awaits7  s    

(z0AsyncMockAssert.test_assert_has_calls_not_awaitsc                 C   s   t |  |   W d    n1 s&0    Y  ddi fg}| | jj| t |  | d W d    n1 st0    Y  t |  | d W d    n1 s0    Y  t tdtdg}| | jj| d S )N r   r[   r  )r/   rY   rN   
mock_callsr   )r   kalls_empty
mock_kallsr   r   r   0test_assert_has_mock_calls_on_async_mock_no_spec?  s    
&
(
(z@AsyncMockAssert.test_assert_has_mock_calls_on_async_mock_no_specc                 C   s   t t}t|  |  W d    n1 s.0    Y  ddi fg}| |jj| | |jt g t| $ |jdddddd W d    n1 s0    Y  t tddddddg}t tjddddddg}| |jj| | |j| d S )	Nr3  r   rG   rK   rL      r   )r&   r'   )r   r   r/   r   rN   r4  r   )r   Za_class_mockr5  Zmethod_kallsr6  r   r   r   2test_assert_has_mock_calls_on_async_mock_with_specL  s    
&
2zBAsyncMockAssert.test_assert_has_mock_calls_on_async_mock_with_specc                 C   s   t |   | jjdd d W d    n1 s.0    Y  t | $ | jjjdtjd W d    n1 sj0    Y  | | jjdddd ifdd	d
tjifgd | | jjjdd	d
tjifgd d S )NrL   )fishr   )cake	something)rL   r:  zsomething_else.something)r   r;  z#method calls not recorded correctly)r/   rY   r<  Zsomething_elser   ZCakerN   method_callsr   r   r   r    test_async_method_calls_recorded[  s    
.
2
z0AsyncMockAssert.test_async_method_calls_recordedc                    sF   fdd}| j  t      W d    n1 s<0    Y  t    dd W d    n1 sp0    Y  t   j dd W d    n1 s0    Y   j   | j  tt}t  |  W d    n1 s0    Y  t  |jddd W d    n1 s(0    Y  |  || d S )Nc                    s@   d}|D ]2}t | |} |t  |t  |g  qd S )N)r   r=  r4  )getattrrB   r   listrN   )rY   namesr   attrr   r   r   assert_attrsk  s    
z:AsyncMockAssert.test_async_arg_lists.<locals>.assert_attrsrG   rK   rL   )r&   )rY   r/   r   r   r   r   )r   rC  r   r   r   r   test_async_arg_listsj  s"    

&
*
*


&
.z$AsyncMockAssert.test_async_arg_listsc                 C   sN   |  t | j  W d    n1 s*0    Y  t|   | j  d S r   )ro   r   rY   r_   r   r$  r   r   r   r   test_assert_awaited  s    (z#AsyncMockAssert.test_assert_awaitedc                 C   s   |  t | j  W d    n1 s*0    Y  t|   | j  t|   |  t | j  W d    n1 s0    Y  d S r   )ro   r   rY   r   r   r$  r   r   r   r   test_assert_awaited_once  s    (
z(AsyncMockAssert.test_assert_awaited_oncec                 C   s   d}|  t| | jd W d    n1 s20    Y  t|   d}|  t| | jd W d    n1 sz0    Y  t| d | jd t| d | t | jd W d    n1 s0    Y  d S )NzNot awaitedr[   zexpected await not foundSomethingElse)assertRaisesRegexr   rY   r   r   r$  ro   )r   msgr   r   r   test_assert_awaited_with  s    **z(AsyncMockAssert.test_assert_awaited_withc                 C   s   |  t | jd W d    n1 s,0    Y  t| d | jd t| d |  t | jd W d    n1 s0    Y  d S r1  )ro   r   rY   r   r   r$  r   r   r   r   test_assert_awaited_once_with  s    *z-AsyncMockAssert.test_assert_awaited_once_withc                 C   s   |  t | jd W d    n1 s,0    Y  t| d |  t | jd W d    n1 sp0    Y  t| d | jd t| d | jd d S )Nr[   r  rG  )ro   r   rY   r   r   r$  r   r   r   r   test_assert_any_wait  s    **z$AsyncMockAssert.test_assert_any_waitc                 C   s  t dt dg}| t}| j| W d    n1 s<0    Y  | t|jjd t	| 
d | t | j| W d    n1 s0    Y  t	| 
d | t | j| W d    n1 s0    Y  t	| 
d | j| t	| 
d | j| d S )Nr[   r  rG   rG  )r   ro   r   rY   r   rN   len	exceptionr   r   r$  )r   callsr   r   r   r   test_assert_has_awaits_no_order  s    ***z/AsyncMockAssert.test_assert_has_awaits_no_orderc                 C   sT   G dd d}t | | d | jttdg | jtd | jtd d S )Nc                   @   s   e Zd Zdd ZdS )z9AsyncMockAssert.test_awaits_asserts_with_any.<locals>.Fooc                 S   s   d S r   r   r   otherr   r   r   __eq__  r   z@AsyncMockAssert.test_awaits_asserts_with_any.<locals>.Foo.__eq__Nr   r   r    rS  r   r   r   r   Foo  s   rU  rG   )r   r$  rY   r   r   r   r   r   )r   rU  r   r   r   test_awaits_asserts_with_any  s
    z,AsyncMockAssert.test_awaits_asserts_with_anyc                    sb   G dd d}t |d  fdd}t|| d  ttdg  td  td d S )Nc                   @   s   e Zd Zdd ZdS )zBAsyncMockAssert.test_awaits_asserts_with_spec_and_any.<locals>.Fooc                 S   s   d S r   r   rQ  r   r   r   rS    r   zIAsyncMockAssert.test_awaits_asserts_with_spec_and_any.<locals>.Foo.__eq__NrT  r   r   r   r   rU    s   rU  rs   c                     s    |  I d H  d S r   r   r  Zmock_with_specr   r   _custom_mock_runnable_test  s    zYAsyncMockAssert.test_awaits_asserts_with_spec_and_any.<locals>._custom_mock_runnable_testrG   )r   r   r   r   r   r   r   )r   rU  rX  r   rW  r   %test_awaits_asserts_with_spec_and_any  s    
z5AsyncMockAssert.test_awaits_asserts_with_spec_and_anyc                 C   s  t dt dg}| t  | jj|dd W d    n1 s@0    Y  t| d | t  | jj|dd W d    n1 s0    Y  t| d | t  | jj|dd W d    n1 s0    Y  t| d | jj|dd t| d | jj|dd d S )Nr[   r  T)Z	any_orderZbamfZqux)r   ro   r   rY   r   r   r$  )r   rO  r   r   r   test_assert_has_awaits_ordered  s    ...z.AsyncMockAssert.test_assert_has_awaits_orderedc                 C   sN   | j   t|   | t | j   W d    n1 s@0    Y  d S r   )rY   r~   r   r$  ro   r   r   r   r   r   test_assert_not_awaited  s    
z'AsyncMockAssert.test_assert_not_awaitedc                 C   s   d	dd}t |d| _t| d | tdtd }| j	t
 g W d    n1 sb0    Y  | |jj | tdtd(}| j	t
 t
ddg W d    n1 s0    Y  | |jjt d S )
Nc                    s   d S r   r   r   r   r   r   f  r   zIAsyncMockAssert.test_assert_has_awaits_not_matching_spec_error.<locals>.frs   rG   z^{}$z6Awaits not found.
Expected: [call()]
Actual: [call(1)]zError processing expected awaits.
Errors: [None, TypeError('too many positional arguments')]
Expected: [call(), call(1, 2)]
Actual: [call(1)]rK   )N)r   rY   r   r$  rH  r   formatreescaper   r   r{   rN  	__cause__rB   r  )r   r\  r   r   r   r   .test_assert_has_awaits_not_matching_spec_error
  s.    
.6z>AsyncMockAssert.test_assert_has_awaits_not_matching_spec_errorN)r   r   r    r#  r$  r&  r*  r+  r,  r.  r/  r0  r2  r7  r9  r>  rD  rE  rF  rJ  rK  rL  rP  rV  rY  rZ  r[  ra  r   r   r   r   r"    s2   

r"  ),r   r-   r:   r^  Zunittest
contextlibr   r   r   r   Zunittest.mockr   r   r   r	   r
   r   r   r   r   r   r   r#   r%   r)   r*   r6   r+   r   r?   r   r/   ZTestCaser0   rP   rW   ri   r   r   r   r   r   r  r"  r   r   r   r   <module>   s<   ,


=!"Tay&rE