@@ -9,6 +9,7 @@ import { PrimaryEntity } from '../../src/dynamodb';
99interface TestEntry extends PrimaryEntity < `Partition#${string } `, `Sort#${string } `> {
1010 content : string ;
1111}
12+
1213describe ( 'A dynamodb client' , ( ) => {
1314 const testTableName = process . env . TABLE ;
1415
@@ -126,6 +127,161 @@ describe('A dynamodb client', () => {
126127 } ) ;
127128 } ) ;
128129
129- //TODO Test item updates
130+ test ( 'UpdateItem (existing)' , async ( ) => {
131+ client . on ( commands . UpdateCommand ) . resolves ( {
132+ Attributes : { content : 'updated' } ,
133+ } ) ;
134+
135+ const result = await dynamo . updateExistingItem < TestEntry > ( 'Partition#foo' , 'Sort#bar' , {
136+ content : 'updated' ,
137+ } ) ;
138+
139+ // Just making sure, we're actually returning what was passed from the dynamo client
140+ expect ( result ) . toBeDefined ( ) ;
141+ expect ( result ! . content ) . toEqual ( 'updated' ) ;
142+
143+ expect ( client ) . toHaveReceivedCommandWith ( commands . UpdateCommand , {
144+ TableName : testTableName ,
145+ Key : {
146+ PK : 'Partition#foo' ,
147+ SK : 'Sort#bar' ,
148+ } ,
149+ ConditionExpression : 'attribute_exists(PK) and attribute_exists(SK)' ,
150+ ExpressionAttributeNames : { '#content' : 'content' } ,
151+ ExpressionAttributeValues : { ':content' : 'updated' } ,
152+ ReturnValues : 'ALL_NEW' ,
153+ UpdateExpression : 'SET #content = :content ' ,
154+ } ) ;
155+ } ) ;
156+
157+ type TestSetup = {
158+ testName : string ;
159+ given : {
160+ Key : {
161+ PK : string ;
162+ SK : string ;
163+ } ;
164+ UpdateExpression ?: string ;
165+ item : { [ key : string ] : any } ;
166+ } ;
167+ expected : commands . UpdateCommandInput ;
168+ }
169+
170+ test . each < TestSetup > ( [
171+ {
172+ testName : 'simple update' ,
173+ given : {
174+ Key : {
175+ PK : 'Partition#simple' ,
176+ SK : 'Sort#update' ,
177+ } ,
178+ item : {
179+ content : 'updated' ,
180+ } ,
181+ } ,
182+ expected : {
183+ TableName : testTableName ,
184+ Key : {
185+ PK : 'Partition#simple' ,
186+ SK : 'Sort#update' ,
187+ } ,
188+ ExpressionAttributeNames : { '#content' : 'content' } ,
189+ ExpressionAttributeValues : { ':content' : 'updated' } ,
190+ UpdateExpression : 'SET #content = :content ' ,
191+ } ,
192+ } ,
193+ {
194+ testName : 'multiple values/types' ,
195+ given : {
196+ Key : {
197+ PK : 'Partition#multiple' ,
198+ SK : 'Sort#fields' ,
199+ } ,
200+ item : {
201+ someValue : 'updated' ,
202+ otherValue : 42 ,
203+ } ,
204+ } ,
205+ expected : {
206+ TableName : testTableName ,
207+ Key : {
208+ PK : 'Partition#multiple' ,
209+ SK : 'Sort#fields' ,
210+ } ,
211+ ExpressionAttributeNames : {
212+ '#otherValue' : 'otherValue' ,
213+ '#someValue' : 'someValue' ,
214+ } ,
215+ ExpressionAttributeValues : {
216+ ':otherValue' : 42 ,
217+ ':someValue' : 'updated' ,
218+ } ,
219+ UpdateExpression : 'SET #someValue = :someValue, #otherValue = :otherValue ' ,
220+ } ,
221+ } ,
222+ {
223+ testName : 'remove field' ,
224+ given : {
225+ Key : {
226+ PK : 'Partition#remove' ,
227+ SK : 'Sort#field' ,
228+ } ,
229+ item : {
230+ content : 'stay' ,
231+ removeMe : undefined ,
232+ } ,
233+ } ,
234+ expected : {
235+ TableName : testTableName ,
236+ Key : {
237+ PK : 'Partition#remove' ,
238+ SK : 'Sort#field' ,
239+ } ,
240+ ExpressionAttributeNames : {
241+ '#content' : 'content' ,
242+ '#removeMe' : 'removeMe' ,
243+ } ,
244+ ExpressionAttributeValues : {
245+ ':content' : 'stay' ,
246+ } ,
247+ UpdateExpression : 'SET #content = :content REMOVE #removeMe ' ,
248+ } ,
249+ } ,
250+ {
251+ testName : 'partial request' ,
252+ given : {
253+ Key : {
254+ PK : 'Partition#partial' ,
255+ SK : 'Sort#request' ,
256+ } ,
257+ UpdateExpression : 'I AM THE UPDATE EXPRESSION' ,
258+ item : {
259+ content : 'someContent' ,
260+ } ,
261+ } ,
262+ expected : {
263+ TableName : testTableName ,
264+ Key : {
265+ PK : 'Partition#partial' ,
266+ SK : 'Sort#request' ,
267+ } ,
268+ ExpressionAttributeNames : {
269+ '#content' : 'content' ,
270+ } ,
271+ ExpressionAttributeValues : {
272+ ':content' : 'someContent' ,
273+ } ,
274+ UpdateExpression : 'SET #content = :content I AM THE UPDATE EXPRESSION' ,
275+ } ,
276+ } ,
277+ ] ) ( 'createUpdate ($testName)' , ( { given, expected } ) => {
278+ const result = dynamo . createUpdate < TestEntry > ( {
279+ Key : given . Key ,
280+ UpdateExpression : given . UpdateExpression ,
281+ } , given . item ) ;
282+
283+ expect ( result . input ) . toEqual ( expected ) ;
284+ } ) ;
130285 } ) ;
131- } ) ;
286+ } )
287+ ;
0 commit comments