From f1a5f54549370a06a1137a4d43672ebbbea3f904 Mon Sep 17 00:00:00 2001 From: Cunliang Geng Date: Wed, 29 May 2024 09:57:56 +0200 Subject: [PATCH] fix use of config for unit tests --- tests/unit/__init__.py | 1 + tests/unit/conftest.py | 3 --- tests/unit/scoring/conftest.py | 3 ++- tests/unit/test_config.py | 6 ++++-- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py index d6a81168..8679d190 100644 --- a/tests/unit/__init__.py +++ b/tests/unit/__init__.py @@ -3,3 +3,4 @@ DATA_DIR = Path(__file__).resolve().parent / "data" GNPS_DATA_DIR = DATA_DIR / "gnps" +CONFIG_FILE_LOCAL_MODE = DATA_DIR / "nplinker_local_mode.toml" diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index b878accc..c6b9afcd 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -1,7 +1,6 @@ import os import shutil import tempfile -from . import DATA_DIR nplinker_root_dir = os.path.join(tempfile.gettempdir(), "nplinker_unit_test") @@ -26,8 +25,6 @@ def pytest_sessionstart(session): os.mkdir(nplinker_root_dir) # NPLinker setting `root_dir` must be a path that exists, so setting it to a temporary directory. os.environ["NPLINKER_ROOT_DIR"] = nplinker_root_dir - # # Specify the config file via environment variable before importing nplinker in any test. - os.environ["NPLINKER_CONFIG_FILE"] = str(DATA_DIR / "nplinker_local_mode.toml") def pytest_sessionfinish(session): diff --git a/tests/unit/scoring/conftest.py b/tests/unit/scoring/conftest.py index 67a6a774..f1517cbd 100644 --- a/tests/unit/scoring/conftest.py +++ b/tests/unit/scoring/conftest.py @@ -8,6 +8,7 @@ from nplinker.scoring.linking import LinkFinder from nplinker.strain import Strain from nplinker.strain import StrainCollection +from .. import CONFIG_FILE_LOCAL_MODE @fixture(scope="session") @@ -86,7 +87,7 @@ def npl(gcfs, spectra, mfs, strains, tmp_path_factory) -> NPLinker: The config file `nplinker_demo1.toml` does not affect the tests, just making sure the NPLinker object can be created succesfully. """ - npl = NPLinker() + npl = NPLinker(CONFIG_FILE_LOCAL_MODE) npl._gcfs = gcfs npl._spectra = spectra npl._molfams = mfs diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py index 343f120c..3e57afa6 100644 --- a/tests/unit/test_config.py +++ b/tests/unit/test_config.py @@ -1,9 +1,11 @@ -from nplinker.config import config +from nplinker.config import load_config +from . import CONFIG_FILE_LOCAL_MODE def test_config(): """Test loading the default config file.""" - # The default config file is set in "./conftest.py", which is "data/nplinker_local_mode.toml" + config = load_config(CONFIG_FILE_LOCAL_MODE) + assert config.mode == "local" assert config.log.level == "DEBUG" assert config["log.level"] == "DEBUG"