From 61dc081e63f10d12640bdc78764264cbd6972a73 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Thu, 21 Nov 2024 16:28:47 +0000 Subject: [PATCH] gh-450: move all `pytest.fixture` into `conftest.py` This makes them available to all tests. I need `cosmo` for use in #449. Have also added `scope="session"` so they are only computed once when testing rather than multiple times. --- tests/conftest.py | 38 ++++++++++++++++++++++++++++++++++++++ tests/test_lensing.py | 34 ---------------------------------- 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 43489682..ab4ae201 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,45 @@ import numpy as np +import numpy.typing as npt import pytest +from cosmology import Cosmology + +from glass import RadialWindow + + +@pytest.fixture(scope="session") +def cosmo() -> Cosmology: + class MockCosmology: + @property + def omega_m(self) -> float: + return 0.3 + + def ef(self, z: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]: + return (self.omega_m * (1 + z) ** 3 + 1 - self.omega_m) ** 0.5 + + def xm( + self, + z: npt.NDArray[np.float64], + z2: npt.NDArray[np.float64] | None = None, + ) -> npt.NDArray[np.float64]: + if z2 is None: + return np.array(z) * 1000 + return (np.array(z2) - np.array(z)) * 1000 + + return MockCosmology() + @pytest.fixture(scope="session") def rng() -> np.random.Generator: return np.random.default_rng(seed=42) + + +@pytest.fixture(scope="session") +def shells() -> list[RadialWindow]: + return [ + RadialWindow(np.array([0.0, 1.0, 2.0]), np.array([0.0, 1.0, 0.0]), 1.0), + RadialWindow(np.array([1.0, 2.0, 3.0]), np.array([0.0, 1.0, 0.0]), 2.0), + RadialWindow(np.array([2.0, 3.0, 4.0]), np.array([0.0, 1.0, 0.0]), 3.0), + RadialWindow(np.array([3.0, 4.0, 5.0]), np.array([0.0, 1.0, 0.0]), 4.0), + RadialWindow(np.array([4.0, 5.0, 6.0]), np.array([0.0, 1.0, 0.0]), 5.0), + ] diff --git a/tests/test_lensing.py b/tests/test_lensing.py index d0c2fb72..cdb343f5 100644 --- a/tests/test_lensing.py +++ b/tests/test_lensing.py @@ -4,7 +4,6 @@ import healpix import numpy as np -import numpy.typing as npt import pytest from glass import ( @@ -19,39 +18,6 @@ from cosmology import Cosmology -@pytest.fixture -def shells() -> list[RadialWindow]: - return [ - RadialWindow(np.array([0.0, 1.0, 2.0]), np.array([0.0, 1.0, 0.0]), 1.0), - RadialWindow(np.array([1.0, 2.0, 3.0]), np.array([0.0, 1.0, 0.0]), 2.0), - RadialWindow(np.array([2.0, 3.0, 4.0]), np.array([0.0, 1.0, 0.0]), 3.0), - RadialWindow(np.array([3.0, 4.0, 5.0]), np.array([0.0, 1.0, 0.0]), 4.0), - RadialWindow(np.array([4.0, 5.0, 6.0]), np.array([0.0, 1.0, 0.0]), 5.0), - ] - - -@pytest.fixture -def cosmo() -> Cosmology: - class MockCosmology: - @property - def omega_m(self) -> float: - return 0.3 - - def ef(self, z: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]: - return (self.omega_m * (1 + z) ** 3 + 1 - self.omega_m) ** 0.5 - - def xm( - self, - z: npt.NDArray[np.float64], - z2: npt.NDArray[np.float64] | None = None, - ) -> npt.NDArray[np.float64]: - if z2 is None: - return np.array(z) * 1000 - return (np.array(z2) - np.array(z)) * 1000 - - return MockCosmology() - - @pytest.mark.parametrize("usecomplex", [True, False]) def test_deflect_nsew(usecomplex: bool) -> None: # noqa: FBT001 d = 5.0