
    d                        d dl mZ d dlm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mZmZmZ eedd                        Zdd	Zdd
Z G d dej        j                  Zd Ze G d d                      ZdS )    )annotations)NumberN_get_default_engine)assert_identicalassert_no_warningsrequires_daskrequires_netCDF4requires_scipyreturnNonec                     t          dd          } | dk    sJ t          d          }|dk    sJ t          d          }|dk    sJ d S )Nzhttp://example.org/test.ncT)allow_remotenetcdf4z/example.gzscipyz/exampler   )engine_remote	engine_gzengine_defaults      >lib/python3.11/site-packages/xarray/tests/test_backends_api.pytest__get_default_enginer      sk     ((DSWXXXMI%%%%#M22I(44NY&&&&&&    c            
     f   t          j        t          dt          j        d          z            t          dt          j        d          t          d          f                     G fd	d
t           j        j                  } t          j        d|           }t          |           d S )N      )axs)units)r   coordsc                       e Zd Z	 dd fdZdS ))test_custom_engine.<locals>.CustomBackendNr   
xr.Datasetc                0                         d          S NT)deepcopy)selffilename_or_objdrop_variableskwargsexpecteds       r   open_datasetz6test_custom_engine.<locals>.CustomBackend.open_dataset&   s     ==d=+++r   Nr   r#   __name__
__module____qualname__r.   )r-   s   r   CustomBackendr"   %   s=          	, 	, 	, 	, 	, 	, 	, 	, 	,r   r5   fake_filenameengine)	xrDatasetdictnparangebackendsBackendEntrypointr.   r   )r5   actualr-   s     @r   test_custom_enginerA       s    zq29Q<<   billDsOOO0T)U)U)U  H, , , , , , ,5 , , , __]CCCFXv&&&&&r   c                     t          j        ddgddgd                              ddg	           G fd
dt           j        j                  } t          j        d|           }t          |           d S )NAB   r   )coord1coord2r   rF   rG   )zc                       e Zd Z	 dd fdZdS )*test_multiindex.<locals>.MultiindexBackendNr   r#   c                0                         d          S r%   r'   )r)   r*   r+   r,   datasets       r   r.   z7test_multiindex.<locals>.MultiindexBackend.open_dataset9   s     <<T<***r   r/   r0   r1   )rL   s   r   MultiindexBackendrJ   8   s=          	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+r   rM   r6   r7   )r9   r:   stackr>   r?   r.   r   )rM   loadedrL   s     @r   test_multiindexrP   2   s     jC:!Q H HIIIGmmx2m33G+ + + + + + +BK9 + + + __5FGGGFWf%%%%%r   c                      e Zd ZdZdddZdS )PassThroughBackendEntrypointz5Access an object passed to the `open_dataset` method.N)r+   c                   |S )zReturn the first argument. )r)   rL   r+   s      r   r.   z)PassThroughBackendEntrypoint.open_datasetH   s    r   )r2   r3   r4   __doc__r.   rT   r   r   rR   rR   E   s6        ??6:       r   rR   c                P    t          d t          | |          D                       S )zLReturn explicit chunks, expanding any integer member to a tuple of integers.c              3     K   | ];\  }}t          |t                    r||z  |fz  ||z  s|d k    r||z  fndz   n|V  <dS )r   rT   N)
isinstancer   ).0chunksizes      r   	<genexpr>z"explicit_chunks.<locals>.<genexpr>P   s         E4 eV$$		U]uh&"&,C$!))uE E      r   )tuplezip)chunksshapes     r   explicit_chunksra   M   s>        vu--     r   c                  L   e Zd ZdZdZd Zd Zej        	                    dg d          ej        	                    ddd	g          d
                         Z
ej        	                    dg d          d             Zej        	                    dg d          d             ZdS )TestPreferredChunksz9Test behaviors related to the backend's preferred chunks.datac                N   t          d t          t          |                    D                       }t          j        | j        t          j        |t          j        |t          j	        d                    dt          t          ||                    i          i          S )zKReturn a dataset with a variable with the given shape and preferred chunks.c              3      K   | ]	}d | V  
dS )dim_NrT   )rY   idxs     r   r\   z5TestPreferredChunks.create_dataset.<locals>.<genexpr>c   s(      ??c\C\\??????r   V1)dtypepreferred_chunks)encoding)r]   rangelenr9   r:   var_nameVariabler<   emptyrj   r;   r^   )r)   r`   pref_chunksdimss       r   create_datasetz"TestPreferredChunks.create_dataseta   s    ??U3u::->->?????zr{HU"(4..9990$s47M7M2N2NO     
 
 	
r   c                V    t          ||           || j                 j        |k    sJ d S r/   )r   ro   r_   )r)   initialfinalexpected_chunkss       r   check_datasetz!TestPreferredChunks.check_datasetn   s3    %(((T]#*o======r   zshape,pref_chunks))r   r   )rz   )r   r   rE   )r      )   r   )r}   )r   )r   r   r   request_with_empty_mapFTc                   |                      ||          }|ri n*t                              || j                 j        d          }t          j        |t          |          }|                     ||t          ||                     dS )z<Honor the backend's preferred chunks when opening a dataset.Nr8   r_   )
rt   r;   fromkeysro   rs   r9   r.   rR   ry   ra   )r)   r`   rr   r   rv   r_   rw   s          r   test_honor_chunksz%TestPreferredChunks.test_honor_chunksr   s      %%e[99
 &BBBwt}5:DAA 	
 8
 
 
 	7E?;+N+NOOOOOr   zshape,pref_chunks,req_chunks))rz   r{      )rz   r{   )r   rE   rE   rE   )rz   r|   r   )rz   r|   r   ))rE   r   )rE   r   )rE   r   c                l   |                      ||          }t          j        t                    5  t	          j        |t          t          t          || j	                 j
        |                              }ddd           n# 1 swxY w Y   |                     ||t          ||                     dS )zGWarn when the requested chunks separate the backend's preferred chunks.r   N)rt   pytestwarnsUserWarningr9   r.   rR   r;   r^   ro   rs   ry   ra   r)   r`   rr   
req_chunksrv   rw   s         r   test_split_chunksz%TestPreferredChunks.test_split_chunks   s    " %%e[99\+&& 	 	O3C 6 ;ZHHII  E	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	7E?:u+M+MNNNNNs   ABBB))rz   r{   r{   )rz   r{   r|   )rz   r{   )r   )rz   r{   )r~   )rz   )rE   rE   rE   r   rE   )rz   r   r{   )rz   r   )r   r   ))r   r   )r   r   )r   r   c                V   |                      ||          }t                      5  t          j        |t          t          t          || j                 j        |                              }ddd           n# 1 swxY w Y   | 	                    ||t          ||                     dS )zGDon't warn when the requested chunks join or keep the preferred chunks.r   N)rt   r   r9   r.   rR   r;   r^   ro   rs   ry   ra   r   s         r   test_join_chunksz$TestPreferredChunks.test_join_chunks   s    . %%e[99!! 	 	O3C 6 ;ZHHII  E	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	7E?:u+M+MNNNNNs   AA99A= A=N)r2   r3   r4   rU   ro   rt   ry   r   markparametrizer   r   r   rT   r   r   rc   rc   [   s3       CCH
 
 
> > > [		
 		
 		
  [5t}EEP P FE P [&	
 	
 	
 	O 	O 	O [&	
 	
 	
 *	O 	O+ *	O 	O 	Or   rc   )r   r   )
__future__r   numbersr   numpyr<   r   xarrayr9   xarray.backends.apir   xarray.testsr   r   r	   r
   r   r   rA   rP   r>   r?   rR   ra   rc   rT   r   r   <module>r      s   " " " " " "                3 3 3 3 3 3              ' ' '  '' ' ' '$& & & &&    2;#@      kO kO kO kO kO kO kO kO kO kOr   