-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtest_dag_command.py
More file actions
227 lines (181 loc) · 6.88 KB
/
test_dag_command.py
File metadata and controls
227 lines (181 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
from __future__ import annotations
import importlib
import os
import subprocess
import sys
import textwrap
import pytest
from _pytask.dag_command import _RankDirection
from pytask import ExitCode
from pytask import cli
try:
importlib.import_module("pygraphviz")
except ImportError: # pragma: no cover
_IS_PYGRAPHVIZ_INSTALLED = False
else:
_IS_PYGRAPHVIZ_INSTALLED = True
# Test should run always on remote except on Windows and locally only with the package
# installed.
_TEST_SHOULD_RUN = _IS_PYGRAPHVIZ_INSTALLED or (
os.environ.get("CI") and sys.platform == "linux"
)
_GRAPH_LAYOUTS = ["dot"]
_TEST_FORMATS = ["dot", "pdf", "png", "jpeg", "svg"]
@pytest.mark.skipif(not _TEST_SHOULD_RUN, reason="pygraphviz is required")
@pytest.mark.parametrize("layout", _GRAPH_LAYOUTS)
@pytest.mark.parametrize("format_", _TEST_FORMATS)
@pytest.mark.parametrize("rankdir", ["LR"])
def test_create_graph_via_cli(tmp_path, runner, format_, layout, rankdir):
if sys.platform == "win32" and format_ == "pdf": # pragma: no cover
pytest.xfail("gvplugin_pango.dll might be missing on Github Actions.")
source = """
from pathlib import Path
def task_example(path=Path("input.txt")): ...
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
tmp_path.joinpath("input.txt").touch()
result = runner.invoke(
cli,
[
"dag",
tmp_path.as_posix(),
"-o",
tmp_path.joinpath(f"dag.{format_}"),
"-l",
layout,
"-r",
rankdir,
],
)
assert result.exit_code == ExitCode.OK
assert tmp_path.joinpath(f"dag.{format_}").exists()
@pytest.mark.skipif(not _TEST_SHOULD_RUN, reason="pygraphviz is required")
@pytest.mark.parametrize("layout", _GRAPH_LAYOUTS)
@pytest.mark.parametrize("format_", _TEST_FORMATS)
@pytest.mark.parametrize("rankdir", [_RankDirection.LR.value, _RankDirection.TB])
def test_create_graph_via_task(tmp_path, format_, layout, rankdir):
if sys.platform == "win32" and format_ == "pdf": # pragma: no cover
pytest.xfail("gvplugin_pango.dll might be missing on Github Actions.")
rankdir_str = rankdir if isinstance(rankdir, str) else rankdir.name
source = f"""
import pytask
from pathlib import Path
import networkx as nx
def task_example(path=Path("input.txt")): ...
def main():
dag = pytask.build_dag({{"paths": Path(__file__).parent}})
dag.graph = {{"rankdir": "{rankdir_str}"}}
graph = nx.nx_agraph.to_agraph(dag)
path = Path(__file__).parent.joinpath("dag.{format_}")
graph.draw(path, prog="{layout}")
if __name__ == "__main__":
main()
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
tmp_path.joinpath("input.txt").touch()
result = subprocess.run(
(sys.executable, "task_example.py"),
cwd=tmp_path,
check=True,
capture_output=True,
)
assert result.returncode == ExitCode.OK
assert tmp_path.joinpath(f"dag.{format_}").exists()
def _raise_exc(exc):
raise exc
def test_raise_error_with_graph_via_cli_missing_optional_dependency(
monkeypatch, tmp_path, runner
):
source = """
from pathlib import Path
def task_example(path=Path("input.txt")): ...
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
tmp_path.joinpath("input.txt").touch()
monkeypatch.setattr(
"_pytask.compat.import_module",
lambda x: _raise_exc(ImportError("pygraphviz not found")), # noqa: ARG005
)
result = runner.invoke(
cli,
["dag", tmp_path.as_posix(), "-o", tmp_path.joinpath("dag.png"), "-l", "dot"],
)
assert result.exit_code == ExitCode.FAILED
assert "pytask requires the optional dependency 'pygraphviz'." in result.output
assert "pip" in result.output
assert "conda" in result.output
assert "Traceback" not in result.output
assert not tmp_path.joinpath("dag.png").exists()
def test_raise_error_with_graph_via_task_missing_optional_dependency(
monkeypatch, tmp_path, runner
):
source = """
import pytask
from pathlib import Path
import networkx as nx
def task_create_graph():
dag = pytask.build_dag({"paths": Path(__file__).parent})
graph = nx.nx_agraph.to_agraph(dag)
path = Path(__file__).parent.joinpath("dag.png")
graph.draw(path, prog="dot")
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
monkeypatch.setattr(
"_pytask.compat.import_module",
lambda x: _raise_exc(ImportError("pygraphviz not found")), # noqa: ARG005
)
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.FAILED
assert "pytask requires the optional dependency 'pygraphviz'." in result.output
assert "pip" in result.output
assert "conda" in result.output
assert "Traceback" in result.output
assert not tmp_path.joinpath("dag.png").exists()
def test_raise_error_with_graph_via_cli_missing_optional_program(
monkeypatch, tmp_path, runner
):
monkeypatch.setattr(
"_pytask.compat.import_module",
lambda x: None, # noqa: ARG005
)
monkeypatch.setattr("_pytask.compat.shutil.which", lambda x: None) # noqa: ARG005
source = """
from pathlib import Path
def task_example(path=Path("input.txt")): ...
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
tmp_path.joinpath("input.txt").touch()
result = runner.invoke(
cli,
["dag", tmp_path.as_posix(), "-o", tmp_path.joinpath("dag.png"), "-l", "dot"],
)
assert result.exit_code == ExitCode.FAILED
assert "pytask requires the optional program 'dot'." in result.output
assert "conda" in result.output
assert "Traceback" not in result.output
assert not tmp_path.joinpath("dag.png").exists()
def test_raise_error_with_graph_via_task_missing_optional_program(
monkeypatch, tmp_path, runner
):
monkeypatch.setattr(
"_pytask.compat.import_module",
lambda x: None, # noqa: ARG005
)
monkeypatch.setattr("_pytask.compat.shutil.which", lambda x: None) # noqa: ARG005
source = """
import pytask
from pathlib import Path
import networkx as nx
def task_create_graph():
dag = pytask.build_dag({"paths": Path(__file__).parent})
graph = nx.nx_agraph.to_agraph(dag)
path = Path(__file__).parent.joinpath("dag.png")
graph.draw(path, prog="dot")
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.FAILED
assert "pytask requires the optional program 'dot'." in result.output
assert "conda" in result.output
assert "Traceback" in result.output
assert not tmp_path.joinpath("dag.png").exists()