Skip to content

Commit

Permalink
restructure components tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mirpedrol committed Jan 29, 2025
1 parent 284bfa6 commit fbe857e
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 201 deletions.
143 changes: 0 additions & 143 deletions tests/components/generate_snapshot.py

This file was deleted.

40 changes: 0 additions & 40 deletions tests/components/snapshot_test.py

This file was deleted.

140 changes: 140 additions & 0 deletions tests/components/test_components_generate_snapshot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
"""Test generate a snapshot"""

import json
from pathlib import Path
from unittest.mock import MagicMock

import pytest

from nf_core.components.components_test import ComponentsTest
from nf_core.utils import set_wd

from ..test_components import TestComponents
from ..utils import GITLAB_NFTEST_BRANCH, GITLAB_URL


class TestTestComponentsUtils(TestComponents):
def test_generate_snapshot_module(self):
"""Generate the snapshot for a module in nf-core/modules clone"""
with set_wd(self.nfcore_modules):
snap_generator = ComponentsTest(
component_type="modules",
component_name="fastqc",
no_prompts=True,
remote_url=GITLAB_URL,
branch=GITLAB_NFTEST_BRANCH,
)
snap_generator.run()

snap_path = Path("modules", "nf-core-test", "fastqc", "tests", "main.nf.test.snap")
assert snap_path.exists()

with open(snap_path) as fh:
snap_content = json.load(fh)
assert "versions" in snap_content
assert "content" in snap_content["versions"]
assert "versions.yml:md5,e1cc25ca8af856014824abd842e93978" in snap_content["versions"]["content"][0]

def test_generate_snapshot_subworkflow(self):
"""Generate the snapshot for a subworkflows in nf-core/modules clone"""
with set_wd(self.nfcore_modules):
snap_generator = ComponentsTest(
component_type="subworkflows",
component_name="bam_sort_stats_samtools",
no_prompts=True,
remote_url=GITLAB_URL,
branch=GITLAB_NFTEST_BRANCH,
)
snap_generator.run()

snap_path = Path("subworkflows", "nf-core-test", "bam_sort_stats_samtools", "tests", "main.nf.test.snap")
assert snap_path.exists()

with open(snap_path) as fh:
snap_content = json.load(fh)
assert "test_bam_sort_stats_samtools_paired_end_flagstats" in snap_content
assert (
"test.flagstat:md5,4f7ffd1e6a5e85524d443209ac97d783"
in snap_content["test_bam_sort_stats_samtools_paired_end_flagstats"]["content"][0][0]
)
assert "test_bam_sort_stats_samtools_paired_end_idxstats" in snap_content
assert (
"test.idxstats:md5,df60a8c8d6621100d05178c93fb053a2"
in snap_content["test_bam_sort_stats_samtools_paired_end_idxstats"]["content"][0][0]
)

def test_generate_snapshot_once(
self,
):
"""Generate the snapshot for a module in nf-core/modules clone only once"""
with set_wd(self.nfcore_modules):
snap_generator = ComponentsTest(
component_type="modules",
component_name="fastqc",
once=True,
no_prompts=True,
remote_url=GITLAB_URL,
branch=GITLAB_NFTEST_BRANCH,
)
snap_generator.repo_type = "modules"
snap_generator.generate_snapshot = MagicMock()
snap_generator.run()
snap_generator.generate_snapshot.assert_called_once()

def test_update_snapshot_module(self):
"""Update the snapshot of a module in nf-core/modules clone"""

with set_wd(self.nfcore_modules):
snap_path = Path("modules", "nf-core-test", "bwa", "mem", "tests", "main.nf.test.snap")
with open(snap_path) as fh:
snap_content = json.load(fh)
original_timestamp = snap_content["Single-End"]["timestamp"]
# delete the timestamp in json
snap_content["Single-End"]["timestamp"] = ""
with open(snap_path, "w") as fh:
json.dump(snap_content, fh)
snap_generator = ComponentsTest(
component_type="modules",
component_name="bwa/mem",
no_prompts=True,
remote_url=GITLAB_URL,
branch=GITLAB_NFTEST_BRANCH,
update=True,
)
snap_generator.run()

with open(snap_path) as fh:
snap_content = json.load(fh)
assert "Single-End" in snap_content
assert snap_content["Single-End"]["timestamp"] != original_timestamp

def test_test_not_found(self):
"""Generate the snapshot for a module in nf-core/modules clone which doesn't contain tests"""
with set_wd(self.nfcore_modules):
snap_generator = ComponentsTest(
component_type="modules",
component_name="fastp",
no_prompts=True,
remote_url=GITLAB_URL,
branch=GITLAB_NFTEST_BRANCH,
)
test_file = Path("modules", "nf-core-test", "fastp", "tests", "main.nf.test")
test_file.rename(test_file.parent / "main.nf.test.bak")
with pytest.raises(UserWarning) as e:
snap_generator.run()
assert "Test file 'main.nf.test' not found" in str(e.value)
Path(test_file.parent / "main.nf.test.bak").rename(test_file)

def test_unstable_snapshot(self):
"""Generate the snapshot for a module in nf-core/modules clone with unstable snapshots"""
with set_wd(self.nfcore_modules):
snap_generator = ComponentsTest(
component_type="modules",
component_name="kallisto/quant",
no_prompts=True,
remote_url=GITLAB_URL,
branch=GITLAB_NFTEST_BRANCH,
)
with pytest.raises(UserWarning) as e:
snap_generator.run()
assert "nf-test snapshot is not stable" in str(e.value)
41 changes: 41 additions & 0 deletions tests/components/test_components_snapshot_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Test the 'modules test' or 'subworkflows test' command which runs nf-test test."""

import shutil
from pathlib import Path

import pytest

from nf_core.components.components_test import ComponentsTest
from nf_core.utils import set_wd

from ..test_components import TestComponents


class TestTestComponentsUtils(TestComponents):
def test_components_test_check_inputs(self):
"""Test the check_inputs() function - raise UserWarning because module doesn't exist"""
with set_wd(self.nfcore_modules):
meta_builder = ComponentsTest(component_type="modules", component_name="none", no_prompts=True)
with pytest.raises(UserWarning) as excinfo:
meta_builder.check_inputs()
assert "Cannot find directory" in str(excinfo.value)

def test_components_test_no_name_no_prompts(self):
"""Test the check_inputs() function - raise UserWarning prompts are deactivated and module name is not provided."""
with set_wd(self.nfcore_modules):
meta_builder = ComponentsTest(component_type="modules", component_name=None, no_prompts=True)
with pytest.raises(UserWarning) as excinfo:
meta_builder.check_inputs()
assert "Module name not provided and prompts deactivated." in str(excinfo.value)

def test_components_test_no_installed_modules(self):
"""Test the check_inputs() function - raise UserWarning because installed modules were not found"""
with set_wd(self.nfcore_modules):
module_dir = Path(self.nfcore_modules, "modules")
shutil.rmtree(module_dir)
module_dir.mkdir()
meta_builder = ComponentsTest(component_type="modules", component_name=None, no_prompts=False)
meta_builder.repo_type = "modules"
with pytest.raises(LookupError) as excinfo:
meta_builder.check_inputs()
assert "Nothing installed from" in str(excinfo.value)
18 changes: 0 additions & 18 deletions tests/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,3 @@ def tearDown(self):
@pytest.fixture(autouse=True)
def _use_caplog(self, caplog):
self.caplog = caplog

############################################
# Test of the individual components commands. #
############################################

from .components.generate_snapshot import ( # type: ignore[misc]
test_generate_snapshot_module,
test_generate_snapshot_once,
test_generate_snapshot_subworkflow,
test_test_not_found,
test_unstable_snapshot,
test_update_snapshot_module,
)
from .components.snapshot_test import ( # type: ignore[misc]
test_components_test_check_inputs,
test_components_test_no_installed_modules,
test_components_test_no_name_no_prompts,
)

0 comments on commit fbe857e

Please sign in to comment.