Skip to content

Commit

Permalink
Merge pull request #1302 from nsoranzo/drop_assert_raises_regexp
Browse files Browse the repository at this point in the history
Use `pytest.raises()` instead of ad-hoc `assert_raises_regexp()` context manager
  • Loading branch information
mvdbeek committed Oct 28, 2022
2 parents 808f9b7 + 008f978 commit dbd1ece
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 113 deletions.
2 changes: 2 additions & 0 deletions planemo/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def cache_download(self, url, destination):
class PlanemoContext(PlanemoContextInterface):
"""Implementation of ``PlanemoContextInterface``"""

planemo_directory: Optional[str]

def __init__(self) -> None:
"""Construct a Context object using execution environment."""
self.home = os.getcwd()
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ max-complexity = 14
exclude=.eggs,.git,.tox,.venv,.venv3,build,docs/conf.py,docs/standards,project_templates/cwl_draft3_spec/

[mypy]
exclude = tests/data/
ignore_missing_imports = True
43 changes: 23 additions & 20 deletions tests/test_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import os
import shutil

import pytest

from planemo import cli
from planemo.runnable import for_path
from planemo.training import Training
from .test_utils import (
assert_raises_regexp,
skip_if_environ,
TEST_DATA_DIR,
)
Expand Down Expand Up @@ -101,15 +102,15 @@
}


def test_training_init():
def test_training_init() -> None:
"""Test :func:`planemo.training.Training.init`."""
train = Training(KWDS)
assert train.topics_dir == "topics"
assert train.topic is not None
assert train.tuto is None


def test_training_init_training():
def test_training_init_training() -> None:
"""Test :func:`planemo.training.Training.init_training`."""
train = Training(KWDS)
# new topic, nothing else
Expand All @@ -123,20 +124,19 @@ def test_training_init_training():
assert not os.listdir(os.path.join(train.topic.dir, "tutorials"))
# no new topic, no tutorial name but hands-on
train.kwds["slides"] = True
exp_exception = "A tutorial name is needed to create the skeleton of a tutorial slide deck"
with assert_raises_regexp(Exception, exp_exception):
with pytest.raises(Exception, match="A tutorial name is needed to create the skeleton of a tutorial slide deck"):
train.init_training(CTX)
# no new topic, no tutorial name but workflow
train.kwds["workflow"] = WF_FP
train.kwds["slides"] = False
exp_exception = "A tutorial name is needed to create the skeleton of the tutorial from a workflow"
with assert_raises_regexp(Exception, exp_exception):
with pytest.raises(
Exception, match="A tutorial name is needed to create the skeleton of the tutorial from a workflow"
):
train.init_training(CTX)
# no new topic, no tutorial name but zenodo
train.kwds["workflow"] = None
train.kwds["zenodo_link"] = zenodo_link
exp_exception = "A tutorial name is needed to add Zenodo information"
with assert_raises_regexp(Exception, exp_exception):
with pytest.raises(Exception, match="A tutorial name is needed to add Zenodo information"):
train.init_training(CTX)
# no new topic, new tutorial
train.kwds["tutorial_name"] = "new_tuto"
Expand All @@ -152,18 +152,17 @@ def test_training_init_training():
shutil.rmtree("metadata")


def create_existing_tutorial(exit_tuto_name, tuto_fp, topic):
def create_existing_tutorial(exit_tuto_name, tuto_fp, topic) -> None:
exist_tuto_dir = os.path.join(topic.dir, "tutorials", exit_tuto_name)
os.makedirs(exist_tuto_dir)
shutil.copyfile(tuto_fp, os.path.join(exist_tuto_dir, "tutorial.md"))


def test_training_check_topic_init_tuto():
def test_training_check_topic_init_tuto() -> None:
"""Test :func:`planemo.training.Training.check_topic_init_tuto`."""
train = Training(KWDS)
# no topic
exp_exception = "The topic my_new_topic does not exists. It should be created"
with assert_raises_regexp(Exception, exp_exception):
with pytest.raises(Exception, match="The topic my_new_topic does not exists. It should be created"):
train.check_topic_init_tuto()
# add topic
train.kwds["tutorial_name"] = None
Expand All @@ -182,7 +181,7 @@ def test_training_check_topic_init_tuto():
shutil.rmtree("metadata")


def test_fill_data_library():
def test_fill_data_library() -> None:
"""Test :func:`planemo.training.fill_data_library`."""
train = Training(KWDS)
train.kwds["tutorial_name"] = None
Expand All @@ -193,8 +192,9 @@ def test_fill_data_library():
create_existing_tutorial("existing_tutorial", tuto_wo_zenodo_fp, train.topic)
# no Zenodo link
train.kwds["zenodo_link"] = None
exp_exception = "A Zenodo link should be provided either in the metadata file or as argument of the command"
with assert_raises_regexp(Exception, exp_exception):
with pytest.raises(
Exception, match="A Zenodo link should be provided either in the metadata file or as argument of the command"
):
train.fill_data_library(CTX)
# with a given Zenodo link and no Zenodo in metadata
train.kwds["zenodo_link"] = zenodo_link
Expand All @@ -208,6 +208,7 @@ def test_fill_data_library():
train.kwds["zenodo_link"] = new_z_link
train.tuto = None
train.fill_data_library(CTX)
assert train.tuto
with open(train.tuto.data_lib_fp) as fh:
assert "DOI: 10.5281/zenodo.1324204" in fh.read()
with open(train.tuto.tuto_fp) as fh:
Expand All @@ -225,7 +226,7 @@ def test_fill_data_library():


@skip_if_environ("PLANEMO_SKIP_GALAXY_TESTS")
def test_generate_tuto_from_wf():
def test_generate_tuto_from_wf() -> None:
"""Test :func:`planemo.training.generate_tuto_from_wf`."""
train = Training(KWDS)
train.kwds["tutorial_name"] = None
Expand All @@ -235,8 +236,10 @@ def test_generate_tuto_from_wf():
create_existing_tutorial("existing_tutorial", tuto_fp, train.topic)
# no workflow
train.kwds["workflow"] = None
exp_exception = "A path to a local workflow or the id of a workflow on a running Galaxy instance should be provided"
with assert_raises_regexp(Exception, exp_exception):
with pytest.raises(
Exception,
match="A path to a local workflow or the id of a workflow on a running Galaxy instance should be provided",
):
train.generate_tuto_from_wf(CTX)
# with workflow
train.kwds["workflow"] = WF_FP
Expand All @@ -251,7 +254,7 @@ def test_generate_tuto_from_wf():
shutil.rmtree("metadata")


def assert_file_contains(file, text):
def assert_file_contains(file: str, text: str) -> None:
with open(file) as fh:
contents = fh.read()
if text not in contents:
Expand Down
35 changes: 16 additions & 19 deletions tests/test_training_tool_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import json
import os

import pytest

from planemo.training.tool_input import (
get_empty_input,
get_empty_param,
Expand All @@ -12,10 +14,7 @@
wf,
wf_param_values,
)
from .test_utils import (
assert_raises_regexp,
TEST_DATA_DIR,
)
from .test_utils import TEST_DATA_DIR

wf_steps = wf["steps"]
# load the output from
Expand All @@ -25,28 +24,27 @@
tool_inp_desc = tool_desc["inputs"]


def test_get_input_tool_name():
def test_get_input_tool_name() -> None:
"""Test :func:`planemo.training.tool_input.get_input_tool_name`."""
assert "Input dataset" in get_input_tool_name("1", wf_steps)
assert "output of" in get_input_tool_name("4", wf_steps)
assert get_input_tool_name("10", wf_steps) == ""


def test_get_empty_input():
def test_get_empty_input() -> None:
"""Test :func:`planemo.training.tool_input.get_empty_input`."""
assert '{% icon param-file %} *"Input file"*: File' in get_empty_input()


def test_get_empty_param():
def test_get_empty_param() -> None:
"""Test :func:`planemo.training.tool_input.get_empty_param`."""
assert '*"Parameter"*: `a value`' in get_empty_param()


def test_ToolInput_init():
def test_ToolInput_init() -> None:
"""Test :func:`planemo.training.tool_input.ToolInput.init`."""
# test type exception
exp_exception = "No type for the parameter t"
with assert_raises_regexp(Exception, exp_exception):
with pytest.raises(Exception, match="No type for the parameter t"):
ToolInput(
tool_inp_desc={"name": "t"},
wf_param_values=wf_param_values,
Expand All @@ -56,8 +54,7 @@ def test_ToolInput_init():
force_default=False,
)
# test with param not in workflow and exception
exp_exception = "t not in workflow"
with assert_raises_regexp(Exception, exp_exception):
with pytest.raises(Exception, match="t not in workflow"):
ToolInput(
tool_inp_desc={"name": "t", "type": ""},
wf_param_values=wf_param_values,
Expand Down Expand Up @@ -89,7 +86,7 @@ def test_ToolInput_init():
assert tool_input.wf_param_values == "workdb.sqlite"


def test_ToolInput_get_formatted_inputs():
def test_ToolInput_get_formatted_inputs() -> None:
"""Test :func:`planemo.training.tool_input.ToolInput.get_formatted_inputs`."""
# test no input
tool_input = ToolInput(
Expand Down Expand Up @@ -128,7 +125,7 @@ def test_ToolInput_get_formatted_inputs():
assert "(Input dataset)" in inputlist


def test_ToolInput_get_lower_param_desc():
def test_ToolInput_get_lower_param_desc() -> None:
"""Test :func:`planemo.training.tool_input.ToolInput.get_lower_param_desc`."""
tool_input = ToolInput(
tool_inp_desc=tool_inp_desc[1],
Expand All @@ -142,7 +139,7 @@ def test_ToolInput_get_lower_param_desc():
assert "> - {% icon param-collection %}" in sub_param_desc


def test_ToolInput_get_formatted_section_desc():
def test_ToolInput_get_formatted_section_desc() -> None:
"""Test :func:`planemo.training.tool_input.ToolInput.get_formatted_section_desc`."""
tool_input = ToolInput(
tool_inp_desc=tool_inp_desc[1],
Expand All @@ -157,7 +154,7 @@ def test_ToolInput_get_formatted_section_desc():
assert "> - {%" in section_paramlist


def test_ToolInput_get_formatted_conditional_desc():
def test_ToolInput_get_formatted_conditional_desc() -> None:
"""Test :func:`planemo.training.tool_input.ToolInput.get_formatted_conditional_desc`."""
tool_input = ToolInput(
tool_inp_desc=tool_inp_desc[5],
Expand All @@ -173,7 +170,7 @@ def test_ToolInput_get_formatted_conditional_desc():
assert '> - *"' in conditional_paramlist


def test_ToolInput_get_formatted_repeat_desc():
def test_ToolInput_get_formatted_repeat_desc() -> None:
"""Test :func:`planemo.training.tool_input.ToolInput.get_formatted_repeat_desc`."""
tool_input = ToolInput(
tool_inp_desc=tool_inp_desc[2],
Expand All @@ -189,7 +186,7 @@ def test_ToolInput_get_formatted_repeat_desc():
assert "> -" in repeat_desc


def test_ToolInput_get_formatted_other_param_desc():
def test_ToolInput_get_formatted_other_param_desc() -> None:
"""Test :func:`planemo.training.tool_input.ToolInput.get_formatted_other_param_desc`."""
# test default value of the tool
tool_input = ToolInput(
Expand Down Expand Up @@ -235,7 +232,7 @@ def test_ToolInput_get_formatted_other_param_desc():
assert "*: ``" in tool_input.get_formatted_other_param_desc()


def test_ToolInput_get_formatted_desc():
def test_ToolInput_get_formatted_desc() -> None:
"""Test :func:`planemo.training.tool_input.ToolInput.get_formatted_desc`."""
# test no param values
tool_input = ToolInput(
Expand Down

0 comments on commit dbd1ece

Please sign in to comment.