Skip to content

Commit c883667

Browse files
authored
Test _pytask.mark_utils. (#201)
1 parent ade72f1 commit c883667

7 files changed

+144
-0
lines changed

docs/source/changes.rst

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ all releases are available on `PyPI <https://pypi.org/project/pytask>`_ and
2020
systems.
2121
- :gh:`200` implements the :func:`@pytask.mark.task <_pytask.task.task>` decorator to
2222
mark functions as tasks regardless whether they are prefixed with ``task_`` or not.
23+
- :gh:`201` adds tests for ``_pytask.mark_utils``.
2324

2425

2526
0.1.5 - 2022-01-10

tests/test_collect.py

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ def test_pytask_collect_node_does_not_raise_error_if_path_is_not_normalized(
181181
assert not record
182182

183183

184+
@pytest.mark.unit
184185
def test_find_shortest_uniquely_identifiable_names_for_tasks(tmp_path):
185186
tasks = []
186187
expected = {}

tests/test_console.py

+3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ def test_render_to_string(color_system, text, strip_styles, expected):
107107
_THIS_FILE = Path(__file__)
108108

109109

110+
@pytest.mark.unit
110111
@pytest.mark.parametrize(
111112
"base_name, short_name, editor_url_scheme, use_short_name, relative_to, expected",
112113
[
@@ -175,6 +176,7 @@ def test_format_task_id(
175176
assert result == expected
176177

177178

179+
@pytest.mark.unit
178180
@pytest.mark.parametrize(
179181
"task_func, skipped_paths, expected",
180182
[
@@ -196,6 +198,7 @@ def test_get_file(task_func, skipped_paths, expected):
196198
assert result == expected
197199

198200

201+
@pytest.mark.unit
199202
@pytest.mark.parametrize(
200203
"task_func, expected",
201204
[

tests/test_logging.py

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ def test_humanize_time(amount, unit, short_label, expectation, expected):
132132
assert result == expected
133133

134134

135+
@pytest.mark.end_to_end
135136
@pytest.mark.parametrize("show_traceback", ["no", "yes"])
136137
def test_show_traceback(runner, tmp_path, show_traceback):
137138
source = "def task_raises(): raise Exception"

tests/test_mark_utils.py

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import attr
2+
import pytask
3+
import pytest
4+
from _pytask.mark_utils import get_marks_from_obj
5+
from _pytask.mark_utils import get_specific_markers_from_task
6+
from _pytask.mark_utils import has_marker
7+
from _pytask.mark_utils import remove_markers_from_func
8+
from _pytask.nodes import MetaTask
9+
10+
11+
@attr.s
12+
class Task(MetaTask):
13+
markers = attr.ib(factory=list)
14+
15+
def execute(self):
16+
...
17+
18+
def state(self):
19+
...
20+
21+
def add_report_section(self):
22+
...
23+
24+
25+
@pytest.mark.unit
26+
@pytest.mark.parametrize(
27+
"markers, marker_name, expected",
28+
[
29+
([], "not_found", []),
30+
(
31+
[pytask.mark.produces(), pytask.mark.depends_on()],
32+
"produces",
33+
[pytask.mark.produces()],
34+
),
35+
(
36+
[pytask.mark.produces(), pytask.mark.produces(), pytask.mark.depends_on()],
37+
"produces",
38+
[pytask.mark.produces(), pytask.mark.produces()],
39+
),
40+
],
41+
)
42+
def test_get_specific_markers_from_task(markers, marker_name, expected):
43+
task = Task()
44+
task.markers = markers
45+
result = get_specific_markers_from_task(task, marker_name)
46+
assert result == expected
47+
48+
49+
@pytest.mark.unit
50+
@pytest.mark.parametrize(
51+
"markers, marker_name, expected",
52+
[
53+
(None, "not_found", []),
54+
([], "not_found", []),
55+
(
56+
[pytask.mark.produces(), pytask.mark.depends_on()],
57+
"produces",
58+
[pytask.mark.produces()],
59+
),
60+
(
61+
[pytask.mark.produces(), pytask.mark.produces(), pytask.mark.depends_on()],
62+
"produces",
63+
[pytask.mark.produces(), pytask.mark.produces()],
64+
),
65+
],
66+
)
67+
def test_get_marks_from_obj(markers, marker_name, expected):
68+
def func():
69+
...
70+
71+
if markers is not None:
72+
func.pytaskmark = markers
73+
74+
result = get_marks_from_obj(func, marker_name)
75+
assert result == expected
76+
77+
78+
@pytest.mark.unit
79+
@pytest.mark.parametrize(
80+
"markers, marker_name, expected",
81+
[
82+
(None, "not_found", False),
83+
([], "not_found", False),
84+
([pytask.mark.produces(), pytask.mark.depends_on()], "produces", True),
85+
(
86+
[pytask.mark.produces(), pytask.mark.produces(), pytask.mark.depends_on()],
87+
"produces",
88+
True,
89+
),
90+
],
91+
)
92+
def test_has_marker(markers, marker_name, expected):
93+
def func():
94+
...
95+
96+
if markers is not None:
97+
func.pytaskmark = markers
98+
99+
result = has_marker(func, marker_name)
100+
assert result == expected
101+
102+
103+
@pytest.mark.unit
104+
@pytest.mark.parametrize(
105+
"markers, marker_name, expected_markers, expected_others",
106+
[
107+
(None, "not_found", [], []),
108+
([], "not_found", [], []),
109+
(
110+
[pytask.mark.produces(), pytask.mark.depends_on()],
111+
"produces",
112+
[pytask.mark.produces()],
113+
[pytask.mark.depends_on()],
114+
),
115+
(
116+
[pytask.mark.produces(), pytask.mark.produces(), pytask.mark.depends_on()],
117+
"produces",
118+
[pytask.mark.produces(), pytask.mark.produces()],
119+
[pytask.mark.depends_on()],
120+
),
121+
],
122+
)
123+
def test_remove_markers_from_func(
124+
markers, marker_name, expected_markers, expected_others
125+
):
126+
def func():
127+
...
128+
129+
if markers is not None:
130+
func.pytaskmark = markers
131+
132+
obj, result_markers = remove_markers_from_func(func, marker_name)
133+
assert obj.pytaskmark == expected_others
134+
assert result_markers == expected_markers

tests/test_outcomes.py

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from _pytask.report import ExecutionReport
77

88

9+
@pytest.mark.unit
910
@pytest.mark.parametrize("outcome_in_report", CollectionOutcome)
1011
def test_count_outcomes_collection(outcome_in_report):
1112
reports = [CollectionReport(outcome_in_report, None, None)]
@@ -19,6 +20,7 @@ def test_count_outcomes_collection(outcome_in_report):
1920
assert count == 0
2021

2122

23+
@pytest.mark.unit
2224
@pytest.mark.parametrize("outcome_in_report", TaskOutcome)
2325
def test_count_outcomes_tasks(outcome_in_report):
2426
reports = [ExecutionReport(None, outcome_in_report, None, None)]

tests/test_task.py

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pytask import main
77

88

9+
@pytest.mark.unit
910
@pytest.mark.parametrize("func_name", ["task_example", "func"])
1011
@pytest.mark.parametrize("task_name", ["the_only_task", None])
1112
def test_task_with_task_decorator(tmp_path, func_name, task_name):
@@ -34,6 +35,7 @@ def {func_name}(produces):
3435
)
3536

3637

38+
@pytest.mark.unit
3739
@pytest.mark.parametrize("func_name", ["task_example", "func"])
3840
@pytest.mark.parametrize("task_name", ["the_only_task", None])
3941
def test_task_with_task_decorator_with_parametrize(tmp_path, func_name, task_name):

0 commit comments

Comments
 (0)