a
    ߙfbM                  k   @   s  d Z ddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ ejdfejd d	fejej d
fejej d	fejejd  dfejej dfe	jdfejejd ej ej  dfejejd ej ej  dfejejd ej ej  dfejdfejdfejjej d
fejej dfejej dfejej ejd  dfejejd  dfejej dfej ej! dfe"ddfejd dfejdfej dfej#dfejejd  dfejd fej$d!fej%d"fej&d#fej'd#fejj(d#fejj)d#fejj)ej d$fejd%fej*dfej+d&fej,dfej-ejd  d'fejd ej- d(fej%ejd  d)fej-ej ej d*fej-ejd  ej dfej ej dfej ejd  d+fej$ejej  d,fejd ej d-fejd. d/fej.dfejd0fej/d1fej0d2fej1d3fej2d4fejej d5fej.ejd  dfej/ej d6fejejd  d7fejejd  d8fej2ej d9fej3dfejd:fej.ej d;fej4ej d<fej4d=fej5d>fej6d?fej7d@fej,ej# dAfej5ejd  dBfej8j9dCfej8j:dDfej;jdfej;jej dEfej<j=dFfej<j>dGfej<j?dHfej<j@dIfejjAejej ejj(  dJfejjBej' dKfejjBej' ej$ dLfej*ejdM  ejd.  dNfej,ejd  dNfejd ej% dOfejej1 dPfej1ej dQfej.ejd  dRfej*ej dRfejCd. ejdS  dTfejejd  dUfejejdV  dWfejejdX  dYfejejdZ  d[fej'ej d\fejj(ej d\fejj)ejjD d\fejjEej$ d]fej%ejF ej d^fejjGejjD ejd  d_fej%ej d`fejjHej' ej% dafej%ej- dbfejjIejJ dcfejjDd ej ddfejej defej+d. dffejKdfgjZLejMNdgeLdhdi ZOejjPZQejjPZRejej jPZSejd jPZTejd. jPZUejKjPZVej+jPZWej-ej ej jPZXejMNdjdkejdfdldXej dfeQeQfej+dmfdngdodp ZYejMNdqdeZfdreZfdshe[fgdtdu Z\dvdw Z]eQeQdxdyfeQeTdxdzfeQddxdyfdeQdxe^feVeVdxdyfeXd*dxdyfeWdmdxdyfejd jPddxdyfejd jPejd{ jPdxdzfeQeQd|dzfeSeRd|dyfeWeVd|dyfeQejdxe^feQeQd}eTfeSeRd}eQfeSeRd~eQfeQeRdeSfeTeQdeQfeQeTdeQfeVeVd}eVfeVeVdeVfeQddeTfeTddeQfeVdVdeVfejeQd}e^fdeQd}e^fejeRde^fdeQde^feQejd}eTfeQejd~eTfeSejd}eQfeQdd}eQfeQdd~eQfeQejdeSfeTddeTfeRejdeSfeQddeUfeQddeTfeQdd}e^feQdd~e^feQdde^feQd.de^feQdd}eTfeQdd~eTfeTddeQfeQddeQfg.Z_ejMNde_dd Z`ejdhfej-ej ej ddhfej+h dfgZaejMNdeadd Zbdd Zcdd Zdejejfejejfejej-ej ejdM  fejej ejd fej&ej'fej&ejj)fej&ejj(fej'ejj(fejd ejed fg	ZfejMNdefdd ZgejMNdefdd Zhejejfejd ejd fejej*fejejj(fgZiejMNdeidd ZjeQdfeSdfeWdfej'jPd#fej*ej& ej- jPdfgZkeQdfeSdfeWdfej'jPdfej*ej& ej- jPdfgZlejMNdekdd ZmejMNdeldd Zndd ZoejMNdep ddd.gdd Zqdd ZrddgZsejMNdesdd ZtG dd dZuejMNdg ddd Zvdd ZwejMNdg ddd ZxddĄ ZydS )zC
Unit tests for the handling of physical types in `astropy.units`.
    N)units)physical)hbar)AstropyDeprecationWarninglength   Zvolumespeed   unknowndimensionlesszangular momentum   zspectral flux density wavzphoton flux density wavzphoton flux densityzdata quantityZpowerzelectrical currentzelectrical current densityzmagnetic fluxZenergyzangular speed   areatimeZanglezsolid angleZaccelerationZ	frequencyZmasszamount of substanceZtemperatureZtemperature_gradientZforcepressurezmass densityzspecific volumezmolar concentrationzmomentum/impulsezangular accelerationzdynamic viscosityzkinematic viscosity
wavenumberzelectrical chargezelectrical potentialzelectrical resistancezelectrical conductancezelectrical capacitancezelectrical dipole momentzelectrical field strengthzelectrical flux densityzelectrical charge densityZpermittivityzmagnetic flux densityzmagnetic field strengthzelectromagnetic field strengthZ
inductancezluminous intensityzluminous fluxzluminous emittance/illuminancezradiant intensityZ	luminancezspectral flux densityzphoton fluxZ	bandwidthzelectrical charge (ESU)zelectrical current (ESU)zelectrical current (EMU)zelectrical charge (EMU)zthermal conductivityzheat capacityzspecific heat capacityzenergy fluxzmolar volumezelectrical resistivityzelectrical conductivityzmagnetic momentzvolumetric rateZjerk   Zsnap   Zcrackle   popztemperature gradientzspecific energyzreaction ratezmoment of inertiazcatalytic activityzmolar heat capacityZmolalityZabsementzvolumetric flow ratezfrequency driftZcompressibilityzunit, physical_typec                 C   s*   | j |ks&J | d|d| j ddS )z
    Test that the `physical_type` attribute of `u.Unit` objects provides
    the expected physical type for various units.

    Many of these tests are used to test backwards compatibility.
    z&.physical_type was expected to return z, but instead returned .Nphysical_type)unitr    r   @lib/python3.9/site-packages/astropy/units/tests/test_physical.pytest_physical_type_names~   s    r   z0physical_type_representation, physical_type_name)      ?r   )workr!   energy_density)r"   r"   c                 C   s*   t | }t|t jsJ ||ks&J dS )z/Test different ways of getting a physical type.N)r   get_physical_type
isinstancePhysicalType)Zphysical_type_representationphysical_type_namer   r   r   r   test_getting_physical_type   s    
r'   zargument, exceptionznot a name of a physical typez'this set cannot be made into a Quantityc                 C   s8   t | t|  W d   n1 s*0    Y  dS )zo
    Test that `get_physical_type` raises appropriate exceptions when
    provided with invalid arguments.
    N)pytestraisesr   r#   )ZargumentZ	exceptionr   r   r   %test_getting_physical_type_exceptions   s    r*   c                   C   s@   t t" ttjjtj W d   n1 s20    Y  dS )z
    Test that `PhysicalType` instances cannot be cast into `Quantity`
    objects.  A failure in this test could be related to failures
    in subsequent tests.
    N)r(   r)   	TypeErroruZQuantitymr   r   r   r   r   )test_physical_type_cannot_become_quantity   s    r.   __eq__TF	   __ne____mul____rmul____truediv____rtruediv____pow__g      ?g	@r        zleft, right, operator, expectedc                 C   s   t | |||ksJ dS )zg
    Test that `PhysicalType` dunder methods that require another
    argument behave as intended.
    N)getattr)leftrightoperatorexpectedr   r   r   test_physical_type_operations   s    r=   Zimpulsemomentum>   stressr   energy densityzunit, expected_setc                 C   s   t | j}||ksJ dS )z<Test making a `physical.PhysicalType` instance into a `set`.N)setr   )r   Zexpected_setZresulting_setr   r   r   test_physical_type_as_set  s    
rB   c                  C   s"   dd t D } | g dksJ dS )z5Test iterating through different physical type names.c                 S   s   g | ]}|qS r   r   ).0r&   r   r   r   
<listcomp>      z0test_physical_type_iteration.<locals>.<listcomp>)r@   r   r?   N)r   )Zphysical_type_namesr   r   r   test_physical_type_iteration  s    rF   c                   C   s   dt v sJ dtv sJ dS )ze
    Test that `in` works as expected for `PhysicalType` objects with one
    or multiple names.
    r   r   N)r   r   r   r   r   r   test_physical_type_in  s    rG      zunit1, unit2c                 C   s,   | j |j kdu sJ | j |j kdu s(J dS )zi
    Test that `physical.PhysicalType` instances for units of the same
    dimensionality are equal.
    TFNr   )unit1unit2r   r   r   $test_physical_type_instance_equality(  s    rK   c                 C   s8   t | }t |}||ks J |dkr4||u s4J dS )z
    Test that `get_physical_type` retrieves the same `PhysicalType`
    instances for equivalent physical types, except for unknown types
    which are not cataloged.
    r
   N)r   r#   rI   rJ   Zphysical_type1Zphysical_type2r   r   r   'test_get_physical_type_equivalent_pairs2  s
    

rM      c                 C   s<   t | d}t |d}||kdu s(J ||kdu s8J dS )zy
    Test that `physical.PhysicalType` instances for units with different
    dimensionality are considered unequal.
    Zptype1Zptype2TFN)r   r%   rL   r   r   r   &test_physical_type_instance_inequalityH  s    rO   zspeed/velocityzenergy density/pressure/stressz'specific entropy/specific heat capacityzPhysicalType('length')z#PhysicalType({'speed', 'velocity'})z6PhysicalType({'energy density', 'pressure', 'stress'})zPhysicalType('temperature')z<PhysicalType({'specific entropy', 'specific heat capacity'})zphysical_type, expected_strc                 C   s   t | |ksJ dS )z.Test using `str` on a `PhysicalType` instance.N)str)r   Zexpected_strr   r   r   test_physical_type_strf  s    rQ   zphysical_type, expected_reprc                 C   s   t | |ksJ dS )z/Test using `repr` on a `PhysicalType` instance.N)repr)r   Zexpected_reprr   r   r   physical_type_reprl  s    rS   c                  C   s   t di} | t  dksJ dS )z>Test that a `PhysicalType` instance can be used as a dict key.*   N)r   )Z
dictionaryr   r   r   test_physical_type_hasht  s    rU   multiplicandrT   c                 C   s6   t t t|   W d   n1 s(0    Y  dS )zv
    Test that multiplication of a physical type returns `NotImplemented`
    when attempted for an invalid type.
    N)r(   r)   r+   r   )rV   r   r   r   !test_physical_type_multiplicationz  s    rW   c                  C   s4   t jddd} | j}t|tjs$J |dks0J dS )zU
    Test basic functionality for the physical type of an unrecognized
    unit.
    ZparrotZsilent)Zparse_strictr
   N)r,   Unitr   r$   r   r%   )Zunrecognized_unitr   r   r   r   $test_unrecognized_unit_physical_type  s    rY   )rT   )zvalid inputrT   invalid_inputc                 C   sR   t jd }tt t||  W d   n1 s60    Y  |jdksNJ dS )z
    Test that `PhysicalType` cannot be instantiated when one of the
    supplied names is not a string, while making sure that the physical
    type for the unit remains unknown.
    W   Nr
   )r,   sr(   r)   
ValueErrorr   r%   r   )rZ   Zobscure_unitr   r   r   test_invalid_physical_types  s    
*r^   c                   @   sT   e Zd Zejd Zejd Zdd Zdd Z	dd Z
d	d
 Zedd Zdd ZdS )TestDefPhysTypec   rT   c                 C   sJ   t t t| jd W d   n1 s.0    Y  dtjvsFJ dS )z.Test that a unit cannot be defined as unknown.r
   N)r(   r)   r]   r   def_physical_type
weird_unit_unit_physical_mappingselfr   r   r   ,test_attempt_to_define_unknown_physical_type  s    ,z<TestDefPhysType.test_attempt_to_define_unknown_physical_typec                 C   sP   t t" t| jddh W d   n1 s20    Y  | jjdksLJ dS )z
        Test that `def_physical_type` raises an exception when it tries to
        set the physical type of a new unit as the name of an existing
        physical type.
        r   Z	somethingNr
   )r(   r)   r]   r   ra   rb   r   rd   r   r   r   &test_multiple_same_physical_type_names  s    0z6TestDefPhysType.test_multiple_same_physical_type_namesc                 C   s|  d}d}zlz.t | j| | jj|ks6J d| j W n tyL    Y n0 W t j|ddd t j|d n&t j|ddd t j|d 0 zz.t | j| t	| jj||hksJ dW n ty    Y n0 W t j|ddd t j|d t j|ddd t j|d nJt j|ddd t j|d t j|ddd t j|d 0 dS )z{
        Test that calling `def_physical_type` on an existing physical
        type adds a new physical type name.
        z
weird namezstrange namez unable to set physical type for  _Nz2did not correctly append a new physical type name.)
r   ra   rb   r   	Exception_attrname_physical_mappingr   replaceZ_name_physical_mappingrA   )re   Z
weird_nameZstrange_namer   r   r   &test_expanding_names_for_physical_type  s@    




z6TestDefPhysType.test_expanding_names_for_physical_typec                 C   s<   t t t| jd W d   n1 s.0    Y  dS )z
        Test that a physical type name already in use cannot be assigned
        for another unit (excluding `"unknown"`).
        r   N)r(   r)   r]   r   ra   rb   rd   r   r   r   test_redundant_physical_type  s    z,TestDefPhysType.test_redundant_physical_typec                 C   s6   t | jD ]}tj|= q
tj|  = | jdks2J dS )z-Reset the physical type of unit to "unknown".r
   N)listr   r   rc   Z_physical_unit_mappingZ_get_physical_type_id)r   namer   r   r   _undef_physical_type  s    
z$TestDefPhysType._undef_physical_typec                 C   sL   | j | jfD ]:}t|}|dkr,| | |jdksJ d| dqdS )z
        Remove the definitions of the physical types that were added
        using `def_physical_unit` for testing purposes.
        r
   zthe physical type for z., which was added fortesting, was not deleted.N)rb   strange_unitr   r#   rq   r   )re   r   r   r   r   r   teardown_method  s    


zTestDefPhysType.teardown_methodN)__name__
__module____qualname__r,   r-   rb   r\   rr   rf   rg   rm   rn   staticmethodrq   rs   r   r   r   r   r_     s   


"
r_   zmethod, expected))titleZLength)isalphaT)	isnumericF)upperZLENGTHc                 C   sJ   t jtdd tt|  }W d   n1 s00    Y  ||ksFJ dS )zj
    Test that str methods work for `PhysicalType` instances while issuing
    a deprecation warning.
    zPhysicalType instancesmatchN)r(   Zwarnsr   r8   r   )methodr<   Zresult_of_method_callr   r   r   .test_that_str_methods_work_with_physical_types  s    *r   c                   C   s4   t t tj W d   n1 s&0    Y  dS )z
    Test that a missing attribute raises an `AttributeError`.

    This test should be removed when the deprecated option of calling
    string methods on PhysicalType instances is removed from
    `PhysicalType.__getattr__`.
    N)r(   r)   AttributeErrorr   Z0not_the_name_of_a_str_or_physical_type_attributer   r   r   r   $test_missing_physical_type_attribute  s    r   
ptype_name)r   r   Zentropyc                 C   s.   t | }t|}t|}||ks*J d S )N)r,   r#   pickledumpsloads)r   ptypeZpklotherr   r   r   test_pickling  s    


r   c                  C   s   t tttj sJ t tttjs4J tj D ].} tj|  }tt| sZJ tt| |u s>J q>t	j
tdd tj W d    n1 s0    Y  d S )Nzhas no attributer|   )rA   dirr   
issupersetrk   keys__all__hasattrr8   r(   r)   r   Znot_a_valid_physical_type_name)Zpnamer   r   r   r   !test_physical_types_module_access  s    
r   )z__doc__r   r(   Zastropyr   r,   Zastropy.unitsr   Zastropy.constantsr   Zastropy.utils.exceptionsr   r-   cmZkmhZbarnZMpcr\   r   ZergZAAZphotonZHzZbytebitZimperialmiZweekCTNZradZmsrX   srgZmolKZdeg_CZdeg_FZdeg_RJZPaWZkgAVZOhmSFZWbHZcdZlmZlxZ	astrophysZJyRZmiscZcgsZFranklinZ
statampereZBiotZ	abcoulombZbtuZcalZyrZftZCalorieLZlbfZkcalZinchZhrZdimensionless_unscaledZunit_physical_type_pairsZmarkZparametrizer   r   r   r   r   r   r   r   r   r>   r'   r]   r+   r*   r.   NotImplementedZoperation_parametersr=   Zunit_with_physical_type_setrB   rF   rG   ZpcZequivalent_unit_pairsrK   rM   Znonequivalent_unit_pairsrO   Zphysical_type_with_expected_strZ physical_type_with_expected_reprrQ   rS   rU   ro   rW   rY   Zinvalid_inputsr^   r_   r   r   r   r   r   r   r   r   <module>   s   








n


	



































2
	






	




	




	
V


