Skip to content

Commit 50bc1d4

Browse files
Rares PolenciucAstraea Quinn S
authored andcommitted
feat: support CONTEXT operations in CheckpointedResult.create_from_operation
Add handling for OperationType.CONTEXT to extract result and error from context_details. Includes test coverage for all scenarios.
1 parent 6beb550 commit 50bc1d4

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

src/aws_durable_execution_sdk_python/state.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ def create_from_operation(cls, operation: Operation) -> CheckpointedResult:
6464
result = invoke_details.result if invoke_details else None
6565
error = invoke_details.error if invoke_details else None
6666

67+
case OperationType.CONTEXT:
68+
context_details = operation.context_details
69+
result = context_details.result if context_details else None
70+
error = context_details.error if context_details else None
71+
6772
return cls(
6873
operation=operation, status=operation.status, result=result, error=error
6974
)

tests/state_test.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
ChainedInvokeDetails,
1111
CheckpointOutput,
1212
CheckpointUpdatedExecutionState,
13+
ContextDetails,
1314
ErrorObject,
1415
LambdaClient,
1516
Operation,
@@ -123,6 +124,74 @@ def test_checkpointed_result_create_from_operation_invoke_with_both_result_and_e
123124
assert result.error == error
124125

125126

127+
def test_checkpointed_result_create_from_operation_context():
128+
"""Test CheckpointedResult.create_from_operation with CONTEXT operation."""
129+
context_details = ContextDetails(result="context_result")
130+
operation = Operation(
131+
operation_id="op1",
132+
operation_type=OperationType.CONTEXT,
133+
status=OperationStatus.SUCCEEDED,
134+
context_details=context_details,
135+
)
136+
result = CheckpointedResult.create_from_operation(operation)
137+
assert result.operation == operation
138+
assert result.status == OperationStatus.SUCCEEDED
139+
assert result.result == "context_result"
140+
assert result.error is None
141+
142+
143+
def test_checkpointed_result_create_from_operation_context_with_error():
144+
"""Test CheckpointedResult.create_from_operation with CONTEXT operation and error."""
145+
error = ErrorObject(
146+
message="Context error", type="ContextError", data=None, stack_trace=None
147+
)
148+
context_details = ContextDetails(error=error)
149+
operation = Operation(
150+
operation_id="op1",
151+
operation_type=OperationType.CONTEXT,
152+
status=OperationStatus.FAILED,
153+
context_details=context_details,
154+
)
155+
result = CheckpointedResult.create_from_operation(operation)
156+
assert result.operation == operation
157+
assert result.status == OperationStatus.FAILED
158+
assert result.result is None
159+
assert result.error == error
160+
161+
162+
def test_checkpointed_result_create_from_operation_context_no_details():
163+
"""Test CheckpointedResult.create_from_operation with CONTEXT operation but no context_details."""
164+
operation = Operation(
165+
operation_id="op1",
166+
operation_type=OperationType.CONTEXT,
167+
status=OperationStatus.STARTED,
168+
)
169+
result = CheckpointedResult.create_from_operation(operation)
170+
assert result.operation == operation
171+
assert result.status == OperationStatus.STARTED
172+
assert result.result is None
173+
assert result.error is None
174+
175+
176+
def test_checkpointed_result_create_from_operation_context_with_both_result_and_error():
177+
"""Test CheckpointedResult.create_from_operation with CONTEXT operation having both result and error."""
178+
error = ErrorObject(
179+
message="Context error", type="ContextError", data=None, stack_trace=None
180+
)
181+
context_details = ContextDetails(result="context_result", error=error)
182+
operation = Operation(
183+
operation_id="op1",
184+
operation_type=OperationType.CONTEXT,
185+
status=OperationStatus.FAILED,
186+
context_details=context_details,
187+
)
188+
result = CheckpointedResult.create_from_operation(operation)
189+
assert result.operation == operation
190+
assert result.status == OperationStatus.FAILED
191+
assert result.result == "context_result"
192+
assert result.error == error
193+
194+
126195
def test_checkpointed_result_create_from_operation_unknown_type():
127196
"""Test CheckpointedResult.create_from_operation with unknown operation type."""
128197
# Create operation with a mock operation type that doesn't match any case

0 commit comments

Comments
 (0)