Skip to content

Commit

Permalink
Move test files to subfolders and update pytest's conftest.py (#100)
Browse files Browse the repository at this point in the history
To solve issue #99 
* move test files to relevant subfolders
* create pytest conftest.py for relevant subfolders
* fix redundant fixtures
* move non-shared fixture to specific file
  • Loading branch information
CunliangGeng authored Feb 22, 2023
1 parent 7b21795 commit a058dd9
Show file tree
Hide file tree
Showing 27 changed files with 101 additions and 93 deletions.
Empty file added tests/class_info/__init__.py
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pathlib import Path
from nplinker.class_info.chem_classes import MolNetEnhancerResults

from . import DATA_DIR
from .. import DATA_DIR


# TODO: No such file or directory: 'canopus.tsv'
Expand Down
File renamed without changes.
48 changes: 42 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,57 @@
from pathlib import Path
import shutil
import pytest

from nplinker.metabolomics.metabolomics import load_spectra
from nplinker.metabolomics.metabolomics import make_families
from nplinker.metabolomics.spectrum import Spectrum
from nplinker.strain_collection import StrainCollection
from nplinker.strains import Strain
from nplinker.utils import extract_archive

from . import DATA_DIR


def _unpack(archive: Path):
filepath = DATA_DIR / archive
outdir = DATA_DIR / filepath.stem
extract_archive(filepath, outdir)
return filepath, outdir


@pytest.fixture(scope="session", autouse=True)
def prepare_data():
_unpack("ProteoSAFe-METABOLOMICS-SNETS-c22f44b1-download_clustered_spectra.zip")
_unpack("ProteoSAFe-FEATURE-BASED-MOLECULAR-NETWORKING-92036537-download_cytoscape_data.zip")
_unpack(
"ProteoSAFe-METABOLOMICS-SNETS-c22f44b1-download_clustered_spectra.zip"
)
_unpack(
"ProteoSAFe-FEATURE-BASED-MOLECULAR-NETWORKING-92036537-download_cytoscape_data.zip"
)
yield
shutil.rmtree(DATA_DIR / "ProteoSAFe-METABOLOMICS-SNETS-c22f44b1-download_clustered_spectra")
shutil.rmtree(DATA_DIR / "ProteoSAFe-FEATURE-BASED-MOLECULAR-NETWORKING-92036537-download_cytoscape_data")
shutil.rmtree(
DATA_DIR /
"ProteoSAFe-METABOLOMICS-SNETS-c22f44b1-download_clustered_spectra")
shutil.rmtree(
DATA_DIR /
"ProteoSAFe-FEATURE-BASED-MOLECULAR-NETWORKING-92036537-download_cytoscape_data"
)


@pytest.fixture
def spec_dict() -> dict[int, Spectrum]:
mgf_file = DATA_DIR / "spectra.mgf"
edges_file = DATA_DIR / "edges.pairsinfo"
return load_spectra(mgf_file, edges_file)


@pytest.fixture
def collection_from_file() -> StrainCollection:
filename = DATA_DIR / "strain_mappings.csv"
sut = StrainCollection()
sut.add_from_file(filename)
return sut


@pytest.fixture
def strain() -> Strain:
item = Strain("peter")
item.aliases = set(["dieter"])
return item
2 changes: 1 addition & 1 deletion tests/test_aa_pred.py → tests/genomics/test_aa_pred.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from Bio import SeqIO
from nplinker.genomics.aa_pred import AntiSmash5Record, predict_aa
from . import DATA_DIR
from .. import DATA_DIR


ANTISMASH_FILE = DATA_DIR / "antismash_v5_GCF_000016425.1_NC_009380.1.region017.gbk"
Expand Down
Empty file added tests/metabolomics/__init__.py
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
from nplinker.metabolomics.gnps.gnps_annotation_loader import \
GNPSAnnotationLoader
from nplinker.metabolomics.spectrum import Spectrum
from . import DATA_DIR
from .test_metabolomics import spec_dict
from .. import DATA_DIR


class GNPSAnnotationLoaderBuilder:
def __init__(self):
self._file = DATA_DIR / "ProteoSAFe-FEATURE-BASED-MOLECULAR-NETWORKING-92036537-download_cytoscape_data/DB_result/7dc5b46b50d94246a1de12ef485d0f75.tsv"

def with_file(self, file) -> Self:
self._file = file
return self

def build(self) -> GNPSAnnotationLoader:
return GNPSAnnotationLoader(self._file)

Expand Down Expand Up @@ -51,9 +50,9 @@ def test_annotations_are_equal(spec_dict: dict[int, Spectrum]):
expected[x.spectrum_id] = x.gnps_annotations

actual = GNPSAnnotationLoaderBuilder().with_file(annotations_file).build().get_annotations()

for key in expected:
expected_annotations = expected[key]
actual_annotations = actual[key]
for key_entry in expected_annotations:
assert expected_annotations[key_entry] == actual_annotations[key_entry]
assert expected_annotations[key_entry] == actual_annotations[key_entry]
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@

import pytest
from nplinker.metabolomics.gnps.gnps_downloader import GNPSDownloader
from . import DATA_DIR
from .. import DATA_DIR


class GNPSDownloaderBuilder:
def __init__(self):
self._task_id = None
self._download_root = gettempdir()

def with_task_id(self, task_id: str) -> Self:
self._task_id = task_id
return self

def with_download_root(self, download_root: Path) -> Self:
self._download_root = download_root
return self

def build(self) -> GNPSDownloader:
return GNPSDownloader(self._task_id, self._download_root)



def test_has_gnps_task_id():
Expand All @@ -51,4 +51,4 @@ def test_downloads_file(tmp_path: Path, task_id, filename_expected):

actual_names = actual.namelist()
expected_names = [x.filename for x in expected.filelist if x.compress_size > 0]
assert all(item in actual_names for item in expected_names)
assert all(item in actual_names for item in expected_names)
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pytest
from nplinker.metabolomics.gnps.gnps_extractor import GNPSExtractor
from nplinker.utils import extract_archive
from . import DATA_DIR
from .. import DATA_DIR


class GNPSExtractorBuilder:
Expand All @@ -24,7 +24,7 @@ def with_file(self, file: Path) -> Self:
def with_extract_path(self, extract_path: Path) -> Self:
self._extract_path = extract_path
return self

def build(self) -> GNPSExtractor:
return GNPSExtractor(self._file, self._extract_path)

Expand All @@ -41,7 +41,7 @@ def _unpack(archive: Path):
extract_archive(file, outdir)
return file, outdir


def test_default():
sut = GNPSExtractorBuilder().build()
assert sut is not None
Expand All @@ -51,7 +51,7 @@ def test_has_zipfile():
file = DATA_DIR / 'ProteoSAFe-METABOLOMICS-SNETS-c22f44b1-download_clustered_spectra.zip'
sut = GNPSExtractorBuilder().with_file(file).build()
actual = sut.get_data()

expected = zipfile.ZipFile(file)
numpy.testing.assert_array_equal(actual.namelist(), expected.namelist())

Expand Down Expand Up @@ -86,7 +86,7 @@ def test_creates_molecular_families(archive: Path, filename: str, tmp_path: Path
sut = GNPSExtractorBuilder().with_file(file).with_extract_path(tmp_path).build()
sut._extract_molecular_families()
actual = Path(sut.get_extract_path()) / "molecular_families.pairsinfo"

assert_extraction_success(filename, outdir, actual)


Expand All @@ -100,7 +100,7 @@ def test_creates_file_mappings(archive: Path, filename: str, tmp_path: Path):
sut = GNPSExtractorBuilder().with_file(file).with_extract_path(tmp_path).build()
sut._extract_file_mappings()
actual = Path(sut.get_extract_path()) / ("file_mappings" + str(Path(filename).suffix))

assert_extraction_success(filename, outdir, actual)


Expand All @@ -113,5 +113,5 @@ def test_creates_annotations(archive: Path, filename: str, tmp_path: Path):

sut = GNPSExtractorBuilder().with_file(file).with_extract_path(tmp_path).build()
sut._extract_annotations()
actual = Path(sut.get_extract_path()) / "annotations.tsv"
assert_extraction_success(filename, outdir, actual)
actual = Path(sut.get_extract_path()) / "annotations.tsv"
assert_extraction_success(filename, outdir, actual)
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from nplinker.metabolomics.gnps.gnps_format import gnps_format_from_file_mapping
from nplinker.metabolomics.gnps.gnps_format import gnps_format_from_task_id
from nplinker.metabolomics.gnps.gnps_format import gnps_format_from_archive
from . import DATA_DIR
from .. import DATA_DIR


@pytest.mark.parametrize("filename, expected", [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import os
import numpy

import pytest
from nplinker.metabolomics.gnps.gnps_molecular_family_loader import GNPSMolecularFamilyLoader
from nplinker.metabolomics.gnps.gnps_molecular_family_loader import \
GNPSMolecularFamilyLoader
from nplinker.metabolomics.metabolomics import make_families
from nplinker.metabolomics.molecular_family import MolecularFamily
from .. import DATA_DIR

from .test_metabolomics import molecular_families, spec_dict
from . import DATA_DIR

@pytest.fixture
def molecular_families_gnps():
filename = os.path.join(DATA_DIR, "edges.pairsinfo")
sut = GNPSMolecularFamilyLoader(filename)
return sut.families()

def molecular_families(spec_dict) -> list[MolecularFamily]:
return make_families(spec_dict.values())

@pytest.mark.parametrize("filename", [
os.path.join(DATA_DIR, "edges.pairsinfo"),
Expand All @@ -36,10 +33,10 @@ def test_families_are_identical(spec_dict, molecular_families):
x.id = i
for spec_id in x.spectra_ids:
x.add_spectrum(spec_dict[spec_id])


for x in molecular_families:
for spec in x.spectra:
x.spectra_ids.add(spec.spectrum_id)

numpy.testing.assert_array_equal(actual, molecular_families)
numpy.testing.assert_array_equal(actual, molecular_families)
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import pytest
from nplinker.metabolomics.gnps.gnps_spectrum_loader import GNPSSpectrumLoader

from . import DATA_DIR
from .. import DATA_DIR

@pytest.mark.parametrize("file, expected", [
[DATA_DIR / "ProteoSAFe-METABOLOMICS-SNETS-c22f44b1-download_clustered_spectra/METABOLOMICS-SNETS-c22f44b1-download_clustered_spectra-main.mgf", 435],
[DATA_DIR / "ProteoSAFe-FEATURE-BASED-MOLECULAR-NETWORKING-92036537-download_cytoscape_data/spectra/specs_ms.mgf", 1492]
])
def test_loads_spectra(file, expected):
actual = GNPSSpectrumLoader(file).spectra()
assert len(actual) == expected
assert len(actual) == expected
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
from nplinker.metabolomics.load_gnps import load_gnps
from nplinker.strain_collection import StrainCollection
from nplinker.utils import get_headers
from .test_metabolomics import spec_dict
from .test_strain_collection import collection_from_file

from . import DATA_DIR
from .. import DATA_DIR


nodes_file = DATA_DIR / "nodes.tsv"
Expand Down Expand Up @@ -77,4 +75,4 @@ def test_parse_mzxml_header():
headers = get_headers(str(DATA_DIR / "nodes_fbmn.csv"))
hdr = headers[10]
actual = _parse_mzxml_header(hdr, StrainCollection(), None, None)
assert actual is not None
assert actual is not None
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
import pytest
from nplinker.metabolomics.metabolomics import make_families
from nplinker.metabolomics.metabolomics import load_dataset
from nplinker.metabolomics.metabolomics import load_spectra
from nplinker.metabolomics.molecular_family import MolecularFamily
from nplinker.metabolomics.spectrum import Spectrum
from nplinker.strain_collection import StrainCollection
from . import DATA_DIR


@pytest.fixture
def spec_dict() -> dict[int, Spectrum]:
mgf_file = DATA_DIR / "spectra.mgf"
edges_file = DATA_DIR / "edges.pairsinfo"
return load_spectra(mgf_file, edges_file)

@pytest.fixture
def spec_with_families(spec_dict) -> dict[int, Spectrum]:
make_families(spec_dict.values())
return spec_dict

@pytest.fixture
def molecular_families(spec_dict) -> list[MolecularFamily]:
return make_families(spec_dict.values())
from .. import DATA_DIR


def test_load_spectra(spec_dict):
Expand All @@ -47,4 +27,4 @@ def test_load_dataset():

def test_make_families(spec_dict):
families = make_families(spec_dict.values())
assert len(families) == 25769
assert len(families) == 25769
File renamed without changes.
Empty file added tests/pairedomics/__init__.py
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from nplinker.pairedomics.downloader import Downloader
from nplinker.pairedomics.downloader import _generate_gnps_download_url
from nplinker.pairedomics.downloader import _execute_download
from . import DATA_DIR
from .. import DATA_DIR


@pytest.fixture
Expand Down Expand Up @@ -80,7 +80,7 @@ def test_download_gnps_data(tmp_path):
gnps_task_id = "c22f44b14a3d450eb836d607cb9521bb"
sut = Downloader("MSV000079284", local_cache=tmp_path / 'actual')
actual = sut._load_gnps_data(gnps_task_id)

expected = zipfile.ZipFile(DATA_DIR / "ProteoSAFe-METABOLOMICS-SNETS-c22f44b1-download_clustered_spectra.zip")

actual.extract("networkedges_selfloop/6da5be36f5b14e878860167fa07004d6.pairsinfo", tmp_path / "actual")
Expand All @@ -101,4 +101,3 @@ def test_extract_metabolomics_data(tmp_path):
assert (Path(sut.project_file_cache) / "networkedges_selfloop/6da5be36f5b14e878860167fa07004d6.pairsinfo").is_file()
assert (Path(sut.project_file_cache) / "clusterinfosummarygroup_attributes_withIDs_withcomponentID/d69356c8e5044c2a9fef3dd2a2f991e1.tsv").is_file()
assert (Path(sut.project_file_cache) / "spectra/METABOLOMICS-SNETS-c22f44b1-download_clustered_spectra-main.mgf").is_file()

Empty file added tests/parsers/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion tests/test_mgf.py → tests/parsers/test_mgf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from nplinker.parsers.mgf import LoadMGF

from . import DATA_DIR
from .. import DATA_DIR


def test_load_mgf():
Expand Down
Empty file added tests/scoring/__init__.py
Empty file.
File renamed without changes.
Loading

0 comments on commit a058dd9

Please sign in to comment.