Skip to content

Commit 97329c7

Browse files
authored
Test dynamically registering workflows, steps, transactions in class (#440)
Test that we can register methods from within the __init__ function of a non-DBOS class. This allows us to give dynamic names to the workflow/step/transaction methods based on a given name if needed.
1 parent 807d41e commit 97329c7

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

tests/classdefs.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,36 @@ def test_recv_workflow(topic: str) -> str:
9292
msg3 = DBOS.recv(timeout_seconds=10)
9393
DBOSSendRecv.recv_counter += 1
9494
return "-".join([str(msg1), str(msg2), str(msg3)])
95+
96+
97+
class DBOSTestWrapperMethods:
98+
# Test that we can register methods from within the __init__ function of a non-DBOS class
99+
# This allows us to give dynamic names to the methods if needed
100+
101+
def __init__(self, name: str) -> None:
102+
self.wf_counter: int = 0
103+
self.step_counter: int = 0
104+
self.txn_counter: int = 0
105+
106+
self.name = name
107+
assert self.name is not None
108+
109+
@DBOS.step(name=f"{self.name}_test_step")
110+
def wrapped_test_step(var: str) -> str:
111+
self.step_counter += 1
112+
return var
113+
114+
@DBOS.transaction(name=f"{self.name}_test_transaction")
115+
def wrapped_test_transaction(var2: str) -> str:
116+
rows = DBOS.sql_session.execute(sa.text("SELECT 1")).fetchall()
117+
self.txn_counter += 1
118+
return var2 + str(rows[0][0])
119+
120+
@DBOS.workflow(name=f"{self.name}_test_workflow")
121+
def wrapped_test_workflow(var: str, var2: str) -> str:
122+
self.wf_counter += 1
123+
res = wrapped_test_transaction(var2)
124+
res2 = wrapped_test_step(var)
125+
return res + res2
126+
127+
self.test_workflow = wrapped_test_workflow

tests/test_singleton.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ def test_dbos_singleton(cleanup_test_databases: None) -> None:
2121

2222
# Simulate an app that does some imports of its own code, then defines DBOS,
2323
# then imports more
24-
from tests.classdefs import DBOSSendRecv, DBOSTestClass, DBOSTestRoles
24+
from tests.classdefs import (
25+
DBOSSendRecv,
26+
DBOSTestClass,
27+
DBOSTestRoles,
28+
DBOSTestWrapperMethods,
29+
)
2530

2631
dbos: DBOS = DBOS(config=default_config())
2732

@@ -53,6 +58,26 @@ def test_dbos_singleton(cleanup_test_databases: None) -> None:
5358
wfhr: WorkflowHandle[str] = DBOS.retrieve_workflow(wh.get_workflow_id())
5459
assert wfhr.workflow_id == wh.get_workflow_id()
5560

61+
# Test wrapper class methods
62+
wrapper_inst = DBOSTestWrapperMethods("dynamicconfig")
63+
res = wrapper_inst.test_workflow("x", "y")
64+
assert res == "y1x"
65+
66+
wh = DBOS.start_workflow(wrapper_inst.test_workflow, "x", "y")
67+
assert wh.get_result() == "y1x"
68+
stati = DBOS.get_workflow_status(wh.get_workflow_id())
69+
assert stati
70+
assert stati.config_name is None
71+
assert stati.class_name is None
72+
assert stati.name == "dynamicconfig_test_workflow"
73+
wfhr = DBOS.retrieve_workflow(wh.get_workflow_id())
74+
assert wfhr.workflow_id == wh.get_workflow_id()
75+
76+
stepi = DBOS.list_workflow_steps(wh.get_workflow_id())
77+
assert len(stepi) == 2
78+
assert stepi[0]["function_name"] == "dynamicconfig_test_transaction"
79+
assert stepi[1]["function_name"] == "dynamicconfig_test_step"
80+
5681
# Roles
5782

5883
with DBOSContextEnsure():

0 commit comments

Comments
 (0)