Commit 25c36492 authored by Christophe Benz's avatar Christophe Benz

Update tests

parent f550354f
Pipeline #114933 passed with stage
in 3 minutes and 21 seconds
......@@ -18,7 +18,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""Tests about event loader."""
"""Tests about ``resources`` module."""
import argparse
from pathlib import Path
......@@ -26,57 +26,44 @@ from typing import List
import pytest
import dbnomics_fetcher_toolbox.status as status
from dbnomics_fetcher_toolbox import status
from dbnomics_fetcher_toolbox.arguments import add_arguments_for_download
from dbnomics_fetcher_toolbox.resources import Resource, process_resources
class FakeArgs(argparse.Namespace):
"""Mimic command-line args."""
def __init__(self, **kwargs):
"""Build a new instance."""
self.attrs = kwargs
def __getattr__(self, attr):
"""Mimic attribute access."""
return self.attrs.get(attr)
@pytest.mark.asyncio
async def process_resources_core(
target_dir: Path, resources: List[Resource], additional_args=None, on_event=None
target_dir: Path, resources: List[Resource], args_str: str = None
):
"""Factorize code for test cases."""
def process_resource(resource: Resource):
pass
try:
events = status.load_events(target_dir)
assert events is None
if args_str is None:
args_str = ""
additional_args = {} if additional_args is None else additional_args
args = FakeArgs(target_dir=target_dir, **additional_args)
events = status.load_events(target_dir)
assert events is None
with status.open_status_writer(args) as append_event:
resource_event_by_id = await process_resources(
resources=resources,
args=args,
process_resource=process_resource,
on_event=append_event,
events=events,
)
except Exception as exc:
if on_event:
on_event(exc)
else:
raise exc
parser = argparse.ArgumentParser()
add_arguments_for_download(parser)
args = parser.parse_args(
args=list(filter(None, [str(target_dir), *args_str.split(" ")]))
)
return resource_event_by_id
with status.open_status_writer(args) as append_event:
return await process_resources(
resources=resources,
args=args,
process_resource=process_resource,
on_event=append_event,
events=events,
)
@pytest.mark.asyncio
async def test_skip_no_resources(tmp_path):
async def test_process_all_resources(tmp_path):
"""Process all resources."""
resources = [Resource(id=letter) for letter in list("ABCDEF")]
......@@ -97,10 +84,8 @@ async def test_process_one_resource(tmp_path):
resources = [Resource(id=letter) for letter in list("ABCDEF")]
selected_resource_id = "C"
additional_args = {"only": selected_resource_id}
resource_event_by_id = await process_resources_core(
tmp_path, resources, additional_args
tmp_path, resources, args_str=f"--only {selected_resource_id}"
)
assert resource_event_by_id is not None
assert len(resource_event_by_id) == len(resources)
......@@ -122,10 +107,10 @@ async def test_exclude_some_resources(tmp_path):
resources = [Resource(id=letter) for letter in list("ABCDEF")]
excluded_resource_ids = ["A", "E"]
additional_args = {"exclude": excluded_resource_ids}
resource_event_by_id = await process_resources_core(
tmp_path, resources, additional_args
tmp_path,
resources,
args_str="--exclude {}".format(" ".join(excluded_resource_ids)),
)
assert resource_event_by_id is not None
assert len(resource_event_by_id) == len(resources)
......@@ -149,10 +134,9 @@ async def test_limit_processed_resources(tmp_path):
limit_count = 4
limited_resource_ids = [resource.id for resource in resources[:limit_count]]
additional_args = {"limit": limit_count}
resource_event_by_id = await process_resources_core(
tmp_path, resources, additional_args
tmp_path, resources, args_str=f"--limit {limit_count}"
)
assert resource_event_by_id is not None
assert len(resource_event_by_id) == len(resources)
......
......@@ -61,6 +61,7 @@ doc =
sphinx
test =
pytest
pytest-asyncio
pytest-cov
tox
......@@ -108,4 +109,4 @@ max-line-length = 88
ignore-path = .git,.tox,dist,build,.eggs,__pycache__,doc/_build,*.egg-info,venv
[tool:pytest]
addopts = --cov=dbnomics_fetcher_toolbox --cov-report=xml --doctest-modules
addopts = --strict --cov=dbnomics_fetcher_toolbox --cov-report=xml --doctest-modules
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment