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