@@ -346,41 +346,75 @@ describe('Class: DynamoDBPersistenceLayer', () => {
346346 persistenceLayerSpy . mockRestore ( ) ;
347347 } ) ;
348348
349- it ( 'throws when called with a record that fails any condition' , async ( ) => {
350- // Prepare
351- const record = new IdempotencyRecord ( {
352- idempotencyKey : dummyKey ,
353- status : IdempotencyRecordStatus . EXPIRED ,
354- expiryTimestamp : 0 ,
355- } ) ;
356- const expiration = Date . now ( ) ;
357- client . on ( PutItemCommand ) . rejects (
358- new ConditionalCheckFailedException ( {
359- $metadata : {
360- httpStatusCode : 400 ,
361- requestId : 'someRequestId' ,
362- } ,
363- message : 'Conditional check failed' ,
364- Item : {
365- id : { S : 'test-key' } ,
366- status : { S : 'INPROGRESS' } ,
367- expiration : { N : expiration . toString ( ) } ,
368- } ,
369- } )
370- ) ;
371-
372- // Act & Assess
373- await expect ( persistenceLayer . _putRecord ( record ) ) . rejects . toThrowError (
374- new IdempotencyItemAlreadyExistsError (
375- `Failed to put record for already existing idempotency key: ${ record . idempotencyKey } ` ,
376- new IdempotencyRecord ( {
377- idempotencyKey : 'test-key' ,
378- status : IdempotencyRecordStatus . INPROGRESS ,
379- expiryTimestamp : expiration ,
349+ it . each ( [
350+ {
351+ keys : {
352+ id : 'idempotency#my-lambda-function' ,
353+ sortKey : dummyKey ,
354+ } ,
355+ case : 'composite key' ,
356+ } ,
357+ {
358+ keys : {
359+ id : dummyKey ,
360+ } ,
361+ case : 'single key' ,
362+ } ,
363+ ] ) (
364+ 'throws when called with a record that fails any condition ($case)' ,
365+ async ( { keys } ) => {
366+ // Prepare
367+ const { id, sortKey } = keys ;
368+
369+ const record = new IdempotencyRecord ( {
370+ idempotencyKey : id ,
371+ sortKey,
372+ status : IdempotencyRecordStatus . EXPIRED ,
373+ expiryTimestamp : 0 ,
374+ } ) ;
375+ const expiration = Date . now ( ) ;
376+ client . on ( PutItemCommand ) . rejects (
377+ new ConditionalCheckFailedException ( {
378+ $metadata : {
379+ httpStatusCode : 400 ,
380+ requestId : 'someRequestId' ,
381+ } ,
382+ message : 'Conditional check failed' ,
383+ Item : {
384+ id : { S : 'test-key' } ,
385+ ...( sortKey ? { sortKey : { S : sortKey } } : { } ) ,
386+ status : { S : 'INPROGRESS' } ,
387+ expiration : { N : expiration . toString ( ) } ,
388+ } ,
380389 } )
381- )
382- ) ;
383- } ) ;
390+ ) ;
391+ const testPersistenceLayer = sortKey
392+ ? new DynamoDBPersistenceLayerTestClass ( {
393+ tableName : dummyTableName ,
394+ sortKeyAttr : 'sortKey' ,
395+ } )
396+ : persistenceLayer ;
397+
398+ // Act & Assess
399+ await expect (
400+ testPersistenceLayer . _putRecord ( record )
401+ ) . rejects . toThrowError (
402+ new IdempotencyItemAlreadyExistsError (
403+ `Failed to put record for already existing idempotency key: ${
404+ sortKey
405+ ? `${ record . idempotencyKey } and sort key: ${ sortKey } `
406+ : record . idempotencyKey
407+ } `,
408+ new IdempotencyRecord ( {
409+ idempotencyKey : 'test-key' ,
410+ sortKey,
411+ status : IdempotencyRecordStatus . INPROGRESS ,
412+ expiryTimestamp : expiration ,
413+ } )
414+ )
415+ ) ;
416+ }
417+ ) ;
384418
385419 it ( 'throws when encountering an unknown error' , async ( ) => {
386420 // Prepare
@@ -445,7 +479,7 @@ describe('Class: DynamoDBPersistenceLayer', () => {
445479 ) ;
446480 } ) ;
447481
448- it ( 'it builds the request correctly when using composite keys' , async ( ) => {
482+ it ( 'builds the request correctly when using composite keys' , async ( ) => {
449483 // Prepare
450484 const persistenceLayer = new DynamoDBPersistenceLayerTestClass ( {
451485 tableName : dummyTableName ,
0 commit comments