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

Replace requests_mock with responses #2165

Merged
merged 6 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -27,6 +27,7 @@
- `nf-core modules/subworkflows info` now prints the include statement for the module/subworkflow ([#2182](https://github.com/nf-core/tools/pull/2182)).
- Add the Nextflow version to Gitpod container matching the minimal Nextflow version for nf-core (according to `nextflow.config`) ([#2196](https://github.com/nf-core/tools/pull/2196))
- Use `nfcore/gitpod:dev` container in the dev branch ([#2196](https://github.com/nf-core/tools/pull/2196))
- Replace requests_mock with responses in test mocks ([#2165](https://github.com/nf-core/tools/pull/2165)).

## [v2.7.2 - Mercury Eagle Patch](https://github.com/nf-core/tools/releases/tag/2.7.2) - [2022-12-19]

Expand Down
3 changes: 1 addition & 2 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ isort
myst_parser
pytest-cov
pytest-datafiles
requests-mock
responses
Sphinx
sphinx-rtd-theme
requests_mock
40 changes: 25 additions & 15 deletions tests/modules/create.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,65 @@
import os

import pytest
import requests_mock
import requests_cache
import responses

import nf_core.modules
from tests.utils import mock_api_calls
from tests.utils import mock_anaconda_api_calls, mock_biocontainers_api_calls


def test_modules_create_succeed(self):
"""Succeed at creating the TrimGalore! module"""
with requests_mock.Mocker() as mock:
mock_api_calls(mock, "trim-galore", "0.6.7")
with responses.RequestsMock() as rsps:
mock_anaconda_api_calls(rsps, "trim-galore", "0.6.7")
mock_biocontainers_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()
with requests_cache.disabled():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which cache is this affecting? Wondering if it ignores the cloning of the modules repository and then we have to clone it again for every test, but it doesn't seem so when I try to run the test :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's not the creation of that, it's just any queries that are running inside that code. For example, if you try to hit anaconda here. It was masking the mock and what I was stuck on months ago because the "mock wasn't used" because it was using the cache.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, that's interesting!

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 requests_mock.Mocker() as mock:
mock_api_calls(mock, "trim-galore", "0.6.7")
with responses.RequestsMock() as rsps:
mock_anaconda_api_calls(rsps, "trim-galore", "0.6.7")
mock_biocontainers_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"
)
module_create.create()
with pytest.raises(UserWarning) as excinfo:
with requests_cache.disabled():
module_create.create()
with pytest.raises(UserWarning) as excinfo:
with requests_cache.disabled():
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 requests_mock.Mocker() as mock:
mock_api_calls(mock, "fastqc", "0.11.9")
with responses.RequestsMock() as rsps:
mock_anaconda_api_calls(rsps, "fastqc", "0.11.9")
mock_biocontainers_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()
with requests_cache.disabled():
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 requests_mock.Mocker() as mock:
mock_api_calls(mock, "star", "2.8.10a")
with responses.RequestsMock() as rsps:
mock_anaconda_api_calls(rsps, "star", "2.8.10a")
mock_biocontainers_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()
with requests_cache.disabled():
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"))
14 changes: 9 additions & 5 deletions tests/test_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import tempfile
import unittest

import requests_mock
import requests_cache
import responses

import nf_core.create
import nf_core.modules
Expand All @@ -18,7 +19,8 @@
GITLAB_URL,
OLD_TRIMGALORE_BRANCH,
OLD_TRIMGALORE_SHA,
mock_api_calls,
mock_anaconda_api_calls,
mock_biocontainers_api_calls,
)


Expand All @@ -35,11 +37,13 @@ def create_modules_repo_dummy(tmp_dir):
fh.writelines(["repository_type: modules", "\n", "org_path: nf-core", "\n"])

# mock biocontainers and anaconda response
with requests_mock.Mocker() as mock:
mock_api_calls(mock, "bpipe", "0.9.11--hdfd78af_0")
with responses.RequestsMock() as rsps:
mock_anaconda_api_calls(rsps, "bpipe", "0.9.11--hdfd78af_0")
mock_biocontainers_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()
with requests_cache.disabled():
module_create.create()

return root_dir

Expand Down
8 changes: 4 additions & 4 deletions tests/test_subworkflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import tempfile
import unittest

import requests_mock
import responses

import nf_core.create
import nf_core.modules
Expand All @@ -30,9 +30,9 @@ 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"])

with requests_mock.Mocker() as mock:
subworkflow_create = nf_core.subworkflows.SubworkflowCreate(root_dir, "test_subworkflow", "@author", True)
subworkflow_create.create()
# TODO Add a mock here
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was there an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There wasn't an error, but responses throws an error if the mock doesn't get used (meaning it's not needed) and when I added the mock calls neither of them were used. So I just removed the mock there for now.

subworkflow_create = nf_core.subworkflows.SubworkflowCreate(root_dir, "test_subworkflow", "@author", True)
subworkflow_create.create()

return root_dir

Expand Down
22 changes: 13 additions & 9 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from contextlib import contextmanager
from pathlib import Path

import responses

OLD_TRIMGALORE_SHA = "06348dffce2a732fc9e656bdc5c64c3e02d302cb"
OLD_TRIMGALORE_BRANCH = "mimic-old-trimgalore"
GITLAB_URL = "https://gitlab.com/nf-core/modules-test.git"
Expand Down Expand Up @@ -68,23 +70,26 @@ def set_wd(path: Path):
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.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 = {
"status_code": 200,
"latest_version": version.split("--")[0],
"summary": "",
"doc_url": "",
"dev_url": "",
"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 = {
"status_code": 200,
"images": [
{
"image_type": "Singularity",
Expand All @@ -98,5 +103,4 @@ def mock_api_calls(mock, module, version):
},
],
}
mock.register_uri("GET", anaconda_api_url, json=anaconda_mock)
mock.register_uri("GET", biocontainers_api_url, json=biocontainers_mock)
rsps.get(biocontainers_api_url, json=biocontainers_mock, status=200)