test_sub_graph_json.py 8.89 KB
Newer Older
Wout De Nolf's avatar
Wout De Nolf committed
1
2
import json
import pytest
3
4
5
from ewokscore import load_graph


Wout De Nolf's avatar
Wout De Nolf committed
6
def savegraph(graph, tmpdir, name):
7
8
    filename = name + ".json"
    with open(tmpdir / filename, mode="w") as f:
Wout De Nolf's avatar
Wout De Nolf committed
9
        json.dump(graph, f, indent=2)
10
    return filename
11
12


Wout De Nolf's avatar
Wout De Nolf committed
13
14
15
@pytest.fixture
def subsubsubgraph(tmpdir):
    graph = {
16
        "graph": {
17
18
            "input_nodes": [{"id": "in", "node": "task1"}],
            "output_nodes": [{"id": "out", "node": "task2"}],
19
        },
20
        "nodes": [
Wout De Nolf's avatar
Wout De Nolf committed
21
22
            {
                "id": "task1",
Wout De Nolf's avatar
Wout De Nolf committed
23
24
                "task_type": "method",
                "task_identifier": "ewokscore.tests.examples.tasks.simplemethods.add",
Wout De Nolf's avatar
Wout De Nolf committed
25
26
27
            },
            {
                "id": "task2",
Wout De Nolf's avatar
Wout De Nolf committed
28
29
                "task_type": "method",
                "task_identifier": "ewokscore.tests.examples.tasks.simplemethods.add",
Wout De Nolf's avatar
Wout De Nolf committed
30
            },
31
32
        ],
        "links": [
33
34
35
            {
                "source": "task1",
                "target": "task2",
36
                "data_mapping": [{"target_input": 0, "source_output": "return_value"}],
37
            },
38
39
40
        ],
    }

Wout De Nolf's avatar
Wout De Nolf committed
41
42
43
44
45
46
    return savegraph(graph, tmpdir, "subsubsubgraph")


@pytest.fixture
def subsubgraph(tmpdir, subsubsubgraph):
    graph = {
47
        "graph": {
48
            "input_nodes": [{"id": "in", "node": "task1"}],
49
            "output_nodes": [
50
                {"id": "out", "node": "subsubsubgraph", "sub_node": "out"}
51
52
            ],
        },
53
        "nodes": [
Wout De Nolf's avatar
Wout De Nolf committed
54
55
            {
                "id": "task1",
Wout De Nolf's avatar
Wout De Nolf committed
56
57
                "task_type": "method",
                "task_identifier": "ewokscore.tests.examples.tasks.simplemethods.add",
Wout De Nolf's avatar
Wout De Nolf committed
58
59
60
            },
            {
                "id": "task2",
Wout De Nolf's avatar
Wout De Nolf committed
61
62
63
64
65
66
67
                "task_type": "method",
                "task_identifier": "ewokscore.tests.examples.tasks.simplemethods.add",
            },
            {
                "id": "subsubsubgraph",
                "task_type": "graph",
                "task_identifier": subsubsubgraph,
Wout De Nolf's avatar
Wout De Nolf committed
68
            },
69
70
        ],
        "links": [
71
72
73
            {
                "source": "task1",
                "target": "task2",
74
                "data_mapping": [{"target_input": 0, "source_output": "return_value"}],
75
            },
76
77
78
            {
                "source": "task2",
                "target": "subsubsubgraph",
79
                "sub_target": "in",
80
                "data_mapping": [{"target_input": 0, "source_output": "return_value"}],
81
82
83
            },
        ],
    }
Wout De Nolf's avatar
Wout De Nolf committed
84
85
    return savegraph(graph, tmpdir, "subsubgraph")

86

Wout De Nolf's avatar
Wout De Nolf committed
87
88
89
@pytest.fixture
def subgraph(tmpdir, subsubgraph):
    graph = {
90
        "graph": {
91
92
            "input_nodes": [{"id": "in", "node": "task1"}],
            "output_nodes": [{"id": "out", "node": "subsubgraph", "sub_node": "out"}],
93
        },
94
        "nodes": [
Wout De Nolf's avatar
Wout De Nolf committed
95
96
            {
                "id": "task1",
Wout De Nolf's avatar
Wout De Nolf committed
97
98
                "task_type": "method",
                "task_identifier": "ewokscore.tests.examples.tasks.simplemethods.add",
Wout De Nolf's avatar
Wout De Nolf committed
99
100
101
            },
            {
                "id": "task2",
Wout De Nolf's avatar
Wout De Nolf committed
102
103
                "task_type": "method",
                "task_identifier": "ewokscore.tests.examples.tasks.simplemethods.add",
Wout De Nolf's avatar
Wout De Nolf committed
104
            },
Wout De Nolf's avatar
Wout De Nolf committed
105
            {"id": "subsubgraph", "task_type": "graph", "task_identifier": subsubgraph},
106
107
        ],
        "links": [
108
109
110
            {
                "source": "task1",
                "target": "task2",
111
                "data_mapping": [{"target_input": 0, "source_output": "return_value"}],
112
            },
113
114
115
            {
                "source": "task2",
                "target": "subsubgraph",
116
                "sub_target": "in",
117
                "data_mapping": [{"target_input": 0, "source_output": "return_value"}],
118
119
120
            },
        ],
    }
Wout De Nolf's avatar
Wout De Nolf committed
121
122
    return savegraph(graph, tmpdir, "subgraph")

123

Wout De Nolf's avatar
Wout De Nolf committed
124
125
@pytest.fixture
def graph(tmpdir, subgraph):
126
127
    graph = {
        "nodes": [
Wout De Nolf's avatar
Wout De Nolf committed
128
129
            {"id": "subgraph1", "task_type": "graph", "task_identifier": subgraph},
            {"id": "subgraph2", "task_type": "graph", "task_identifier": subgraph},
Wout De Nolf's avatar
Wout De Nolf committed
130
131
            {
                "id": "append",
Wout De Nolf's avatar
Wout De Nolf committed
132
133
                "task_type": "method",
                "task_identifier": "ewokscore.tests.examples.tasks.simplemethods.append",
Wout De Nolf's avatar
Wout De Nolf committed
134
            },
135
136
137
138
        ],
        "links": [
            {
                "source": "subgraph1",
139
                "sub_source": "out",
140
                "target": "subgraph2",
141
                "sub_target": "in",
142
                "data_mapping": [{"target_input": 0, "source_output": "return_value"}],
143
            },
144
            # Link all nodes from "subgraph1" to "append"
145
146
            {
                "source": "subgraph1",
147
                "sub_source": "task1",
148
                "target": "append",
149
                "data_mapping": [{"target_input": 0, "source_output": "return_value"}],
150
151
152
            },
            {
                "source": "subgraph1",
153
                "sub_source": "task2",
154
                "target": "append",
155
                "data_mapping": [{"target_input": 1, "source_output": "return_value"}],
156
157
158
            },
            {
                "source": "subgraph1",
159
                "sub_source": ("subsubgraph", "task1"),
160
                "target": "append",
161
                "data_mapping": [{"target_input": 2, "source_output": "return_value"}],
162
163
164
            },
            {
                "source": "subgraph1",
165
                "sub_source": ("subsubgraph", "task2"),
166
                "target": "append",
167
                "data_mapping": [{"target_input": 3, "source_output": "return_value"}],
168
169
170
            },
            {
                "source": "subgraph1",
171
                "sub_source": ("subsubgraph", ("subsubsubgraph", "task1")),
172
                "target": "append",
173
                "data_mapping": [{"target_input": 4, "source_output": "return_value"}],
174
175
176
            },
            {
                "source": "subgraph1",
177
                "sub_source": ("subsubgraph", ("subsubsubgraph", "task2")),
178
                "target": "append",
179
                "data_mapping": [{"target_input": 5, "source_output": "return_value"}],
180
            },
181
            # Link all nodes from "subgraph2" to "append"
182
183
            {
                "source": "subgraph2",
184
                "sub_source": "task1",
185
                "target": "append",
186
                "data_mapping": [{"target_input": 6, "source_output": "return_value"}],
187
188
189
            },
            {
                "source": "subgraph2",
190
                "sub_source": "task2",
191
                "target": "append",
192
                "data_mapping": [{"target_input": 7, "source_output": "return_value"}],
193
194
195
            },
            {
                "source": "subgraph2",
196
                "sub_source": ("subsubgraph", "task1"),
197
                "target": "append",
198
                "data_mapping": [{"target_input": 8, "source_output": "return_value"}],
199
200
201
            },
            {
                "source": "subgraph2",
202
                "sub_source": ("subsubgraph", "task2"),
203
                "target": "append",
204
                "data_mapping": [{"target_input": 9, "source_output": "return_value"}],
205
206
207
            },
            {
                "source": "subgraph2",
208
                "sub_source": ("subsubgraph", ("subsubsubgraph", "task1")),
209
                "target": "append",
210
                "data_mapping": [{"target_input": 10, "source_output": "return_value"}],
211
212
213
            },
            {
                "source": "subgraph2",
214
                "sub_source": ("subsubgraph", ("subsubsubgraph", "task2")),
215
                "target": "append",
216
                "data_mapping": [{"target_input": 11, "source_output": "return_value"}],
217
218
219
            },
        ],
    }
Wout De Nolf's avatar
Wout De Nolf committed
220
221
    return savegraph(graph, tmpdir, "graph")

222

223
224
def test_load_from_json(tmpdir, graph):
    taskgraph = load_graph(graph, root_dir=str(tmpdir))
225
    tasks = taskgraph.execute(results_of_all_nodes=True)
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256

    assert len(tasks) == 13

    task = tasks[("subgraph1", "task1")]
    assert task.outputs.return_value == 1
    task = tasks[("subgraph1", "task2")]
    assert task.outputs.return_value == 2
    task = tasks[("subgraph1", ("subsubgraph", "task1"))]
    assert task.outputs.return_value == 3
    task = tasks[("subgraph1", ("subsubgraph", "task2"))]
    assert task.outputs.return_value == 4
    task = tasks[("subgraph1", ("subsubgraph", ("subsubsubgraph", "task1")))]
    assert task.outputs.return_value == 5
    task = tasks[("subgraph1", ("subsubgraph", ("subsubsubgraph", "task2")))]
    assert task.outputs.return_value == 6

    task = tasks[("subgraph2", "task1")]
    assert task.outputs.return_value == 7
    task = tasks[("subgraph2", "task2")]
    assert task.outputs.return_value == 8
    task = tasks[("subgraph2", ("subsubgraph", "task1"))]
    assert task.outputs.return_value == 9
    task = tasks[("subgraph2", ("subsubgraph", "task2"))]
    assert task.outputs.return_value == 10
    task = tasks[("subgraph2", ("subsubgraph", ("subsubsubgraph", "task1")))]
    assert task.outputs.return_value == 11
    task = tasks[("subgraph2", ("subsubgraph", ("subsubsubgraph", "task2")))]
    assert task.outputs.return_value == 12

    task = tasks["append"]
    assert task.outputs.return_value == tuple(range(1, 13))