Skip to content

Commit 6760a0d

Browse files
committed
IWF-464: Add test complexity
1 parent d92495d commit 6760a0d

File tree

1 file changed

+72
-8
lines changed

1 file changed

+72
-8
lines changed

iwf/tests/test_persistence_search_attributes.py

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import inspect
22
import time
33
import unittest
4+
from time import sleep
45

56
from iwf.client import Client
67
from iwf.command_request import CommandRequest
@@ -14,26 +15,31 @@
1415
from iwf.tests.worker_server import registry
1516
from iwf.workflow import ObjectWorkflow
1617
from iwf.workflow_context import WorkflowContext
18+
from iwf.workflow_options import WorkflowOptions
1719
from iwf.workflow_state import T, WorkflowState
1820

1921
sa_keyword_key = "CustomKeywordField"
20-
sa_text_key = "CustomTextField"
2122
sa_double_key = "CustomDoubleField"
2223
sa_int_key = "CustomIntField"
2324
sa_bool_key = "CustomBoolField"
2425
sa_datetime_key = "CustomDatetimeField"
2526
sa_keyword_array_key = "CustomKeywordArrayField"
2627

2728
sa_keyword: str = "keyword"
28-
sa_text: str = "text"
2929
sa_double: float = 2.34
3030
sa_int: int = 234
3131
sa_bool: bool = False
32-
sa_datetime: str = "2024-12-13T16:00:01.731455544-08:00"
32+
sa_datetime: str = "2024-11-12T16:00:01.731455544-08:00"
3333
sa_keyword_array: list[str] = ["keyword-1", "keyword-2"]
3434

35+
final_sa_keyword: str = "final_keyword"
36+
final_sa_int: int = 567
37+
final_sa_bool: bool = False
38+
final_sa_datetime: str = "2024-12-13T16:00:01.731455544-08:00"
39+
final_sa_keyword_array: list[str] = ["final_keyword-1", "final_keyword-2"]
3540

36-
class SearchAttributeState(WorkflowState[None]):
41+
42+
class SearchAttributeState1(WorkflowState[None]):
3743
def wait_until(
3844
self,
3945
ctx: WorkflowContext,
@@ -59,12 +65,44 @@ def execute(
5965
)
6066
persistence.set_search_attribute_int64(sa_int_key, sa_int)
6167
persistence.set_search_attribute_datetime(sa_datetime_key, sa_datetime)
68+
return StateDecision.single_next_state(SearchAttributeState2)
69+
70+
71+
class SearchAttributeState2(WorkflowState[None]):
72+
def wait_until(
73+
self,
74+
ctx: WorkflowContext,
75+
input: T,
76+
persistence: Persistence,
77+
communication: Communication,
78+
) -> CommandRequest:
79+
return CommandRequest.empty()
80+
81+
def execute(
82+
self,
83+
ctx: WorkflowContext,
84+
input: T,
85+
command_results: CommandResults,
86+
persistence: Persistence,
87+
communication: Communication,
88+
) -> StateDecision:
89+
# Delay updating search attributes to allow for the first assertion
90+
sleep(1)
91+
persistence.set_search_attribute_keyword(sa_keyword_key, final_sa_keyword)
92+
persistence.set_search_attribute_boolean(sa_bool_key, final_sa_bool)
93+
persistence.set_search_attribute_keyword_array(
94+
sa_keyword_array_key, final_sa_keyword_array
95+
)
96+
persistence.set_search_attribute_int64(sa_int_key, final_sa_int)
97+
persistence.set_search_attribute_datetime(sa_datetime_key, final_sa_datetime)
6298
return StateDecision.graceful_complete_workflow()
6399

64100

65101
class PersistenceSearchAttributesWorkflow(ObjectWorkflow):
66102
def get_workflow_states(self) -> StateSchema:
67-
return StateSchema.with_starting_state(SearchAttributeState())
103+
return StateSchema.with_starting_state(
104+
SearchAttributeState1(), SearchAttributeState2()
105+
)
68106

69107
def get_persistence_schema(self) -> PersistenceSchema:
70108
return PersistenceSchema.create(
@@ -99,9 +137,15 @@ def setUpClass(cls):
99137
def test_persistence_search_attributes_workflow(self):
100138
wf_id = f"{inspect.currentframe().f_code.co_name}-{time.time_ns()}"
101139

102-
self.client.start_workflow(PersistenceSearchAttributesWorkflow, wf_id, 100)
140+
wf_opts = WorkflowOptions()
141+
wf_opts.add_wait_for_completion_state_ids(SearchAttributeState1)
142+
self.client.start_workflow(
143+
PersistenceSearchAttributesWorkflow, wf_id, 100, None, wf_opts
144+
)
103145

104-
self.client.wait_for_workflow_completion(wf_id)
146+
self.client.wait_for_state_execution_completion_with_state_execution_id(
147+
SearchAttributeState1, wf_id
148+
)
105149

106150
returned_search_attributes = self.client.get_all_search_attributes(
107151
PersistenceSearchAttributesWorkflow,
@@ -115,8 +159,28 @@ def test_persistence_search_attributes_workflow(self):
115159
expected_search_attributes[sa_keyword_array_key] = sa_keyword_array
116160
expected_search_attributes[sa_int_key] = sa_int
117161
expected_search_attributes[sa_datetime_key] = (
162+
"2024-11-13T00:00:01.731455544Z" # This is a bug. The iwf-server always returns utc time. See https://github.com/indeedeng/iwf/issues/261
163+
# "2024-11-12T18:00:01.731455544-06:00"
164+
)
165+
166+
assert expected_search_attributes == returned_search_attributes
167+
168+
self.client.wait_for_workflow_completion(wf_id)
169+
170+
final_expected_search_attributes = dict()
171+
final_expected_search_attributes[sa_keyword_key] = final_sa_keyword
172+
final_expected_search_attributes[sa_double_key] = sa_double
173+
final_expected_search_attributes[sa_int_key] = final_sa_int
174+
final_expected_search_attributes[sa_bool_key] = final_sa_bool
175+
final_expected_search_attributes[sa_keyword_array_key] = final_sa_keyword_array
176+
final_expected_search_attributes[sa_datetime_key] = (
118177
"2024-12-14T00:00:01.731455544Z" # This is a bug. The iwf-server always returns utc time. See https://github.com/indeedeng/iwf/issues/261
119178
# "2024-12-13T18:00:01.731455544-06:00"
120179
)
121180

122-
assert expected_search_attributes == returned_search_attributes
181+
final_returned_search_attributes = self.client.get_all_search_attributes(
182+
PersistenceSearchAttributesWorkflow,
183+
wf_id,
184+
)
185+
186+
assert final_expected_search_attributes == final_returned_search_attributes

0 commit comments

Comments
 (0)