|
| 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 |
0 commit comments