o
    }oi%                     @   sr   d dl Z d dlZd dlmZmZ d dlZd dlmZmZm	Z	m
Z
mZ G dd dZG dd dZG dd	 d	ZdS )
    N)	MagicMockpatch)UnavailableErrorUnavailableMetais_unavailablesafe_importsafe_import_fromc                   @   sP   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S )TestUnavailableMetaz-Test suite for the UnavailableMeta metaclass.c                 C   s,   t ddi }|jdksJ |jdksJ dS )zGTest that UnavailableMeta creates a class with the expected properties.	TestClass MISSINGTestClasszTestClass could not be importedN)r   __name___msgselfr
   r   r   Q/home/ubuntu/.local/lib/python3.10/site-packages/tests/utils/test_import_utils.pytest_metaclass_creation   s   z+TestUnavailableMeta.test_metaclass_creationc                 C   s\   d}t ddd|i}|j|ksJ tjt|d |  W d   dS 1 s'w   Y  dS )z1Test that a custom error message can be provided.Custom error messager
   r   r   matchN)r   r   pytestraisesr   )r   
custom_msgr
   r   r   r   test_custom_error_message&   s   "z-TestUnavailableMeta.test_custom_error_messagec                 C   s|   t ddi }tt |  W d   n1 sw   Y  tt |ddddd W d   dS 1 s7w   Y  dS )	zFTest that attempting to instantiate the class raises UnavailableError.r
   r   N         value)keyr   r   r   r   r   r   r   r   test_call_raises_error1   s   "z*TestUnavailableMeta.test_call_raises_errorc                 C   sB   t ddi }tt |j W d   dS 1 sw   Y  dS )z7Test that accessing attributes raises UnavailableError.r
   r   N)r   r   r   r   some_attributer   r   r   r   "test_attribute_access_raises_error;   s   "z6TestUnavailableMeta.test_attribute_access_raises_errorc                 C   s   t ddi }dd dd dd dd dd d	d d
d dd dd dd dd dd dd dd g}|D ]}tt || W d   n1 sJw   Y  q4dS )z7Test that arithmetic operations raise UnavailableError.r
   r   c                 S   s   | d S Nr   r   cr   r   r   <lambda>G       zLTestUnavailableMeta.test_arithmetic_operations_raise_error.<locals>.<lambda>c                 S   s   d|  S r#   r   r$   r   r   r   r&   H   r'   c                 S   s   | d S r#   r   r$   r   r   r   r&   I   r'   c                 S   s   d|  S r#   r   r$   r   r   r   r&   J   r'   c                 S   s   | d S Nr   r   r$   r   r   r   r&   K   r'   c                 S   s   d|  S r(   r   r$   r   r   r   r&   L   r'   c                 S   s   | d S r(   r   r$   r   r   r   r&   M   r'   c                 S   s   d|  S r(   r   r$   r   r   r   r&   N   r'   c                 S   s   | d S r(   r   r$   r   r   r   r&   O   r'   c                 S   s   d|  S r(   r   r$   r   r   r   r&   P   r'   c                 S   s   | d S r(   r   r$   r   r   r   r&   Q   r'   c                 S   s   d|  S r(   r   r$   r   r   r   r&   R   r'   c                 S   s   |  S Nr   r$   r   r   r   r&   S   s    c                 S   s   t | S r)   )absr$   r   r   r   r&   T   r'   Nr   )r   r
   
operationsopr   r   r   &test_arithmetic_operations_raise_errorB   s,   
z:TestUnavailableMeta.test_arithmetic_operations_raise_errorc              	      s   t ddi }t ddi   fdd fdd fdd fdd fd	d fd
dg}|D ]}tt || W d   n1 sDw   Y  q.dS )z7Test that comparison operations raise UnavailableError.r
   r   AnotherClassc                    s   |  kS r)   r   r$   another_classr   r   r&   a   r'   zLTestUnavailableMeta.test_comparison_operations_raise_error.<locals>.<lambda>c                    s   |  kS r)   r   r$   r/   r   r   r&   b   r'   c                    s   |  k S r)   r   r$   r/   r   r   r&   c   r'   c                    s   |  kS r)   r   r$   r/   r   r   r&   d   r'   c                    s   |  kS r)   r   r$   r/   r   r   r&   e   r'   c                    s   |  kS r)   r   r$   r/   r   r   r&   f   r'   Nr   )r   r
   comparisonscompr   r/   r   &test_comparison_operations_raise_error[   s   





	
z:TestUnavailableMeta.test_comparison_operations_raise_errorc                 C   s
  t ddi }tt t| W d   n1 sw   Y  tt |d  W d   n1 s3w   Y  tt d|d< W d   n1 sLw   Y  tt |d= W d   n1 sdw   Y  tt t| W d   dS 1 s~w   Y  dS )z6Test that container operations raise UnavailableError.r
   r   Nr   r   )r   r   r   r   leniterr   r   r   r   %test_container_operations_raise_errorm   s    



"z9TestUnavailableMeta.test_container_operations_raise_errorc                    s   t ddi  G  fddd}| }tt  dd W d   n1 s(w   Y  tt  d W d   dS 1 sCw   Y  dS )z7Test that descriptor operations raise UnavailableError.r
   r   c                       s   e Zd Z ZdS )zNTestUnavailableMeta.test_descriptor_operations_raise_error.<locals>.DummyClassN)r   
__module____qualname__propr   r
   r   r   
DummyClass   s    r;   N)r   r   r   r   __get__
__delete__)r   r;   dummyr   r:   r   &test_descriptor_operations_raise_error   s   "z:TestUnavailableMeta.test_descriptor_operations_raise_errorN)r   r7   r8   __doc__r   r   r    r"   r-   r3   r6   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 )TestSafeImportc                 C   s:   t d\}}|du sJ t|tjsJ |jdksJ dS )z+Test safe_import with a module that exists.osTN)r   
isinstancetypes
ModuleTyper   r   modulesuccessr   r   r   test_successful_import   s   z%TestSafeImport.test_successful_importc                 C   s8   t d\}}|du sJ t|sJ t|tu sJ dS )z2Test safe_import with a module that doesn't exist.nonexistent_moduleFN)r   r   typer   rF   r   r   r   test_failed_import   s   z!TestSafeImport.test_failed_importc                 C   sf   d}t d|d\}}|du sJ t|sJ tjt|d |  W d   dS 1 s,w   Y  dS )z-Test safe_import with a custom error message.r   rJ   msgFr   N)r   r   r   r   r   )r   r   rG   rH   r   r   r   test_import_with_custom_message   s   "z.TestSafeImport.test_import_with_custom_messagec                 C   s2   t  }td|d\}}|du sJ ||u sJ dS )z,Test safe_import with an alternative module.rJ   altFN)objectr   )r   
alt_modulerG   rH   r   r   r   test_import_with_alternative   s   z+TestSafeImport.test_import_with_alternativec                 C   s   t d\}}|du sJ tt |  W d   n1 sw   Y  tt |j W d   n1 s7w   Y  tt |d  W d   n1 sPw   Y  tt |dk W d   dS 1 sjw   Y  dS )zFTest that using a UnavailableMeta placeholder raises UnavailableError.rJ   FNr   )r   r   r   r   	attributerF   r   r   r   .test_unavailable_module_raises_error_when_used   s   

"z=TestSafeImport.test_unavailable_module_raises_error_when_usedN)r   r7   r8   rI   rL   rO   rT   rV   r   r   r   r   rA      s    rA   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S )TestSafeImportFromc                 C   s4   t dd\}}|du sJ ddl}||ju sJ dS )z0Test safe_import_from with a symbol that exists.rB   pathTr   N)r   rB   rX   )r   symbolrH   rB   r   r   r   test_successful_import_from   s   z.TestSafeImportFrom.test_successful_import_fromc                 C   *   t dd\}}|du sJ t|sJ dS )z7Test safe_import_from with a module that doesn't exist.rJ   nonexistent_symbolFNr   r   r   rY   rH   r   r   r   *test_failed_import_from_nonexistent_module      z=TestSafeImportFrom.test_failed_import_from_nonexistent_modulec                 C   r[   )zMTest safe_import_from with a symbol that doesn't exist in an existing module.rB   r\   FNr]   r^   r   r   r   *test_failed_import_from_nonexistent_symbol   r`   z=TestSafeImportFrom.test_failed_import_from_nonexistent_symbolc                 C   s\   d}t dd|d\}}|du sJ tjt|d |  W d   dS 1 s'w   Y  dS )z2Test safe_import_from with a custom error message.zCustom error message for symbolrB   r\   rM   Fr   N)r   r   r   r   )r   r   rY   rH   r   r   r   $test_import_from_with_custom_message   s   "z7TestSafeImportFrom.test_import_from_with_custom_messagec                 C   s4   t  }tdd|d\}}|du sJ ||u sJ dS )z1Test safe_import_from with an alternative symbol.rB   r\   rP   FN)rR   r   )r   
alt_symbolrY   rH   r   r   r   !test_import_from_with_alternative   s   z4TestSafeImportFrom.test_import_from_with_alternativec                 C   sf   t d%}dd }||_tdddd\}}|du sJ |d	ks!J W d
   d
S 1 s,w   Y  d
S )z-Test safe_import_from with a fallback module.zimportlib.import_modulec                 S   s6   | dkrt d| dkrt }d|_|S td|  )Nprimary_modulezSymbol not foundfallback_modulefallback_symbolzUnexpected module: )AttributeErrorr   rY   ImportError)namemock_moduler   r   r   side_effect   s   z<TestSafeImportFrom.test_fallback_module.<locals>.side_effectre   rY   rf   rf   Trg   N)r   rl   r   )r   mock_importrl   rY   rH   r   r   r   test_fallback_module   s   

"z'TestSafeImportFrom.test_fallback_modulec                 C   s.   t dddd\}}|du sJ t|sJ dS )zBTest safe_import_from when both primary and fallback modules fail.nonexistent_primaryrY   nonexistent_fallbackrm   FNr]   r^   r   r   r   test_fallback_module_both_fail  s   z1TestSafeImportFrom.test_fallback_module_both_failN)
r   r7   r8   rZ   r_   ra   rb   rd   ro   rr   r   r   r   r   rW      s    	rW   )sysrD   unittest.mockr   r   r   nemo.utils.import_utilsr   r   r   r   r   r	   rA   rW   r   r   r   r   <module>   s   x7