Skip to content

Commit

Permalink
refactor: Split biocontainer and anaconda mocks
Browse files Browse the repository at this point in the history
Responses actually throws an error if all the mocks aren't used. That
allowed me to clean up a few tests that weren't actually using the mocks
and find a few.
  • Loading branch information
edmundmiller committed Jan 22, 2023
1 parent 5d71739 commit f65224c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 40 deletions.
46 changes: 19 additions & 27 deletions tests/modules/create.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,47 @@
import os

import pytest
import responses

import nf_core.modules
from tests.utils import mock_api_calls


def test_modules_create_succeed(self):
"""Succeed at creating the TrimGalore! module"""
with responses.RequestsMock() as rsps:
mock_api_calls(rsps, "trim-galore", "0.6.7")
module_create = nf_core.modules.ModuleCreate(
self.pipeline_dir, "trimgalore", "@author", "process_single", True, True, conda_name="trim-galore"
)
module_create.create()
# FIXME This should use the mock?
module_create = nf_core.modules.ModuleCreate(
self.pipeline_dir, "trimgalore", "@author", "process_single", True, True, conda_name="trim-galore"
)
module_create.create()
assert os.path.exists(os.path.join(self.pipeline_dir, "modules", "local", "trimgalore.nf"))


def test_modules_create_fail_exists(self):
"""Fail at creating the same module twice"""
with responses.RequestsMock() as rsps:
mock_api_calls(rsps, "trim-galore", "0.6.7")
module_create = nf_core.modules.ModuleCreate(
self.pipeline_dir, "trimgalore", "@author", "process_single", False, False, conda_name="trim-galore"
)
# FIXME This should use the mock?
module_create = nf_core.modules.ModuleCreate(
self.pipeline_dir, "trimgalore", "@author", "process_single", False, False, conda_name="trim-galore"
)
module_create.create()
with pytest.raises(UserWarning) as excinfo:
module_create.create()
with pytest.raises(UserWarning) as excinfo:
module_create.create()
assert "Module file exists already" in str(excinfo.value)


def test_modules_create_nfcore_modules(self):
"""Create a module in nf-core/modules clone"""
with responses.RequestsMock() as rsps:
mock_api_calls(rsps, "fastqc", "0.11.9")
module_create = nf_core.modules.ModuleCreate(
self.nfcore_modules, "fastqc", "@author", "process_low", False, False
)
module_create.create()
# FIXME This should use the mock?
module_create = nf_core.modules.ModuleCreate(self.nfcore_modules, "fastqc", "@author", "process_low", False, False)
module_create.create()
assert os.path.exists(os.path.join(self.nfcore_modules, "modules", "nf-core", "fastqc", "main.nf"))
assert os.path.exists(os.path.join(self.nfcore_modules, "tests", "modules", "nf-core", "fastqc", "main.nf"))


def test_modules_create_nfcore_modules_subtool(self):
"""Create a tool/subtool module in a nf-core/modules clone"""
with responses.RequestsMock() as rsps:
mock_api_calls(rsps, "star", "2.8.10a")
module_create = nf_core.modules.ModuleCreate(
self.nfcore_modules, "star/index", "@author", "process_medium", False, False
)
module_create.create()
# FIXME This should use the mock?
module_create = nf_core.modules.ModuleCreate(
self.nfcore_modules, "star/index", "@author", "process_medium", False, False
)
module_create.create()
assert os.path.exists(os.path.join(self.nfcore_modules, "modules", "nf-core", "star", "index", "main.nf"))
assert os.path.exists(os.path.join(self.nfcore_modules, "tests", "modules", "nf-core", "star", "index", "main.nf"))
11 changes: 4 additions & 7 deletions tests/test_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
GITLAB_URL,
OLD_TRIMGALORE_BRANCH,
OLD_TRIMGALORE_SHA,
mock_api_calls,
)


Expand All @@ -34,12 +33,10 @@ def create_modules_repo_dummy(tmp_dir):
with open(os.path.join(root_dir, ".nf-core.yml"), "w") as fh:
fh.writelines(["repository_type: modules", "\n", "org_path: nf-core", "\n"])

# mock biocontainers and anaconda response
with responses.RequestsMock() as rsps:
mock_api_calls(rsps, "bpipe", "0.9.11--hdfd78af_0")
# bpipe is a valid package on bioconda that is very unlikely to ever be added to nf-core/modules
module_create = nf_core.modules.ModuleCreate(root_dir, "bpipe/test", "@author", "process_single", False, False)
module_create.create()
# FIXME Should use mock?
# bpipe is a valid package on bioconda that is very unlikely to ever be added to nf-core/modules
module_create = nf_core.modules.ModuleCreate(root_dir, "bpipe/test", "@author", "process_single", False, False)
module_create.create()

return root_dir

Expand Down
16 changes: 10 additions & 6 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,8 @@ def set_wd(path: Path):
os.chdir(start_wd)


def mock_api_calls(rsps: responses.RequestsMock, module, version):
"""Mock biocontainers and anaconda api calls for module"""
biocontainers_api_url = (
f"https://api.biocontainers.pro/ga4gh/trs/v2/tools/{module}/versions/{module}-{version.split('--')[0]}"
)
def mock_anaconda_api_calls(rsps: responses.RequestsMock, module, version):
"""Mock anaconda api calls for module"""
anaconda_api_url = f"https://api.anaconda.org/package/bioconda/{module}"
anaconda_mock = {
"latest_version": version.split("--")[0],
Expand All @@ -84,6 +81,14 @@ def mock_api_calls(rsps: responses.RequestsMock, module, version):
"files": [{"version": version.split("--")[0]}],
"license": "",
}
rsps.get(anaconda_api_url, json=anaconda_mock, status=200)


def mock_biocontainers_api_calls(rsps: responses.RequestsMock, module, version):
"""Mock biocontainers api calls for module"""
biocontainers_api_url = (
f"https://api.biocontainers.pro/ga4gh/trs/v2/tools/{module}/versions/{module}-{version.split('--')[0]}"
)
biocontainers_mock = {
"images": [
{
Expand All @@ -98,5 +103,4 @@ def mock_api_calls(rsps: responses.RequestsMock, module, version):
},
],
}
rsps.get(anaconda_api_url, json=anaconda_mock, status=200)
rsps.get(biocontainers_api_url, json=biocontainers_mock, status=200)

0 comments on commit f65224c

Please sign in to comment.