Skip to content

Commit

Permalink
gh-450: move all pytest.fixture into conftest.py
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
paddyroddy committed Nov 21, 2024
1 parent 6fbeb97 commit 61dc081
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 34 deletions.
38 changes: 38 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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),
]
34 changes: 0 additions & 34 deletions tests/test_lensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import healpix
import numpy as np
import numpy.typing as npt
import pytest

from glass import (
Expand All @@ -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
Expand Down

0 comments on commit 61dc081

Please sign in to comment.