Skip to content

Commit

Permalink
test: move FLRW-specific test (astropy#16969)
Browse files Browse the repository at this point in the history
* 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
nstarman authored Sep 9, 2024
1 parent 7329eeb commit 7c77a0f
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 98 deletions.
106 changes: 8 additions & 98 deletions astropy/cosmology/flrw/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst

"""Testing :mod:`astropy.cosmology.flrw.base`."""
"""Testing :mod:`astropy.cosmology.flrw.base`.
This module sets up the tests for subclasses of :class:`astropy.cosmology.FLRW`. The
tests for the specific abstract class :class:`astropy.cosmology.FLRW` are in
``test_flrw``.
"""

import abc
import copy
Expand Down Expand Up @@ -35,16 +41,6 @@

from .conftest import filter_keys_from_items

##############################################################################
# SETUP / TEARDOWN


@dataclass_decorator
class SubFLRW(FLRW):
def w(self, z):
return super().w(z)


##############################################################################
# TESTS
##############################################################################
Expand Down Expand Up @@ -862,93 +858,7 @@ def test_comoving_distance_example(self, cosmo_cls, args, kwargs, expected):
assert u.allclose(cosmo.comoving_distance(z), expected, rtol=1e-4)


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)


# -----------------------------------------------------------------------------
# ==============================================================================


class ParameterFlatOde0TestMixin(ParameterOde0TestMixin):
Expand Down
108 changes: 108 additions & 0 deletions astropy/cosmology/flrw/tests/test_flrw.py
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)

0 comments on commit 7c77a0f

Please sign in to comment.