U
    e»{jï  ã                   @   sš   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	m
Z
 d dlmZ d dlmZ d dlmZ ddlmZmZ G d	d
„ d
eƒZG dd„ deeƒZdS )é    N)Ú	Cosmology)Ú_CosmologyModelÚ
from_modelÚto_model)Úget_redshift_methods)Ú
Gaussian1D)Ú	HAS_SCIPYé   )ÚToFromDirectTestBaseÚToFromTestMixinBasec                   @   sr   e Zd ZdZejdd�dd„ ƒZdd„ Zdd	„ Zd
d„ Z	dd„ Z
dd„ Zdd„ Zej dddddg¡dd„ ƒZdS )ÚToFromModelTestMixinaÁ  Tests for a Cosmology[To/From]Format with ``format="astropy.model"``.

    This class will not be directly called by :mod:`pytest` since its name does
    not begin with ``Test``. To activate the contained tests this class must
    be inherited in a subclass. Subclasses must define a :func:`pytest.fixture`
    ``cosmo`` that returns/yields an instance of a |Cosmology|.
    See ``TestCosmologyToFromFormat`` or ``TestCosmology`` for examples.
    Úclass)Úscopec              	   C   s¢   t |ddd�}t|ƒD ]t}t t||ƒ¡j ¡ }ttf}t	sF|t
f }t t|ƒ¡d }zt||ƒ|Ž  W q |k
rˆ   | |¡ Y qX q|ržt t|ƒ¡S d S )NFT)Zinclude_privateZ
include_z2r	   )r   ÚtupleÚinspectÚ	signatureÚgetattrÚ
parametersÚkeysÚNotImplementedErrorÚ
ValueErrorr   ÚModuleNotFoundErrorÚnpÚarangeÚlenÚdiscardÚrandomÚchoice)ÚselfÚcosmoÚmethodsÚnÚparamsZERROR_SEIVEÚargs© r$   úd/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/cosmology/io/tests/test_model.pyÚmethod_name!   s    
z ToFromModelTestMixin.method_namec              	   C   s.   t ddd�}t t¡� ||ƒ W 5 Q R X dS )z)Test when Model is not the correct class.é
   é   )Z	amplitudeZmeanN)r   ÚpytestÚraisesÚAttributeError)r   Úfrom_formatÚmodelr$   r$   r%   Útest_fromformat_model_wrong_cls<   s    z4ToFromModelTestMixin.test_fromformat_model_wrong_clsc              	   C   s&   t  t¡� |ddd� W 5 Q R X dS )z!Test when method is not a method.úastropy.modelz this is definitely not a method.©ÚmethodN)r)   r*   r+   ©r   Ú	to_formatr$   r$   r%   Útest_toformat_model_not_methodC   s    z3ToFromModelTestMixin.test_toformat_model_not_methodc              	   C   s&   t  t¡� |ddd� W 5 Q R X dS )z*Test when method is actually an attribute.r/   Únamer0   N)r)   r*   r   r2   r$   r$   r%   Ú test_toformat_model_not_callableH   s    z5ToFromModelTestMixin.test_toformat_model_not_callablec           	         s&  |dkrdS |d|d�}t |tƒs&t‚t‡ fdd„ˆ jD ƒƒ}|j|ksLt‚t |j¡d }|j	|Ž }t
ˆ |ƒ|Ž }t ||k¡s†t‚||Ž }t
ˆ |ƒ|Ž }tj ||¡ d|k�r"t |jdf¡jt |j¡ j}|j	|Ž }t
ˆ |ƒ|Ž }t ||k¡sþt‚||Ž }t
ˆ |ƒ|Ž }tj ||¡ dS )	z Test cosmology -> astropy.model.Nr/   r0   c                 3   s    | ]}t ˆ |ƒd k	r|V  qd S )N)r   )Ú.0r!   ©r   r$   r%   Ú	<genexpr>V   s      z;ToFromModelTestMixin.test_toformat_model.<locals>.<genexpr>r	   Zscalaré   )Ú
isinstancer   ÚAssertionErrorr   Ú__parameters__Zparam_namesr   r   Zn_inputsÚevaluater   ÚallZtestingZassert_allcloseZonesÚT)	r   r   r3   r&   r-   Úexpectr#   ÚgotÚexpectedr$   r8   r%   Útest_toformat_modelM   s*    

 
z(ToFromModelTestMixin.test_toformat_modelc                 C   s¼   |dkrdS |d|d�}t |tƒs&t‚|j|ks4t‚|j|ksBt‚|j|ksPt‚||dd�}||ksht‚t|j ¡ ƒ 	|j ¡ ¡s†t‚||ƒ}||ksšt‚t|j ¡ ƒ 	|j ¡ ¡s¸t‚dS )z-Test cosmology -> astropy.model -> cosmology.Nr/   r0   ©Úformat)
r;   r   r<   Zcosmology_classZ	cosmologyr&   ÚsetÚmetar   Úissubset)r   Z	cosmo_clsr   r&   r3   r,   r-   rB   r$   r$   r%   Ú test_tofromformat_model_instancep   s    z5ToFromModelTestMixin.test_tofromformat_model_instancec                 C   s   dS )zy
        Test writing from an instance and reading from that class.
        This works with missing information.
        Nr$   ©r   r$   r$   r%   Ú+test_fromformat_model_subclass_partial_info‘   s    z@ToFromModelTestMixin.test_fromformat_model_subclass_partial_inforF   TFNr/   c                 C   sP   |dkrdS |d|d�}t |tƒr&t‚|j||d�}||dk	rBdndksLt‚dS )zèTest :meth:`astropy.cosmology.Cosmology.is_equivalent`.

        This test checks that Cosmology equivalency can be extended to any
        Python object that can be converted to a Cosmology -- in this case
        a model.
        Nr/   r0   rE   FT)r;   r   r<   Zis_equivalent)r   r   r&   r3   rF   ÚobjZis_equivr$   r$   r%   Útest_is_equivalent_to_model˜   s    z0ToFromModelTestMixin.test_is_equivalent_to_model)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r)   Zfixturer&   r.   r4   r6   rD   rJ   rL   ÚmarkZparametrizerN   r$   r$   r$   r%   r      s   	

#!r   c                   @   s   e Zd ZdZdd„ ZdS )ÚTestToFromModelz Directly test ``to/from_model``.c                 C   s   t tdœ| _d S )N)ÚtoÚfrom)r   r   Z	functionsrK   r$   r$   r%   Úsetup_class­   s    zTestToFromModel.setup_classN)rO   rP   rQ   rR   rW   r$   r$   r$   r%   rT   ª   s   rT   )r   r   Únumpyr   r)   Zastropy.cosmology.corer   Zastropy.cosmology.io.modelr   r   r   Zastropy.cosmology.tests.helperr   Zastropy.modeling.modelsr   Z"astropy.utils.compat.optional_depsr   Úbaser
   r   r   rT   r$   r$   r$   r%   Ú<module>   s    