|
| 1 | +import { of } from 'rxjs' |
| 2 | +import { ComplexModel, SimpleWithPartitionKeyModel } from '../../../test/models' |
| 3 | +import { getTableName } from '../get-table-name.function' |
| 4 | +import { BatchWriteRequest } from './batch-write.request' |
| 5 | + |
| 6 | +describe('batchWriteRequest', () => { |
| 7 | + let req: BatchWriteRequest |
| 8 | + |
| 9 | + describe('constructor', () => { |
| 10 | + it('should initialize params', () => { |
| 11 | + req = new BatchWriteRequest() |
| 12 | + expect(req.params.RequestItems).toBeDefined() |
| 13 | + expect(req.params.RequestItems).toEqual({}) |
| 14 | + }) |
| 15 | + }) |
| 16 | + |
| 17 | + describe('returnConsumedCapacity', () => { |
| 18 | + it('should set params', () => { |
| 19 | + req = new BatchWriteRequest().returnConsumedCapacity('TOTAL') |
| 20 | + expect(req.params.ReturnConsumedCapacity).toBe('TOTAL') |
| 21 | + }) |
| 22 | + }) |
| 23 | + |
| 24 | + describe('returnItemCollectionMetrics', () => { |
| 25 | + it('should set params', () => { |
| 26 | + req = new BatchWriteRequest().returnItemCollectionMetrics('SIZE') |
| 27 | + expect(req.params.ReturnItemCollectionMetrics).toBe('SIZE') |
| 28 | + }) |
| 29 | + }) |
| 30 | + |
| 31 | + describe('delete', () => { |
| 32 | + const now = new Date() |
| 33 | + |
| 34 | + it('should set params', () => { |
| 35 | + req = new BatchWriteRequest() |
| 36 | + .delete(SimpleWithPartitionKeyModel, [{ id: 'myId1' }]) |
| 37 | + .delete(ComplexModel, [{ id: 'myId2', creationDate: now }]) |
| 38 | + |
| 39 | + expect(Object.keys(req.params.RequestItems)).toEqual([ |
| 40 | + getTableName(SimpleWithPartitionKeyModel), |
| 41 | + getTableName(ComplexModel), |
| 42 | + ]) |
| 43 | + |
| 44 | + const simpleReqItems = req.params.RequestItems[getTableName(SimpleWithPartitionKeyModel)] |
| 45 | + expect(simpleReqItems).toBeDefined() |
| 46 | + expect(simpleReqItems.length).toBe(1) |
| 47 | + expect(simpleReqItems[0].DeleteRequest).toEqual({ |
| 48 | + Key: { id: { S: 'myId1' } }, |
| 49 | + }) |
| 50 | + |
| 51 | + const complexReqItems = req.params.RequestItems[getTableName(ComplexModel)] |
| 52 | + expect(complexReqItems).toBeDefined() |
| 53 | + expect(complexReqItems.length).toBe(1) |
| 54 | + expect(complexReqItems[0].DeleteRequest).toEqual({ |
| 55 | + Key: { id: { S: 'myId2' }, creationDate: { S: now.toISOString() } }, |
| 56 | + }) |
| 57 | + }) |
| 58 | + |
| 59 | + it('should throw when too many items', () => { |
| 60 | + const items: Array<Partial<SimpleWithPartitionKeyModel>> = [...new Array(30)].map((_, ix) => ({ |
| 61 | + id: `mydId-${ix}`, |
| 62 | + })) |
| 63 | + expect(() => new BatchWriteRequest().delete(SimpleWithPartitionKeyModel, items)).toThrow() |
| 64 | + }) |
| 65 | + }) |
| 66 | + |
| 67 | + describe('put', () => { |
| 68 | + const now = new Date() |
| 69 | + |
| 70 | + it('should set params', () => { |
| 71 | + req = new BatchWriteRequest() |
| 72 | + .put(SimpleWithPartitionKeyModel, [{ id: 'myId1' }]) |
| 73 | + .put(ComplexModel, [{ id: 'myId2', creationDate: now }]) |
| 74 | + |
| 75 | + const simpleReqItems = req.params.RequestItems[getTableName(SimpleWithPartitionKeyModel)] |
| 76 | + expect(simpleReqItems).toBeDefined() |
| 77 | + expect(simpleReqItems.length).toBe(1) |
| 78 | + expect(simpleReqItems[0].PutRequest).toEqual({ |
| 79 | + Item: { id: { S: 'myId1' } }, |
| 80 | + }) |
| 81 | + |
| 82 | + const complexReqItems = req.params.RequestItems[getTableName(ComplexModel)] |
| 83 | + expect(complexReqItems).toBeDefined() |
| 84 | + expect(complexReqItems.length).toBe(1) |
| 85 | + expect(complexReqItems[0].PutRequest).toEqual({ |
| 86 | + Item: { id: { S: 'myId2' }, creationDate: { S: now.toISOString() } }, |
| 87 | + }) |
| 88 | + }) |
| 89 | + |
| 90 | + it('should throw when too many items', () => { |
| 91 | + const items: SimpleWithPartitionKeyModel[] = [...new Array(30)].map((_, ix) => ({ id: `mydId-${ix}`, age: ix })) |
| 92 | + expect(() => new BatchWriteRequest().put(SimpleWithPartitionKeyModel, items)).toThrow() |
| 93 | + }) |
| 94 | + }) |
| 95 | + |
| 96 | + describe('put and delete mixed', () => { |
| 97 | + const now = new Date() |
| 98 | + const tnSimple = getTableName(SimpleWithPartitionKeyModel) |
| 99 | + const tnComplex = getTableName(ComplexModel) |
| 100 | + |
| 101 | + const simpleItem: SimpleWithPartitionKeyModel = { id: 'myId', age: 25 } |
| 102 | + const simpleItems: SimpleWithPartitionKeyModel[] = [...new Array(16)].map((_, ix) => ({ |
| 103 | + id: `mydId-${ix}`, |
| 104 | + age: ix, |
| 105 | + })) |
| 106 | + const complexItems: ComplexModel[] = [...new Array(16)].map( |
| 107 | + (_, ix) => <ComplexModel>{ id: `myId-${ix}`, creationDate: now }, |
| 108 | + ) |
| 109 | + |
| 110 | + beforeEach(() => (req = new BatchWriteRequest())) |
| 111 | + |
| 112 | + it('should add correct request items', () => { |
| 113 | + req.put(ComplexModel, complexItems).delete(SimpleWithPartitionKeyModel, [simpleItem]) |
| 114 | + expect(req.params.RequestItems[tnComplex].length).toBe(16) |
| 115 | + expect(req.params.RequestItems[tnComplex][5].PutRequest).toBeDefined() |
| 116 | + |
| 117 | + expect(req.params.RequestItems[tnSimple].length).toBe(1) |
| 118 | + expect(req.params.RequestItems[tnSimple][0].DeleteRequest).toBeDefined() |
| 119 | + }) |
| 120 | + |
| 121 | + it('should throw when too many (1)', () => { |
| 122 | + expect(() => req.put(ComplexModel, complexItems).delete(SimpleWithPartitionKeyModel, simpleItems)).toThrow() |
| 123 | + }) |
| 124 | + it('should throw when too many (2)', () => { |
| 125 | + expect(() => req.delete(ComplexModel, complexItems).put(SimpleWithPartitionKeyModel, simpleItems)).toThrow() |
| 126 | + }) |
| 127 | + }) |
| 128 | + |
| 129 | + describe('exec functions', () => { |
| 130 | + let batchWriteItemSpy: jasmine.Spy |
| 131 | + |
| 132 | + beforeEach(() => { |
| 133 | + const output = { |
| 134 | + myResponse: true, |
| 135 | + } |
| 136 | + batchWriteItemSpy = jasmine.createSpy().and.returnValue(of(output)) |
| 137 | + const dynamoRx = <any>{ batchWriteItem: batchWriteItemSpy } |
| 138 | + req = new BatchWriteRequest() |
| 139 | + Object.assign(req, { dynamoRx }) |
| 140 | + }) |
| 141 | + |
| 142 | + it('exec should return void', async () => { |
| 143 | + expect(await req.exec().toPromise()).toBeUndefined() |
| 144 | + }) |
| 145 | + |
| 146 | + it('execFullResponse should return the full response', async () => { |
| 147 | + expect(await req.execFullResponse().toPromise()).toEqual({ myResponse: true }) |
| 148 | + }) |
| 149 | + }) |
| 150 | +}) |
0 commit comments