|
| 1 | +// tslint:disable:no-non-null-assertion |
| 2 | +import { |
| 3 | + ModelWithABunchOfIndexes, ModelWithAutogeneratedId, |
| 4 | + ModelWithGSI, |
| 5 | + ModelWithLSI, |
| 6 | + SimpleWithCompositePartitionKeyModel, |
| 7 | + SimpleWithPartitionKeyModel, |
| 8 | +} from '../../../test/models' |
| 9 | +import { INDEX_ACTIVE, INDEX_ACTIVE_CREATED_AT, INDEX_COUNT } from '../../../test/models/model-with-indexes.model' |
| 10 | +import { Metadata } from './metadata' |
| 11 | + |
| 12 | +describe('metadata', () => { |
| 13 | + let metaDataPartitionKey: Metadata<SimpleWithPartitionKeyModel> |
| 14 | + let metaDataComposite: Metadata<SimpleWithCompositePartitionKeyModel> |
| 15 | + let metaDataLsi: Metadata<ModelWithLSI> |
| 16 | + let metaDataGsi: Metadata<ModelWithGSI> |
| 17 | + let metaDataIndexes: Metadata<ModelWithABunchOfIndexes> |
| 18 | + let metaDataUuid: Metadata<ModelWithAutogeneratedId> |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + metaDataPartitionKey = new Metadata(SimpleWithPartitionKeyModel) |
| 22 | + metaDataComposite = new Metadata(SimpleWithCompositePartitionKeyModel) |
| 23 | + metaDataLsi = new Metadata(ModelWithLSI) |
| 24 | + metaDataGsi = new Metadata(ModelWithGSI) |
| 25 | + metaDataIndexes = new Metadata(ModelWithABunchOfIndexes) |
| 26 | + metaDataUuid = new Metadata(ModelWithAutogeneratedId) |
| 27 | + }) |
| 28 | + |
| 29 | + it('forProperty', () => { |
| 30 | + const forId = metaDataPartitionKey.forProperty('id') |
| 31 | + expect(forId).toBeDefined() |
| 32 | + expect(forId!.key).toBeDefined() |
| 33 | + expect(forId!.name).toBe('id') |
| 34 | + expect(forId!.typeInfo).toBeDefined() |
| 35 | + expect(forId!.typeInfo!.isCustom).toBeFalsy() |
| 36 | + }) |
| 37 | + |
| 38 | + it('getKeysWithUUID', () => { |
| 39 | + const uuid = metaDataUuid.getKeysWithUUID() |
| 40 | + expect(uuid.length).toBe(1) |
| 41 | + expect(uuid[0].key).toBeDefined() |
| 42 | + expect(uuid[0].key!.uuid).toBeTruthy() |
| 43 | + expect(uuid[0].name).toBe('id') |
| 44 | + |
| 45 | + }) |
| 46 | + |
| 47 | + it('getPartitionKey', () => { |
| 48 | + expect(metaDataPartitionKey.getPartitionKey()).toEqual('id') |
| 49 | + expect(metaDataGsi.getPartitionKey(INDEX_ACTIVE)).toEqual('active') |
| 50 | + expect(metaDataIndexes.getPartitionKey(INDEX_COUNT)).toEqual('myId') |
| 51 | + expect(metaDataIndexes.getPartitionKey(INDEX_ACTIVE_CREATED_AT)).toEqual('active') |
| 52 | + |
| 53 | + }) |
| 54 | + |
| 55 | + it('getSortKey', () => { |
| 56 | + expect(metaDataPartitionKey.getSortKey()).toBe(null) |
| 57 | + expect(metaDataComposite.getSortKey()).toBe('creationDate') |
| 58 | + expect(metaDataLsi.getSortKey(INDEX_ACTIVE)).toBe('active') |
| 59 | + expect(() => metaDataGsi.getSortKey(INDEX_ACTIVE)).toThrow() |
| 60 | + expect(metaDataIndexes.getSortKey(INDEX_ACTIVE_CREATED_AT)).toBe('createdAt') |
| 61 | + }) |
| 62 | + |
| 63 | + it('getIndexes', () => { |
| 64 | + expect(metaDataLsi.getIndexes()).toEqual([ |
| 65 | + { partitionKey: 'id', sortKey: 'active' }, |
| 66 | + ]) |
| 67 | + expect(metaDataGsi.getIndexes()).toEqual([ |
| 68 | + { partitionKey: 'active' }, |
| 69 | + ]) |
| 70 | + expect(metaDataIndexes.getIndexes()).toEqual([ |
| 71 | + { partitionKey: 'active', sortKey: 'createdAt' }, |
| 72 | + { partitionKey: 'myId', sortKey: 'count' }, |
| 73 | + ]) |
| 74 | + }) |
| 75 | + |
| 76 | + it('getIndex', () => { |
| 77 | + expect(metaDataPartitionKey.getIndexes().length).toBe(0) |
| 78 | + expect(metaDataPartitionKey.getIndex('blub')).toBe(null) |
| 79 | + expect(metaDataIndexes.getIndex(INDEX_ACTIVE_CREATED_AT)).toEqual({ |
| 80 | + partitionKey: 'active', |
| 81 | + sortKey: 'createdAt', |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | +}) |
0 commit comments