forked from astropy/astropy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: move FLRW-specific test (astropy#16969)
* test: move FLRW-specific test Signed-off-by: nstarman <[email protected]> * test: clean up import Signed-off-by: nstarman <[email protected]> --------- Signed-off-by: nstarman <[email protected]>
- Loading branch information
Showing
2 changed files
with
116 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# Licensed under a 3-clause BSD style license - see LICENSE.rst | ||
|
||
"""Testing :mod:`astropy.cosmology.FLRW`.""" | ||
|
||
from typing import final | ||
|
||
import pytest | ||
|
||
from astropy.cosmology import FLRW | ||
from astropy.cosmology.core import _COSMOLOGY_CLASSES, dataclass_decorator | ||
from astropy.cosmology.tests.helper import get_redshift_methods | ||
from astropy.cosmology.tests.test_core import invalid_zs | ||
from astropy.utils.compat.optional_deps import HAS_SCIPY | ||
|
||
from .test_base import FLRWTest | ||
|
||
|
||
@dataclass_decorator | ||
class SubFLRW(FLRW): | ||
def w(self, z): | ||
return super().w(z) | ||
|
||
|
||
@final | ||
class TestFLRW(FLRWTest): | ||
"""Test :class:`astropy.cosmology.FLRW`.""" | ||
|
||
abstract_w = True | ||
|
||
def setup_class(self): | ||
""" | ||
Setup for testing. | ||
FLRW is abstract, so tests are done on a subclass. | ||
""" | ||
super().setup_class(self) | ||
|
||
# make sure SubCosmology is known | ||
_COSMOLOGY_CLASSES["SubFLRW"] = SubFLRW | ||
|
||
self.cls = SubFLRW | ||
|
||
def teardown_class(self): | ||
super().teardown_class(self) | ||
_COSMOLOGY_CLASSES.pop("SubFLRW", None) | ||
|
||
# =============================================================== | ||
# Method & Attribute Tests | ||
|
||
# --------------------------------------------------------------- | ||
# Methods | ||
|
||
def test_w(self, cosmo): | ||
"""Test abstract :meth:`astropy.cosmology.FLRW.w`.""" | ||
with pytest.raises(NotImplementedError, match="not implemented"): | ||
cosmo.w(1) | ||
|
||
def test_Otot(self, cosmo): | ||
"""Test :meth:`astropy.cosmology.FLRW.Otot`.""" | ||
exception = NotImplementedError if HAS_SCIPY else ModuleNotFoundError | ||
with pytest.raises(exception): | ||
assert cosmo.Otot(1) | ||
|
||
def test_efunc_vs_invefunc(self, cosmo): | ||
""" | ||
Test that efunc and inv_efunc give inverse values. | ||
Here they just fail b/c no ``w(z)`` or no scipy. | ||
""" | ||
exception = NotImplementedError if HAS_SCIPY else ModuleNotFoundError | ||
|
||
with pytest.raises(exception): | ||
cosmo.efunc(0.5) | ||
|
||
with pytest.raises(exception): | ||
cosmo.inv_efunc(0.5) | ||
|
||
@pytest.mark.skip(reason="w(z) is abstract") | ||
def test_luminosity_distance_pandas(self, cosmo): | ||
"""Test :meth:`astropy.cosmology.FLRW.luminosity_distance`.""" | ||
|
||
_FLRW_redshift_methods = get_redshift_methods( | ||
FLRW, include_private=True, include_z2=False | ||
) - {"w"} | ||
|
||
@pytest.mark.skipif(not HAS_SCIPY, reason="scipy is not installed") | ||
@pytest.mark.parametrize("z, exc", invalid_zs) | ||
@pytest.mark.parametrize("method", sorted(_FLRW_redshift_methods)) | ||
def test_redshift_method_bad_input(self, cosmo, method, z, exc): | ||
"""Test all the redshift methods for bad input.""" | ||
with pytest.raises(exc): | ||
getattr(cosmo, method)(z) | ||
|
||
# =============================================================== | ||
# Usage Tests | ||
|
||
@pytest.mark.skipif(not HAS_SCIPY, reason="scipy required for this test.") | ||
@pytest.mark.parametrize("method", ("Om", "Ode", "w", "de_density_scale")) | ||
def test_distance_broadcast(self, cosmo, method): | ||
with pytest.raises(NotImplementedError): | ||
super().test_distance_broadcast(cosmo, method) | ||
|
||
@pytest.mark.skipif(not HAS_SCIPY, reason="scipy required for this test.") | ||
@pytest.mark.parametrize( | ||
("args", "kwargs", "expected"), | ||
[((70, 0.27, 0.73), {"Tcmb0": 3.0, "Ob0": 0.03}, None)], | ||
) | ||
def test_comoving_distance_example(self, cosmo_cls, args, kwargs, expected): | ||
with pytest.raises(NotImplementedError): | ||
super().test_comoving_distance_example(cosmo_cls, args, kwargs, expected) |