11// tslint:disable:no-non-null-assertion
22import {
33 ComplexModel ,
4+ FAIL_MODEL_GSI ,
5+ INDEX_ACTIVE ,
6+ INDEX_ACTIVE_CREATED_AT ,
7+ INDEX_COUNT ,
48 ModelWithABunchOfIndexes ,
59 ModelWithAutogeneratedId ,
610 ModelWithGSI ,
711 ModelWithLSI ,
12+ ModelWithoutPartitionKeyModel ,
813 SimpleWithCompositePartitionKeyModel ,
914 SimpleWithPartitionKeyModel ,
1015} from '../../../test/models'
11- import { INDEX_ACTIVE , INDEX_ACTIVE_CREATED_AT , INDEX_COUNT } from '../../../test/models/model-with-indexes.model'
1216import { Metadata } from './metadata'
1317
1418describe ( 'metadata' , ( ) => {
1519 let metaDataPartitionKey : Metadata < SimpleWithPartitionKeyModel >
20+ let metaDataNoPartitionKey : Metadata < ModelWithoutPartitionKeyModel >
1621 let metaDataComposite : Metadata < SimpleWithCompositePartitionKeyModel >
1722 let metaDataLsi : Metadata < ModelWithLSI >
1823 let metaDataGsi : Metadata < ModelWithGSI >
@@ -22,6 +27,7 @@ describe('metadata', () => {
2227
2328 beforeEach ( ( ) => {
2429 metaDataPartitionKey = new Metadata ( SimpleWithPartitionKeyModel )
30+ metaDataNoPartitionKey = new Metadata ( ModelWithoutPartitionKeyModel )
2531 metaDataComposite = new Metadata ( SimpleWithCompositePartitionKeyModel )
2632 metaDataLsi = new Metadata ( ModelWithLSI )
2733 metaDataGsi = new Metadata ( ModelWithGSI )
@@ -70,13 +76,26 @@ describe('metadata', () => {
7076 expect ( metaDataIndexes . getPartitionKey ( INDEX_ACTIVE_CREATED_AT ) ) . toEqual ( 'active' )
7177 } )
7278
79+ it ( 'getPartitionKey throws if no partitionKey defined [no index]' , ( ) => {
80+ expect ( ( ) => metaDataNoPartitionKey . getPartitionKey ( ) ) . toThrow ( )
81+ } )
82+ it ( 'getPartitionKey throws if no partitionKey defined [GSI]' , ( ) => {
83+ expect ( ( ) => metaDataNoPartitionKey . getPartitionKey ( FAIL_MODEL_GSI ) ) . toThrow ( )
84+ } )
85+ it ( 'getPartitionKey throws if given index is not defined' , ( ) => {
86+ expect ( ( ) => metaDataNoPartitionKey . getPartitionKey ( 'not-existing-index' ) ) . toThrow ( )
87+ } )
88+
7389 it ( 'getSortKey' , ( ) => {
7490 expect ( metaDataPartitionKey . getSortKey ( ) ) . toBe ( null )
7591 expect ( metaDataComposite . getSortKey ( ) ) . toBe ( 'creationDate' )
7692 expect ( metaDataLsi . getSortKey ( INDEX_ACTIVE ) ) . toBe ( 'active' )
7793 expect ( ( ) => metaDataGsi . getSortKey ( INDEX_ACTIVE ) ) . toThrow ( )
7894 expect ( metaDataIndexes . getSortKey ( INDEX_ACTIVE_CREATED_AT ) ) . toBe ( 'createdAt' )
7995 } )
96+ it ( 'getSortKey throws if given index is not defined' , ( ) => {
97+ expect ( ( ) => metaDataNoPartitionKey . getSortKey ( 'non-existent-index-name' ) ) . toThrow ( )
98+ } )
8099
81100 it ( 'getIndexes' , ( ) => {
82101 expect ( metaDataLsi . getIndexes ( ) ) . toEqual ( [ { partitionKey : 'id' , sortKey : 'active' } ] )
@@ -95,4 +114,13 @@ describe('metadata', () => {
95114 sortKey : 'createdAt' ,
96115 } )
97116 } )
117+ it ( 'getIndex returns null if not existent' , ( ) => {
118+ // no indexes at all --> should always be defined
119+ expect ( metaDataNoPartitionKey . modelOptions ) . toBeDefined ( )
120+ expect ( metaDataNoPartitionKey . modelOptions . indexes ) . toBeInstanceOf ( Map )
121+ // no indexes at all
122+ expect ( metaDataPartitionKey . getIndex ( 'non-existent-index' ) ) . toBeNull ( )
123+ // indexes defined, but not the one requesting
124+ expect ( metaDataIndexes . getIndex ( 'non-existent-index' ) ) . toBeNull ( )
125+ } )
98126} )
0 commit comments