1
1
import { AttributeValue } from "@aws-sdk/client-dynamodb" ;
2
2
3
3
import { convertToAttr } from "./convertToAttr" ;
4
+ import { marshallOptions } from "./marshall" ;
4
5
import { NativeAttributeValue } from "./models" ;
5
6
6
7
describe ( "convertToAttr" , ( ) => {
@@ -179,6 +180,31 @@ describe("convertToAttr", () => {
179
180
L : [ { NULL : true } , { NULL : true } , { NULL : true } ] ,
180
181
} ) ;
181
182
} ) ;
183
+
184
+ describe ( `testing list with options.removeUndefinedValues` , ( ) => {
185
+ describe ( "throws error" , ( ) => {
186
+ const testErrorListWithUndefinedValues = ( options ?: marshallOptions ) => {
187
+ expect ( ( ) => {
188
+ convertToAttr ( [ "defined" , undefined ] , options ) ;
189
+ } ) . toThrowError ( `Pass options.removeUndefinedValues=true to remove undefined values from map/array/set.` ) ;
190
+ } ;
191
+
192
+ [ undefined , { } , { convertEmptyValues : false } ] . forEach ( ( options ) => {
193
+ it ( `when options=${ options } ` , ( ) => {
194
+ testErrorListWithUndefinedValues ( options ) ;
195
+ } ) ;
196
+ } ) ;
197
+ } ) ;
198
+
199
+ it ( `returns when options.removeUndefinedValues=true` , ( ) => {
200
+ expect ( convertToAttr ( [ "defined" , undefined ] , { removeUndefinedValues : true } ) ) . toEqual ( {
201
+ L : [ { S : "defined" } ] ,
202
+ } ) ;
203
+ expect ( convertToAttr ( [ undefined , "defined" , undefined ] , { removeUndefinedValues : true } ) ) . toEqual ( {
204
+ L : [ { S : "defined" } ] ,
205
+ } ) ;
206
+ } ) ;
207
+ } ) ;
182
208
} ) ;
183
209
184
210
describe ( "set" , ( ) => {
@@ -204,21 +230,53 @@ describe("convertToAttr", () => {
204
230
expect ( convertToAttr ( set ) ) . toEqual ( { SS : Array . from ( set ) } ) ;
205
231
} ) ;
206
232
207
- it ( "returns null for empty set for options.convertEmptyValues=true" , ( ) => {
208
- expect ( convertToAttr ( new Set ( [ ] ) , { convertEmptyValues : true } ) ) . toEqual ( { NULL : true } ) ;
233
+ describe ( "set with undefined" , ( ) => {
234
+ describe ( "throws error" , ( ) => {
235
+ const testErrorSetWithUndefined = ( options ?: marshallOptions ) => {
236
+ expect ( ( ) => {
237
+ convertToAttr ( new Set ( [ 1 , undefined , 3 ] ) , options ) ;
238
+ } ) . toThrowError ( `Pass options.removeUndefinedValues=true to remove undefined values from map/array/set.` ) ;
239
+ } ;
240
+
241
+ [ undefined , { } , { convertEmptyValues : false } ] . forEach ( ( options ) => {
242
+ it ( `when options=${ options } ` , ( ) => {
243
+ testErrorSetWithUndefined ( options ) ;
244
+ } ) ;
245
+ } ) ;
246
+ } ) ;
247
+
248
+ it ( "returns when options.removeUndefinedValues=true" , ( ) => {
249
+ expect ( convertToAttr ( new Set ( [ 1 , undefined , 3 ] ) , { removeUndefinedValues : true } ) ) . toEqual ( { NS : [ "1" , "3" ] } ) ;
250
+ } ) ;
209
251
} ) ;
210
252
211
- it ( "throws error for empty set" , ( ) => {
212
- expect ( ( ) => {
213
- convertToAttr ( new Set ( [ ] ) ) ;
214
- } ) . toThrowError ( `Please pass a non-empty set, or set convertEmptyValues to true.` ) ;
253
+ describe ( "empty set" , ( ) => {
254
+ describe ( "throws error" , ( ) => {
255
+ const testErrorEmptySet = ( options ?: marshallOptions ) => {
256
+ expect ( ( ) => {
257
+ convertToAttr ( new Set ( [ ] ) , options ) ;
258
+ } ) . toThrowError ( `Pass a non-empty set, or options.convertEmptyValues=true.` ) ;
259
+ } ;
260
+
261
+ [ undefined , { } , { convertEmptyValues : false } ] . forEach ( ( options ) => {
262
+ it ( `when options=${ options } ` , ( ) => {
263
+ testErrorEmptySet ( options ) ;
264
+ } ) ;
265
+ } ) ;
266
+ } ) ;
267
+
268
+ it ( "returns null when options.convertEmptyValues=true" , ( ) => {
269
+ expect ( convertToAttr ( new Set ( [ ] ) , { convertEmptyValues : true } ) ) . toEqual ( { NULL : true } ) ;
270
+ } ) ;
215
271
} ) ;
216
272
217
- it ( "thows error for unallowed set" , ( ) => {
218
- expect ( ( ) => {
219
- // @ts -expect-error Type 'Set<boolean>' is not assignable
220
- convertToAttr ( new Set ( [ true , false ] ) ) ;
221
- } ) . toThrowError ( `Only Number Set (NS), Binary Set (BS) or String Set (SS) are allowed.` ) ;
273
+ describe ( "unallowed set" , ( ) => {
274
+ it ( "throws error" , ( ) => {
275
+ expect ( ( ) => {
276
+ // @ts -expect-error Type 'Set<boolean>' is not assignable
277
+ convertToAttr ( new Set ( [ true , false ] ) ) ;
278
+ } ) . toThrowError ( `Only Number Set (NS), Binary Set (BS) or String Set (SS) are allowed.` ) ;
279
+ } ) ;
222
280
} ) ;
223
281
} ) ;
224
282
@@ -278,6 +336,29 @@ describe("convertToAttr", () => {
278
336
M : { stringKey : { NULL : true } , binaryKey : { NULL : true } , setKey : { NULL : true } } ,
279
337
} ) ;
280
338
} ) ;
339
+
340
+ describe ( `testing map with options.removeUndefinedValues` , ( ) => {
341
+ describe ( "throws error" , ( ) => {
342
+ const testErrorMapWithUndefinedValues = ( options ?: marshallOptions ) => {
343
+ expect ( ( ) => {
344
+ convertToAttr ( { definedKey : "definedKey" , undefinedKey : undefined } , options ) ;
345
+ } ) . toThrowError ( `Pass options.removeUndefinedValues=true to remove undefined values from map/array/set.` ) ;
346
+ } ;
347
+
348
+ [ undefined , { } , { convertEmptyValues : false } ] . forEach ( ( options ) => {
349
+ it ( `when options=${ options } ` , ( ) => {
350
+ testErrorMapWithUndefinedValues ( options ) ;
351
+ } ) ;
352
+ } ) ;
353
+ } ) ;
354
+
355
+ it ( `returns when options.removeUndefinedValues=true` , ( ) => {
356
+ const input = { definedKey : "definedKey" , undefinedKey : undefined } ;
357
+ expect ( convertToAttr ( input , { removeUndefinedValues : true } ) ) . toEqual ( {
358
+ M : { definedKey : { S : "definedKey" } } ,
359
+ } ) ;
360
+ } ) ;
361
+ } ) ;
281
362
} ) ;
282
363
283
364
describe ( "string" , ( ) => {
@@ -297,8 +378,14 @@ describe("convertToAttr", () => {
297
378
constructor ( private readonly foo : string ) { }
298
379
}
299
380
381
+ it ( `throws for: undefined` , ( ) => {
382
+ expect ( ( ) => {
383
+ convertToAttr ( undefined ) ;
384
+ } ) . toThrowError ( `Pass options.removeUndefinedValues=true to remove undefined values from map/array/set.` ) ;
385
+ } ) ;
386
+
300
387
// ToDo: Serialize ES6 class objects as string https://github.com/aws/aws-sdk-js-v3/issues/1535
301
- [ undefined , new Date ( ) , new FooObj ( "foo" ) ] . forEach ( ( data ) => {
388
+ [ new Date ( ) , new FooObj ( "foo" ) ] . forEach ( ( data ) => {
302
389
it ( `throws for: ${ String ( data ) } ` , ( ) => {
303
390
expect ( ( ) => {
304
391
// @ts -expect-error Argument is not assignable to parameter of type 'NativeAttributeValue'
0 commit comments