This repository was archived by the owner on Aug 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathtest_dataflow.py
200 lines (185 loc) · 7.67 KB
/
test_dataflow.py
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
"""
WARNING Adding too many testcases to this file starts causing weird errors where
pkgresources will try to __import__ a package and then that package won't be
found. It only happens when there's too many testcases in one file, which is why
test_integration_cli.py was split into a directory. Also, it will only happen
when all the testcases in the file are run at once. Not when running only the
testcase that fails by itself. It's very werid.
"""
import re
import json
import pathlib
import contextlib
from dffml.df.types import Operation, DataFlow, Input
from dffml.cli.cli import CLI
from dffml.util.entrypoint import load
from dffml.configloader.configloader import BaseConfigLoader, ConfigLoaders
from dffml.util.asynctestcase import AsyncTestCase, relative_path
class TestDataFlow(AsyncTestCase):
REQUIRED_PLUGINS = ["shouldi", "dffml-config-yaml", "dffml-feature-git"]
async def setUp(self):
await super().setUp()
# Use shouldi's dataflow for tests
self.DATAFLOW = list(load("shouldi.cli:DATAFLOW"))[0]
class TestDataFlowDiagram(TestDataFlow):
async def test_default(self):
filename = self.mktempfile() + ".json"
pathlib.Path(filename).write_text(json.dumps(self.DATAFLOW.export()))
with contextlib.redirect_stdout(self.stdout):
await CLI.cli("dataflow", "diagram", filename)
stdout = self.stdout.getvalue()
# Check that a subgraph is being made for each operation
self.assertTrue(re.findall(r"subgraph.*run_bandit", stdout))
# Check that all stages are included
for check in ["Processing", "Output", "Cleanup"]:
self.assertIn(f"{check} Stage", stdout)
async def test_simple(self):
filename = self.mktempfile() + ".json"
pathlib.Path(filename).write_text(json.dumps(self.DATAFLOW.export()))
with contextlib.redirect_stdout(self.stdout):
await CLI.cli("dataflow", "diagram", "-simple", filename)
# Check that a subgraph is not being made for each operation
self.assertFalse(
re.findall(r"subgraph.*run_bandit", self.stdout.getvalue())
)
async def test_single_stage(self):
filename = self.mktempfile() + ".json"
pathlib.Path(filename).write_text(json.dumps(self.DATAFLOW.export()))
with contextlib.redirect_stdout(self.stdout):
await CLI.cli(
"dataflow", "diagram", "-stages", "processing", "--", filename
)
stdout = self.stdout.getvalue()
# Check that the single stage is not its own subgraph
for check in ["Processing", "Output", "Cleanup"]:
self.assertNotIn(f"{check} Stage", stdout)
async def test_multi_stage(self):
filename = self.mktempfile() + ".json"
pathlib.Path(filename).write_text(json.dumps(self.DATAFLOW.export()))
with contextlib.redirect_stdout(self.stdout):
await CLI.cli(
"dataflow",
"diagram",
"-stages",
"processing",
"output",
"--",
filename,
)
stdout = self.stdout.getvalue()
# Check that the single stage is not its own subgraph
for check in ["Processing", "Output"]:
self.assertIn(f"{check} Stage", stdout)
for check in ["Cleanup"]:
self.assertNotIn(f"{check} Stage", stdout)
class TestDataFlowMerge(TestDataFlow):
async def test_dataflow_usage_example(self):
# Write out shouldi dataflow
orig = self.mktempfile() + ".json"
pathlib.Path(orig).write_text(json.dumps(self.DATAFLOW.export()))
# Import from feature/git
transform_to_record = Operation.load("dffml.mapping.create")
lines_of_code_by_language, lines_of_code_to_comments = list(
load(
"dffml_feature_git.feature.operations:lines_of_code_by_language",
"dffml_feature_git.feature.operations:lines_of_code_to_comments",
relative=relative_path("..", "..", "feature", "git"),
)
)
# Create new dataflow
override = DataFlow.auto(
transform_to_record,
lines_of_code_by_language,
lines_of_code_to_comments,
)
# TODO Modify and compare against yaml in docs example
# Write out override dataflow
created = self.mktempfile() + ".json"
pathlib.Path(created).write_text(json.dumps(override.export()))
# Merge the two
with contextlib.redirect_stdout(self.stdout):
await CLI.cli("dataflow", "merge", orig, created)
DataFlow._fromdict(**json.loads(self.stdout.getvalue()))
async def test_run(self):
self.required_plugins("dffml-config-yaml", "dffml-model-scratch")
# Load get_single and model_predict
get_single = Operation.load("get_single")
model_predict = list(load("dffml.operation.model:model_predict"))[0]
# Create new dataflow from operations
dataflow = DataFlow.auto(get_single, model_predict)
# Add the seed inputs
dataflow.seed.append(
Input(
value=[
definition.name
for definition in model_predict.op.outputs.values()
],
definition=get_single.inputs["spec"],
)
)
# Write out the dataflow
dataflow_yaml = pathlib.Path(self.mktempfile() + ".yaml")
async with BaseConfigLoader.load("yaml").withconfig(
{}
) as configloader:
async with configloader() as loader:
dataflow_yaml.write_bytes(
await loader.dumpb(dataflow.export(linked=True))
)
# TODO Figure out how nested model config options will work
# print(dataflow_yaml.read_text())
return
class TestDataFlowCreate(TestDataFlow):
async def test_dataflow_run_cli_example(self):
# Write out override dataflow
created = self.mktempfile() + ".yaml"
with open(created, "w") as fileobj:
with contextlib.redirect_stdout(fileobj):
await CLI.cli(
"dataflow",
"create",
"dffml.mapping.create",
"print_output",
"-configloader",
"yaml",
)
# Load the generated dataflow
async with ConfigLoaders() as cfgl:
_, exported = await cfgl.load_file(created)
dataflow = DataFlow._fromdict(**exported)
# Modify the dataflow
dataflow.flow["print_output"].inputs["data"] = [
{"dffml.mapping.create": "mapping"}
]
# Write back modified dataflow
async with BaseConfigLoader.load("yaml").withconfig(
{}
) as configloader:
async with configloader() as loader:
with open(created, "wb") as fileobj:
fileobj.write(
await loader.dumpb(dataflow.export(linked=True))
)
# Run the dataflow
with contextlib.redirect_stdout(self.stdout):
await CLI.cli(
"dataflow",
"run",
"records",
"all",
"-no-echo",
"-record-def",
"value",
"-inputs",
"hello=key",
"-dataflow",
created,
"-sources",
"m=memory",
"-source-records",
"world",
"user",
)
self.assertEqual(
self.stdout.getvalue(), "{'hello': 'world'}\n{'hello': 'user'}\n"
)