Skip to content

Commit 0153bc1

Browse files
authored
fix(testing-sdk): generate invocation id if not provided (#101)
1 parent 0c63078 commit 0153bc1

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

src/aws_durable_execution_sdk_python_testing/executor.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import logging
6+
import uuid
67
from datetime import UTC, datetime
78
from typing import TYPE_CHECKING
89

@@ -67,6 +68,21 @@ def start_execution(
6768
self,
6869
input: StartDurableExecutionInput, # noqa: A002
6970
) -> StartDurableExecutionOutput:
71+
# Generate invocation_id if not provided
72+
if input.invocation_id is None:
73+
input = StartDurableExecutionInput(
74+
account_id=input.account_id,
75+
function_name=input.function_name,
76+
function_qualifier=input.function_qualifier,
77+
execution_name=input.execution_name,
78+
execution_timeout_seconds=input.execution_timeout_seconds,
79+
execution_retention_period_days=input.execution_retention_period_days,
80+
invocation_id=str(uuid.uuid4()),
81+
trace_fields=input.trace_fields,
82+
tenant_id=input.tenant_id,
83+
input=input.input,
84+
)
85+
7086
execution = Execution.new(input=input)
7187
execution.start()
7288
self._store.save(execution)

tests/executor_test.py

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Unit tests for executor module."""
22

33
import asyncio
4+
import uuid
45
from datetime import UTC, datetime
56
from unittest.mock import Mock, patch
67

@@ -142,7 +143,26 @@ def test_start_execution(
142143
result = executor.start_execution(start_input)
143144

144145
# Test observable behavior through public API
145-
mock_execution_class.new.assert_called_once_with(input=start_input)
146+
# The executor should generate an invocation_id if not provided
147+
call_args = mock_execution_class.new.call_args
148+
actual_input = call_args.kwargs["input"]
149+
150+
# Verify all fields match except invocation_id should be generated
151+
assert actual_input.account_id == start_input.account_id
152+
assert actual_input.function_name == start_input.function_name
153+
assert actual_input.function_qualifier == start_input.function_qualifier
154+
assert actual_input.execution_name == start_input.execution_name
155+
assert (
156+
actual_input.execution_timeout_seconds == start_input.execution_timeout_seconds
157+
)
158+
assert (
159+
actual_input.execution_retention_period_days
160+
== start_input.execution_retention_period_days
161+
)
162+
assert actual_input.invocation_id is not None # Should be generated
163+
assert actual_input.trace_fields == start_input.trace_fields
164+
assert actual_input.tenant_id == start_input.tenant_id
165+
assert actual_input.input == start_input.input
146166
mock_execution.start.assert_called_once()
147167
mock_store.save.assert_called_once_with(mock_execution)
148168
mock_scheduler.create_event.assert_called_once()
@@ -157,7 +177,39 @@ def test_start_execution(
157177
mock_event.wait.assert_called_once_with(1)
158178

159179

160-
def test_get_execution(executor, mock_store):
180+
@patch("aws_durable_execution_sdk_python_testing.executor.Execution")
181+
def test_start_execution_with_provided_invocation_id(
182+
mock_execution_class, executor, mock_store, mock_scheduler
183+
):
184+
# Create input with invocation_id already provided
185+
provided_invocation_id = "user-provided-id-123"
186+
start_input = StartDurableExecutionInput(
187+
account_id="123456789012",
188+
function_name="test-function",
189+
function_qualifier="$LATEST",
190+
execution_name="test-execution",
191+
execution_timeout_seconds=300,
192+
execution_retention_period_days=7,
193+
invocation_id=provided_invocation_id,
194+
)
195+
196+
mock_execution = Mock()
197+
mock_execution.durable_execution_arn = "test-arn"
198+
mock_execution_class.new.return_value = mock_execution
199+
mock_event = Mock()
200+
mock_scheduler.create_event.return_value = mock_event
201+
202+
with patch.object(executor, "_invoke_execution") as mock_invoke:
203+
result = executor.start_execution(start_input)
204+
205+
# Should use the provided invocation_id unchanged
206+
mock_execution_class.new.assert_called_once_with(input=start_input)
207+
mock_execution.start.assert_called_once()
208+
mock_store.save.assert_called_once_with(mock_execution)
209+
mock_scheduler.create_event.assert_called_once()
210+
mock_invoke.assert_called_once_with("test-arn")
211+
assert result.execution_arn == "test-arn"
212+
161213
mock_execution = Mock()
162214
mock_store.load.return_value = mock_execution
163215

0 commit comments

Comments
 (0)