33# This product includes software developed at Datadog (https://www.datadoghq.com/).
44# Copyright 2019 Datadog, Inc.
55import unittest
6- from unittest .mock import MagicMock
76
87from datadog_lambda .durable import (
98 _parse_durable_execution_arn ,
@@ -46,31 +45,31 @@ def test_works_with_numeric_version_qualifier(self):
4645
4746
4847class TestExtractDurableFunctionTags (unittest .TestCase ):
49- def test_extracts_tags_from_event_with_durable_execution_arn (self ):
48+ def test_sets_first_invocation_true_when_only_execution_operation (self ):
49+ # One operation (the EXECUTION op itself) → not replaying → first invocation
5050 event = {
5151 "DurableExecutionArn" : "arn:aws:lambda:us-east-1:123456789012:function:my-func:1/durable-execution/my-execution/550e8400-e29b-41d4-a716-446655440004" ,
5252 "CheckpointToken" : "some-token" ,
53- "InitialExecutionState" : {"Operations" : []},
53+ "InitialExecutionState" : {"Operations" : [{ "OperationType" : "EXECUTION" } ]},
5454 }
55- state = MagicMock ()
56- state .is_replaying .return_value = True
57- result = extract_durable_function_tags (event , state )
55+ result = extract_durable_function_tags (event )
5856 self .assertEqual (
5957 result ,
6058 {
6159 "durable_function_execution_name" : "my-execution" ,
6260 "durable_function_execution_id" : "550e8400-e29b-41d4-a716-446655440004" ,
63- "durable_function_first_invocation" : "false " ,
61+ "durable_function_first_invocation" : "true " ,
6462 },
6563 )
6664
67- def test_sets_first_invocation_true_when_not_replaying (self ):
65+ def test_sets_first_invocation_true_when_no_operations (self ):
66+ # Empty operations list → not replaying → first invocation
6867 event = {
6968 "DurableExecutionArn" : "arn:aws:lambda:us-east-1:123456789012:function:my-func:1/durable-execution/my-execution/550e8400-e29b-41d4-a716-446655440004" ,
69+ "CheckpointToken" : "some-token" ,
70+ "InitialExecutionState" : {"Operations" : []},
7071 }
71- state = MagicMock ()
72- state .is_replaying .return_value = False
73- result = extract_durable_function_tags (event , state )
72+ result = extract_durable_function_tags (event )
7473 self .assertEqual (
7574 result ,
7675 {
@@ -80,13 +79,19 @@ def test_sets_first_invocation_true_when_not_replaying(self):
8079 },
8180 )
8281
83- def test_sets_first_invocation_false_when_replaying (self ):
82+ def test_sets_first_invocation_false_when_multiple_operations (self ):
83+ # More than one operation → replaying → not first invocation
8484 event = {
8585 "DurableExecutionArn" : "arn:aws:lambda:us-east-1:123456789012:function:my-func:1/durable-execution/my-execution/550e8400-e29b-41d4-a716-446655440004" ,
86+ "CheckpointToken" : "some-token" ,
87+ "InitialExecutionState" : {
88+ "Operations" : [
89+ {"OperationType" : "EXECUTION" },
90+ {"OperationType" : "STEP" },
91+ ]
92+ },
8693 }
87- state = MagicMock ()
88- state .is_replaying .return_value = True
89- result = extract_durable_function_tags (event , state )
94+ result = extract_durable_function_tags (event )
9095 self .assertEqual (
9196 result ,
9297 {
@@ -96,6 +101,22 @@ def test_sets_first_invocation_false_when_replaying(self):
96101 },
97102 )
98103
104+ def test_sets_first_invocation_true_when_initial_execution_state_absent (self ):
105+ # No InitialExecutionState key → treated as empty → first invocation
106+ event = {
107+ "DurableExecutionArn" : "arn:aws:lambda:us-east-1:123456789012:function:my-func:1/durable-execution/my-execution/550e8400-e29b-41d4-a716-446655440004" ,
108+ "CheckpointToken" : "some-token" ,
109+ }
110+ result = extract_durable_function_tags (event )
111+ self .assertEqual (
112+ result ,
113+ {
114+ "durable_function_execution_name" : "my-execution" ,
115+ "durable_function_execution_id" : "550e8400-e29b-41d4-a716-446655440004" ,
116+ "durable_function_first_invocation" : "true" ,
117+ },
118+ )
119+
99120 def test_returns_empty_dict_for_regular_lambda_event (self ):
100121 event = {
101122 "body" : '{"key": "value"}' ,
0 commit comments