-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathlambda_service_test.py
More file actions
2740 lines (2213 loc) · 93.9 KB
/
Copy pathlambda_service_test.py
File metadata and controls
2740 lines (2213 loc) · 93.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Tests for the service module."""
import datetime
from datetime import UTC
from unittest.mock import Mock, patch
import pytest
from aws_durable_execution_sdk_python.__about__ import __version__
from aws_durable_execution_sdk_python.exceptions import (
CallableRuntimeError,
CheckpointError,
GetExecutionStateError,
)
from aws_durable_execution_sdk_python.identifier import OperationIdentifier
from aws_durable_execution_sdk_python.lambda_service import (
CallbackDetails,
CallbackOptions,
ChainedInvokeDetails,
ChainedInvokeOptions,
CheckpointOutput,
CheckpointUpdatedExecutionState,
ContextDetails,
ContextOptions,
DurableServiceClient,
ErrorObject,
ExecutionDetails,
LambdaClient,
Operation,
OperationAction,
OperationStatus,
OperationSubType,
OperationType,
OperationUpdate,
StateOutput,
StepDetails,
StepOptions,
TimestampConverter,
WaitDetails,
WaitOptions,
)
# =============================================================================
# Fixtures
# =============================================================================
@pytest.fixture
def reset_lambda_client_cache():
"""Reset the class-level boto3 client cache before and after each test."""
LambdaClient._cached_boto_client = None # noqa: SLF001
yield
LambdaClient._cached_boto_client = None # noqa: SLF001
# =============================================================================
# Tests for Data Classes (ExecutionDetails, ContextDetails, ErrorObject, etc.)
# =============================================================================
def test_execution_details_from_dict():
"""Test ExecutionDetails.from_dict method."""
data = {"InputPayload": "test_payload"}
details = ExecutionDetails.from_dict(data)
assert details.input_payload == "test_payload"
def test_execution_details_empty():
"""Test ExecutionDetails.from_dict with empty data."""
data = {}
details = ExecutionDetails.from_dict(data)
assert details.input_payload is None
def test_context_details_from_dict():
"""Test ContextDetails.from_dict method."""
data = {"Result": "test_result"}
details = ContextDetails.from_dict(data)
assert details.result == "test_result"
assert details.error is None
def test_context_details_with_error():
"""Test ContextDetails.from_dict with error."""
error_data = {"ErrorMessage": "Context error", "ErrorType": "ContextError"}
data = {"Result": "test_result", "Error": error_data}
details = ContextDetails.from_dict(data)
assert details.result == "test_result"
assert details.error.message == "Context error"
assert details.error.type == "ContextError"
def test_context_details_error_only():
"""Test ContextDetails.from_dict with only error."""
error_data = {"ErrorMessage": "Context failed"}
data = {"Error": error_data}
details = ContextDetails.from_dict(data)
assert details.result is None
assert details.error.message == "Context failed"
def test_context_details_empty():
"""Test ContextDetails.from_dict with empty data."""
data = {}
details = ContextDetails.from_dict(data)
assert details.replay_children is False
assert details.result is None
assert details.error is None
def test_context_details_with_replay_children():
"""Test ContextDetails.from_dict with replay_children field."""
data = {"ReplayChildren": True, "Result": "test_result"}
details = ContextDetails.from_dict(data)
assert details.replay_children is True
assert details.result == "test_result"
assert details.error is None
def test_error_object_from_dict():
"""Test ErrorObject.from_dict method."""
data = {
"ErrorMessage": "Test error",
"ErrorType": "TestError",
"ErrorData": "test_data",
"StackTrace": ["line1", "line2"],
}
error = ErrorObject.from_dict(data)
assert error.message == "Test error"
assert error.type == "TestError"
assert error.data == "test_data"
assert error.stack_trace == ["line1", "line2"]
def test_error_object_from_exception():
"""Test ErrorObject.from_exception method."""
exception = ValueError("Test value error")
error = ErrorObject.from_exception(exception)
assert error.message == "Test value error"
assert error.type == "ValueError"
assert error.data is None
assert error.stack_trace is None
def test_error_object_from_exception_runtime_error():
"""Test ErrorObject.from_exception with RuntimeError."""
runtime_error = RuntimeError("Runtime issue")
error = ErrorObject.from_exception(runtime_error)
assert error.message == "Runtime issue"
assert error.type == "RuntimeError"
assert error.data is None
assert error.stack_trace is None
def test_error_object_from_exception_custom_error():
"""Test ErrorObject.from_exception with custom exception."""
class CustomError(Exception):
pass
custom_error = CustomError("Custom message")
error = ErrorObject.from_exception(custom_error)
assert error.message == "Custom message"
assert error.type == "CustomError"
assert error.data is None
assert error.stack_trace is None
def test_error_object_from_exception_empty_message():
"""Test ErrorObject.from_exception with exception that has no message."""
empty_error = ValueError()
error = ErrorObject.from_exception(empty_error)
assert not error.message
assert error.type == "ValueError"
assert error.data is None
assert error.stack_trace is None
def test_error_object_from_message_regular():
"""Test ErrorObject.from_message with regular message."""
error = ErrorObject.from_message("Test error message")
assert error.message == "Test error message"
assert error.type is None
assert error.data is None
assert error.stack_trace is None
def test_error_object_from_message_empty():
"""Test ErrorObject.from_message with empty message."""
error = ErrorObject.from_message("")
assert not error.message
assert error.type is None
assert error.data is None
assert error.stack_trace is None
def test_error_object_to_dict():
"""Test ErrorObject.to_dict method."""
error = ErrorObject(
message="Test error",
type="TestError",
data="test_data",
stack_trace=["line1", "line2"],
)
result = error.to_dict()
expected = {
"ErrorMessage": "Test error",
"ErrorType": "TestError",
"ErrorData": "test_data",
"StackTrace": ["line1", "line2"],
}
assert result == expected
def test_error_object_to_dict_partial():
"""Test ErrorObject.to_dict with None values."""
error = ErrorObject(message="Test error", type=None, data=None, stack_trace=None)
result = error.to_dict()
assert result == {"ErrorMessage": "Test error"}
def test_error_object_to_dict_all_none():
"""Test ErrorObject.to_dict with all None values."""
error = ErrorObject(message=None, type=None, data=None, stack_trace=None)
result = error.to_dict()
assert result == {}
def test_error_object_to_callable_runtime_error():
"""Test ErrorObject.to_callable_runtime_error method."""
error = ErrorObject(
message="Test error",
type="TestError",
data="test_data",
stack_trace=["line1"],
)
runtime_error = error.to_callable_runtime_error()
assert isinstance(runtime_error, CallableRuntimeError)
assert runtime_error.message == "Test error"
assert runtime_error.error_type == "TestError"
assert runtime_error.data == "test_data"
assert runtime_error.stack_trace == ["line1"]
def test_step_details_from_dict():
"""Test StepDetails.from_dict method."""
error_data = {"ErrorMessage": "Step error"}
data = {
"Attempt": 2,
"NextAttemptTimestamp": datetime.datetime(
2023, 1, 1, 0, 0, 0, tzinfo=datetime.UTC
),
"Result": "step_result",
"Error": error_data,
}
details = StepDetails.from_dict(data)
assert details.attempt == 2
assert details.next_attempt_timestamp == datetime.datetime(
2023, 1, 1, 0, 0, 0, tzinfo=datetime.UTC
)
assert details.result == "step_result"
assert details.error.message == "Step error"
def test_step_details_all_fields():
"""Test StepDetails.from_dict with all fields."""
error_data = {"ErrorMessage": "Step failed", "ErrorType": "StepError"}
data = {
"Attempt": 3,
"NextAttemptTimestamp": datetime.datetime(
2023, 1, 1, 12, 0, 0, tzinfo=datetime.UTC
),
"Result": "step_success",
"Error": error_data,
}
details = StepDetails.from_dict(data)
assert details.attempt == 3
assert details.next_attempt_timestamp == datetime.datetime(
2023, 1, 1, 12, 0, 0, tzinfo=datetime.UTC
)
assert details.result == "step_success"
assert details.error.message == "Step failed"
assert details.error.type == "StepError"
def test_step_details_minimal():
"""Test StepDetails.from_dict with minimal data."""
data = {}
details = StepDetails.from_dict(data)
assert details.attempt == 0
assert details.next_attempt_timestamp is None
assert details.result is None
assert details.error is None
def test_wait_details_from_dict():
"""Test WaitDetails.from_dict method."""
timestamp = datetime.datetime(2023, 1, 1, 12, 0, 0, tzinfo=datetime.UTC)
data = {"ScheduledEndTimestamp": timestamp}
details = WaitDetails.from_dict(data)
assert details.scheduled_end_timestamp == timestamp
def test_wait_details_from_dict_empty():
"""Test WaitDetails.from_dict with empty data."""
data = {}
details = WaitDetails.from_dict(data)
assert details.scheduled_end_timestamp is None
def test_callback_details_from_dict():
"""Test CallbackDetails.from_dict method."""
error_data = {"ErrorMessage": "Callback error"}
data = {
"CallbackId": "cb123",
"Result": "callback_result",
"Error": error_data,
}
details = CallbackDetails.from_dict(data)
assert details.callback_id == "cb123"
assert details.result == "callback_result"
assert details.error.message == "Callback error"
def test_callback_details_all_fields():
"""Test CallbackDetails.from_dict with all fields."""
error_data = {"ErrorMessage": "Callback failed", "ErrorType": "CallbackError"}
data = {
"CallbackId": "cb456",
"Result": "callback_success",
"Error": error_data,
}
details = CallbackDetails.from_dict(data)
assert details.callback_id == "cb456"
assert details.result == "callback_success"
assert details.error.message == "Callback failed"
assert details.error.type == "CallbackError"
def test_callback_details_minimal():
"""Test CallbackDetails.from_dict with minimal required data."""
data = {"CallbackId": "cb789"}
details = CallbackDetails.from_dict(data)
assert details.callback_id == "cb789"
assert details.result is None
assert details.error is None
def test_invoke_details_from_dict():
"""Test ChainedInvokeDetails.from_dict method."""
error_data = {"ErrorMessage": "Invoke error"}
data = {
"Result": "invoke_result",
"Error": error_data,
}
details = ChainedInvokeDetails.from_dict(data)
assert details.result == "invoke_result"
assert details.error.message == "Invoke error"
def test_invoke_details_all_fields():
"""Test ChainedInvokeDetails.from_dict with all fields."""
error_data = {"ErrorMessage": "Invoke failed", "ErrorType": "InvokeError"}
data = {
"Result": "invoke_success",
"Error": error_data,
}
details = ChainedInvokeDetails.from_dict(data)
assert details.result == "invoke_success"
assert details.error.message == "Invoke failed"
assert details.error.type == "InvokeError"
def test_invoke_details_minimal():
"""Test ChainedInvokeDetails.from_dict with minimal required data."""
data = {"DurableExecutionArn": "arn:minimal"}
details = ChainedInvokeDetails.from_dict(data)
assert hasattr(details, "durable_execution_arn") is False
assert details.result is None
assert details.error is None
# =============================================================================
# Tests for Options Classes (StepOptions, WaitOptions, etc.)
# =============================================================================
def test_step_options_from_dict():
"""Test StepOptions.from_dict method."""
data = {"NextAttemptDelaySeconds": 30}
options = StepOptions.from_dict(data)
assert options.next_attempt_delay_seconds == 30
def test_step_options_from_dict_empty():
"""Test StepOptions.from_dict with empty dict."""
options = StepOptions.from_dict({})
assert options.next_attempt_delay_seconds == 0
def test_callback_options_from_dict():
"""Test CallbackOptions.from_dict method."""
data = {"TimeoutSeconds": 300, "HeartbeatTimeoutSeconds": 60}
options = CallbackOptions.from_dict(data)
assert options.timeout_seconds == 300
assert options.heartbeat_timeout_seconds == 60
def test_callback_options_from_dict_partial():
"""Test CallbackOptions.from_dict with partial data."""
data = {"TimeoutSeconds": 300}
options = CallbackOptions.from_dict(data)
assert options.timeout_seconds == 300
assert options.heartbeat_timeout_seconds == 0
def test_invoke_options_from_dict():
"""Test ChainedInvokeOptions.from_dict method."""
data = {"FunctionName": "test-function", "TenantId": "test-tenant"}
options = ChainedInvokeOptions.from_dict(data)
assert options.function_name == "test-function"
assert options.tenant_id == "test-tenant"
def test_invoke_options_from_dict_required_only():
"""Test ChainedInvokeOptions.from_dict with only required field."""
data = {"FunctionName": "test-function"}
options = ChainedInvokeOptions.from_dict(data)
assert options.function_name == "test-function"
assert options.tenant_id is None
def test_invoke_options_from_dict_with_none_tenant():
"""Test ChainedInvokeOptions.from_dict with explicit None tenant_id."""
data = {"FunctionName": "test-function", "TenantId": None}
options = ChainedInvokeOptions.from_dict(data)
assert options.function_name == "test-function"
assert options.tenant_id is None
def test_context_options_from_dict():
"""Test ContextOptions.from_dict method."""
data = {"ReplayChildren": True}
options = ContextOptions.from_dict(data)
assert options.replay_children is True
def test_context_options_from_dict_empty():
"""Test ContextOptions.from_dict with empty dict."""
options = ContextOptions.from_dict({})
assert options.replay_children is False
def test_step_options_roundtrip():
"""Test StepOptions to_dict -> from_dict roundtrip."""
original = StepOptions(next_attempt_delay_seconds=45)
data = original.to_dict()
restored = StepOptions.from_dict(data)
assert restored == original
def test_callback_options_roundtrip():
"""Test CallbackOptions to_dict -> from_dict roundtrip."""
original = CallbackOptions(timeout_seconds=300, heartbeat_timeout_seconds=60)
data = original.to_dict()
restored = CallbackOptions.from_dict(data)
assert restored == original
def test_invoke_options_roundtrip():
"""Test ChainedInvokeOptions to_dict -> from_dict roundtrip."""
original = ChainedInvokeOptions(function_name="test-func")
data = original.to_dict()
restored = ChainedInvokeOptions.from_dict(data)
assert restored == original
def test_context_options_roundtrip():
"""Test ContextOptions to_dict -> from_dict roundtrip."""
original = ContextOptions(replay_children=True)
data = original.to_dict()
restored = ContextOptions.from_dict(data)
assert restored == original
def test_wait_options_from_dict():
"""Test WaitOptions.from_dict method."""
data = {"WaitSeconds": 30}
options = WaitOptions.from_dict(data)
assert options.wait_seconds == 30
def test_step_options_to_dict():
"""Test StepOptions.to_dict method."""
options = StepOptions(next_attempt_delay_seconds=30)
result = options.to_dict()
assert result == {"NextAttemptDelaySeconds": 30}
def test_wait_options_to_dict():
"""Test WaitOptions.to_dict method."""
options = WaitOptions(wait_seconds=60)
result = options.to_dict()
assert result == {"WaitSeconds": 60}
def test_callback_options_to_dict():
"""Test CallbackOptions.to_dict method."""
options = CallbackOptions(timeout_seconds=300, heartbeat_timeout_seconds=60)
result = options.to_dict()
assert result == {"TimeoutSeconds": 300, "HeartbeatTimeoutSeconds": 60}
def test_callback_options_all_fields():
"""Test CallbackOptions with all fields."""
options = CallbackOptions(timeout_seconds=300, heartbeat_timeout_seconds=60)
result = options.to_dict()
assert result["TimeoutSeconds"] == 300
assert result["HeartbeatTimeoutSeconds"] == 60
def test_invoke_options_to_dict():
"""Test ChainedInvokeOptions.to_dict method."""
options = ChainedInvokeOptions(
function_name="test_function",
)
result = options.to_dict()
expected = {
"FunctionName": "test_function",
}
assert result == expected
def test_invoke_options_to_dict_minimal():
"""Test ChainedInvokeOptions.to_dict with minimal fields."""
options = ChainedInvokeOptions(function_name="test_function")
result = options.to_dict()
assert result == {"FunctionName": "test_function"}
def test_context_options_to_dict():
"""Test ContextOptions.to_dict method."""
options = ContextOptions(replay_children=True)
result = options.to_dict()
assert result == {"ReplayChildren": True}
def test_context_options_to_dict_default():
"""Test ContextOptions.to_dict with default value."""
options = ContextOptions()
result = options.to_dict()
assert result == {"ReplayChildren": False}
def test_context_options_to_dict_false():
"""Test ContextOptions.to_dict with replay_children=False."""
options = ContextOptions(replay_children=False)
result = options.to_dict()
assert result == {"ReplayChildren": False}
def test_invoke_options_from_dict_missing_function_name():
"""Test ChainedInvokeOptions.from_dict with missing required FunctionName."""
data = {"TimeoutSeconds": 60}
with pytest.raises(KeyError):
ChainedInvokeOptions.from_dict(data)
def test_invoke_options_to_dict_complete():
"""Test ChainedInvokeOptions.to_dict with all fields."""
options = ChainedInvokeOptions(function_name="test_func")
result = options.to_dict()
assert result["FunctionName"] == "test_func"
# =============================================================================
# Tests for OperationUpdate Class
# =============================================================================
def test_operation_update_create_invoke_start():
"""Test OperationUpdate.create_invoke_start method to cover line 545."""
identifier = OperationIdentifier("test-id", "parent-id")
invoke_options = ChainedInvokeOptions("test-func")
update = OperationUpdate.create_invoke_start(identifier, "payload", invoke_options)
assert update.operation_id == "test-id"
def test_operation_update_to_dict():
"""Test OperationUpdate.to_dict method."""
error = ErrorObject(
message="Test error", type="TestError", data=None, stack_trace=None
)
step_options = StepOptions(next_attempt_delay_seconds=30)
update = OperationUpdate(
operation_id="op1",
operation_type=OperationType.STEP,
action=OperationAction.RETRY,
parent_id="parent1",
name="test_step",
payload="test_payload",
error=error,
step_options=step_options,
)
result = update.to_dict()
expected = {
"Id": "op1",
"Type": "STEP",
"Action": "RETRY",
"ParentId": "parent1",
"Name": "test_step",
"Payload": "test_payload",
"Error": {"ErrorMessage": "Test error", "ErrorType": "TestError"},
"StepOptions": {"NextAttemptDelaySeconds": 30},
}
assert result == expected
def test_operation_update_to_dict_complete():
"""Test OperationUpdate.to_dict with all optional fields."""
error = ErrorObject(
message="Test error", type="TestError", data=None, stack_trace=None
)
step_options = StepOptions(next_attempt_delay_seconds=30)
wait_options = WaitOptions(wait_seconds=60)
callback_options = CallbackOptions(
timeout_seconds=300, heartbeat_timeout_seconds=60
)
chained_invoke_options = ChainedInvokeOptions(function_name="test_func")
update = OperationUpdate(
operation_id="op1",
operation_type=OperationType.STEP,
action=OperationAction.RETRY,
parent_id="parent1",
name="test_step",
payload="test_payload",
error=error,
step_options=step_options,
wait_options=wait_options,
callback_options=callback_options,
chained_invoke_options=chained_invoke_options,
)
result = update.to_dict()
expected = {
"Id": "op1",
"Type": "STEP",
"Action": "RETRY",
"ParentId": "parent1",
"Name": "test_step",
"Payload": "test_payload",
"Error": {"ErrorMessage": "Test error", "ErrorType": "TestError"},
"StepOptions": {"NextAttemptDelaySeconds": 30},
"WaitOptions": {"WaitSeconds": 60},
"CallbackOptions": {"TimeoutSeconds": 300, "HeartbeatTimeoutSeconds": 60},
"ChainedInvokeOptions": {"FunctionName": "test_func"},
}
assert result == expected
def test_operation_update_minimal():
"""Test OperationUpdate.to_dict with minimal required fields."""
update = OperationUpdate(
operation_id="minimal_op",
operation_type=OperationType.EXECUTION,
action=OperationAction.START,
)
result = update.to_dict()
expected = {
"Id": "minimal_op",
"Type": "EXECUTION",
"Action": "START",
}
assert result == expected
def test_operation_update_create_callback():
"""Test OperationUpdate.create_callback factory method."""
callback_options = CallbackOptions(timeout_seconds=300)
update = OperationUpdate.create_callback(
OperationIdentifier("cb1", None, "test_callback"), callback_options
)
assert update.operation_id == "cb1"
assert update.operation_type is OperationType.CALLBACK
assert update.action is OperationAction.START
assert update.name == "test_callback"
assert update.callback_options == callback_options
assert update.sub_type is OperationSubType.CALLBACK
def test_operation_update_create_wait_start():
"""Test OperationUpdate.create_wait_start factory method."""
wait_options = WaitOptions(wait_seconds=30)
update = OperationUpdate.create_wait_start(
OperationIdentifier("wait1", "parent1", "test_wait"), wait_options
)
assert update.operation_id == "wait1"
assert update.parent_id == "parent1"
assert update.operation_type is OperationType.WAIT
assert update.action is OperationAction.START
assert update.name == "test_wait"
assert update.wait_options == wait_options
assert update.sub_type is OperationSubType.WAIT
@patch("aws_durable_execution_sdk_python.lambda_service.datetime")
def test_operation_update_create_execution_succeed(mock_datetime):
"""Test OperationUpdate.create_execution_succeed factory method."""
mock_datetime.datetime.now.return_value = datetime.datetime.fromtimestamp(
1672531200.0, tz=datetime.UTC
)
update = OperationUpdate.create_execution_succeed("success_payload")
assert update.operation_id == "execution-result-1672531200000"
assert update.operation_type == OperationType.EXECUTION
assert update.action == OperationAction.SUCCEED
assert update.payload == "success_payload"
def test_operation_update_create_step_succeed():
"""Test OperationUpdate.create_step_succeed factory method."""
update = OperationUpdate.create_step_succeed(
OperationIdentifier("step1", None, "test_step"), "step_payload"
)
assert update.operation_id == "step1"
assert update.operation_type is OperationType.STEP
assert update.action is OperationAction.SUCCEED
assert update.name == "test_step"
assert update.payload == "step_payload"
assert update.sub_type is OperationSubType.STEP
def test_operation_update_factory_methods():
"""Test all OperationUpdate factory methods."""
error = ErrorObject(
message="Test error", type="TestError", data=None, stack_trace=None
)
# Test create_context_start
update = OperationUpdate.create_context_start(
OperationIdentifier("ctx1", None, "test_context"),
OperationSubType.RUN_IN_CHILD_CONTEXT,
)
assert update.operation_type is OperationType.CONTEXT
assert update.action is OperationAction.START
assert update.sub_type is OperationSubType.RUN_IN_CHILD_CONTEXT
# Test create_context_succeed
update = OperationUpdate.create_context_succeed(
OperationIdentifier("ctx1", None, "test_context"),
"payload",
OperationSubType.RUN_IN_CHILD_CONTEXT,
)
assert update.action is OperationAction.SUCCEED
assert update.payload == "payload"
assert update.sub_type is OperationSubType.RUN_IN_CHILD_CONTEXT
# Test create_context_fail
update = OperationUpdate.create_context_fail(
OperationIdentifier("ctx1", None, "test_context"),
error,
OperationSubType.RUN_IN_CHILD_CONTEXT,
)
assert update.action is OperationAction.FAIL
assert update.error == error
assert update.sub_type is OperationSubType.RUN_IN_CHILD_CONTEXT
# Test create_execution_fail
update = OperationUpdate.create_execution_fail(error)
assert update.operation_type is OperationType.EXECUTION
assert update.action is OperationAction.FAIL
# Test create_step_fail
update = OperationUpdate.create_step_fail(
OperationIdentifier("step1", None, "test_step"), error
)
assert update.operation_type is OperationType.STEP
assert update.action is OperationAction.FAIL
assert update.sub_type is OperationSubType.STEP
# Test create_step_start
update = OperationUpdate.create_step_start(
OperationIdentifier("step1", None, "test_step")
)
assert update.action is OperationAction.START
assert update.sub_type is OperationSubType.STEP
# Test create_step_retry
update = OperationUpdate.create_step_retry(
OperationIdentifier("step1", None, "test_step"), error, 30
)
assert update.action is OperationAction.RETRY
assert update.step_options.next_attempt_delay_seconds == 30
assert update.sub_type is OperationSubType.STEP
def test_operation_update_with_parent_id():
"""Test OperationUpdate with parent_id field."""
update = OperationUpdate(
operation_id="child_op",
operation_type=OperationType.STEP,
action=OperationAction.START,
parent_id="parent_op",
name="child_step",
)
result = update.to_dict()
assert result["ParentId"] == "parent_op"
def test_operation_update_wait_and_invoke_types():
"""Test OperationUpdate with WAIT and INVOKE operation types."""
# Test WAIT operation
wait_options = WaitOptions(wait_seconds=30)
wait_update = OperationUpdate(
operation_id="wait_op",
operation_type=OperationType.WAIT,
action=OperationAction.START,
wait_options=wait_options,
)
result = wait_update.to_dict()
assert result["Type"] == "WAIT"
assert result["WaitOptions"]["WaitSeconds"] == 30
# Test INVOKE operation
chained_invoke_options = ChainedInvokeOptions(function_name="test_func")
invoke_update = OperationUpdate(
operation_id="invoke_op",
operation_type=OperationType.CHAINED_INVOKE,
action=OperationAction.START,
chained_invoke_options=chained_invoke_options,
)
result = invoke_update.to_dict()
assert result["Type"] == "CHAINED_INVOKE"
assert result["ChainedInvokeOptions"]["FunctionName"] == "test_func"
def test_operation_update_create_wait():
"""Test OperationUpdate factory method for WAIT operations."""
wait_options = WaitOptions(wait_seconds=30)
update = OperationUpdate(
operation_id="wait1",
operation_type=OperationType.WAIT,
action=OperationAction.START,
wait_options=wait_options,
)
assert update.operation_type == OperationType.WAIT
assert update.wait_options == wait_options
def test_operation_update_create_invoke():
"""Test OperationUpdate factory method for INVOKE operations."""
chained_invoke_options = ChainedInvokeOptions(function_name="test-function")
update = OperationUpdate(
operation_id="invoke1",
operation_type=OperationType.CHAINED_INVOKE,
action=OperationAction.START,
chained_invoke_options=chained_invoke_options,
)
assert update.operation_type == OperationType.CHAINED_INVOKE
assert update.chained_invoke_options == chained_invoke_options
def test_operation_update_with_sub_type():
"""Test OperationUpdate with sub_type field."""
update = OperationUpdate(
operation_id="op1",
operation_type=OperationType.STEP,
action=OperationAction.START,
sub_type=OperationSubType.STEP,
)
result = update.to_dict()
assert result["SubType"] == "Step"
def test_operation_update_with_context_options():
"""Test OperationUpdate with context_options field."""
context_options = ContextOptions(replay_children=True)
update = OperationUpdate(
operation_id="op1",
operation_type=OperationType.CONTEXT,
action=OperationAction.START,
context_options=context_options,
)
result = update.to_dict()
assert result["ContextOptions"] == {"ReplayChildren": True}
def test_operation_update_complete_with_new_fields():
"""Test OperationUpdate.to_dict with all fields including new ones."""
error = ErrorObject(
message="Test error", type="TestError", data=None, stack_trace=None
)
context_options = ContextOptions(replay_children=True)
step_options = StepOptions(next_attempt_delay_seconds=30)
wait_options = WaitOptions(wait_seconds=60)
callback_options = CallbackOptions(
timeout_seconds=300, heartbeat_timeout_seconds=60
)
chained_invoke_options = ChainedInvokeOptions(function_name="test_func")
update = OperationUpdate(
operation_id="op1",
operation_type=OperationType.CONTEXT,
action=OperationAction.RETRY,
parent_id="parent1",
name="test_context",
sub_type=OperationSubType.RUN_IN_CHILD_CONTEXT,
payload="test_payload",
error=error,
context_options=context_options,
step_options=step_options,
wait_options=wait_options,
callback_options=callback_options,
chained_invoke_options=chained_invoke_options,
)
result = update.to_dict()
expected = {
"Id": "op1",
"Type": "CONTEXT",
"Action": "RETRY",
"ParentId": "parent1",
"Name": "test_context",
"SubType": "RunInChildContext",
"Payload": "test_payload",
"Error": {"ErrorMessage": "Test error", "ErrorType": "TestError"},
"ContextOptions": {"ReplayChildren": True},
"StepOptions": {"NextAttemptDelaySeconds": 30},
"WaitOptions": {"WaitSeconds": 60},
"CallbackOptions": {"TimeoutSeconds": 300, "HeartbeatTimeoutSeconds": 60},
"ChainedInvokeOptions": {"FunctionName": "test_func"},
}
assert result == expected
# =============================================================================
# Tests for new wait-for-condition factory methods
# =============================================================================
def test_operation_update_create_wait_for_condition_start():
"""Test OperationUpdate.create_wait_for_condition_start factory method."""
identifier = OperationIdentifier("wait_cond_1", "parent1", "test_wait_condition")
update = OperationUpdate.create_wait_for_condition_start(identifier)
assert update.operation_id == "wait_cond_1"
assert update.parent_id == "parent1"
assert update.operation_type == OperationType.STEP
assert update.sub_type == OperationSubType.WAIT_FOR_CONDITION
assert update.action == OperationAction.START
assert update.name == "test_wait_condition"
def test_operation_update_create_wait_for_condition_succeed():
"""Test OperationUpdate.create_wait_for_condition_succeed factory method."""
identifier = OperationIdentifier("wait_cond_1", "parent1", "test_wait_condition")
update = OperationUpdate.create_wait_for_condition_succeed(
identifier, "success_payload"
)
assert update.operation_id == "wait_cond_1"
assert update.parent_id == "parent1"
assert update.operation_type == OperationType.STEP
assert update.sub_type == OperationSubType.WAIT_FOR_CONDITION
assert update.action == OperationAction.SUCCEED
assert update.name == "test_wait_condition"
assert update.payload == "success_payload"
def test_operation_update_create_wait_for_condition_retry():
"""Test OperationUpdate.create_wait_for_condition_retry factory method."""
identifier = OperationIdentifier("wait_cond_1", "parent1", "test_wait_condition")
update = OperationUpdate.create_wait_for_condition_retry(
identifier, "retry_payload", 45
)
assert update.operation_id == "wait_cond_1"
assert update.parent_id == "parent1"
assert update.operation_type == OperationType.STEP
assert update.sub_type == OperationSubType.WAIT_FOR_CONDITION
assert update.action == OperationAction.RETRY
assert update.name == "test_wait_condition"
assert update.payload == "retry_payload"
assert update.step_options.next_attempt_delay_seconds == 45
def test_operation_update_create_wait_for_condition_fail():
"""Test OperationUpdate.create_wait_for_condition_fail factory method."""
identifier = OperationIdentifier("wait_cond_1", "parent1", "test_wait_condition")