@@ -15,6 +15,8 @@ import {
15
15
IdempotencyPersistenceLayerError ,
16
16
} from '../../src/Exceptions' ;
17
17
import { IdempotencyConfig } from '../../src' ;
18
+ import { Context } from 'aws-lambda' ;
19
+ import { helloworldContext } from '@aws-lambda-powertools/commons/lib/samples/resources/contexts' ;
18
20
19
21
const mockSaveInProgress = jest
20
22
. spyOn ( BasePersistenceLayer . prototype , 'saveInProgress' )
@@ -26,6 +28,10 @@ const mockGetRecord = jest
26
28
. spyOn ( BasePersistenceLayer . prototype , 'getRecord' )
27
29
. mockImplementation ( ) ;
28
30
31
+ const dummyContext = helloworldContext ;
32
+
33
+ const mockConfig : IdempotencyConfig = new IdempotencyConfig ( { } ) ;
34
+
29
35
class PersistenceLayerTestClass extends BasePersistenceLayer {
30
36
protected _deleteRecord = jest . fn ( ) ;
31
37
protected _getRecord = jest . fn ( ) ;
@@ -39,21 +45,25 @@ class TestinClassWithLambdaHandler {
39
45
@idempotentLambdaHandler ( {
40
46
persistenceStore : new PersistenceLayerTestClass ( ) ,
41
47
} )
42
- public testing ( record : Record < string , unknown > ) : string {
48
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
49
+ public testing ( record : Record < string , unknown > , context : Context ) : string {
43
50
functionalityToDecorate ( record ) ;
44
51
45
52
return 'Hi' ;
46
53
}
47
54
}
48
55
49
56
class TestingClassWithFunctionDecorator {
50
- public handler ( record : Record < string , unknown > ) : string {
57
+ public handler ( record : Record < string , unknown > , context : Context ) : string {
58
+ mockConfig . registerLambdaContext ( context ) ;
59
+
51
60
return this . proccessRecord ( record ) ;
52
61
}
53
62
54
63
@idempotentFunction ( {
55
64
persistenceStore : new PersistenceLayerTestClass ( ) ,
56
65
dataKeywordArgument : 'testingKey' ,
66
+ config : mockConfig ,
57
67
} )
58
68
public proccessRecord ( record : Record < string , unknown > ) : string {
59
69
functionalityToDecorate ( record ) ;
@@ -72,11 +82,14 @@ describe('Given a class with a function to decorate', (classWithLambdaHandler =
72
82
73
83
describe ( 'When wrapping a function with no previous executions' , ( ) => {
74
84
beforeEach ( async ( ) => {
75
- await classWithFunctionDecorator . handler ( inputRecord ) ;
85
+ await classWithFunctionDecorator . handler ( inputRecord , dummyContext ) ;
76
86
} ) ;
77
87
78
88
test ( 'Then it will save the record to INPROGRESS' , ( ) => {
79
- expect ( mockSaveInProgress ) . toBeCalledWith ( keyValueToBeSaved ) ;
89
+ expect ( mockSaveInProgress ) . toBeCalledWith (
90
+ keyValueToBeSaved ,
91
+ dummyContext . getRemainingTimeInMillis ( )
92
+ ) ;
80
93
} ) ;
81
94
82
95
test ( 'Then it will call the function that was decorated' , ( ) => {
@@ -92,11 +105,14 @@ describe('Given a class with a function to decorate', (classWithLambdaHandler =
92
105
} ) ;
93
106
describe ( 'When wrapping a function with no previous executions' , ( ) => {
94
107
beforeEach ( async ( ) => {
95
- await classWithLambdaHandler . testing ( inputRecord ) ;
108
+ await classWithLambdaHandler . testing ( inputRecord , dummyContext ) ;
96
109
} ) ;
97
110
98
111
test ( 'Then it will save the record to INPROGRESS' , ( ) => {
99
- expect ( mockSaveInProgress ) . toBeCalledWith ( inputRecord ) ;
112
+ expect ( mockSaveInProgress ) . toBeCalledWith (
113
+ inputRecord ,
114
+ dummyContext . getRemainingTimeInMillis ( )
115
+ ) ;
100
116
} ) ;
101
117
102
118
test ( 'Then it will call the function that was decorated' , ( ) => {
@@ -122,14 +138,17 @@ describe('Given a class with a function to decorate', (classWithLambdaHandler =
122
138
new IdempotencyRecord ( idempotencyOptions )
123
139
) ;
124
140
try {
125
- await classWithLambdaHandler . testing ( inputRecord ) ;
141
+ await classWithLambdaHandler . testing ( inputRecord , dummyContext ) ;
126
142
} catch ( e ) {
127
143
resultingError = e as Error ;
128
144
}
129
145
} ) ;
130
146
131
147
test ( 'Then it will attempt to save the record to INPROGRESS' , ( ) => {
132
- expect ( mockSaveInProgress ) . toBeCalledWith ( inputRecord ) ;
148
+ expect ( mockSaveInProgress ) . toBeCalledWith (
149
+ inputRecord ,
150
+ dummyContext . getRemainingTimeInMillis ( )
151
+ ) ;
133
152
} ) ;
134
153
135
154
test ( 'Then it will get the previous execution record' , ( ) => {
@@ -159,14 +178,17 @@ describe('Given a class with a function to decorate', (classWithLambdaHandler =
159
178
new IdempotencyRecord ( idempotencyOptions )
160
179
) ;
161
180
try {
162
- await classWithLambdaHandler . testing ( inputRecord ) ;
181
+ await classWithLambdaHandler . testing ( inputRecord , dummyContext ) ;
163
182
} catch ( e ) {
164
183
resultingError = e as Error ;
165
184
}
166
185
} ) ;
167
186
168
187
test ( 'Then it will attempt to save the record to INPROGRESS' , ( ) => {
169
- expect ( mockSaveInProgress ) . toBeCalledWith ( inputRecord ) ;
188
+ expect ( mockSaveInProgress ) . toBeCalledWith (
189
+ inputRecord ,
190
+ dummyContext . getRemainingTimeInMillis ( )
191
+ ) ;
170
192
} ) ;
171
193
172
194
test ( 'Then it will get the previous execution record' , ( ) => {
@@ -195,11 +217,14 @@ describe('Given a class with a function to decorate', (classWithLambdaHandler =
195
217
mockGetRecord . mockResolvedValue (
196
218
new IdempotencyRecord ( idempotencyOptions )
197
219
) ;
198
- await classWithLambdaHandler . testing ( inputRecord ) ;
220
+ await classWithLambdaHandler . testing ( inputRecord , dummyContext ) ;
199
221
} ) ;
200
222
201
223
test ( 'Then it will attempt to save the record to INPROGRESS' , ( ) => {
202
- expect ( mockSaveInProgress ) . toBeCalledWith ( inputRecord ) ;
224
+ expect ( mockSaveInProgress ) . toBeCalledWith (
225
+ inputRecord ,
226
+ dummyContext . getRemainingTimeInMillis ( )
227
+ ) ;
203
228
} ) ;
204
229
205
230
test ( 'Then it will get the previous execution record' , ( ) => {
@@ -215,7 +240,7 @@ describe('Given a class with a function to decorate', (classWithLambdaHandler =
215
240
class TestinClassWithLambdaHandlerWithConfig {
216
241
@idempotentLambdaHandler ( {
217
242
persistenceStore : new PersistenceLayerTestClass ( ) ,
218
- config : new IdempotencyConfig ( { } ) ,
243
+ config : new IdempotencyConfig ( { lambdaContext : dummyContext } ) ,
219
244
} )
220
245
public testing ( record : Record < string , unknown > ) : string {
221
246
functionalityToDecorate ( record ) ;
@@ -237,7 +262,10 @@ describe('Given a class with a function to decorate', (classWithLambdaHandler =
237
262
} ) ;
238
263
239
264
test ( 'Then it will attempt to save the record to INPROGRESS' , ( ) => {
240
- expect ( mockSaveInProgress ) . toBeCalledWith ( inputRecord ) ;
265
+ expect ( mockSaveInProgress ) . toBeCalledWith (
266
+ inputRecord ,
267
+ dummyContext . getRemainingTimeInMillis ( )
268
+ ) ;
241
269
} ) ;
242
270
243
271
test ( 'Then an IdempotencyPersistenceLayerError is thrown' , ( ) => {
0 commit comments