|
| 1 | +import textwrap |
| 2 | + |
| 3 | +import pytest |
| 4 | +from _pytask.database import create_database |
| 5 | +from _pytask.database import State |
| 6 | +from _pytask.outcomes import Persisted |
| 7 | +from _pytask.outcomes import SkippedUnchanged |
| 8 | +from _pytask.persist import pytask_execute_task_process_report |
| 9 | +from pony import orm |
| 10 | +from pytask import main |
| 11 | + |
| 12 | + |
| 13 | +class DummyClass: |
| 14 | + pass |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.end_to_end |
| 18 | +def test_persist_marker_is_set(tmp_path): |
| 19 | + session = main({"paths": tmp_path}) |
| 20 | + assert "persist" in session.config["markers"] |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.end_to_end |
| 24 | +def test_multiple_runs_with_persist(tmp_path): |
| 25 | + """Perform multiple consecutive runs and check intermediate outcomes with persist. |
| 26 | +
|
| 27 | + 1. The product is missing which should result in a normal execution of the task. |
| 28 | + 2. Change the product, check that run is successful and state in database has |
| 29 | + changed. |
| 30 | + 3. Run the task another time. Now, the task is skipped successfully. |
| 31 | +
|
| 32 | + """ |
| 33 | + source = """ |
| 34 | + import pytask |
| 35 | +
|
| 36 | + @pytask.mark.persist |
| 37 | + @pytask.mark.depends_on("in.txt") |
| 38 | + @pytask.mark.produces("out.txt") |
| 39 | + def task_dummy(depends_on, produces): |
| 40 | + produces.write_text(depends_on.read_text()) |
| 41 | + """ |
| 42 | + tmp_path.joinpath("task_dummy.py").write_text(textwrap.dedent(source)) |
| 43 | + tmp_path.joinpath("in.txt").write_text("I'm not the reason you care.") |
| 44 | + |
| 45 | + session = main({"paths": tmp_path}) |
| 46 | + |
| 47 | + assert session.exit_code == 0 |
| 48 | + assert len(session.execution_reports) == 1 |
| 49 | + assert session.execution_reports[0].success |
| 50 | + assert session.execution_reports[0].exc_info is None |
| 51 | + assert tmp_path.joinpath("out.txt").exists() |
| 52 | + |
| 53 | + tmp_path.joinpath("out.txt").write_text("Never again in despair.") |
| 54 | + |
| 55 | + session = main({"paths": tmp_path}) |
| 56 | + |
| 57 | + assert session.exit_code == 0 |
| 58 | + assert len(session.execution_reports) == 1 |
| 59 | + assert session.execution_reports[0].success |
| 60 | + assert isinstance(session.execution_reports[0].exc_info[1], Persisted) |
| 61 | + |
| 62 | + orm.db_session.__enter__() |
| 63 | + |
| 64 | + create_database( |
| 65 | + "sqlite", tmp_path.joinpath(".pytask.sqlite3").as_posix(), True, False |
| 66 | + ) |
| 67 | + task_id = tmp_path.joinpath("task_dummy.py").as_posix() + "::task_dummy" |
| 68 | + node_id = tmp_path.joinpath("out.txt").as_posix() |
| 69 | + |
| 70 | + state = State[task_id, node_id].state |
| 71 | + assert float(state) == tmp_path.joinpath("out.txt").stat().st_mtime |
| 72 | + |
| 73 | + orm.db_session.__exit__() |
| 74 | + |
| 75 | + session = main({"paths": tmp_path}) |
| 76 | + |
| 77 | + assert session.exit_code == 0 |
| 78 | + assert len(session.execution_reports) == 1 |
| 79 | + assert session.execution_reports[0].success |
| 80 | + assert isinstance(session.execution_reports[0].exc_info[1], SkippedUnchanged) |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.end_to_end |
| 84 | +def test_migrating_a_whole_task_with_persist(tmp_path): |
| 85 | + source = """ |
| 86 | + import pytask |
| 87 | +
|
| 88 | + @pytask.mark.persist |
| 89 | + @pytask.mark.depends_on("in.txt") |
| 90 | + @pytask.mark.produces("out.txt") |
| 91 | + def task_dummy(depends_on, produces): |
| 92 | + produces.write_text(depends_on.read_text()) |
| 93 | + """ |
| 94 | + tmp_path.joinpath("task_dummy.py").write_text(textwrap.dedent(source)) |
| 95 | + for name in ["in.txt", "out.txt"]: |
| 96 | + tmp_path.joinpath(name).write_text( |
| 97 | + "They say oh my god I see the way you shine." |
| 98 | + ) |
| 99 | + |
| 100 | + session = main({"paths": tmp_path}) |
| 101 | + |
| 102 | + assert session.exit_code == 0 |
| 103 | + assert len(session.execution_reports) == 1 |
| 104 | + assert session.execution_reports[0].success |
| 105 | + assert isinstance(session.execution_reports[0].exc_info[1], Persisted) |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.unit |
| 109 | +@pytest.mark.parametrize( |
| 110 | + "exc_info, expected", |
| 111 | + [ |
| 112 | + (None, None), |
| 113 | + ((None, None, None), None), |
| 114 | + ((None, Persisted(), None), True), |
| 115 | + ], |
| 116 | +) |
| 117 | +def test_pytask_execute_task_process_report(exc_info, expected): |
| 118 | + report = DummyClass() |
| 119 | + report.exc_info = exc_info |
| 120 | + result = pytask_execute_task_process_report(report) |
| 121 | + |
| 122 | + if expected: |
| 123 | + assert report.success |
| 124 | + else: |
| 125 | + assert result is None |
0 commit comments