Skip to content

Commit 6e22fb0

Browse files
committed
modify test
1 parent 5e2d963 commit 6e22fb0

File tree

1 file changed

+153
-105
lines changed

1 file changed

+153
-105
lines changed

tests/cadence/worker/test_registry.py

Lines changed: 153 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -22,136 +22,184 @@ def test_basic_registry_creation(self):
2222
assert len(reg._workflows) == 0
2323
assert len(reg._activities) == 0
2424

25-
def test_register_and_retrieve_workflow(self):
26-
"""Test registering and retrieving workflows."""
25+
@pytest.mark.parametrize("registration_type", ["workflow", "activity"])
26+
def test_basic_registration_and_retrieval(self, registration_type):
27+
"""Test basic registration and retrieval for both workflows and activities."""
2728
reg = Registry()
2829

29-
@reg.workflow
30-
def test_workflow():
31-
return "test"
32-
33-
assert "test_workflow" in reg._workflows
34-
wf = reg.get_workflow("test_workflow")
35-
assert wf() == "test"
36-
37-
def test_register_and_retrieve_activity(self):
38-
"""Test registering and retrieving activities."""
39-
reg = Registry()
40-
41-
@reg.activity
42-
def test_activity():
43-
return "test"
44-
45-
assert "test_activity" in reg._activities
46-
act = reg.get_activity("test_activity")
47-
assert act() == "test"
30+
if registration_type == "workflow":
31+
@reg.workflow
32+
def test_func():
33+
return "test"
34+
35+
assert "test_func" in reg._workflows
36+
func = reg.get_workflow("test_func")
37+
else:
38+
@reg.activity
39+
def test_func():
40+
return "test"
41+
42+
assert "test_func" in reg._activities
43+
func = reg.get_activity("test_func")
44+
45+
assert func() == "test"
4846

49-
def test_workflow_registration(self):
50-
"""Test workflow registration."""
47+
@pytest.mark.parametrize("registration_type", ["workflow", "activity"])
48+
def test_direct_call_behavior(self, registration_type):
49+
"""Test direct function call behavior for both workflows and activities."""
5150
reg = Registry()
5251

53-
@reg.workflow
54-
def global_workflow():
55-
return "global"
56-
57-
assert "global_workflow" in reg._workflows
58-
wf = reg.get_workflow("global_workflow")
59-
assert wf() == "global"
52+
def test_func():
53+
return "direct_call"
54+
55+
if registration_type == "workflow":
56+
# Direct call behavior - should register and return the function
57+
registered_func = reg.workflow(test_func)
58+
assert "test_func" in reg._workflows
59+
func = reg.get_workflow("test_func")
60+
else:
61+
# Direct call behavior - should register and return the function
62+
registered_func = reg.activity(test_func)
63+
assert "test_func" in reg._activities
64+
func = reg.get_activity("test_func")
65+
66+
# Should be the same function
67+
assert registered_func == test_func
68+
assert func() == "direct_call"
6069

61-
def test_activity_registration(self):
62-
"""Test activity registration."""
70+
@pytest.mark.parametrize("registration_type", ["workflow", "activity"])
71+
def test_decorator_with_options(self, registration_type):
72+
"""Test decorator with options for both workflows and activities."""
6373
reg = Registry()
6474

65-
@reg.activity
66-
def global_activity():
67-
return "global"
68-
69-
assert "global_activity" in reg._activities
70-
act = reg.get_activity("global_activity")
71-
assert act() == "global"
72-
73-
75+
if registration_type == "workflow":
76+
@reg.workflow(name="custom_name", alias="custom_alias")
77+
def test_func():
78+
return "decorator_with_options"
79+
80+
assert "custom_name" in reg._workflows
81+
assert "custom_alias" in reg._workflow_aliases
82+
func = reg.get_workflow("custom_name")
83+
else:
84+
@reg.activity(name="custom_name", alias="custom_alias")
85+
def test_func():
86+
return "decorator_with_options"
87+
88+
assert "custom_name" in reg._activities
89+
assert "custom_alias" in reg._activity_aliases
90+
func = reg.get_activity("custom_name")
91+
92+
assert func() == "decorator_with_options"
7493

75-
def test_workflow_not_found_error(self):
76-
"""Test KeyError is raised when workflow not found."""
94+
@pytest.mark.parametrize("registration_type", ["workflow", "activity"])
95+
def test_direct_call_with_options(self, registration_type):
96+
"""Test direct call with options for both workflows and activities."""
7797
reg = Registry()
7898

79-
with pytest.raises(KeyError):
80-
reg.get_workflow("nonexistent")
99+
def test_func():
100+
return "direct_call_with_options"
101+
102+
if registration_type == "workflow":
103+
# Direct call with options
104+
registered_func = reg.workflow(test_func, name="custom_name", alias="custom_alias")
105+
assert "custom_name" in reg._workflows
106+
assert "custom_alias" in reg._workflow_aliases
107+
func = reg.get_workflow("custom_name")
108+
else:
109+
# Direct call with options
110+
registered_func = reg.activity(test_func, name="custom_name", alias="custom_alias")
111+
assert "custom_name" in reg._activities
112+
assert "custom_alias" in reg._activity_aliases
113+
func = reg.get_activity("custom_name")
114+
115+
assert registered_func == test_func
116+
assert func() == "direct_call_with_options"
81117

82-
def test_activity_not_found_error(self):
83-
"""Test KeyError is raised when activity not found."""
118+
@pytest.mark.parametrize("registration_type", ["workflow", "activity"])
119+
def test_not_found_error(self, registration_type):
120+
"""Test KeyError is raised when function not found."""
84121
reg = Registry()
85122

86-
with pytest.raises(KeyError):
87-
reg.get_activity("nonexistent")
123+
if registration_type == "workflow":
124+
with pytest.raises(KeyError):
125+
reg.get_workflow("nonexistent")
126+
else:
127+
with pytest.raises(KeyError):
128+
reg.get_activity("nonexistent")
88129

89-
def test_duplicate_registration_error(self):
130+
@pytest.mark.parametrize("registration_type", ["workflow", "activity"])
131+
def test_duplicate_registration_error(self, registration_type):
90132
"""Test KeyError is raised for duplicate registrations."""
91133
reg = Registry()
92134

93-
@reg.workflow
94-
def test_workflow():
95-
return "test"
96-
97-
with pytest.raises(KeyError):
135+
if registration_type == "workflow":
98136
@reg.workflow
99-
def test_workflow():
100-
return "duplicate"
137+
def test_func():
138+
return "test"
139+
140+
with pytest.raises(KeyError):
141+
@reg.workflow
142+
def test_func():
143+
return "duplicate"
144+
else:
145+
@reg.activity
146+
def test_func():
147+
return "test"
148+
149+
with pytest.raises(KeyError):
150+
@reg.activity
151+
def test_func():
152+
return "duplicate"
101153

102-
def test_workflow_alias(self):
103-
"""Test workflow alias functionality."""
154+
@pytest.mark.parametrize("registration_type", ["workflow", "activity"])
155+
def test_alias_functionality(self, registration_type):
156+
"""Test alias functionality for both workflows and activities."""
104157
reg = Registry()
105158

106-
@reg.workflow(name="custom_name")
107-
def test_workflow():
108-
return "test"
109-
110-
assert "custom_name" in reg._workflows
111-
wf = reg.get_workflow("custom_name")
112-
assert wf() == "test"
159+
if registration_type == "workflow":
160+
@reg.workflow(name="custom_name")
161+
def test_func():
162+
return "test"
163+
164+
assert "custom_name" in reg._workflows
165+
func = reg.get_workflow("custom_name")
166+
else:
167+
@reg.activity(alias="custom_alias")
168+
def test_func():
169+
return "test"
170+
171+
assert "custom_alias" in reg._activity_aliases
172+
func = reg.get_activity("custom_alias")
173+
174+
assert func() == "test"
113175

114-
def test_activity_alias(self):
115-
"""Test activity alias functionality."""
176+
@pytest.mark.parametrize("registration_type", ["workflow", "activity"])
177+
def test_options_class(self, registration_type):
178+
"""Test using options classes for both workflows and activities."""
116179
reg = Registry()
117180

118-
@reg.activity(alias="custom_alias")
119-
def test_activity():
120-
return "test"
121-
122-
assert "custom_alias" in reg._activity_aliases
123-
act = reg.get_activity("custom_alias")
124-
assert act() == "test"
125-
126-
def test_workflow_options_class(self):
127-
"""Test using RegisterWorkflowOptions class."""
128-
reg = Registry()
129-
130-
options = RegisterWorkflowOptions(name="custom_name", alias="custom_alias")
131-
132-
@reg.workflow(**options.__dict__)
133-
def test_workflow():
134-
return "test"
135-
136-
assert "custom_name" in reg._workflows
137-
assert "custom_alias" in reg._workflow_aliases
138-
wf = reg.get_workflow("custom_name")
139-
assert wf() == "test"
140-
141-
def test_activity_options_class(self):
142-
"""Test using RegisterActivityOptions class."""
143-
reg = Registry()
144-
145-
options = RegisterActivityOptions(name="custom_name", alias="custom_alias")
146-
147-
@reg.activity(**options.__dict__)
148-
def test_activity():
149-
return "test"
150-
151-
assert "custom_name" in reg._activities
152-
assert "custom_alias" in reg._activity_aliases
153-
act = reg.get_activity("custom_name")
154-
assert act() == "test"
181+
if registration_type == "workflow":
182+
options = RegisterWorkflowOptions(name="custom_name", alias="custom_alias")
183+
184+
@reg.workflow(**options.__dict__)
185+
def test_func():
186+
return "test"
187+
188+
assert "custom_name" in reg._workflows
189+
assert "custom_alias" in reg._workflow_aliases
190+
func = reg.get_workflow("custom_name")
191+
else:
192+
options = RegisterActivityOptions(name="custom_name", alias="custom_alias")
193+
194+
@reg.activity(**options.__dict__)
195+
def test_func():
196+
return "test"
197+
198+
assert "custom_name" in reg._activities
199+
assert "custom_alias" in reg._activity_aliases
200+
func = reg.get_activity("custom_name")
201+
202+
assert func() == "test"
155203

156204

157205
if __name__ == "__main__":

0 commit comments

Comments
 (0)