Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock biocontainers and anaconda api calls in module tests #1967

Merged
merged 6 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- The default branch can now be specified when creating a new pipeline repo [#1959](https://github.com/nf-core/tools/pull/1959).
- Only warn when checking that the pipeline directory contains a `main.nf` and a `nextflow.config` file if the pipeline is not an nf-core pipeline [#1964](https://github.com/nf-core/tools/pull/1964)
- Add file `versions.yml` when generating `test.yml` with `nf-core modules create-test-yml` but don't check for md5sum [#1963](https://github.com/nf-core/tools/pull/1963)
- Mock biocontainers and anaconda api calls in modules and subworkflows tests [#1967](https://github.com/nf-core/tools/pull/1967)

### Modules

Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pytest-cov
pytest-datafiles
Sphinx
sphinx_rtd_theme
requests_mock
42 changes: 27 additions & 15 deletions tests/modules/create.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,55 @@
import os

import pytest
import requests_mock

import nf_core.modules
from tests.utils import mock_api_calls


def test_modules_create_succeed(self):
"""Succeed at creating the TrimGalore! module"""
module_create = nf_core.modules.ModuleCreate(
self.pipeline_dir, "trimgalore", "@author", "process_single", True, True, conda_name="trim-galore"
)
module_create.create()
with requests_mock.Mocker() as mock:
mock_api_calls(mock, "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()
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"""
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:
with requests_mock.Mocker() as mock:
mock_api_calls(mock, "trim-galore", "0.6.7")
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()
assert "Module file exists already" in str(excinfo.value)


def test_modules_create_nfcore_modules(self):
"""Create a module in nf-core/modules clone"""
module_create = nf_core.modules.ModuleCreate(self.nfcore_modules, "fastqc", "@author", "process_low", False, False)
module_create.create()
with requests_mock.Mocker() as mock:
mock_api_calls(mock, "fastqc", "0.11.9")
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"""
module_create = nf_core.modules.ModuleCreate(
self.nfcore_modules, "star/index", "@author", "process_medium", False, False
)
module_create.create()
with requests_mock.Mocker() as mock:
mock_api_calls(mock, "star", "2.8.10a")
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"))
12 changes: 9 additions & 3 deletions tests/test_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import tempfile
import unittest

import requests_mock

import nf_core.create
import nf_core.modules

Expand All @@ -17,6 +19,7 @@
GITLAB_URL,
OLD_TRIMGALORE_BRANCH,
OLD_TRIMGALORE_SHA,
mock_api_calls,
)


Expand All @@ -32,9 +35,12 @@ 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"])

# 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()
# mock biocontainers and anaconda response
with requests_mock.Mocker() as mock:
mock_api_calls(mock, "bpipe", "0.9.11")
# 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
12 changes: 8 additions & 4 deletions tests/test_subworkflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import tempfile
import unittest

import requests_mock

import nf_core.create
import nf_core.modules
import nf_core.subworkflows

from .utils import GITLAB_URL
from .utils import GITLAB_URL, mock_api_calls


def create_modules_repo_dummy(tmp_dir):
Expand All @@ -29,9 +31,11 @@ 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"])

# 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_medium", False, False)
module_create.create()
with requests_mock.Mocker() as mock:
mock_api_calls(mock, "bpipe", "0.9.11")
# 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_medium", False, False)
module_create.create()

return root_dir

Expand Down
19 changes: 19 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,22 @@ def set_wd(path: Path):
yield
finally:
os.chdir(start_wd)


def mock_api_calls(mock, 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}"
anaconda_api_url = f"https://api.anaconda.org/package/bioconda/{module}"
mock.register_uri("GET", biocontainers_api_url, text="to modify when the api works and I can know what to add")
anaconda_mock = {
"status_code": 200,
"latest_version": version,
"summary": "",
"doc_url": "",
"dev_url": "",
"files": [{"version": version}],
"license": "",
}
biocontainers_mock = {"status_code": 200, "images": [{"image_type": "Docker", "image_name": ""}]}
mirpedrol marked this conversation as resolved.
Show resolved Hide resolved
mock.register_uri("GET", anaconda_api_url, json=anaconda_mock)
mock.register_uri("GET", biocontainers_api_url, json=biocontainers_mock)