Skip to content

Commit a27e8e7

Browse files
committed
test(decorator): add tests for model decorator
1 parent eeeca92 commit a27e8e7

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// tslint:disable:max-classes-per-file
2+
import { GSIPartitionKey } from '../index/gsi-partition-key.decorator'
3+
import { GSISortKey } from '../index/gsi-sort-key.decorator'
4+
import { LSISortKey } from '../index/lsi-sort-key.decorator'
5+
import { PartitionKey } from '../key/partition-key.decorator'
6+
import { modelErrors } from './errors.const'
7+
import { Model } from './model.decorator'
8+
9+
const IX_NAME = 'anIndexName'
10+
11+
describe('@model decorator', () => {
12+
describe('getGlobalSecondaryIndexes', () => {
13+
// throws on applying decorator
14+
15+
it('throws when defining multiple partitionKeys for same gsi', () => {
16+
expect(() => {
17+
// @ts-ignore
18+
@Model()
19+
class FailModel {
20+
@GSIPartitionKey(IX_NAME)
21+
pk1: string
22+
@GSIPartitionKey(IX_NAME)
23+
pk2: string
24+
@GSISortKey(IX_NAME)
25+
sk1: string
26+
}
27+
}).toThrow(modelErrors.gsiMultiplePk(IX_NAME, 'pk2'))
28+
})
29+
it('throws when defining multiple sortKeys for same gsi', () => {
30+
expect(() => {
31+
// @ts-ignore
32+
@Model()
33+
class FailModel {
34+
@GSIPartitionKey(IX_NAME)
35+
pk1: string
36+
@GSISortKey(IX_NAME)
37+
sk1: string
38+
@GSISortKey(IX_NAME)
39+
sk2: string
40+
}
41+
}).toThrow(modelErrors.gsiMultipleSk(IX_NAME, 'sk2'))
42+
})
43+
})
44+
describe('getLocalSecondaryIndexes', () => {
45+
it('throws when defining LSI sortKey but no PartitionKey', () => {
46+
expect(() => {
47+
// @ts-ignore
48+
@Model()
49+
class FailModel {
50+
@LSISortKey(IX_NAME)
51+
sk1: string
52+
}
53+
}).toThrow(modelErrors.lsiRequiresPk(IX_NAME, 'sk1'))
54+
})
55+
it('throws when defining multiple sortKeys for same lsi', () => {
56+
expect(() => {
57+
// @ts-ignore
58+
@Model()
59+
class FailModel {
60+
@PartitionKey()
61+
pk1: string
62+
@LSISortKey(IX_NAME)
63+
sk1: string
64+
@LSISortKey(IX_NAME)
65+
sk2: string
66+
}
67+
}).toThrow(modelErrors.lsiMultipleSk(IX_NAME, 'sk2'))
68+
})
69+
})
70+
})

src/decorator/metadata/metadata.spec.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
// tslint:disable:no-non-null-assertion
22
import {
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'
1216
import { Metadata } from './metadata'
1317

1418
describe('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
})

test/models/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export * from './model-with-enum.model'
99
export * from './model-with-indexes.model'
1010
export * from './model-with-date-as-key.model'
1111
export * from './model-without-custom-mapper.model'
12+
export * from './model-without-partition-key.model'
1213
export * from './nested-complex.model'
1314
export * from './nested-object.model'
1415
export * from './organization.model'
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { GSISortKey } from '../../src/decorator/impl/index/gsi-sort-key.decorator'
2+
import { Model } from '../../src/decorator/impl/model/model.decorator'
3+
import { Property } from '../../src/decorator/impl/property/property.decorator'
4+
5+
export const FAIL_MODEL_GSI = 'failModelGsi'
6+
7+
@Model()
8+
export class ModelWithoutPartitionKeyModel {
9+
@Property()
10+
name: string
11+
12+
@GSISortKey(FAIL_MODEL_GSI)
13+
gsiRange: string
14+
}

0 commit comments

Comments
 (0)