|
| 1 | +import { getTableName } from '../../../test/helper' |
| 2 | +import { |
| 3 | + ModelWithABunchOfIndexes, |
| 4 | + SimpleWithCompositePartitionKeyModel, |
| 5 | + SimpleWithPartitionKeyModel, |
| 6 | +} from '../../../test/models' |
| 7 | +import { INDEX_ACTIVE_CREATED_AT, INDEX_COUNT } from '../../../test/models/model-with-indexes.model' |
| 8 | +import { ModelConstructor } from '../../model/model-constructor' |
| 9 | +import { Request } from './request.model' |
| 10 | + |
| 11 | + |
| 12 | +class TestRequest<T> extends Request<T, any, any, any> { |
| 13 | + |
| 14 | + constructor(modelClazz: ModelConstructor<T>) { |
| 15 | + super(<any>null, modelClazz, getTableName(modelClazz)) |
| 16 | + } |
| 17 | + |
| 18 | + exec() {return <any>null} |
| 19 | + |
| 20 | + execCount() {return <any>null} |
| 21 | + |
| 22 | + execFullResponse() {return <any>null} |
| 23 | + |
| 24 | + execNoMap() {return <any>null} |
| 25 | + |
| 26 | + execSingle() {return <any>null} |
| 27 | + |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +describe('Request', () => { |
| 32 | + let request: TestRequest<any> |
| 33 | + |
| 34 | + describe('constructor', () => { |
| 35 | + beforeEach(() => { |
| 36 | + request = new TestRequest(SimpleWithPartitionKeyModel) |
| 37 | + }) |
| 38 | + it('should set the default limit to params', () => { |
| 39 | + expect(request.params).toBeDefined() |
| 40 | + expect(request.params.Limit).toBe(Request.DEFAULT_LIMIT) |
| 41 | + }) |
| 42 | + }) |
| 43 | + |
| 44 | + describe('exclusiveStartKey', () => { |
| 45 | + beforeEach(() => { |
| 46 | + request = new TestRequest(SimpleWithPartitionKeyModel) |
| 47 | + }) |
| 48 | + |
| 49 | + it('should add ExclusiveStartKey to params', () => { |
| 50 | + request.exclusiveStartKey({ id: { S: 'myId' } }) |
| 51 | + expect(request.params.ExclusiveStartKey).toEqual({ id: { S: 'myId' } }) |
| 52 | + }) |
| 53 | + |
| 54 | + it('should remove ExclusiveStartKey when providing null', () => { |
| 55 | + request.exclusiveStartKey({ id: { S: 'myId' } }) |
| 56 | + expect(request.params.ExclusiveStartKey).toEqual({ id: { S: 'myId' } }) |
| 57 | + |
| 58 | + request.exclusiveStartKey(null) |
| 59 | + expect(request.params.ExclusiveStartKey).toBeUndefined() |
| 60 | + expect(Object.keys(request.params).includes('ExclusiveStartKey')).toBeFalsy() |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + describe('index', () => { |
| 65 | + beforeEach(() => { |
| 66 | + request = new TestRequest(ModelWithABunchOfIndexes) |
| 67 | + }) |
| 68 | + |
| 69 | + it('[GSI] should set index on params ', () => { |
| 70 | + request.index(INDEX_ACTIVE_CREATED_AT) |
| 71 | + expect(request.params.IndexName).toBe(INDEX_ACTIVE_CREATED_AT) |
| 72 | + }) |
| 73 | + |
| 74 | + it('[LSI] should set index on params ', () => { |
| 75 | + request.index(INDEX_COUNT) |
| 76 | + expect(request.params.IndexName).toBe(INDEX_COUNT) |
| 77 | + }) |
| 78 | + |
| 79 | + it('should throw when index does not exist on model', () => { |
| 80 | + expect(() => request.index('NON_EXISTENT_INDEX')).toThrow() |
| 81 | + }) |
| 82 | + |
| 83 | + }) |
| 84 | + |
| 85 | + describe('limit', () => { |
| 86 | + beforeEach(() => { |
| 87 | + request = new TestRequest(SimpleWithCompositePartitionKeyModel) |
| 88 | + }) |
| 89 | + |
| 90 | + it('should throw when a number was given less than 1 (except -1, which means INFINITE)', () => { |
| 91 | + expect(() => request.limit(-100)).toThrow() |
| 92 | + expect(() => request.limit(0)).toThrow() |
| 93 | + }) |
| 94 | + |
| 95 | + it('should set limit to params and be overridable', () => { |
| 96 | + request.limit(5) |
| 97 | + expect(request.params.Limit).toBe(5) |
| 98 | + |
| 99 | + request.limit(10) |
| 100 | + expect(request.params.Limit).toBe(10) |
| 101 | + }) |
| 102 | + |
| 103 | + it('should remove limit when INFINITE_LIMIT was given as arg', () => { |
| 104 | + request.limit(5) |
| 105 | + expect(request.params.Limit).toBe(5) |
| 106 | + request.limit(Request.INFINITE_LIMIT) |
| 107 | + expect(request.params.Limit).toBeUndefined() |
| 108 | + expect(Object.keys(request.params).includes('Limit')).toBeFalsy() |
| 109 | + }) |
| 110 | + |
| 111 | + }) |
| 112 | + |
| 113 | +}) |
0 commit comments