U
    /e                     @  s   d dl mZ d dlZd dlZd dlZd dlmZ erDd dlmZm	Z	 dd Z
dd Zd	d
 Zd!ddZG dd dZdd ZddddddZddddddZejdddddd ZdS )"    )annotationsN)TYPE_CHECKING)HighLevelGraphLayerc                 C  s   | d S N    xr   r   3/tmp/pip-unpacked-wheel-dbjnr7gq/dask/utils_test.pyinc   s    r   c                 C  s   | d S r   r   r	   r   r   r   dec   s    r   c                 C  s   | | S Nr   r
   yr   r   r   add   s    r   皙?c                 C  s   t | | | S r   )timesleep)abdelayr   r   r   slowadd   s    
r   c                   @  sh   e Zd 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 )GetFunctionTestMixina  
    The GetFunctionTestCase class can be imported and used to test foreign
    implementations of the `get` function specification. It aims to enforce all
    known expectations of `get` functions.

    To use the class, inherit from it and override the `get` function. For
    example:

    > from dask.utils_test import GetFunctionTestMixin
    > class TestCustomGet(GetFunctionTestMixin):
         get = staticmethod(myget)

    Note that the foreign `myget` function has to be explicitly decorated as a
    staticmethod.
    c                 C  sV   dt dftddfd}| |ddks*t| |ddks>t| |ddksRtd S )Nr   :x:yr   r   :z   r      r   r   getAssertionErrorselfdr   r   r   test_get.   s    zGetFunctionTestMixin.test_getc                 C  sj   dt dftddfd}z| |d}W n tk
r:   Y n,X d}|d| d7 }d	sft|| jjd S )
Nr   r   r   r   badkey-Expected `{}` with badkey to raise KeyError.

Obtained '
' instead.F)r   r   r!   KeyErrorr"   format__name__r$   r%   resultmsgr   r   r   test_badkey5   s    z GetFunctionTestMixin.test_badkeyc                 C  sn   ddt ddgfd}z| |dgdg}W n tk
r>   Y n,X d}|d| d	7 }d
sjt|| jjd S )Nr   r   r
   r   r
   r   zr'   r(   r)   r*   F)sumr!   r+   r"   r,   r-   r.   r   r   r   test_nested_badkey@   s    z'GetFunctionTestMixin.test_nested_badkeyc                 C  s(   dt ddfd}| |ddks$td S )Nr   r
   
   r   r      )r   r!   r"   r#   r   r   r   test_data_not_in_dict_is_okL   s    z0GetFunctionTestMixin.test_data_not_in_dict_is_okc                 C  sD   ddt ddgfd}| |ddgdks,t| |ddks@td S )	Nr   r   r
   r   r2   )r   r   r3   r   r4   r!   r"   r#   r   r   r   test_get_with_listP   s    z'GetFunctionTestMixin.test_get_with_listc                 C  s   dddgddt dfgtdfgddgtdfdgtdfgdgd}| |ddddgksXt| |ddddgksrt| |dddgkst| |dd	gkst| |d
dddgdddggkst| |dd	ddggd	gdgkstd S )Nr   r   r   r   r   c)r   r   r;   r%   efr%      r<   r=   )r   r4   r!   r"   r#   r   r   r   test_get_with_list_top_levelV   s    
$z1GetFunctionTestMixin.test_get_with_list_top_levelc                 C  sF   ddt ddgfd}| |dgdgdks.t| |ddksBtd S )	Nr   r   r
   r   r2   ))r   r   r3   r   r9   r#   r   r   r   test_get_with_nested_listf   s    z.GetFunctionTestMixin.test_get_with_nested_listc                 C  s2   dd }d|ddhfd}|  |ddks.td S )Nc                 S  s   | t | S r   )lenr   r   r   r   <lambda>m       zPGetFunctionTestMixin.test_get_works_with_unhashables_in_values.<locals>.<lambda>r   r
   r   r   r   )r!   r"   )r$   r=   r%   r   r   r   )test_get_works_with_unhashables_in_valuesl   s    z>GetFunctionTestMixin.test_get_works_with_unhashables_in_valuesc                 C  s2   dt dftt dfdfd}| |ddks.td S )Nr   r
   r   r2   r3      r    r#   r   r   r   test_nested_tasksr   s    z&GetFunctionTestMixin.test_nested_tasksc                 C  s2   dd t dD }d|d< | |ddks.td S )Nc                 S  s"   i | ]}d |d  t d | fqS )zx%dr   )r   ).0ir   r   r   
<dictcomp>x   s     
 z=GetFunctionTestMixin.test_get_stack_limit.<locals>.<dictcomp>i'  r   Zx0Zx10000)ranger!   r"   r#   r   r   r   test_get_stack_limitw   s    z)GetFunctionTestMixin.test_get_stack_limitc                 C  s\   ddl m} dtdfddttdfdfid}d	d
hd}|||}| |ddksXtd S )Nr   )r   r   r
   r   r3   r   )r   r   r   r   rE   )dask.highlevelgraphr   r   r   r!   r"   )r$   r   layersZdependenciesgraphr   r   r   test_with_HighLevelGraph|   s
    "
z-GetFunctionTestMixin.test_with_HighLevelGraphN)r-   
__module____qualname____doc__r&   r1   r5   r8   r:   r?   r@   rD   rF   rK   rO   r   r   r   r   r      s   r   c              	   C  s,   zt | W S  ttfk
r&   Y dS X dS )z>Import a module and return it; in case of failure; return NoneN)	importlibimport_moduleImportErrorAttributeError)namer   r   r   import_or_none   s    rX   r   strr   )hlgprefixreturnc                 C  sD   | j  D ]\}}||r
|  S q
td|dt| j  dS )zIGet the first layer from a HighLevelGraph whose name starts with a prefixzNo layer starts with z: N)rM   items
startswithr+   list)rZ   r[   keyZlyrr   r   r   	hlg_layer   s    

ra   int)rZ   rH   r\   c                 C  s   | j |  |  S )zDGet the layer from a HighLevelGraph at position ``i``, topologically)rM   Z_toposort_layers)rZ   rH   r   r   r   hlg_layer_topological   s    rc   boolztype[Warning])	conditioncategorymessagec              	   c  sL   | r.ddl }|j||d}|V  W 5 Q R X nt }|V  W 5 Q R X dS )z*Conditionally check if a warning is raisedr   N)match)pytestZwarns
contextlibnullcontext)re   rf   rg   ri   ctxr   r   r   _check_warning   s    
rm   )r   )
__future__r   rj   rS   r   typingr   rL   r   r   r   r   r   r   r   rX   ra   rc   contextmanagerrm   r   r   r   r   <module>   s    
h