LCOV - code coverage report
Current view: top level - tests - conftest.py (source / functions) Hit Total Coverage
Test: coverage.info Lines: 47 49 95.9 %
Date: 2026-03-03 11:15:30 Functions: 8 8 100.0 %

          Line data    Source code
       1             : # SPDX-FileCopyrightText: 2024 PairInteraction Developers
       2             : # SPDX-License-Identifier: LGPL-3.0-or-later
       3             : 
       4           1 : from __future__ import annotations
       5             : 
       6           1 : import multiprocessing
       7           1 : import os
       8           1 : from typing import TYPE_CHECKING
       9             : 
      10           1 : import pytest
      11           1 : from pint import UnitRegistry
      12             : 
      13             : if TYPE_CHECKING:
      14             :     from _pytest.config import Config
      15             :     from _pytest.config.argparsing import Parser
      16             :     from pairinteraction.green_tensor import GreenTensorInterpolator
      17             :     from pairinteraction_gui.app import Application
      18             : 
      19             :     from .utils import PairinteractionModule
      20             : 
      21             : 
      22           1 : def pytest_addoption(parser: Parser) -> None:
      23           1 :     parser.addoption("--generate-reference", action="store_true", default=False, help="Generate reference data")
      24           1 :     parser.addoption(
      25             :         "--database-dir",
      26             :         action="store",
      27             :         default=None,
      28             :         help="Path to the database directory",
      29             :     )
      30           1 :     parser.addoption("--download-missing", action="store_true", default=False, help="Download missing database files")
      31             : 
      32             : 
      33           1 : @pytest.fixture(scope="session")
      34           1 : def generate_reference(pytestconfig: Config) -> bool:
      35           1 :     return pytestconfig.getoption("--generate-reference")  # type: ignore [no-any-return]
      36             : 
      37             : 
      38           1 : @pytest.fixture(scope="session")
      39           1 : def ureg() -> UnitRegistry:
      40             :     """Create and return a UnitRegistry with atomic units."""
      41           1 :     return UnitRegistry(system="atomic")
      42             : 
      43             : 
      44           1 : def pytest_sessionstart(session: pytest.Session) -> None:
      45             :     """Initialize everything before the tests are run."""
      46           1 :     download_missing: bool = session.config.getoption("--download-missing")
      47           1 :     database_dir: str | None = session.config.getoption("--database-dir")
      48             : 
      49             :     # Disable the test mode of PairInteraction that would call _setup_test_mode
      50             :     # automatically. This would be necessary for testing the jupyter notebooks
      51             :     # but we want to call _setup_test_mode manually.
      52           1 :     test_mode = os.environ.get("PAIRINTERACTION_TEST_MODE", "1")
      53           1 :     os.environ["PAIRINTERACTION_TEST_MODE"] = "0"
      54             : 
      55             :     # Call _setup_test_mode manually with the given options
      56           1 :     from pairinteraction import _setup_test_mode
      57             : 
      58           1 :     _setup_test_mode(download_missing, database_dir)
      59             : 
      60             :     # Set the test mode environment variables, so they can be used by subprocesses
      61           1 :     os.environ["PAIRINTERACTION_TEST_MODE"] = test_mode
      62           1 :     if database_dir is not None:
      63           0 :         os.environ["PAIRINTERACTION_TEST_DOWNLOAD_MISSING"] = str(int(download_missing))
      64           0 :         os.environ["PAIRINTERACTION_TEST_DATABASE_DIR"] = database_dir
      65             : 
      66             :     # For pairinteraction_gui set the multiprocessing start method to "spawn" (see also pairinteraction_gui/__init__.py)
      67           1 :     multiprocessing.set_start_method("spawn")
      68             : 
      69             : 
      70           1 : @pytest.fixture(scope="session")
      71           1 : def qapp_cls() -> type[Application]:
      72             :     """Let the qapp and qtbot fixtures use our custom Application class."""
      73           1 :     from pairinteraction_gui.app import Application
      74             : 
      75           1 :     return Application
      76             : 
      77             : 
      78           1 : @pytest.fixture(params=["real", "complex"])
      79           1 : def use_real(request: pytest.FixtureRequest) -> bool:
      80             :     """Import and return the pairinteraction module, either real or complex version."""
      81           1 :     return bool(request.param == "real")
      82             : 
      83             : 
      84           1 : @pytest.fixture
      85           1 : def pi_module(use_real: bool) -> PairinteractionModule:
      86             :     """Import and return the pairinteraction module, either real or complex version."""
      87           1 :     if use_real:
      88           1 :         import pairinteraction.real as pi_real
      89             : 
      90           1 :         return pi_real  # type: ignore [return-value]
      91             : 
      92           1 :     import pairinteraction as pi_complex
      93             : 
      94           1 :     return pi_complex
      95             : 
      96             : 
      97           1 : @pytest.fixture
      98           1 : def green_tensor_interpolator_class(use_real: bool) -> type[GreenTensorInterpolator]:
      99             :     """Import and return the GreenTensorInterpolator class, either real or complex version."""
     100           1 :     if use_real:
     101           1 :         from pairinteraction.green_tensor import GreenTensorInterpolatorReal as GTIClassReal
     102             : 
     103           1 :         return GTIClassReal
     104             : 
     105           1 :     from pairinteraction.green_tensor import GreenTensorInterpolator as GTIClassComplex
     106             : 
     107           1 :     return GTIClassComplex

Generated by: LCOV version 1.16