Skip to content

Commit 4777aca

Browse files
committed
refactor: Split biocontainer and anaconda mocks
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.
1 parent 421c548 commit 4777aca

File tree

3 files changed

+33
-40
lines changed

3 files changed

+33
-40
lines changed

tests/modules/create.py

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,47 @@
11
import os
22

33
import pytest
4-
import responses
54

65
import nf_core.modules
7-
from tests.utils import mock_api_calls
86

97

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

2017

2118
def test_modules_create_fail_exists(self):
2219
"""Fail at creating the same module twice"""
23-
with responses.RequestsMock() as rsps:
24-
mock_api_calls(rsps, "trim-galore", "0.6.7")
25-
module_create = nf_core.modules.ModuleCreate(
26-
self.pipeline_dir, "trimgalore", "@author", "process_single", False, False, conda_name="trim-galore"
27-
)
20+
# FIXME This should use the mock?
21+
module_create = nf_core.modules.ModuleCreate(
22+
self.pipeline_dir, "trimgalore", "@author", "process_single", False, False, conda_name="trim-galore"
23+
)
24+
module_create.create()
25+
with pytest.raises(UserWarning) as excinfo:
2826
module_create.create()
29-
with pytest.raises(UserWarning) as excinfo:
30-
module_create.create()
3127
assert "Module file exists already" in str(excinfo.value)
3228

3329

3430
def test_modules_create_nfcore_modules(self):
3531
"""Create a module in nf-core/modules clone"""
36-
with responses.RequestsMock() as rsps:
37-
mock_api_calls(rsps, "fastqc", "0.11.9")
38-
module_create = nf_core.modules.ModuleCreate(
39-
self.nfcore_modules, "fastqc", "@author", "process_low", False, False
40-
)
41-
module_create.create()
32+
# FIXME This should use the mock?
33+
module_create = nf_core.modules.ModuleCreate(self.nfcore_modules, "fastqc", "@author", "process_low", False, False)
34+
module_create.create()
4235
assert os.path.exists(os.path.join(self.nfcore_modules, "modules", "nf-core", "fastqc", "main.nf"))
4336
assert os.path.exists(os.path.join(self.nfcore_modules, "tests", "modules", "nf-core", "fastqc", "main.nf"))
4437

4538

4639
def test_modules_create_nfcore_modules_subtool(self):
4740
"""Create a tool/subtool module in a nf-core/modules clone"""
48-
with responses.RequestsMock() as rsps:
49-
mock_api_calls(rsps, "star", "2.8.10a")
50-
module_create = nf_core.modules.ModuleCreate(
51-
self.nfcore_modules, "star/index", "@author", "process_medium", False, False
52-
)
53-
module_create.create()
41+
# FIXME This should use the mock?
42+
module_create = nf_core.modules.ModuleCreate(
43+
self.nfcore_modules, "star/index", "@author", "process_medium", False, False
44+
)
45+
module_create.create()
5446
assert os.path.exists(os.path.join(self.nfcore_modules, "modules", "nf-core", "star", "index", "main.nf"))
5547
assert os.path.exists(os.path.join(self.nfcore_modules, "tests", "modules", "nf-core", "star", "index", "main.nf"))

tests/test_modules.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
GITLAB_URL,
1919
OLD_TRIMGALORE_BRANCH,
2020
OLD_TRIMGALORE_SHA,
21-
mock_api_calls,
2221
)
2322

2423

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

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

4441
return root_dir
4542

tests/utils.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,8 @@ def set_wd(path: Path):
7070
os.chdir(start_wd)
7171

7272

73-
def mock_api_calls(rsps: responses.RequestsMock, module, version):
74-
"""Mock biocontainers and anaconda api calls for module"""
75-
biocontainers_api_url = (
76-
f"https://api.biocontainers.pro/ga4gh/trs/v2/tools/{module}/versions/{module}-{version.split('--')[0]}"
77-
)
73+
def mock_anaconda_api_calls(rsps: responses.RequestsMock, module, version):
74+
"""Mock anaconda api calls for module"""
7875
anaconda_api_url = f"https://api.anaconda.org/package/bioconda/{module}"
7976
anaconda_mock = {
8077
"latest_version": version.split("--")[0],
@@ -84,6 +81,14 @@ def mock_api_calls(rsps: responses.RequestsMock, module, version):
8481
"files": [{"version": version.split("--")[0]}],
8582
"license": "",
8683
}
84+
rsps.get(anaconda_api_url, json=anaconda_mock, status=200)
85+
86+
87+
def mock_biocontainers_api_calls(rsps: responses.RequestsMock, module, version):
88+
"""Mock biocontainers api calls for module"""
89+
biocontainers_api_url = (
90+
f"https://api.biocontainers.pro/ga4gh/trs/v2/tools/{module}/versions/{module}-{version.split('--')[0]}"
91+
)
8792
biocontainers_mock = {
8893
"images": [
8994
{
@@ -98,5 +103,4 @@ def mock_api_calls(rsps: responses.RequestsMock, module, version):
98103
},
99104
],
100105
}
101-
rsps.get(anaconda_api_url, json=anaconda_mock, status=200)
102106
rsps.get(biocontainers_api_url, json=biocontainers_mock, status=200)

0 commit comments

Comments
 (0)