@@ -58,6 +58,90 @@ describe('sinks/dynamodb.js', () => {
5858 } ) ;
5959 } ) ;
6060
61+ it ( 'should calculate updateExpression adding values to a set' , ( ) => {
62+ const result = updateExpression ( {
63+ tags : new Set ( [ 'a' , 'b' ] ) ,
64+ } ) ;
65+
66+ expect ( normalizeObj ( result ) ) . to . deep . equal ( {
67+ ExpressionAttributeNames : {
68+ '#tags' : 'tags' ,
69+ } ,
70+ ExpressionAttributeValues : {
71+ ':tags' : [ 'a' , 'b' ] ,
72+ } ,
73+ UpdateExpression : 'ADD #tags :tags' ,
74+ ReturnValues : 'ALL_NEW' ,
75+ } ) ;
76+ } ) ;
77+
78+ it ( 'should calculate updateExpression removing values from a set' , ( ) => {
79+ const result = updateExpression ( {
80+ tags_delete : new Set ( [ 'x' , 'y' ] ) ,
81+ } ) ;
82+
83+ expect ( normalizeObj ( result ) ) . to . deep . equal ( {
84+ ExpressionAttributeNames : {
85+ '#tags' : 'tags' ,
86+ } ,
87+ ExpressionAttributeValues : {
88+ ':tags_delete' : [ 'x' , 'y' ] ,
89+ } ,
90+ UpdateExpression : 'DELETE #tags :tags_delete' ,
91+ ReturnValues : 'ALL_NEW' ,
92+ } ) ;
93+ } ) ;
94+
95+ it ( 'should wrap calculate updateExpression wrapping a delete set value in a set' , ( ) => {
96+ const result = updateExpression ( {
97+ tags_delete : 'x' ,
98+ } ) ;
99+
100+ expect ( normalizeObj ( result ) ) . to . deep . equal ( {
101+ ExpressionAttributeNames : {
102+ '#tags' : 'tags' ,
103+ } ,
104+ ExpressionAttributeValues : {
105+ ':tags_delete' : [ 'x' ] ,
106+ } ,
107+ UpdateExpression : 'DELETE #tags :tags_delete' ,
108+ ReturnValues : 'ALL_NEW' ,
109+ } ) ;
110+ } ) ;
111+
112+ it ( 'should calculate complex updateExpression using SET, REMOVE, ADD, and DELETE' , ( ) => {
113+ const result = updateExpression ( {
114+ id : '123' ,
115+ name : 'Complex Thing' ,
116+ description : null ,
117+ tags : new Set ( [ 'blue' , 'green' ] ) ,
118+ categories : new Set ( [ 'a' , 'b' ] ) ,
119+ tags_delete : 'red' ,
120+ categories_delete : new Set ( [ 'x' , 'y' ] ) ,
121+ ignoredField : undefined ,
122+ } ) ;
123+
124+ expect ( normalizeObj ( result ) ) . to . deep . equal ( {
125+ ExpressionAttributeNames : {
126+ '#id' : 'id' ,
127+ '#name' : 'name' ,
128+ '#description' : 'description' ,
129+ '#tags' : 'tags' ,
130+ '#categories' : 'categories' ,
131+ } ,
132+ ExpressionAttributeValues : {
133+ ':id' : '123' ,
134+ ':name' : 'Complex Thing' ,
135+ ':tags' : [ 'blue' , 'green' ] ,
136+ ':categories' : [ 'a' , 'b' ] ,
137+ ':tags_delete' : [ 'red' ] ,
138+ ':categories_delete' : [ 'x' , 'y' ] ,
139+ } ,
140+ UpdateExpression : 'SET #id = :id, #name = :name REMOVE #description ADD #tags :tags, #categories :categories DELETE #tags :tags_delete, #categories :categories_delete' ,
141+ ReturnValues : 'ALL_NEW' ,
142+ } ) ;
143+ } ) ;
144+
61145 it ( 'should calculate timestampCondition' , ( ) => {
62146 expect ( timestampCondition ( ) ) . to . deep . equal ( {
63147 ConditionExpression : 'attribute_not_exists(#timestamp) OR #timestamp < :timestamp' ,
@@ -134,3 +218,8 @@ describe('sinks/dynamodb.js', () => {
134218 . done ( done ) ;
135219 } ) ;
136220} ) ;
221+
222+ // Chai doesn't like sets...we can convert them to arrays to help it out.
223+ const normalizeObj = ( obj ) =>
224+ JSON . parse ( JSON . stringify ( obj , ( thisArg , value ) =>
225+ ( value instanceof Set ? [ ...value ] : value ) ) ) ;
0 commit comments