o
    KXh                     @   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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,ed.kre-  dS dS )/    N)contextmanager)runiscoroutinefunction)IsolatedAsyncioTestCase)
ANYcall	AsyncMockpatch	MagicMockMockcreate_autospecsentinel	_CallListsealc                   C   s   t d  d S N)asyncioZset_event_loop_policy r   r   J/opt/python-3.10.19/usr/lib/python3.10/unittest/test/testmock/testasync.pytearDownModule      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      d S r   r   selfr   r   r   __init__       zAsyncClass.__init__c                       d S r   r   r   r   r   r   async_method       zAsyncClass.async_methodc                 C   r   r   r   r   r   r   r   normal_method   r   zAsyncClass.normal_methodc                    r   r   r   )clsr   r   r   async_class_method      zAsyncClass.async_class_methodc                      r   r   r   r   r   r   r   async_static_method   r"   zAsyncClass.async_static_methodN)
__name__
__module____qualname__r   r   r   classmethodr!   staticmethodr#   r   r   r   r   r      s    
r   c                   @      e Zd Zdd ZdS )AwaitableClassc                 c   s    d V  d S r   r   r   r   r   r   	__await__   s   
 zAwaitableClass.__await__N)r$   r%   r&   r+   r   r   r   r   r*          r*   c                      r   r   r   r   r   r   r   
async_func!   r   r-   c                   r   r   r   )abcr   r   r   async_func_args#   r   r1   c                   C   r   r   r   r   r   r   r   normal_func%   r   r2   c                   @   r)   )NormalClassc                 C   r   r   r   r   r   r   r   r.   (   r   zNormalClass.aN)r$   r%   r&   r.   r   r   r   r   r3   '   r,   r3   z.AsyncClassz.NormalClassc                 c   sB    |  td d V  t  W d    d S 1 sw   Y  d S )Nzwas never awaited$)ZassertWarnsRegexRuntimeWarninggcZcollect)testr   r   r   assertNeverAwaited/   s
   
"r7   c                   @   <   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                    "   t td fdd}|  d S )Nr   c                    s     t|  d S r   )
assertTruer   mock_methodr   r   r   
test_async:      zLAsyncPatchDecoratorTest.test_is_coroutine_function_patch.<locals>.test_asyncr	   objectr   r   r>   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                    "   |  }  t| t| d S r   r;   inspectisawaitabler   r=   mr   r   r   r>   @      z?AsyncPatchDecoratorTest.test_is_async_patch.<locals>.test_asyncz.async_methodc                    rD   r   rE   rH   r   r   r   test_no_parent_attributeF   rJ   zMAsyncPatchDecoratorTest.test_is_async_patch.<locals>.test_no_parent_attribute)r	   rA   r   async_foo_name)r   r>   rK   r   r   r   test_is_async_patch?   s   

z+AsyncPatchDecoratorTest.test_is_async_patchc                    r:   )Nr   c                         | t d S r   assertIsInstancer   r<   r   r   r   r>   P      zCAsyncPatchDecoratorTest.test_is_AsyncMock_patch.<locals>.test_asyncr@   rB   r   r   r   test_is_AsyncMock_patchO      

z/AsyncPatchDecoratorTest.test_is_AsyncMock_patchc                    r:   )Nr#   c                    rN   r   rO   r<   r   r   r   r>   W   rQ   zPAsyncPatchDecoratorTest.test_is_AsyncMock_patch_staticmethod.<locals>.test_asyncr@   rB   r   r   r   $test_is_AsyncMock_patch_staticmethodV   rS   z<AsyncPatchDecoratorTest.test_is_AsyncMock_patch_staticmethodc                    r:   )Nr!   c                    rN   r   rO   r<   r   r   r   r>   ^   rQ   zOAsyncPatchDecoratorTest.test_is_AsyncMock_patch_classmethod.<locals>.test_asyncr@   rB   r   r   r   #test_is_AsyncMock_patch_classmethod]   rS   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                    sj     | jd   |jd  tt  tt   t I d H d   tddddI d H d d S )Nr1   r-   rW   r[      r0   )assertEqualZ
_mock_namerP   r-   r   r1   )Zfunc_args_mockZ	func_mockr   r   r   r>   e   s    z@AsyncPatchDecoratorTest.test_async_def_patch.<locals>.test_async)r	   r$   r   r;   rF   r   r-   rB   r   r   r   test_async_def_patchd   s
   

z,AsyncPatchDecoratorTest.test_async_def_patchN)	r$   r%   r&   rC   rM   rR   rT   rU   r_   r   r   r   r   r9   8   s    r9   c                   @   r8   )AsyncPatchCMTestc                        fdd}|  d S )Nc                     s@   t td}  t|  W d    d S 1 sw   Y  d S Nr   )r	   rA   r   r;   r   r<   r   r   r   r>   w   s   "z>AsyncPatchCMTest.test_is_async_function_cm.<locals>.test_asyncr   rB   r   r   r   test_is_async_function_cmv      
z*AsyncPatchCMTest.test_is_async_function_cmc                    ra   )Nc                     sP   t td} |  } t| t| W d    d S 1 s!w   Y  d S rb   )r	   rA   r   r;   rF   rG   r   rH   r   r   r   r>   ~   s
   
"z5AsyncPatchCMTest.test_is_async_cm.<locals>.test_asyncr   rB   r   r   r   test_is_async_cm}   s   
z!AsyncPatchCMTest.test_is_async_cmc                    ra   )Nc                     s>   t td}  | t W d    d S 1 sw   Y  d S rb   )r	   rA   r   rP   r   r<   r   r   r   r>      s   "z9AsyncPatchCMTest.test_is_AsyncMock_cm.<locals>.test_asyncr   rB   r   r   r   test_is_AsyncMock_cm   rd   z%AsyncPatchCMTest.test_is_AsyncMock_cmc                        fdd}t |  d S )Nc                      sT   t t dt   tt W d    n1 sw   Y   tt d S )NrV   )r	   r$   r   rP   r-   r;   rF   r   r   r   r   r   r>      s
   z6AsyncPatchCMTest.test_async_def_cm.<locals>.test_asyncr   rB   r   r   r   test_async_def_cm   s   z"AsyncPatchCMTest.test_async_def_cmc                    sB   ddi t  ddi fdd}t| t|  d S )Nr.   r/   c                      s     d d d S Nr.   r/   )r^   r   foor   r   r   r>      s   z>AsyncPatchCMTest.test_patch_dict_async_def.<locals>.test_async)r	   dictr;   r   r   rB   r   rk   r   test_patch_dict_async_def   s
   z*AsyncPatchCMTest.test_patch_dict_async_defc                    s$   ddi  fdd}t |  d S )Nr.   c                      sH   t  ddi  d d W d    d S 1 sw   Y  d S rj   )r	   rm   r^   r   rk   r   r   r>      s   "zFAsyncPatchCMTest.test_patch_dict_async_def_context.<locals>.test_asyncrh   rB   r   rk   r   !test_patch_dict_async_def_context   s   z2AsyncPatchCMTest.test_patch_dict_async_def_contextN)	r$   r%   r&   rc   re   rf   ri   rn   ro   r   r   r   r   r`   u   s    		r`   c                   @   4   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   r;   r   r   mockr   r   r    test_iscoroutinefunction_default   s   z.AsyncMockTest.test_iscoroutinefunction_defaultc                 C   2   dd }t |}| t| | t| d S )Nc                      r   r   r   r   r   r   r   rl      r   z<AsyncMockTest.test_iscoroutinefunction_function.<locals>.foor   r;   r   rF   r   rl   rs   r   r   r   !test_iscoroutinefunction_function      z/AsyncMockTest.test_iscoroutinefunction_functionc                 C   s8   t  }| }| t| t| | dt| d S )Nassert_awaited)r   r;   rF   rG   r   ZassertIndirr   rs   rI   r   r   r   test_isawaitable   s
   zAsyncMockTest.test_isawaitablec                 C   ru   )Nc                   S   r   r   r   r   r   r   r   rl      r   zCAsyncMockTest.test_iscoroutinefunction_normal_function.<locals>.foorv   rw   r   r   r   (test_iscoroutinefunction_normal_function   ry   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   rP   )r   Zloopfutrs   r   r   r   test_future_isfuture   s   
z"AsyncMockTest.test_future_isfutureN)r$   r%   r&   rt   rx   r}   r~   r   r   r   r   r   rq      s    rq   c                   @   s>   e Zd Zdd Zdd Zed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                         | jt   | t d S r   rP   r   r   r
   r<   r   r   r   r>         z=AsyncAutospecTest.test_is_AsyncMock_patch.<locals>.test_asyncc                    s     | jt d S r   )rP   r   r
   r<   r   r   r   test_normal_method   r?   zEAsyncAutospecTest.test_is_AsyncMock_patch.<locals>.test_normal_methodr	   rL   )r   r>   r   r   r   r   rR      s   


z)AsyncAutospecTest.test_is_AsyncMock_patchc                 C   s<   |  t ttdd W d    d S 1 sw   Y  d S )NT)instance)assertRaisesRuntimeErrorr   r-   r   r   r   r   test_create_autospec_instance   s   "z/AsyncAutospecTest.test_create_autospec_instancez3Broken test from https://bugs.python.org/issue37251c                 C   s   |  ttt d S r   )rP   r   r*   r   r   r   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    d S 1 sw   Y  d S )	NrW   r[   r\   r]   c                      s    I d H  d S r   r   r   	awaitabler   r   main   s   z4AsyncAutospecTest.test_create_autospec.<locals>.mainr   )e)r   r1   r^   await_countassertIsNone
await_argsawait_args_listassert_not_awaitedr   r;   r   r   iscoroutiner   assert_awaited_onceassert_awaited_once_withassert_awaited_withrz   r   AssertionErrorassert_any_await)r   specr   r   r   r   test_create_autospec   s(   
"z&AsyncAutospecTest.test_create_autospecc                    rg   )Nc                     s\  t t dddJ} | dddd} | jt  t|   t|  t	
|  | jd  | jg   | j |   |I d H  W d    n1 sWw   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 )	NrZ   Tr   rW   r[   r\   r]   r   )r	   r$   rP   rs   r   r;   r   r   r   rF   rG   r^   r   r   r   r   r   r   r   r   r   rz   
reset_mock)r=   r   r   r   r   r>      s0   z>AsyncAutospecTest.test_patch_with_autospec.<locals>.test_asyncrh   rB   r   r   r   test_patch_with_autospec   s   z*AsyncAutospecTest.test_patch_with_autospecN)
r$   r%   r&   rR   r   unittestskipr   r   r   r   r   r   r   r      s    
r   c                   @   sl   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S )AsyncSpecTestc              	      V    fdd}t tfD ]} d|  || W d    n1 s#w   Y  q
d S )Nc                    s(   | t } |jt  |jt d S r   )r   rP   r   r   r   r
   )	mock_typers   r   r   r   
inner_test     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   rP   r   r   r   rr   r   r   r   +test_spec_normal_methods_on_class_with_mock(  r   z9AsyncSpecTest.test_spec_normal_methods_on_class_with_mockc                 C   sv   t t}t| | t |j W d    n1 sw   Y  | t |j W d    d S 1 s4w   Y  d S r   )r   r   r   r   AttributeErrorr   r   rr   r   r   r   0test_spec_normal_methods_on_class_with_mock_seal-  s   "z>AsyncSpecTest.test_spec_normal_methods_on_class_with_mock_sealc              	      X    fdd}t ttfD ]} d|  || W d    n1 s$w   Y  qd S )Nc                    sj   | t d} ||  t   t|  W d    n1 s#w   Y  | td} ||  d S Nr   r-   rP   r7   r;   rF   rG   r2   r   
async_mockZ	sync_mockr   r   r   r   6  s   


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_kw5     	
z$AsyncSpecTest.test_spec_mock_type_kwc              	      r   )Nc                    sf   | t } ||  t   t|  W d    n1 s"w   Y  | t} ||  d S r   r   r   r   r   r   r   D  s   
z@AsyncSpecTest.test_spec_mock_type_positional.<locals>.inner_testztest spec positional with r   r   r   r   r   test_spec_mock_type_positionalC  r   z,AsyncSpecTest.test_spec_mock_type_positionalc                 C   s8   t td}| |t  | }| t| t| d S r   r   r2   rP   r;   rF   rG   r   r|   r   r   r    test_spec_as_normal_kw_AsyncMockQ  s
   
z.AsyncSpecTest.test_spec_as_normal_kw_AsyncMockc                 C   s6   t t}| |t  | }| t| t| d S r   r   r|   r   r   r   (test_spec_as_normal_positional_AsyncMockX  s
   z6AsyncSpecTest.test_spec_as_normal_positional_AsyncMockc                    &   t jtddd fdd}|  d S )Nr   Tr   c                    rN   r   rO   r<   r   r   r   r>   `  rQ   z6AsyncSpecTest.test_spec_async_mock.<locals>.test_asyncr@   rB   r   r   r   test_spec_async_mock_  s   
z"AsyncSpecTest.test_spec_async_mockc                    s"   t tdd fdd}|  d S )NTr   c                    s     | t   | jt d S r   )rP   r
   r   r   r<   r   r   r   r>   g  s   zIAsyncSpecTest.test_spec_parent_not_async_attribute_is.<locals>.test_asyncr   rB   r   r   r   'test_spec_parent_not_async_attribute_isf     

z5AsyncSpecTest.test_spec_parent_not_async_attribute_isc                    s(   t jtdtjd fdd}|  d S )Nr   r   c                    s0     | t  t|   t|  d S r   )rP   r
   assertFalserF   r   rG   r<   r   r   r   test_async_attributeo  s   zFAsyncSpecTest.test_target_async_spec_not.<locals>.test_async_attribute)r	   rA   r   r3   r.   )r   r   r   r   r   test_target_async_spec_notn  s   
z(AsyncSpecTest.test_target_async_spec_notc                    s&   t jtdtd fdd}|  d S )Nr.   r   c                    rN   r   rO   )Zmock_async_funcr   r   r    test_attribute_not_async_spec_isx  rQ   zUAsyncSpecTest.test_target_not_async_spec_is.<locals>.test_attribute_not_async_spec_is)r	   rA   r3   r-   )r   r   r   r   r   test_target_not_async_spec_isw     
z+AsyncSpecTest.test_target_not_async_spec_isc                    s"   t ttd fdd}|  d S )Nr   c                    r   r   r   )ZMockNormalClassr   r   r    test_async_attributes_coroutines~  r   zRAsyncSpecTest.test_spec_async_attributes.<locals>.test_async_attributes_coroutines)r	   normal_foo_namer   )r   r   r   r   r   test_spec_async_attributes}  r   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   r     s    
	r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
AsyncSpecSetTestc                    r   )Nr   TZspec_setc                    rN   r   rO   )r   r   r   r   r>     rQ   z<AsyncSpecSetTest.test_is_AsyncMock_patch.<locals>.test_asyncr@   rB   r   r   r   rR     r   z(AsyncSpecSetTest.test_is_AsyncMock_patchc                 C   s*   t tjd}| t| | |t  d S Nr   )r   r   r   r;   r   rP   rr   r   r   r   test_is_async_AsyncMock  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   r;   r   r   r   r   rP   r   rr   r   r   r   test_is_child_AsyncMock  s   
z(AsyncSpecSetTest.test_is_child_AsyncMockc                 C   sT   t  }t dd d|_t|d}| |t  W d    d S 1 s#w   Y  d S )Nc                 S   s   | S r   r   xr   r   r   <lambda>  r   z=AsyncSpecSetTest.test_magicmock_lambda_spec.<locals>.<lambda>r   	mock_func)r
   r   r	   rA   rP   )r   Zmock_objcmr   r   r   test_magicmock_lambda_spec  s
   "z+AsyncSpecSetTest.test_magicmock_lambda_specN)r$   r%   r&   rR   r   r   r   r   r   r   r   r     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                    s4   dd }t |dd}|dI d H }| |d d S )Nc                    r   r   r   )r   varr   r   r   addition  r   z6AsyncArguments.test_add_return_value.<locals>.addition
   rX      r   r^   )r   r   rs   outputr   r   r   test_add_return_value  s
   z$AsyncArguments.test_add_return_valuec                    sX   dd }t |tdd}| t |dI d H  W d    d S 1 s%w   Y  d S )Nc                    r   r   r   r   r   r   r   r     r   z?AsyncArguments.test_add_side_effect_exception.<locals>.additionerrZside_effectr   )r   	Exceptionr   )r   r   rs   r   r   r   test_add_side_effect_exception  s   "z-AsyncArguments.test_add_side_effect_exceptionc                    2   dd }t |d}|dI d H }| |d d S )Nc                    s
   | d S NrW   r   r   r   r   r   r     s   z?AsyncArguments.test_add_side_effect_coroutine.<locals>.additionr   r      r   r   r   rs   resultr   r   r   test_add_side_effect_coroutine  
   
z-AsyncArguments.test_add_side_effect_coroutinec                    r   )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  r   z3AsyncArguments.test_add_side_effect_normal_functionc                    sn   g d}t |d}|D ]}| | I d H | q| t}| I d H  W d    d S 1 s0w   Y  d S )NrW   r[   r\   r   )r   r^   r   StopAsyncIteration)r   valsrs   itemr   r   r   r   test_add_side_effect_iterable  s   
"z,AsyncArguments.test_add_side_effect_iterablec                    sx   G dd dt }d|dg}t|d}| | I d H d | |}| I d H  W d    d S 1 s5w   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   rW   rl   r   )r   r   r^   r   )r   r   r   rs   r   r   r   r   'test_add_side_effect_exception_iterable  s   
"z6AsyncArguments.test_add_side_effect_exception_iterablec                    s2   t dd}t |d}| I d H }| || d S )Nr   rX   )r   assertIs)r   valuers   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 )NrX   )r   r   
set_resultr   rP   r   r   rs   r   r   r   r   test_return_value_awaitable  s   

z*AsyncArguments.test_return_value_awaitablec                    sv   t  }|d  t|gd}| I d H }| |t j | t | I d H  W d    d S 1 s4w   Y  d S )Nr   )r   r   r   r   rP   r   r   r   r   r   r   !test_side_effect_awaitable_values  s   
"z0AsyncArguments.test_side_effect_awaitable_valuesc                    s2   t dd}t |d}| I d H }| |d d S )Nr   rX   r   r   )r   Zeffectrs   r   r   r   r   test_side_effect_is_AsyncMock  s
   

z,AsyncArguments.test_side_effect_is_AsyncMockc                    sT   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   r   r   r^   rz   r;   r   r   rs   r   r   r   r   test_wraps_coroutine  s   
z#AsyncArguments.test_wraps_coroutinec                    sP   dd  fdd}t |d}| I d H }| | |  |   d S )NrW   Fc                      s   d S r   r   r   r   r   r   r     s   z8AsyncArguments.test_wraps_normal_function.<locals>.innerr   )r   r^   rz   r;   r   r   r   r   test_wraps_normal_function  s   
z)AsyncArguments.test_wraps_normal_functionc                    sz   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 )Nr[   rW   )r   assert_has_awaitsr   r^   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                   @   rp   )AsyncMagicMethodsc                 C   sB   t  }| |jt | |jt | |jt | |jt  d S r   )r
   rP   
__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   rP   	__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
   r;   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   r;   r  r  r   r   r   %test_asyncmock_has_sync_magic_methods2  s
   z7AsyncMagicMethods.test_asyncmock_has_sync_magic_methodsc                 C   sF   t  }| |jt | |jt | t|j | t|j d S r   )r
   rP   r  r   r  r;   r   r
  r   r   r   &test_magic_methods_are_async_functions9  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                   @      e Zd Zdd Zdd ZdS )z/AsyncContextManagerTest.WithAsyncContextManagerc                    r   r   r   r   argskwargsr   r   r   r  D  r   z:AsyncContextManagerTest.WithAsyncContextManager.__aenter__c                    r   r   r   r  r   r   r   r  F  r   z9AsyncContextManagerTest.WithAsyncContextManager.__aexit__N)r$   r%   r&   r  r  r   r   r   r   WithAsyncContextManagerC      r  c                   @   r  )z.AsyncContextManagerTest.WithSyncContextManagerc                 O   r   r   r   r  r   r   r   r  I  r   z8AsyncContextManagerTest.WithSyncContextManager.__enter__c                 O   r   r   r   r  r   r   r   r  K  r   z7AsyncContextManagerTest.WithSyncContextManager.__exit__N)r$   r%   r&   r  r  r   r   r   r   WithSyncContextManagerH  r  r  c                   @   r  )z&AsyncContextManagerTest.ProductionCodec                 C   s
   d | _ d S r   )sessionr   r   r   r   r   O  s   
z/AsyncContextManagerTest.ProductionCode.__init__c              	      sX   | j d4 I d H }| I d H }|W  d   I d H  S 1 I d H s%w   Y  d S )Nzhttps://python.org)r  postjson)r   responsevalr   r   r   r   R  s
   0z+AsyncContextManagerTest.ProductionCode.mainN)r$   r%   r&   r   r   r   r   r   r   ProductionCodeM  s    r$  c              	      r   )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!  {   rX   )r$  r
   r  r   r!  r  rY   r   r   r   r^   )r   Zpcr   r"  r   r   r   r   r   X  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_aenterW     
z7AsyncContextManagerTest.test_set_return_value_of_aenterc              	      r   )Nc                    sv   d   }| | fdd}t| }  jj jj j  j  | d S )NFc               	      sD   4 I d H } d W d   I d H  | S 1 I d H sw   Y  | S r   r   r   calledZcm_mockr   r   use_context_managerm  s   zqAsyncContextManagerTest.test_mock_supports_async_context_manager.<locals>.inner_test.<locals>.use_context_manager)r  r   r;   r  r,  r  rz   ZassertIsNot)r   r   r-  Z	cm_resultr   r+  r   r   h  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_managerg  s   
z@AsyncContextManagerTest.test_mock_supports_async_context_managerc                    s@   |   }t| t }| j_ fdd}| t| | d S )Nc               	      sB    4 I d H } | W  d   I d H  S 1 I d H sw   Y  d S r   r   r*  mock_instancer   r   r-    s   0z^AsyncContextManagerTest.test_mock_customize_async_context_manager.<locals>.use_context_manager)r  r
   rA   r  rY   r   r   )r   r   Zexpected_resultr-  r   r/  r   )test_mock_customize_async_context_manager  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                     
   d d S r   r   r  )enter_calledr   r   enter_coroutine     ziAsyncContextManagerTest.test_mock_customize_async_context_manager_with_coroutine.<locals>.enter_coroutinec                     r2  r   r   r3  )exit_calledr   r   exit_coroutine  r6  zhAsyncContextManagerTest.test_mock_customize_async_context_manager_with_coroutine.<locals>.exit_coroutinec                	      s@    4 I d H  W d   I d H  d S 1 I d H sw   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   r;   )r   r5  r8  r   r-  r   )r4  r7  r0  r   8test_mock_customize_async_context_manager_with_coroutine  s   

zPAsyncContextManagerTest.test_mock_customize_async_context_manager_with_coroutinec                 C   sT   dd }|   }t|}| t t|| W d    d S 1 s#w   Y  d S )Nc              	      s0   | 4 I d H  t  1 I d H sw   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
   r   r:  r   )r   r;  r   r0  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.  r1  r9  r<  r   r   r   r   r  A  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)rl   Z	NormalFoobaz)itemsr   r   r   r   r     r   z,AsyncIteratorTest.WithAsyncIterator.__init__c                 C   r   r   r   r   r   r   r   r	    r   z-AsyncIteratorTest.WithAsyncIterator.__aiter__c                    r   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>  s    zOAsyncIteratorTest.test_aiter_set_return_value.<locals>.main.<locals>.<listcomp>r   r   Z	mock_iterr   r   r     s   z;AsyncIteratorTest.test_aiter_set_return_value.<locals>.main)r   r	  rY   r   r^   )r   r   r   r   rD  r   test_aiter_set_return_value  s
   

z-AsyncIteratorTest.test_aiter_set_return_valuec              	      r   )Nc                    sT      }| |} t|j  t|j  t|j  t|j d S r   )r@  r   r   r	  r;   r  )r   r   r0  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  r)  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 ]V}d
|  || W d    n1 s<w   Y  d|  || W d    n1 sXw   Y  d|  || W d    n1 stw   Y  q#d 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@  r^   r   r   r0  )rI  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	  rY   r^   r   rJ  ZexpectedrI  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	  rY   r^   r   rJ  rL  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   rK  rM  rO  r   r   rL  r   test_mock_async_for  s"   


z%AsyncIteratorTest.test_mock_async_forN)r$   r%   r&   rA   r@  rE  rF  rP  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   rs   r   r   r   r   setUp  s   zAsyncMockAssert.setUpc                    s   | j |i |I d H  d S r   )rs   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w   Y  | t|j |j  |j  |j  | 	t
 |  W d    n1 sGw   Y  | 	t
 |j  W d    d S 1 sbw   Y  d S r   )r   r   r7   r   r;   r   assert_calledassert_called_onceassert_called_once_withr   r   rz   rr   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 s,w   Y  t	| 
| |j  |j  |j  |j  d S r   )r   r   r   rV  rW  rX  r   r   rz   r   rU  r   r   )r   rs   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w   Y  |  t | j  W d    n1 s/w   Y  t|   | j  | j  d S r   )	r   r   rs   rz   rV  r   rS  rW  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 sw   Y  | t |j  W d    n1 s5w   Y  |j  t| 	| |j  |j
  d S r   )r   r   r   r7   r   r   rz   rV  r   rU  r   r   rs   rT  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 s+w   Y  |j  d S r   )	r   r   r   rW  r   rU  r   r   rz   r\  r   r   r   )test_assert_called_once_and_awaited_twice8  s   
z9AsyncMockAssert.test_assert_called_once_and_awaited_twicec                 C   s  |  t | j  W d    n1 sw   Y  |  t | j  W d    n1 s/w   Y  |  t t| | j W d    n1 sLw   Y  |  t | j  W d    n1 sfw   Y  |  t | j  W d    d S 1 sw   Y  d S r   )r   r   rs   rz   rV  r:  r   rU  r   r   r   r   "test_assert_awaited_but_not_calledB  s   "z2AsyncMockAssert.test_assert_awaited_but_not_calledc                 C   s   t dg}t|  | d W d    n1 sw   Y  | j| | t | j| W d    d S 1 s;w   Y  d S Nrl   )r   r7   rs   Zassert_has_callsr   r   r  )r   Zkallsr   r   r    test_assert_has_calls_not_awaitsP  s   

"z0AsyncMockAssert.test_assert_has_calls_not_awaitsc                 C   s   t |  |   W d    n1 sw   Y  ddi fg}| | jj| t |  | d W d    n1 s:w   Y  t |  | d W d    n1 sSw   Y  t tdtdg}| | jj| d S )N r   rl   r>  )r7   rs   r^   
mock_callsr   )r   kalls_empty
mock_kallsr   r   r   0test_assert_has_mock_calls_on_async_mock_no_specX  s   



z@AsyncMockAssert.test_assert_has_mock_calls_on_async_mock_no_specc                 C   s   t t}t|  |  W d    n1 sw   Y  ddi fg}| |jj| | |jt g t|  |jdddddd W d    n1 sMw   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 )	Nrb  r   rW   r[   r\      r   )r.   r/   )r   r   r7   r   r^   rc  r   )r   Za_class_mockrd  Zmethod_kallsre  r   r   r   2test_assert_has_mock_calls_on_async_mock_with_spece  s   


zBAsyncMockAssert.test_assert_has_mock_calls_on_async_mock_with_specc                 C   s   t |  | jjdd d W d    n1 sw   Y  t |  | jjjdtjd W d    n1 s5w   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 )Nr\   )fishr   )cake	something)r\   ri  zsomething_else.something)r   rj  z#method calls not recorded correctly)r7   rs   rk  Zsomething_elser   ZCaker^   method_callsr   r   r   r    test_async_method_calls_recordedt  s    


z0AsyncMockAssert.test_async_method_calls_recordedc                    sD   fdd}| j  t      W d    n1 sw   Y  t    dd W d    n1 s8w   Y  t   j dd W d    n1 sRw   Y   j   | j  tt}t  |  W d    n1 sxw   Y  t  |jddd W d    n1 sw   Y  |  || d S )Nc                    s@   d}|D ]}t | |} |t  |t  |g  qd S )N)r  rl  rc  )getattrrP   r   listr^   )rs   namesr&  attrr   r   r   assert_attrs  s   
z:AsyncMockAssert.test_async_arg_lists.<locals>.assert_attrsrW   r[   r\   )r.   )rs   r7   r   r   r   r   )r   rr  r  r   r   r   test_async_arg_lists  s,   









z$AsyncMockAssert.test_async_arg_listsc                 C   sN   |  t | j  W d    n1 sw   Y  t|   | j  d S r   )r   r   rs   rz   r   rS  r   r   r   r   test_assert_awaited  s
   z#AsyncMockAssert.test_assert_awaitedc                 C   s   |  t | j  W d    n1 sw   Y  t|   | j  t|   |  t | j  W d    d S 1 sAw   Y  d S r   )r   r   rs   r   r   rS  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 sw   Y  t|   d}|  t| | jd W d    n1 s=w   Y  t| d | jd t| d | t | jd W d    d S 1 smw   Y  d S )NzNot awaitedrl   zexpected await not foundSomethingElse)assertRaisesRegexr   rs   r   r   rS  r   )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w   Y  t| d | jd t| d |  t | jd W d    d S 1 sFw   Y  d S r`  )r   r   rs   r   r   rS  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w   Y  t| d |  t | jd W d    n1 s8w   Y  t| d | jd t| d | jd d S )Nrl   r>  rv  )r   r   rs   r   r   rS  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w   Y  | t|jjd t	| 
d | t | j| W d    n1 sJw   Y  t	| 
d | t | j| W d    n1 slw   Y  t	| 
d | j| t	| 
d | j| d S )Nrl   r>  rW   rv  )r   r   r   rs   r  r^   len	exceptionr  r   rS  )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                   @   r)   )z9AsyncMockAssert.test_awaits_asserts_with_any.<locals>.Fooc                 S   r   r   r   r   otherr   r   r   __eq__  r   z@AsyncMockAssert.test_awaits_asserts_with_any.<locals>.Foo.__eq__Nr$   r%   r&   r  r   r   r   r   Foo  r,   r  rW   )r   rS  rs   r  r   r   r   r   )r   r  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                   @   r)   )zBAsyncMockAssert.test_awaits_asserts_with_spec_and_any.<locals>.Fooc                 S   r   r   r   r  r   r   r   r    r   zIAsyncMockAssert.test_awaits_asserts_with_spec_and_any.<locals>.Foo.__eq__Nr  r   r   r   r   r    r,   r  r   c                     s    |  I d H  d S r   r   r3  Zmock_with_specr   r   _custom_mock_runnable_test  s   zYAsyncMockAssert.test_awaits_asserts_with_spec_and_any.<locals>._custom_mock_runnable_testrW   )r   r   r  r   r   r   r   )r   r  r  r   r  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 w   Y  t| d | t | jj|dd W d    n1 sDw   Y  t| d | t | jj|dd W d    n1 shw   Y  t| d | jj|dd t| d | jj|dd d S )Nrl   r>  T)Z	any_orderZbamfZqux)r   r   r   rs   r  r   rS  )r   r~  r   r   r   test_assert_has_awaits_ordered	  s    z.AsyncMockAssert.test_assert_has_awaits_orderedc                 C   sP   | j   t|   | t | j   W d    d S 1 s!w   Y  d S r   )rs   r   r   rS  r   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 s1w   Y  | |jj | tdtd}| j	t
 t
ddg W d    n1 s`w   Y  | |jjt d S )
Nc                    r   r   r   r   r   r   r   f$  r   zIAsyncMockAssert.test_assert_has_awaits_not_matching_spec_error.<locals>.fr   rW   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)]r[   r   )r   rs   r   rS  rw  r   formatreescaper  r   r   r}  	__cause__rP   r:  )r   r  r   r   r   r   .test_assert_has_awaits_not_matching_spec_error#  s2   

z>AsyncMockAssert.test_assert_has_awaits_not_matching_spec_errorN)r$   r%   r&   rR  rS  rU  rY  rZ  r[  r]  r^  r_  ra  rf  rh  rm  rs  rt  ru  ry  rz  r{  r  r  r  r  r  r  r   r   r   r   rQ    s4    

rQ  __main__).r   r5   rF   r  r   
contextlibr   r   r   r   Zunittest.mockr   r   r   r	   r
   r   r   r   r   r   r   r   r*   r-   r1   r2   rA   r3   r$   rL   r   r7   ZTestCaser9   r`   rq   r   r   r   r   r  r  r=  rQ  r   r   r   r   r   <module>   sH    0


=2"Tiy&rE  I