Skip to content

Commit

Permalink
test(decorator): add tests for model decorator
Browse files Browse the repository at this point in the history
simonmumenthaler committed Oct 10, 2019
1 parent eeeca92 commit a27e8e7
Showing 4 changed files with 114 additions and 1 deletion.
70 changes: 70 additions & 0 deletions src/decorator/impl/model/model.decorator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// tslint:disable:max-classes-per-file
import { GSIPartitionKey } from '../index/gsi-partition-key.decorator'
import { GSISortKey } from '../index/gsi-sort-key.decorator'
import { LSISortKey } from '../index/lsi-sort-key.decorator'
import { PartitionKey } from '../key/partition-key.decorator'
import { modelErrors } from './errors.const'
import { Model } from './model.decorator'

const IX_NAME = 'anIndexName'

describe('@model decorator', () => {
describe('getGlobalSecondaryIndexes', () => {
// throws on applying decorator

it('throws when defining multiple partitionKeys for same gsi', () => {
expect(() => {
// @ts-ignore
@Model()
class FailModel {
@GSIPartitionKey(IX_NAME)
pk1: string
@GSIPartitionKey(IX_NAME)
pk2: string
@GSISortKey(IX_NAME)
sk1: string
}
}).toThrow(modelErrors.gsiMultiplePk(IX_NAME, 'pk2'))
})
it('throws when defining multiple sortKeys for same gsi', () => {
expect(() => {
// @ts-ignore
@Model()
class FailModel {
@GSIPartitionKey(IX_NAME)
pk1: string
@GSISortKey(IX_NAME)
sk1: string
@GSISortKey(IX_NAME)
sk2: string
}
}).toThrow(modelErrors.gsiMultipleSk(IX_NAME, 'sk2'))
})
})
describe('getLocalSecondaryIndexes', () => {
it('throws when defining LSI sortKey but no PartitionKey', () => {
expect(() => {
// @ts-ignore
@Model()
class FailModel {
@LSISortKey(IX_NAME)
sk1: string
}
}).toThrow(modelErrors.lsiRequiresPk(IX_NAME, 'sk1'))
})
it('throws when defining multiple sortKeys for same lsi', () => {
expect(() => {
// @ts-ignore
@Model()
class FailModel {
@PartitionKey()
pk1: string
@LSISortKey(IX_NAME)
sk1: string
@LSISortKey(IX_NAME)
sk2: string
}
}).toThrow(modelErrors.lsiMultipleSk(IX_NAME, 'sk2'))
})
})
})
30 changes: 29 additions & 1 deletion src/decorator/metadata/metadata.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
// tslint:disable:no-non-null-assertion
import {
ComplexModel,
FAIL_MODEL_GSI,
INDEX_ACTIVE,
INDEX_ACTIVE_CREATED_AT,
INDEX_COUNT,
ModelWithABunchOfIndexes,
ModelWithAutogeneratedId,
ModelWithGSI,
ModelWithLSI,
ModelWithoutPartitionKeyModel,
SimpleWithCompositePartitionKeyModel,
SimpleWithPartitionKeyModel,
} from '../../../test/models'
import { INDEX_ACTIVE, INDEX_ACTIVE_CREATED_AT, INDEX_COUNT } from '../../../test/models/model-with-indexes.model'
import { Metadata } from './metadata'

describe('metadata', () => {
let metaDataPartitionKey: Metadata<SimpleWithPartitionKeyModel>
let metaDataNoPartitionKey: Metadata<ModelWithoutPartitionKeyModel>
let metaDataComposite: Metadata<SimpleWithCompositePartitionKeyModel>
let metaDataLsi: Metadata<ModelWithLSI>
let metaDataGsi: Metadata<ModelWithGSI>
@@ -22,6 +27,7 @@ describe('metadata', () => {

beforeEach(() => {
metaDataPartitionKey = new Metadata(SimpleWithPartitionKeyModel)
metaDataNoPartitionKey = new Metadata(ModelWithoutPartitionKeyModel)
metaDataComposite = new Metadata(SimpleWithCompositePartitionKeyModel)
metaDataLsi = new Metadata(ModelWithLSI)
metaDataGsi = new Metadata(ModelWithGSI)
@@ -70,13 +76,26 @@ describe('metadata', () => {
expect(metaDataIndexes.getPartitionKey(INDEX_ACTIVE_CREATED_AT)).toEqual('active')
})

it('getPartitionKey throws if no partitionKey defined [no index]', () => {
expect(() => metaDataNoPartitionKey.getPartitionKey()).toThrow()
})
it('getPartitionKey throws if no partitionKey defined [GSI]', () => {
expect(() => metaDataNoPartitionKey.getPartitionKey(FAIL_MODEL_GSI)).toThrow()
})
it('getPartitionKey throws if given index is not defined', () => {
expect(() => metaDataNoPartitionKey.getPartitionKey('not-existing-index')).toThrow()
})

it('getSortKey', () => {
expect(metaDataPartitionKey.getSortKey()).toBe(null)
expect(metaDataComposite.getSortKey()).toBe('creationDate')
expect(metaDataLsi.getSortKey(INDEX_ACTIVE)).toBe('active')
expect(() => metaDataGsi.getSortKey(INDEX_ACTIVE)).toThrow()
expect(metaDataIndexes.getSortKey(INDEX_ACTIVE_CREATED_AT)).toBe('createdAt')
})
it('getSortKey throws if given index is not defined', () => {
expect(() => metaDataNoPartitionKey.getSortKey('non-existent-index-name')).toThrow()
})

it('getIndexes', () => {
expect(metaDataLsi.getIndexes()).toEqual([{ partitionKey: 'id', sortKey: 'active' }])
@@ -95,4 +114,13 @@ describe('metadata', () => {
sortKey: 'createdAt',
})
})
it('getIndex returns null if not existent', () => {
// no indexes at all --> should always be defined
expect(metaDataNoPartitionKey.modelOptions).toBeDefined()
expect(metaDataNoPartitionKey.modelOptions.indexes).toBeInstanceOf(Map)
// no indexes at all
expect(metaDataPartitionKey.getIndex('non-existent-index')).toBeNull()
// indexes defined, but not the one requesting
expect(metaDataIndexes.getIndex('non-existent-index')).toBeNull()
})
})
1 change: 1 addition & 0 deletions test/models/index.ts
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ export * from './model-with-enum.model'
export * from './model-with-indexes.model'
export * from './model-with-date-as-key.model'
export * from './model-without-custom-mapper.model'
export * from './model-without-partition-key.model'
export * from './nested-complex.model'
export * from './nested-object.model'
export * from './organization.model'
14 changes: 14 additions & 0 deletions test/models/model-without-partition-key.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { GSISortKey } from '../../src/decorator/impl/index/gsi-sort-key.decorator'
import { Model } from '../../src/decorator/impl/model/model.decorator'
import { Property } from '../../src/decorator/impl/property/property.decorator'

export const FAIL_MODEL_GSI = 'failModelGsi'

@Model()
export class ModelWithoutPartitionKeyModel {
@Property()
name: string

@GSISortKey(FAIL_MODEL_GSI)
gsiRange: string
}

0 comments on commit a27e8e7

Please sign in to comment.