1
+ import json
1
2
import os
2
3
import unittest
4
+ from os import listdir
3
5
4
6
from serverlessworkflow .sdk .action import Action
7
+ from serverlessworkflow .sdk .action_data_filter import ActionDataFilter
5
8
from serverlessworkflow .sdk .function import Function
6
9
from serverlessworkflow .sdk .function_ref import FunctionRef
10
+ from serverlessworkflow .sdk .operation_state import OperationState
7
11
from serverlessworkflow .sdk .workflow import Workflow
8
12
9
13
10
14
class TestWorkflow (unittest .TestCase ):
11
-
12
- workflow = Workflow (id_ = "greeting" ,
13
- name = "Greeting Workflow" ,
14
- description = "Greet Someone" ,
15
- version = '1.0' ,
16
- specVersion = '0.8' ,
17
- start = "Greet" ,
18
- states = [
19
- {
20
- "name" : "Greet" ,
21
- "type" : "operation" ,
22
- "actions" : [
23
- {
24
- "functionRef" : {
25
- "refName" : "greetingFunction" ,
26
- "arguments" : {
27
- "name" : "${ .person.name }"
28
- }
29
- },
30
- "actionDataFilter" : {
31
- "results" : "${ .greeting }"
32
- }
33
- }
34
- ],
35
- "end" : True
36
- }
37
- ],
38
- functions = [
39
- {
40
- "name" : "greetingFunction" ,
41
- "operation" : "file://myapis/greetingapis.json#greeting"
15
+ workflow = Workflow (
16
+ id_ = "greeting" ,
17
+ name = "Greeting Workflow" ,
18
+ description = "Greet Someone" ,
19
+ version = '1.0' ,
20
+ specVersion = '0.8' ,
21
+ start = "Greet" ,
22
+ states = [
23
+ OperationState (
24
+ name = "Greet" ,
25
+ type = "operation" ,
26
+ actions = [
27
+ Action (
28
+ functionRef = FunctionRef (
29
+ refName = "greetingFunction" ,
30
+ arguments = {
31
+ "name" : "${ .person.name }"
42
32
}
43
- ]
33
+ ),
34
+ actionDataFilter = ActionDataFilter (
35
+ results = "${ .greeting }"
44
36
)
37
+ )
38
+ ],
39
+ end = True
40
+ )
41
+ ],
42
+ functions = [
43
+ Function (name = "greetingFunction" ,
44
+ operation = "file://myapis/greetingapis.json#greeting" )
45
+ ]
46
+ )
45
47
46
48
def test_workflow_to_json (self ):
47
49
expected = """{
@@ -105,9 +107,37 @@ def test_workflow_to_yaml(self):
105
107
"""
106
108
self .assertEqual (expected , self .workflow .to_yaml ())
107
109
110
+ def test_programmatically_create_workflow (self ):
111
+
112
+ self .assertEqual ("greeting" , self .workflow .id )
113
+ self .assertEqual ("operation" , self .workflow .states [0 ].type )
114
+ self .assertTrue (isinstance (self .workflow .states [0 ], OperationState ))
115
+ self .assertEqual (True , self .workflow .states [0 ].end )
116
+ self .assertTrue (isinstance (self .workflow .states [0 ].actions [0 ], Action ))
117
+ self .assertTrue (isinstance (self .workflow .states [0 ].actions [0 ].functionRef , FunctionRef ))
118
+ self .assertTrue (isinstance (self .workflow .functions [0 ], Function ))
119
+
108
120
def test_workflow_from_source_json (self ):
109
- wf_file = os .path .join (os .path .dirname (__file__ ), 'test_workflow.json' )
110
- self .assert_test_workflow_file (wf_file )
121
+ examples_dir = os .path .join (os .path .dirname (__file__ ), '../../examples' )
122
+ examples = listdir (examples_dir )
123
+ self .assertEqual (len (examples ), 10 )
124
+
125
+ for example in examples :
126
+ with self .subTest (f"test_{ example } " ):
127
+ with open (examples_dir + "/" + example , "r" ) as swf_file :
128
+ workflow = Workflow .from_source (swf_file )
129
+ self .assertTrue (isinstance (workflow , Workflow ))
130
+
131
+ def test_instance_workflow_class (self ):
132
+ examples_dir = os .path .join (os .path .dirname (__file__ ), '../../examples' )
133
+ examples = listdir (examples_dir )
134
+ self .assertEqual (len (examples ), 10 )
135
+
136
+ for example in examples :
137
+ with self .subTest (f"test_{ example } " ):
138
+ with open (examples_dir + "/" + example , "r" ) as swf_file :
139
+ workflow = Workflow (** json .load (swf_file ))
140
+ self .assertTrue (isinstance (workflow , Workflow ))
111
141
112
142
def test_workflow_from_source_yaml (self ):
113
143
wf_file = os .path .join (os .path .dirname (__file__ ), 'test_workflow.yaml' )
@@ -125,4 +155,3 @@ def assert_test_workflow_file(self, wf_file):
125
155
self .assertTrue (isinstance (workflow .states [0 ].actions [0 ], Action ))
126
156
self .assertTrue (isinstance (workflow .states [0 ].actions [0 ].functionRef , FunctionRef ))
127
157
self .assertTrue (isinstance (workflow .functions [0 ], Function ))
128
-
0 commit comments