|
| 1 | +import * as DynamoDB from 'aws-sdk/clients/dynamodb' |
| 2 | +import * as moment from 'moment' |
| 3 | +import { of } from 'rxjs' |
| 4 | +import { getTableName } from '../../../../test/helper/get-table-name.function' |
| 5 | +import { Organization } from '../../../../test/models/organization.model' |
| 6 | +import { DEFAULT_SESSION_VALIDITY_ENSURER } from '../../default-session-validity-ensurer.const' |
| 7 | +import { DynamoRx } from '../../dynamo-rx' |
| 8 | +import { BatchWriteSingleTableRequest } from './batch-write-single-table.request' |
| 9 | + |
| 10 | +describe('batch write single table request', () => { |
| 11 | + const tableName = getTableName(Organization) |
| 12 | + |
| 13 | + let item: Organization |
| 14 | + let dynamoRx: DynamoRx |
| 15 | + let request: BatchWriteSingleTableRequest<Organization> |
| 16 | + |
| 17 | + let nextSpyFn: () => { value: number } |
| 18 | + const generatorMock = () => <any>{ next: nextSpyFn } |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + item = <any>{ |
| 22 | + id: 'myId', |
| 23 | + createdAtDate: moment(), |
| 24 | + name: 'myOrg', |
| 25 | + } |
| 26 | + nextSpyFn = jest.fn().mockImplementation(() => ({ value: 0 })) |
| 27 | + }) |
| 28 | + |
| 29 | + describe('correct params', () => { |
| 30 | + beforeEach(() => { |
| 31 | + dynamoRx = new DynamoRx(DEFAULT_SESSION_VALIDITY_ENSURER) |
| 32 | + request = new BatchWriteSingleTableRequest(dynamoRx, Organization, tableName) |
| 33 | + |
| 34 | + const output: DynamoDB.BatchWriteItemOutput = {} |
| 35 | + spyOn(dynamoRx, 'batchWriteItem').and.returnValue(of(output)) |
| 36 | + }) |
| 37 | + |
| 38 | + it('delete with complex primary key', async () => { |
| 39 | + request.delete([item]) |
| 40 | + await request.exec(generatorMock).toPromise() |
| 41 | + |
| 42 | + expect(dynamoRx.batchWriteItem).toHaveBeenCalledTimes(1) |
| 43 | + expect(dynamoRx.batchWriteItem).toHaveBeenCalledWith({ |
| 44 | + RequestItems: { |
| 45 | + [tableName]: [ |
| 46 | + { |
| 47 | + DeleteRequest: { |
| 48 | + Key: { |
| 49 | + id: { S: 'myId' }, |
| 50 | + createdAtDate: { S: item.createdAtDate.utc().format() }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + ], |
| 55 | + }, |
| 56 | + }) |
| 57 | + expect(nextSpyFn).toHaveBeenCalledTimes(0) |
| 58 | + }) |
| 59 | + |
| 60 | + it('put object', async () => { |
| 61 | + request.put([item]) |
| 62 | + await request.exec(generatorMock).toPromise() |
| 63 | + |
| 64 | + expect(dynamoRx.batchWriteItem).toHaveBeenCalledTimes(1) |
| 65 | + expect(dynamoRx.batchWriteItem).toHaveBeenCalledWith({ |
| 66 | + RequestItems: { |
| 67 | + [tableName]: [ |
| 68 | + { |
| 69 | + PutRequest: { |
| 70 | + Item: { |
| 71 | + id: { S: 'myId' }, |
| 72 | + createdAtDate: { S: item.createdAtDate.utc().format() }, |
| 73 | + name: { S: 'myOrg' }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + ], |
| 78 | + }, |
| 79 | + }) |
| 80 | + expect(nextSpyFn).toHaveBeenCalledTimes(0) |
| 81 | + }) |
| 82 | + |
| 83 | + it('delete >25 items in two requests', async () => { |
| 84 | + const twentyFiveItems = [] |
| 85 | + for (let i = 0; i < 25; i++) { |
| 86 | + twentyFiveItems.push(item) |
| 87 | + } |
| 88 | + request.delete(twentyFiveItems) |
| 89 | + request.delete(twentyFiveItems) |
| 90 | + await request.exec(generatorMock).toPromise() |
| 91 | + expect(dynamoRx.batchWriteItem).toHaveBeenCalledTimes(2) |
| 92 | + expect(nextSpyFn).toHaveBeenCalledTimes(0) |
| 93 | + }) |
| 94 | + }) |
| 95 | + |
| 96 | + describe('correct backoff', () => { |
| 97 | + beforeEach(() => { |
| 98 | + dynamoRx = new DynamoRx(DEFAULT_SESSION_VALIDITY_ENSURER) |
| 99 | + request = new BatchWriteSingleTableRequest(dynamoRx, Organization, tableName) |
| 100 | + |
| 101 | + const output: DynamoDB.BatchWriteItemOutput = { |
| 102 | + UnprocessedItems: { |
| 103 | + [tableName]: [ |
| 104 | + { |
| 105 | + PutRequest: { |
| 106 | + Item: { |
| 107 | + id: { S: 'myId' }, |
| 108 | + createdAtDate: { S: item.createdAtDate.utc().format() }, |
| 109 | + name: { S: 'myOrg' }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + ], |
| 114 | + }, |
| 115 | + } |
| 116 | + spyOn(dynamoRx, 'batchWriteItem').and.returnValues(of(output), of({})) |
| 117 | + }) |
| 118 | + |
| 119 | + it('should retry when capacity is exceeded', async () => { |
| 120 | + request.put([item]) |
| 121 | + await request.exec(generatorMock).toPromise() |
| 122 | + expect(dynamoRx.batchWriteItem).toHaveBeenCalledTimes(2) |
| 123 | + expect(nextSpyFn).toHaveBeenCalledTimes(1) |
| 124 | + }) |
| 125 | + }) |
| 126 | +}) |
0 commit comments