Skip to content

Commit

Permalink
fix pytests by mocking biotools, adding a meta to the test module and…
Browse files Browse the repository at this point in the history
… running prettier to meta.yml file
  • Loading branch information
mirpedrol committed Sep 20, 2024
1 parent eb58583 commit ebfbd98
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
15 changes: 11 additions & 4 deletions tests/test_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pytest
import requests_cache
import responses
import yaml
import ruamel.yaml

import nf_core.modules
import nf_core.modules.create
Expand All @@ -16,6 +16,7 @@
import nf_core.modules.remove
import nf_core.pipelines.create.create
from nf_core import __version__
from nf_core.pipelines.lint_utils import run_prettier_on_file
from nf_core.utils import NFCoreYamlConfig

from .utils import (
Expand All @@ -28,11 +29,15 @@
create_tmp_pipeline,
mock_anaconda_api_calls,
mock_biocontainers_api_calls,
mock_biotools_api_calls,
)


def create_modules_repo_dummy(tmp_dir):
"""Create a dummy copy of the nf-core/modules repo"""
yaml = ruamel.yaml.YAML()
yaml.preserve_quotes = True
yaml.indent(mapping=2, sequence=2, offset=0)

root_dir = Path(tmp_dir, "modules")
Path(root_dir, "modules", "nf-core").mkdir(parents=True)
Expand All @@ -42,13 +47,14 @@ def create_modules_repo_dummy(tmp_dir):
nf_core_yml = NFCoreYamlConfig(nf_core_version=__version__, repository_type="modules", org_path="nf-core")
with open(Path(root_dir, ".nf-core.yml"), "w") as fh:
yaml.dump(nf_core_yml.model_dump(), fh)
# mock biocontainers and anaconda response
# mock biocontainers and anaconda response and biotools response
with responses.RequestsMock() as rsps:
mock_anaconda_api_calls(rsps, "bpipe", "0.9.13--hdfd78af_0")
mock_biocontainers_api_calls(rsps, "bpipe", "0.9.13--hdfd78af_0")
mock_biotools_api_calls(rsps, "bpipe")
# bpipe is a valid package on bioconda that is very unlikely to ever be added to nf-core/modules
module_create = nf_core.modules.create.ModuleCreate(
root_dir, "bpipe/test", "@author", "process_single", False, False
root_dir, "bpipe/test", "@author", "process_single", True, False
)
with requests_cache.disabled():
assert module_create.create()
Expand All @@ -57,10 +63,11 @@ def create_modules_repo_dummy(tmp_dir):
meta_yml_path = Path(root_dir, "modules", "nf-core", "bpipe", "test", "meta.yml")

with open(str(meta_yml_path)) as fh:
meta_yml = yaml.safe_load(fh)
meta_yml = yaml.load(fh)
del meta_yml["tools"][0]["bpipe"]["doi"]
with open(str(meta_yml_path), "w") as fh:
yaml.dump(meta_yml, fh)
run_prettier_on_file(fh.name)
# Add dummy content to main.nf.test.snap
test_snap_path = Path(root_dir, "modules", "nf-core", "bpipe", "test", "tests", "main.nf.test.snap")

Expand Down
9 changes: 9 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ def mock_biocontainers_api_calls(rsps: responses.RequestsMock, module: str, vers
rsps.get(biocontainers_api_url, json=biocontainers_mock, status=200)


def mock_biotools_api_calls(rsps: responses.RequestsMock, module: str) -> None:
"""Mock biotools api calls for module"""
biotools_api_url = f"https://bio.tools/api/t/?q={module}&format=json"
biotools_mock = {
"list": [{"name": "Bpipe", "biotoolsCURIE": "biotools:bpipe"}],
}
rsps.get(biotools_api_url, json=biotools_mock, status=200)


def create_tmp_pipeline(no_git: bool = False) -> Tuple[Path, Path, str, Path]:
"""Create a new Pipeline for testing"""

Expand Down

0 comments on commit ebfbd98

Please sign in to comment.