@@ -19,6 +19,8 @@ import {
19
19
SimpleModel ,
20
20
} from '../../test/models'
21
21
import { Form } from '../../test/models/real-world'
22
+ import { updateDynamoEasyConfig } from '../config/update-config.function'
23
+ import { LogLevel } from '../logger/log-level.type'
22
24
import { CollectionProperty } from './impl/collection/collection-property.decorator'
23
25
import { GSIPartitionKey } from './impl/index/gsi-partition-key.decorator'
24
26
import { GSISortKey } from './impl/index/gsi-sort-key.decorator'
@@ -65,7 +67,7 @@ describe('Decorators should add correct metadata', () => {
65
67
} )
66
68
67
69
it ( 'with no properties' , ( ) => {
68
- expect ( modelOptions . properties ) . toBeUndefined ( )
70
+ expect ( modelOptions . properties ) . toEqual ( [ ] )
69
71
} )
70
72
} )
71
73
@@ -317,6 +319,77 @@ describe('Decorators should add correct metadata', () => {
317
319
} )
318
320
} )
319
321
322
+ describe ( 'multiple property decorators' , ( ) => {
323
+ const REVERSE_INDEX = 'reverse-index'
324
+ const OTHER_INDEX = 'other-index'
325
+ const LSI_1 = 'lsi-1'
326
+ const LSI_2 = 'lsi-2'
327
+
328
+ @Model ( )
329
+ class ABC {
330
+ @PartitionKey ( )
331
+ @Property ( { name : 'pk' } )
332
+ @GSISortKey ( REVERSE_INDEX )
333
+ id : string
334
+
335
+ @SortKey ( )
336
+ @Property ( { name : 'sk' } )
337
+ @GSIPartitionKey ( REVERSE_INDEX )
338
+ @GSISortKey ( OTHER_INDEX )
339
+ timestamp : number
340
+
341
+ @GSIPartitionKey ( OTHER_INDEX )
342
+ @LSISortKey ( LSI_1 )
343
+ @LSISortKey ( LSI_2 )
344
+ otherId : string
345
+ }
346
+
347
+ let metaData : Metadata < ABC >
348
+
349
+ beforeEach ( ( ) => ( metaData = metadataForModel ( ABC ) ) )
350
+
351
+ it ( 'PartitionKey & Property & GSISortKey should combine the data' , ( ) => {
352
+ const propData = metaData . forProperty ( 'id' )
353
+ expect ( propData ) . toEqual ( {
354
+ key : { type : 'HASH' } ,
355
+ name : 'id' ,
356
+ nameDb : 'pk' ,
357
+ typeInfo : { type : String } ,
358
+ keyForGSI : { [ REVERSE_INDEX ] : 'RANGE' } ,
359
+ } )
360
+ } )
361
+ it ( 'SortKey & Property & GSIPartitionKey & GSISortKey should combine the data' , ( ) => {
362
+ const propData = metaData . forProperty ( 'timestamp' )
363
+ expect ( propData ) . toEqual ( {
364
+ key : { type : 'RANGE' } ,
365
+ name : 'timestamp' ,
366
+ nameDb : 'sk' ,
367
+ typeInfo : { type : Number } ,
368
+ keyForGSI : { [ REVERSE_INDEX ] : 'HASH' , [ OTHER_INDEX ] : 'RANGE' } ,
369
+ } )
370
+ } )
371
+ it ( 'GSIPartitionKey & multiple LSISortkey should combine the data' , ( ) => {
372
+ const propData = metaData . forProperty ( 'otherId' )
373
+ expect ( propData ) . toBeDefined ( )
374
+ expect ( propData ! . name ) . toEqual ( 'otherId' )
375
+ expect ( propData ! . nameDb ) . toEqual ( 'otherId' )
376
+ expect ( propData ! . typeInfo ) . toEqual ( { type : String } )
377
+ expect ( propData ! . keyForGSI ) . toEqual ( { [ OTHER_INDEX ] : 'HASH' } )
378
+ expect ( propData ! . sortKeyForLSI ) . toContain ( LSI_1 )
379
+ expect ( propData ! . sortKeyForLSI ) . toContain ( LSI_2 )
380
+ } )
381
+ it ( 'correctly defines the indexes' , ( ) => {
382
+ const reverseIndex = metaData . getIndex ( REVERSE_INDEX )
383
+ const otherIndex = metaData . getIndex ( OTHER_INDEX )
384
+ const lsi1 = metaData . getIndex ( LSI_1 )
385
+ const lsi2 = metaData . getIndex ( LSI_2 )
386
+ expect ( reverseIndex ) . toEqual ( { partitionKey : 'sk' , sortKey : 'pk' } )
387
+ expect ( otherIndex ) . toEqual ( { partitionKey : 'otherId' , sortKey : 'sk' } )
388
+ expect ( lsi1 ) . toEqual ( { partitionKey : 'pk' , sortKey : 'otherId' } )
389
+ expect ( lsi2 ) . toEqual ( { partitionKey : 'pk' , sortKey : 'otherId' } )
390
+ } )
391
+ } )
392
+
320
393
describe ( 'enum (no Enum decorator)' , ( ) => {
321
394
let metadata : Metadata < ModelWithEnum >
322
395
@@ -544,17 +617,43 @@ describe('Decorators should add correct metadata', () => {
544
617
} )
545
618
546
619
describe ( 'should throw when more than one partitionKey was defined in a model' , ( ) => {
547
- expect ( ( ) => {
620
+ it ( 'does so' , ( ) => {
621
+ expect ( ( ) => {
622
+ @Model ( )
623
+ class InvalidModel {
624
+ @PartitionKey ( )
625
+ partKeyA : string
626
+
627
+ @PartitionKey ( )
628
+ partKeyB : string
629
+ }
630
+
631
+ return new InvalidModel ( )
632
+ } ) . toThrow ( )
633
+ } )
634
+ } )
635
+
636
+ describe ( 'decorate property multiple times identically' , ( ) => {
637
+ let logReceiver : jest . Mock
638
+
639
+ beforeEach ( ( ) => {
640
+ logReceiver = jest . fn ( )
641
+ updateDynamoEasyConfig ( { logReceiver } )
642
+ } )
643
+
644
+ it ( 'should not throw but warn, if the PartitionKey is two times annotated' , ( ) => {
548
645
@Model ( )
549
- class InvalidModel {
646
+ class NotCoolButOkModel {
550
647
@PartitionKey ( )
551
- partKeyA : string
552
-
553
648
@PartitionKey ( )
554
- partKeyB : string
649
+ doppeltGemoppelt : string
555
650
}
556
651
557
- return new InvalidModel ( )
558
- } ) . toThrow ( )
652
+ const propertyMetaData = metadataForModel ( NotCoolButOkModel ) . forProperty ( 'doppeltGemoppelt' )
653
+ expect ( propertyMetaData ) . toBeDefined ( )
654
+ expect ( propertyMetaData ! . key ) . toEqual ( { type : 'HASH' } )
655
+ expect ( logReceiver ) . toBeCalledTimes ( 1 )
656
+ expect ( logReceiver . mock . calls [ 0 ] [ 0 ] . level ) . toBe ( LogLevel . WARNING )
657
+ } )
559
658
} )
560
659
} )
0 commit comments