diff --git a/docs/source/changes.rst b/docs/source/changes.rst index e8011d46..1af48ab9 100644 --- a/docs/source/changes.rst +++ b/docs/source/changes.rst @@ -20,6 +20,7 @@ all releases are available on `PyPI `_ and systems. - :gh:`200` implements the :func:`@pytask.mark.task <_pytask.task.task>` decorator to mark functions as tasks regardless whether they are prefixed with ``task_`` or not. +- :gh:`201` adds tests for ``_pytask.mark_utils``. 0.1.5 - 2022-01-10 diff --git a/tests/test_collect.py b/tests/test_collect.py index 5c280a67..db12cf63 100644 --- a/tests/test_collect.py +++ b/tests/test_collect.py @@ -181,6 +181,7 @@ def test_pytask_collect_node_does_not_raise_error_if_path_is_not_normalized( assert not record +@pytest.mark.unit def test_find_shortest_uniquely_identifiable_names_for_tasks(tmp_path): tasks = [] expected = {} diff --git a/tests/test_console.py b/tests/test_console.py index ae4f5e9a..2deb54e6 100644 --- a/tests/test_console.py +++ b/tests/test_console.py @@ -107,6 +107,7 @@ def test_render_to_string(color_system, text, strip_styles, expected): _THIS_FILE = Path(__file__) +@pytest.mark.unit @pytest.mark.parametrize( "base_name, short_name, editor_url_scheme, use_short_name, relative_to, expected", [ @@ -175,6 +176,7 @@ def test_format_task_id( assert result == expected +@pytest.mark.unit @pytest.mark.parametrize( "task_func, skipped_paths, expected", [ @@ -196,6 +198,7 @@ def test_get_file(task_func, skipped_paths, expected): assert result == expected +@pytest.mark.unit @pytest.mark.parametrize( "task_func, expected", [ diff --git a/tests/test_logging.py b/tests/test_logging.py index 116d9c74..6728bff0 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -132,6 +132,7 @@ def test_humanize_time(amount, unit, short_label, expectation, expected): assert result == expected +@pytest.mark.end_to_end @pytest.mark.parametrize("show_traceback", ["no", "yes"]) def test_show_traceback(runner, tmp_path, show_traceback): source = "def task_raises(): raise Exception" diff --git a/tests/test_mark_utils.py b/tests/test_mark_utils.py new file mode 100644 index 00000000..35fa2b72 --- /dev/null +++ b/tests/test_mark_utils.py @@ -0,0 +1,134 @@ +import attr +import pytask +import pytest +from _pytask.mark_utils import get_marks_from_obj +from _pytask.mark_utils import get_specific_markers_from_task +from _pytask.mark_utils import has_marker +from _pytask.mark_utils import remove_markers_from_func +from _pytask.nodes import MetaTask + + +@attr.s +class Task(MetaTask): + markers = attr.ib(factory=list) + + def execute(self): + ... + + def state(self): + ... + + def add_report_section(self): + ... + + +@pytest.mark.unit +@pytest.mark.parametrize( + "markers, marker_name, expected", + [ + ([], "not_found", []), + ( + [pytask.mark.produces(), pytask.mark.depends_on()], + "produces", + [pytask.mark.produces()], + ), + ( + [pytask.mark.produces(), pytask.mark.produces(), pytask.mark.depends_on()], + "produces", + [pytask.mark.produces(), pytask.mark.produces()], + ), + ], +) +def test_get_specific_markers_from_task(markers, marker_name, expected): + task = Task() + task.markers = markers + result = get_specific_markers_from_task(task, marker_name) + assert result == expected + + +@pytest.mark.unit +@pytest.mark.parametrize( + "markers, marker_name, expected", + [ + (None, "not_found", []), + ([], "not_found", []), + ( + [pytask.mark.produces(), pytask.mark.depends_on()], + "produces", + [pytask.mark.produces()], + ), + ( + [pytask.mark.produces(), pytask.mark.produces(), pytask.mark.depends_on()], + "produces", + [pytask.mark.produces(), pytask.mark.produces()], + ), + ], +) +def test_get_marks_from_obj(markers, marker_name, expected): + def func(): + ... + + if markers is not None: + func.pytaskmark = markers + + result = get_marks_from_obj(func, marker_name) + assert result == expected + + +@pytest.mark.unit +@pytest.mark.parametrize( + "markers, marker_name, expected", + [ + (None, "not_found", False), + ([], "not_found", False), + ([pytask.mark.produces(), pytask.mark.depends_on()], "produces", True), + ( + [pytask.mark.produces(), pytask.mark.produces(), pytask.mark.depends_on()], + "produces", + True, + ), + ], +) +def test_has_marker(markers, marker_name, expected): + def func(): + ... + + if markers is not None: + func.pytaskmark = markers + + result = has_marker(func, marker_name) + assert result == expected + + +@pytest.mark.unit +@pytest.mark.parametrize( + "markers, marker_name, expected_markers, expected_others", + [ + (None, "not_found", [], []), + ([], "not_found", [], []), + ( + [pytask.mark.produces(), pytask.mark.depends_on()], + "produces", + [pytask.mark.produces()], + [pytask.mark.depends_on()], + ), + ( + [pytask.mark.produces(), pytask.mark.produces(), pytask.mark.depends_on()], + "produces", + [pytask.mark.produces(), pytask.mark.produces()], + [pytask.mark.depends_on()], + ), + ], +) +def test_remove_markers_from_func( + markers, marker_name, expected_markers, expected_others +): + def func(): + ... + + if markers is not None: + func.pytaskmark = markers + + obj, result_markers = remove_markers_from_func(func, marker_name) + assert obj.pytaskmark == expected_others + assert result_markers == expected_markers diff --git a/tests/test_outcomes.py b/tests/test_outcomes.py index b38d29a2..2bb3ff93 100644 --- a/tests/test_outcomes.py +++ b/tests/test_outcomes.py @@ -6,6 +6,7 @@ from _pytask.report import ExecutionReport +@pytest.mark.unit @pytest.mark.parametrize("outcome_in_report", CollectionOutcome) def test_count_outcomes_collection(outcome_in_report): reports = [CollectionReport(outcome_in_report, None, None)] @@ -19,6 +20,7 @@ def test_count_outcomes_collection(outcome_in_report): assert count == 0 +@pytest.mark.unit @pytest.mark.parametrize("outcome_in_report", TaskOutcome) def test_count_outcomes_tasks(outcome_in_report): reports = [ExecutionReport(None, outcome_in_report, None, None)] diff --git a/tests/test_task.py b/tests/test_task.py index 511f8b20..a32ca2f1 100644 --- a/tests/test_task.py +++ b/tests/test_task.py @@ -6,6 +6,7 @@ from pytask import main +@pytest.mark.unit @pytest.mark.parametrize("func_name", ["task_example", "func"]) @pytest.mark.parametrize("task_name", ["the_only_task", None]) def test_task_with_task_decorator(tmp_path, func_name, task_name): @@ -34,6 +35,7 @@ def {func_name}(produces): ) +@pytest.mark.unit @pytest.mark.parametrize("func_name", ["task_example", "func"]) @pytest.mark.parametrize("task_name", ["the_only_task", None]) def test_task_with_task_decorator_with_parametrize(tmp_path, func_name, task_name):