@@ -3,6 +3,9 @@ import { expect } from 'chai';
33import sinon from 'sinon' ;
44
55import { KmsConnector , MOCK_GEN_DK_RESPONSE } from 'aws-kms-ee' ;
6+ import { DynamoDBDocumentClient , UpdateCommand } from '@aws-sdk/lib-dynamodb' ;
7+ import { ConditionalCheckFailedException } from '@aws-sdk/client-dynamodb' ;
8+ import { mockClient } from 'aws-sdk-client-mock' ;
69
710import {
811 initialize , initializeFrom ,
@@ -17,16 +20,21 @@ import {
1720import {
1821 updateExpression , timestampCondition ,
1922} from '../../../src/sinks/dynamodb' ;
20- import { DynamoDBConnector } from '../../../src/connectors' ;
23+ import { DynamoDBConnector , EventBridgeConnector } from '../../../src/connectors' ;
2124
2225import { update } from '../../../src/flavors/update' ;
2326
2427describe ( 'flavors/update.js' , ( ) => {
28+ let mockDdb ;
29+
2530 beforeEach ( ( ) => {
2631 sinon . stub ( DynamoDBConnector . prototype , 'update' ) . resolves ( { } ) ;
2732 } ) ;
2833
29- afterEach ( sinon . restore ) ;
34+ afterEach ( ( ) => {
35+ sinon . restore ( ) ;
36+ mockDdb ?. restore ( ) ;
37+ } ) ;
3038
3139 it ( 'should execute' , ( done ) => {
3240 sinon . stub ( DynamoDBConnector . prototype , 'query' ) . resolves ( [ ] ) ;
@@ -157,6 +165,54 @@ describe('flavors/update.js', () => {
157165 } )
158166 . done ( done ) ;
159167 } ) ;
168+
169+ it ( 'should optionally throw conditional check' , ( done ) => {
170+ sinon . restore ( ) ;
171+ sinon . stub ( EventBridgeConnector . prototype , 'putEvents' ) . resolves ( { } ) ;
172+ mockDdb = mockClient ( DynamoDBDocumentClient ) ;
173+ mockDdb . on ( UpdateCommand ) . rejects ( new ConditionalCheckFailedException ( { } ) ) ;
174+
175+ const events = toDynamodbRecords ( [
176+ {
177+ timestamp : 1572832690 ,
178+ keys : {
179+ pk : '1' ,
180+ sk : 'thing' ,
181+ } ,
182+ newImage : {
183+ pk : '1' ,
184+ sk : 'thing' ,
185+ discriminator : 'thing' ,
186+ ttl : 1549053422 ,
187+ timestamp : 1548967022000 ,
188+ } ,
189+ } ,
190+ ] ) ;
191+
192+ const rule = {
193+ id : 'updateThrow' ,
194+ flavor : update ,
195+ eventType : / t h i n g - * / ,
196+ toUpdateRequest : ( ) => ( { } ) ,
197+ throwConditionFailure : true ,
198+ } ;
199+
200+ initialize ( {
201+ ...initializeFrom ( [
202+ rule ,
203+ ] ) ,
204+ } , { ...defaultOptions , AES : false } )
205+ . assemble ( fromDynamodb ( events ) , true )
206+ . collect ( )
207+ // .tap((collected) => console.log(JSON.stringify(collected, null, 2)))
208+ . tap ( ( collected ) => {
209+ expect ( collected . length ) . to . equal ( 1 ) ;
210+ expect ( collected [ 0 ] . event . tags . pipeline ) . to . equal ( 'updateThrow' ) ;
211+ expect ( collected [ 0 ] . event . type ) . to . equal ( 'fault' ) ;
212+ expect ( collected [ 0 ] . event . err . name ) . to . equal ( 'ConditionalCheckFailedException' ) ;
213+ } )
214+ . done ( done ) ;
215+ } ) ;
160216} ) ;
161217
162218const toUpdateRequest = ( uow ) => ( {
0 commit comments