-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_workflow.py
155 lines (140 loc) · 5.26 KB
/
test_workflow.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
import os
import unittest
from os import listdir
from serverlessworkflow.sdk.action import Action
from serverlessworkflow.sdk.action_data_filter import ActionDataFilter
from serverlessworkflow.sdk.function import Function
from serverlessworkflow.sdk.function_ref import FunctionRef
from serverlessworkflow.sdk.operation_state import OperationState
from serverlessworkflow.sdk.workflow import Workflow
class TestWorkflow(unittest.TestCase):
workflow = Workflow(
id="greeting",
name="Greeting Workflow",
description="Greet Someone",
version='1.0',
specVersion='0.8',
start="Greet",
states=[
OperationState(
name="Greet",
type="operation",
actions=[
Action(
functionRef=FunctionRef(
refName="greetingFunction",
arguments={
"name": "${ .person.name }"
}
),
actionDataFilter=ActionDataFilter(
results="${ .greeting }"
)
)
],
end=True
)
],
functions=[
Function(name="greetingFunction",
operation="file://myapis/greetingapis.json#greeting")
]
)
def test_workflow_to_json(self):
expected = """{
"id": "greeting",
"name": "Greeting Workflow",
"version": "1.0",
"description": "Greet Someone",
"specVersion": "0.8",
"start": "Greet",
"states": [
{
"name": "Greet",
"type": "operation",
"actions": [
{
"functionRef": {
"refName": "greetingFunction",
"arguments": {
"name": "${ .person.name }"
}
},
"actionDataFilter": {
"results": "${ .greeting }"
}
}
],
"end": true
}
],
"functions": [
{
"name": "greetingFunction",
"operation": "file://myapis/greetingapis.json#greeting"
}
]
}"""
self.assertEqual(expected, self.workflow.to_json())
def test_workflow_to_yaml(self):
expected = """id: greeting
name: Greeting Workflow
version: '1.0'
description: Greet Someone
specVersion: '0.8'
start: Greet
states:
- name: Greet
type: operation
actions:
- functionRef:
refName: greetingFunction
arguments:
name: ${ .person.name }
actionDataFilter:
results: ${ .greeting }
end: true
functions:
- name: greetingFunction
operation: file://myapis/greetingapis.json#greeting
"""
self.assertEqual(expected, self.workflow.to_yaml())
def test_programmatically_create_workflow(self):
self.assertEqual("greeting", self.workflow.id)
self.assertEqual("operation", self.workflow.states[0].type)
self.assertTrue(isinstance(self.workflow.states[0], OperationState))
self.assertEqual(True, self.workflow.states[0].end)
self.assertTrue(isinstance(self.workflow.states[0].actions[0], Action))
self.assertTrue(isinstance(self.workflow.states[0].actions[0].functionRef, FunctionRef))
self.assertTrue(isinstance(self.workflow.functions[0], Function))
def test_workflow_from_source_json(self):
examples_dir = os.path.join(os.path.dirname(__file__), '../../examples')
examples = listdir(examples_dir)
self.assertEqual(len(examples), 10)
for example in examples:
with self.subTest(f"test_{example}"):
with open(examples_dir + "/" + example, "r") as swf_file:
workflow = Workflow.from_source(swf_file.read())
self.assertTrue(isinstance(workflow, Workflow))
def test_instance_workflow_class(self):
examples_dir = os.path.join(os.path.dirname(__file__), '../../examples')
examples = listdir(examples_dir)
self.assertEqual(len(examples), 10)
for example in examples:
with self.subTest(f"test_{example}"):
with open(examples_dir + "/" + example, "r") as swf_file:
workflow = Workflow.from_source(swf_file.read())
self.assertTrue(isinstance(workflow, Workflow))
def test_workflow_from_source_yaml(self):
wf_file = os.path.join(os.path.dirname(__file__), 'test_workflow.yaml')
self.assert_test_workflow_file(wf_file)
def assert_test_workflow_file(self, wf_file):
with open(wf_file, "r") as swf_file:
workflow = Workflow.from_source(swf_file.read())
self.assertEqual("greeting", workflow.id)
self.assertEqual("operation", workflow.states[0].type)
self.assertEqual(True, workflow.states[0].end)
self.assertEqual('jq', workflow.expressionLang)
self.assertTrue(isinstance(workflow.states[0].actions[0], Action))
self.assertTrue(isinstance(workflow.states[0].actions[0].functionRef, FunctionRef))
self.assertTrue(isinstance(workflow.functions[0], Function))