15
15
16
16
from typing import List
17
17
import unittest
18
+ import logging
19
+ import inspect
18
20
from dapr .ext .workflow .dapr_workflow_context import DaprWorkflowContext
19
21
from unittest import mock
20
- from dapr .ext .workflow .workflow_runtime import WorkflowRuntime
22
+ from dapr .ext .workflow .workflow_runtime import WorkflowRuntime , alternate_name
21
23
from dapr .ext .workflow .workflow_activity_context import WorkflowActivityContext
22
24
23
25
listOrchestrators : List [str ] = []
@@ -35,14 +37,20 @@ def add_named_activity(self, name: str, fn):
35
37
class WorkflowRuntimeTest (unittest .TestCase ):
36
38
37
39
def setUp (self ):
40
+ self .log = logging .getLogger (__name__ )
38
41
listActivities .clear ()
39
42
listOrchestrators .clear ()
40
43
mock .patch ('durabletask.worker._Registry' , return_value = FakeTaskHubGrpcWorker ()).start ()
41
44
self .runtime_options = WorkflowRuntime ()
42
- if hasattr (self .mock_client_wf , "_registered_name" ):
43
- del self .mock_client_wf .__dict__ ["_registered_name" ]
44
- if hasattr (self .mock_client_activity , "_registered_name" ):
45
- del self .mock_client_activity .__dict__ ["_registered_name" ]
45
+ if hasattr (self .mock_client_wf , "_alternate_name" ):
46
+ del self .mock_client_wf .__dict__ ["_alternate_name" ]
47
+ if hasattr (self .mock_client_activity , "_alternate_name" ):
48
+ del self .mock_client_activity .__dict__ ["_alternate_name" ]
49
+ if hasattr (self .mock_client_wf , "_workflow_registered" ):
50
+ del self .mock_client_wf .__dict__ ["_workflow_registered" ]
51
+ if hasattr (self .mock_client_activity , "_activity_registered" ):
52
+ del self .mock_client_activity .__dict__ ["_activity_registered" ]
53
+
46
54
47
55
def mock_client_wf (ctx : DaprWorkflowContext , input ):
48
56
print (f'{ input } ' )
@@ -54,39 +62,46 @@ def test_register(self):
54
62
self .runtime_options .register_workflow (self .mock_client_wf , name = "mock_client_wf" )
55
63
wanted_orchestrator = [self .mock_client_wf .__name__ ]
56
64
assert listOrchestrators == wanted_orchestrator
65
+ assert self .mock_client_wf ._alternate_name == "mock_client_wf"
66
+ assert self .mock_client_wf ._workflow_registered
57
67
58
68
self .runtime_options .register_activity (self .mock_client_activity )
59
69
wanted_activity = [self .mock_client_activity .__name__ ]
60
70
assert listActivities == wanted_activity
71
+ assert self .mock_client_activity ._activity_registered
61
72
62
73
def test_decorator_register (self ):
63
74
client_wf = (self .runtime_options .workflow ())(self .mock_client_wf )
64
75
wanted_orchestrator = [self .mock_client_wf .__name__ ]
65
76
assert listOrchestrators == wanted_orchestrator
66
- assert client_wf ._registered_name == self .mock_client_wf .__name__
77
+ assert client_wf ._alternate_name == self .mock_client_wf .__name__
78
+ assert self .mock_client_wf ._workflow_registered
67
79
68
80
client_activity = (self .runtime_options .activity ())(self .mock_client_activity )
69
81
wanted_activity = [self .mock_client_activity .__name__ ]
70
82
assert listActivities == wanted_activity
71
- assert client_activity ._registered_name == self .mock_client_activity .__name__
83
+ assert client_activity ._alternate_name == self .mock_client_activity .__name__
84
+ assert self .mock_client_activity ._activity_registered
72
85
73
86
def test_both_decorator_and_register (self ):
74
87
client_wf = (self .runtime_options .workflow (name = "test_wf" ))(self .mock_client_wf )
75
88
wanted_orchestrator = ["test_wf" ]
76
89
assert listOrchestrators == wanted_orchestrator
77
- assert client_wf ._registered_name == "test_wf"
90
+ assert client_wf ._alternate_name == "test_wf"
91
+ assert self .mock_client_wf ._workflow_registered
78
92
79
93
self .runtime_options .register_activity (self .mock_client_activity , name = "test_act" )
80
94
wanted_activity = ["test_act" ]
81
95
assert listActivities == wanted_activity
82
- assert hasattr (self .mock_client_activity , "_registered_name" )
96
+ assert hasattr (self .mock_client_activity , "_alternate_name" )
97
+ assert self .mock_client_activity ._activity_registered
83
98
84
99
def test_register_wf_act_using_both_decorator_and_method (self ):
85
100
client_wf = (self .runtime_options .workflow (name = "test_wf" ))(self .mock_client_wf )
86
101
87
102
wanted_orchestrator = ["test_wf" ]
88
103
assert listOrchestrators == wanted_orchestrator
89
- assert client_wf ._registered_name == "test_wf"
104
+ assert client_wf ._alternate_name == "test_wf"
90
105
with self .assertRaises (ValueError ) as exeception_context :
91
106
self .runtime_options .register_workflow (self .mock_client_wf )
92
107
wf_name = self .mock_client_wf .__name__
@@ -96,19 +111,32 @@ def test_register_wf_act_using_both_decorator_and_method(self):
96
111
client_act = (self .runtime_options .activity (name = "test_act" ))(self .mock_client_activity )
97
112
wanted_activity = ["test_act" ]
98
113
assert listActivities == wanted_activity
99
- assert client_act ._registered_name == "test_act"
114
+ assert client_act ._alternate_name == "test_act"
100
115
with self .assertRaises (ValueError ) as exeception_context :
101
116
self .runtime_options .register_activity (self .mock_client_activity )
102
117
act_name = self .mock_client_activity .__name__
103
118
self .assertEqual (exeception_context .exception .args [0 ],
104
119
f'Activity { act_name } already registered as test_act' )
105
120
121
+ def test_duplicate_alternate_name_registration (self ):
122
+ client_wf = (alternate_name (name = "test" ))(self .mock_client_wf )
123
+ with self .assertRaises (ValueError ) as exeception_context :
124
+ (self .runtime_options .workflow (name = "random" ))(client_wf )
125
+ self .assertEqual (exeception_context .exception .args [0 ],
126
+ f'Workflow { client_wf .__name__ } already has an alternate name test' )
127
+
128
+ client_act = (alternate_name (name = "test" ))(self .mock_client_activity )
129
+ with self .assertRaises (ValueError ) as exeception_context :
130
+ (self .runtime_options .activity (name = "random" ))(client_act )
131
+ self .assertEqual (exeception_context .exception .args [0 ],
132
+ f'Activity { client_act .__name__ } already has an alternate name test' )
133
+
106
134
def test_register_wf_act_using_both_decorator_and_method_without_name (self ):
107
135
client_wf = (self .runtime_options .workflow ())(self .mock_client_wf )
108
136
109
137
wanted_orchestrator = ["mock_client_wf" ]
110
138
assert listOrchestrators == wanted_orchestrator
111
- assert client_wf ._registered_name == "mock_client_wf"
139
+ assert client_wf ._alternate_name == "mock_client_wf"
112
140
with self .assertRaises (ValueError ) as exeception_context :
113
141
self .runtime_options .register_workflow (self .mock_client_wf , name = "test_wf" )
114
142
wf_name = self .mock_client_wf .__name__
@@ -118,7 +146,7 @@ def test_register_wf_act_using_both_decorator_and_method_without_name(self):
118
146
client_act = (self .runtime_options .activity ())(self .mock_client_activity )
119
147
wanted_activity = ["mock_client_activity" ]
120
148
assert listActivities == wanted_activity
121
- assert client_act ._registered_name == "mock_client_activity"
149
+ assert client_act ._alternate_name == "mock_client_activity"
122
150
with self .assertRaises (ValueError ) as exeception_context :
123
151
self .runtime_options .register_activity (self .mock_client_activity , name = "test_act" )
124
152
act_name = self .mock_client_activity .__name__
@@ -129,9 +157,9 @@ def test_decorator_register_optinal_name(self):
129
157
client_wf = (self .runtime_options .workflow (name = "test_wf" ))(self .mock_client_wf )
130
158
wanted_orchestrator = ["test_wf" ]
131
159
assert listOrchestrators == wanted_orchestrator
132
- assert client_wf ._registered_name == "test_wf"
160
+ assert client_wf ._alternate_name == "test_wf"
133
161
134
162
client_act = (self .runtime_options .activity (name = "test_act" ))(self .mock_client_activity )
135
163
wanted_activity = ["test_act" ]
136
164
assert listActivities == wanted_activity
137
- assert client_act ._registered_name == "test_act"
165
+ assert client_act ._alternate_name == "test_act"
0 commit comments