Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a68db2c

Browse files
erikayao93dreamorosi
authored andcommittedJul 15, 2023
feat(batch): Batch processing wrapper function (#1605)
* Refactored some types, added function wrapper and base test * Added record check and tests, renamed factories * Refactored type check logic in function * Refactor test to remove error ignore
1 parent cffe3ed commit a68db2c

File tree

8 files changed

+299
-75
lines changed

8 files changed

+299
-75
lines changed
 

‎packages/batch/src/BasePartialBatchProcessor.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import {
99
DEFAULT_RESPONSE,
1010
EventSourceDataClassTypes,
1111
EventType,
12-
ItemIdentifier,
13-
BatchResponse,
12+
PartialItemFailures,
13+
PartialItemFailureResponse,
1414
} from '.';
1515

1616
abstract class BasePartialBatchProcessor extends BasePartialProcessor {
1717
public COLLECTOR_MAPPING;
1818

19-
public batchResponse: BatchResponse;
19+
public batchResponse: PartialItemFailureResponse;
2020

2121
public eventType: keyof typeof EventType;
2222

@@ -52,16 +52,16 @@ abstract class BasePartialBatchProcessor extends BasePartialProcessor {
5252
);
5353
}
5454

55-
const messages: ItemIdentifier[] = this.getMessagesToReport();
55+
const messages: PartialItemFailures[] = this.getMessagesToReport();
5656
this.batchResponse = { batchItemFailures: messages };
5757
}
5858

5959
/**
6060
* Collects identifiers of failed items for a DynamoDB stream
6161
* @returns list of identifiers for failed items
6262
*/
63-
public collectDynamoDBFailures(): ItemIdentifier[] {
64-
const failures: ItemIdentifier[] = [];
63+
public collectDynamoDBFailures(): PartialItemFailures[] {
64+
const failures: PartialItemFailures[] = [];
6565

6666
for (const msg of this.failureMessages) {
6767
const msgId = (msg as DynamoDBRecord).dynamodb?.SequenceNumber;
@@ -77,8 +77,8 @@ abstract class BasePartialBatchProcessor extends BasePartialProcessor {
7777
* Collects identifiers of failed items for a Kinesis stream
7878
* @returns list of identifiers for failed items
7979
*/
80-
public collectKinesisFailures(): ItemIdentifier[] {
81-
const failures: ItemIdentifier[] = [];
80+
public collectKinesisFailures(): PartialItemFailures[] {
81+
const failures: PartialItemFailures[] = [];
8282

8383
for (const msg of this.failureMessages) {
8484
const msgId = (msg as KinesisStreamRecord).kinesis.sequenceNumber;
@@ -92,8 +92,8 @@ abstract class BasePartialBatchProcessor extends BasePartialProcessor {
9292
* Collects identifiers of failed items for an SQS batch
9393
* @returns list of identifiers for failed items
9494
*/
95-
public collectSqsFailures(): ItemIdentifier[] {
96-
const failures: ItemIdentifier[] = [];
95+
public collectSqsFailures(): PartialItemFailures[] {
96+
const failures: PartialItemFailures[] = [];
9797

9898
for (const msg of this.failureMessages) {
9999
const msgId = (msg as SQSRecord).messageId;
@@ -115,7 +115,7 @@ abstract class BasePartialBatchProcessor extends BasePartialProcessor {
115115
* Collects identifiers for failed batch items
116116
* @returns formatted messages to use in batch deletion
117117
*/
118-
public getMessagesToReport(): ItemIdentifier[] {
118+
public getMessagesToReport(): PartialItemFailures[] {
119119
return this.COLLECTOR_MAPPING[this.eventType]();
120120
}
121121

@@ -146,7 +146,7 @@ abstract class BasePartialBatchProcessor extends BasePartialProcessor {
146146
/**
147147
* @returns Batch items that failed processing, if any
148148
*/
149-
public response(): BatchResponse {
149+
public response(): PartialItemFailureResponse {
150150
return this.batchResponse;
151151
}
152152

‎packages/batch/src/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
* Constants for batch processor classes
33
*/
44
import { DynamoDBRecord, KinesisStreamRecord, SQSRecord } from 'aws-lambda';
5-
import type { BatchResponse, EventSourceDataClassTypes } from '.';
5+
import type { PartialItemFailureResponse, EventSourceDataClassTypes } from '.';
66

77
const EventType = {
88
SQS: 'SQS',
99
KinesisDataStreams: 'KinesisDataStreams',
1010
DynamoDBStreams: 'DynamoDBStreams',
1111
} as const;
1212

13-
const DEFAULT_RESPONSE: BatchResponse = {
13+
const DEFAULT_RESPONSE: PartialItemFailureResponse = {
1414
batchItemFailures: [],
1515
};
1616

‎packages/batch/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ export * from './errors';
33
export * from './types';
44
export * from './BasePartialProcessor';
55
export * from './BasePartialBatchProcessor';
6+
export * from './BatchProcessor';
7+
export * from './processPartialResponse';
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {
2+
BasePartialBatchProcessor,
3+
BaseRecord,
4+
EventType,
5+
PartialItemFailureResponse,
6+
} from '.';
7+
8+
const processPartialResponse = async (
9+
event: { Records: BaseRecord[] },
10+
recordHandler: CallableFunction,
11+
processor: BasePartialBatchProcessor
12+
): Promise<PartialItemFailureResponse> => {
13+
if (!event.Records) {
14+
const eventTypes: string = Object.values(EventType).toString();
15+
throw new Error(
16+
'Failed to convert event to record batch for processing.\nPlease ensure batch event is a valid ' +
17+
eventTypes +
18+
' event.'
19+
);
20+
}
21+
22+
const records = event['Records'];
23+
24+
processor.register(records, recordHandler);
25+
await processor.process();
26+
27+
return processor.response();
28+
};
29+
30+
export { processPartialResponse };

‎packages/batch/src/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ type SuccessResponse = [string, ResultType, EventSourceDataClassTypes];
1717

1818
type FailureResponse = [string, string, EventSourceDataClassTypes];
1919

20-
type ItemIdentifier = { [key: string]: string };
21-
type BatchResponse = { [key: string]: ItemIdentifier[] };
20+
type PartialItemFailures = { itemIdentifier: string };
21+
type PartialItemFailureResponse = { batchItemFailures: PartialItemFailures[] };
2222

2323
export type {
2424
BaseRecord,
2525
EventSourceDataClassTypes,
2626
ResultType,
2727
SuccessResponse,
2828
FailureResponse,
29-
ItemIdentifier,
30-
BatchResponse,
29+
PartialItemFailures,
30+
PartialItemFailureResponse,
3131
};

‎packages/batch/tests/helpers/factories.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { DynamoDBRecord, KinesisStreamRecord, SQSRecord } from 'aws-lambda';
22
import { randomInt } from 'crypto';
33
import { v4 } from 'uuid';
44

5-
const sqsEventFactory = (body: string): SQSRecord => {
5+
const sqsRecordFactory = (body: string): SQSRecord => {
66
return {
77
messageId: v4(),
88
receiptHandle: 'AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a',
@@ -21,7 +21,7 @@ const sqsEventFactory = (body: string): SQSRecord => {
2121
};
2222
};
2323

24-
const kinesisEventFactory = (body: string): KinesisStreamRecord => {
24+
const kinesisRecordFactory = (body: string): KinesisStreamRecord => {
2525
let seq = '';
2626
for (let i = 0; i < 52; i++) {
2727
seq = seq + randomInt(10);
@@ -46,7 +46,7 @@ const kinesisEventFactory = (body: string): KinesisStreamRecord => {
4646
};
4747
};
4848

49-
const dynamodbEventFactory = (body: string): DynamoDBRecord => {
49+
const dynamodbRecordFactory = (body: string): DynamoDBRecord => {
5050
let seq = '';
5151
for (let i = 0; i < 10; i++) {
5252
seq = seq + randomInt(10);
@@ -69,4 +69,4 @@ const dynamodbEventFactory = (body: string): DynamoDBRecord => {
6969
};
7070
};
7171

72-
export { sqsEventFactory, kinesisEventFactory, dynamodbEventFactory };
72+
export { sqsRecordFactory, kinesisRecordFactory, dynamodbRecordFactory };

‎packages/batch/tests/unit/BatchProcessor.test.ts

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
import { BatchProcessingError, BatchProcessor, EventType } from '../../src';
88
import {
9-
sqsEventFactory,
10-
kinesisEventFactory,
11-
dynamodbEventFactory,
9+
sqsRecordFactory,
10+
kinesisRecordFactory,
11+
dynamodbRecordFactory,
1212
} from '../../tests/helpers/factories';
1313
import {
1414
sqsRecordHandler,
@@ -35,8 +35,8 @@ describe('Class: BatchProcessor', () => {
3535
describe('Synchronously processing SQS Records', () => {
3636
test('Batch processing SQS records with no failures', async () => {
3737
// Prepare
38-
const firstRecord = sqsEventFactory('success');
39-
const secondRecord = sqsEventFactory('success');
38+
const firstRecord = sqsRecordFactory('success');
39+
const secondRecord = sqsRecordFactory('success');
4040
const records = [firstRecord, secondRecord];
4141
const processor = new BatchProcessor(EventType.SQS);
4242

@@ -53,9 +53,9 @@ describe('Class: BatchProcessor', () => {
5353

5454
test('Batch processing SQS records with some failures', async () => {
5555
// Prepare
56-
const firstRecord = sqsEventFactory('failure');
57-
const secondRecord = sqsEventFactory('success');
58-
const thirdRecord = sqsEventFactory('fail');
56+
const firstRecord = sqsRecordFactory('failure');
57+
const secondRecord = sqsRecordFactory('success');
58+
const thirdRecord = sqsRecordFactory('fail');
5959
const records = [firstRecord, secondRecord, thirdRecord];
6060
const processor = new BatchProcessor(EventType.SQS);
6161

@@ -80,9 +80,9 @@ describe('Class: BatchProcessor', () => {
8080

8181
test('Batch processing SQS records with all failures', async () => {
8282
// Prepare
83-
const firstRecord = sqsEventFactory('failure');
84-
const secondRecord = sqsEventFactory('failure');
85-
const thirdRecord = sqsEventFactory('fail');
83+
const firstRecord = sqsRecordFactory('failure');
84+
const secondRecord = sqsRecordFactory('failure');
85+
const thirdRecord = sqsRecordFactory('fail');
8686

8787
const records = [firstRecord, secondRecord, thirdRecord];
8888
const processor = new BatchProcessor(EventType.SQS);
@@ -98,8 +98,8 @@ describe('Class: BatchProcessor', () => {
9898
describe('Asynchronously processing SQS Records', () => {
9999
test('Batch processing SQS records with no failures', async () => {
100100
// Prepare
101-
const firstRecord = sqsEventFactory('success');
102-
const secondRecord = sqsEventFactory('success');
101+
const firstRecord = sqsRecordFactory('success');
102+
const secondRecord = sqsRecordFactory('success');
103103
const records = [firstRecord, secondRecord];
104104
const processor = new BatchProcessor(EventType.SQS);
105105

@@ -116,9 +116,9 @@ describe('Class: BatchProcessor', () => {
116116

117117
test('Batch processing SQS records with some failures', async () => {
118118
// Prepare
119-
const firstRecord = sqsEventFactory('failure');
120-
const secondRecord = sqsEventFactory('success');
121-
const thirdRecord = sqsEventFactory('fail');
119+
const firstRecord = sqsRecordFactory('failure');
120+
const secondRecord = sqsRecordFactory('success');
121+
const thirdRecord = sqsRecordFactory('fail');
122122
const records = [firstRecord, secondRecord, thirdRecord];
123123
const processor = new BatchProcessor(EventType.SQS);
124124

@@ -143,9 +143,9 @@ describe('Class: BatchProcessor', () => {
143143

144144
test('Batch processing SQS records with all failures', async () => {
145145
// Prepare
146-
const firstRecord = sqsEventFactory('failure');
147-
const secondRecord = sqsEventFactory('failure');
148-
const thirdRecord = sqsEventFactory('fail');
146+
const firstRecord = sqsRecordFactory('failure');
147+
const secondRecord = sqsRecordFactory('failure');
148+
const thirdRecord = sqsRecordFactory('fail');
149149

150150
const records = [firstRecord, secondRecord, thirdRecord];
151151
const processor = new BatchProcessor(EventType.SQS);
@@ -163,8 +163,8 @@ describe('Class: BatchProcessor', () => {
163163
describe('Synchronously processing Kinesis Records', () => {
164164
test('Batch processing Kinesis records with no failures', async () => {
165165
// Prepare
166-
const firstRecord = kinesisEventFactory('success');
167-
const secondRecord = kinesisEventFactory('success');
166+
const firstRecord = kinesisRecordFactory('success');
167+
const secondRecord = kinesisRecordFactory('success');
168168
const records = [firstRecord, secondRecord];
169169
const processor = new BatchProcessor(EventType.KinesisDataStreams);
170170

@@ -181,9 +181,9 @@ describe('Class: BatchProcessor', () => {
181181

182182
test('Batch processing Kinesis records with some failures', async () => {
183183
// Prepare
184-
const firstRecord = kinesisEventFactory('failure');
185-
const secondRecord = kinesisEventFactory('success');
186-
const thirdRecord = kinesisEventFactory('fail');
184+
const firstRecord = kinesisRecordFactory('failure');
185+
const secondRecord = kinesisRecordFactory('success');
186+
const thirdRecord = kinesisRecordFactory('fail');
187187
const records = [firstRecord, secondRecord, thirdRecord];
188188
const processor = new BatchProcessor(EventType.KinesisDataStreams);
189189

@@ -207,9 +207,9 @@ describe('Class: BatchProcessor', () => {
207207
});
208208

209209
test('Batch processing Kinesis records with all failures', async () => {
210-
const firstRecord = kinesisEventFactory('failure');
211-
const secondRecord = kinesisEventFactory('failure');
212-
const thirdRecord = kinesisEventFactory('fail');
210+
const firstRecord = kinesisRecordFactory('failure');
211+
const secondRecord = kinesisRecordFactory('failure');
212+
const thirdRecord = kinesisRecordFactory('fail');
213213

214214
const records = [firstRecord, secondRecord, thirdRecord];
215215
const processor = new BatchProcessor(EventType.KinesisDataStreams);
@@ -227,8 +227,8 @@ describe('Class: BatchProcessor', () => {
227227
describe('Asynchronously processing Kinesis Records', () => {
228228
test('Batch processing Kinesis records with no failures', async () => {
229229
// Prepare
230-
const firstRecord = kinesisEventFactory('success');
231-
const secondRecord = kinesisEventFactory('success');
230+
const firstRecord = kinesisRecordFactory('success');
231+
const secondRecord = kinesisRecordFactory('success');
232232
const records = [firstRecord, secondRecord];
233233
const processor = new BatchProcessor(EventType.KinesisDataStreams);
234234

@@ -245,9 +245,9 @@ describe('Class: BatchProcessor', () => {
245245

246246
test('Batch processing Kinesis records with some failures', async () => {
247247
// Prepare
248-
const firstRecord = kinesisEventFactory('failure');
249-
const secondRecord = kinesisEventFactory('success');
250-
const thirdRecord = kinesisEventFactory('fail');
248+
const firstRecord = kinesisRecordFactory('failure');
249+
const secondRecord = kinesisRecordFactory('success');
250+
const thirdRecord = kinesisRecordFactory('fail');
251251
const records = [firstRecord, secondRecord, thirdRecord];
252252
const processor = new BatchProcessor(EventType.KinesisDataStreams);
253253

@@ -272,9 +272,9 @@ describe('Class: BatchProcessor', () => {
272272

273273
test('Batch processing Kinesis records with all failures', async () => {
274274
// Prepare
275-
const firstRecord = kinesisEventFactory('failure');
276-
const secondRecord = kinesisEventFactory('failure');
277-
const thirdRecord = kinesisEventFactory('fail');
275+
const firstRecord = kinesisRecordFactory('failure');
276+
const secondRecord = kinesisRecordFactory('failure');
277+
const thirdRecord = kinesisRecordFactory('fail');
278278

279279
const records = [firstRecord, secondRecord, thirdRecord];
280280
const processor = new BatchProcessor(EventType.KinesisDataStreams);
@@ -292,8 +292,8 @@ describe('Class: BatchProcessor', () => {
292292
describe('Synchronously processing DynamoDB Records', () => {
293293
test('Batch processing DynamoDB records with no failures', async () => {
294294
// Prepare
295-
const firstRecord = dynamodbEventFactory('success');
296-
const secondRecord = dynamodbEventFactory('success');
295+
const firstRecord = dynamodbRecordFactory('success');
296+
const secondRecord = dynamodbRecordFactory('success');
297297
const records = [firstRecord, secondRecord];
298298
const processor = new BatchProcessor(EventType.DynamoDBStreams);
299299

@@ -308,11 +308,11 @@ describe('Class: BatchProcessor', () => {
308308
]);
309309
});
310310

311-
test('Batch processing DynamoDB records with failures', async () => {
311+
test('Batch processing DynamoDB records with some failures', async () => {
312312
// Prepare
313-
const firstRecord = dynamodbEventFactory('failure');
314-
const secondRecord = dynamodbEventFactory('success');
315-
const thirdRecord = dynamodbEventFactory('fail');
313+
const firstRecord = dynamodbRecordFactory('failure');
314+
const secondRecord = dynamodbRecordFactory('success');
315+
const thirdRecord = dynamodbRecordFactory('fail');
316316
const records = [firstRecord, secondRecord, thirdRecord];
317317
const processor = new BatchProcessor(EventType.DynamoDBStreams);
318318

@@ -337,9 +337,9 @@ describe('Class: BatchProcessor', () => {
337337

338338
test('Batch processing DynamoDB records with all failures', async () => {
339339
// Prepare
340-
const firstRecord = dynamodbEventFactory('failure');
341-
const secondRecord = dynamodbEventFactory('failure');
342-
const thirdRecord = dynamodbEventFactory('fail');
340+
const firstRecord = dynamodbRecordFactory('failure');
341+
const secondRecord = dynamodbRecordFactory('failure');
342+
const thirdRecord = dynamodbRecordFactory('fail');
343343

344344
const records = [firstRecord, secondRecord, thirdRecord];
345345
const processor = new BatchProcessor(EventType.DynamoDBStreams);
@@ -357,8 +357,8 @@ describe('Class: BatchProcessor', () => {
357357
describe('Asynchronously processing DynamoDB Records', () => {
358358
test('Batch processing DynamoDB records with no failures', async () => {
359359
// Prepare
360-
const firstRecord = dynamodbEventFactory('success');
361-
const secondRecord = dynamodbEventFactory('success');
360+
const firstRecord = dynamodbRecordFactory('success');
361+
const secondRecord = dynamodbRecordFactory('success');
362362
const records = [firstRecord, secondRecord];
363363
const processor = new BatchProcessor(EventType.DynamoDBStreams);
364364

@@ -373,11 +373,11 @@ describe('Class: BatchProcessor', () => {
373373
]);
374374
});
375375

376-
test('Batch processing DynamoDB records with failures', async () => {
376+
test('Batch processing DynamoDB records with some failures', async () => {
377377
// Prepare
378-
const firstRecord = dynamodbEventFactory('failure');
379-
const secondRecord = dynamodbEventFactory('success');
380-
const thirdRecord = dynamodbEventFactory('fail');
378+
const firstRecord = dynamodbRecordFactory('failure');
379+
const secondRecord = dynamodbRecordFactory('success');
380+
const thirdRecord = dynamodbRecordFactory('fail');
381381
const records = [firstRecord, secondRecord, thirdRecord];
382382
const processor = new BatchProcessor(EventType.DynamoDBStreams);
383383

@@ -402,9 +402,9 @@ describe('Class: BatchProcessor', () => {
402402

403403
test('Batch processing DynamoDB records with all failures', async () => {
404404
// Prepare
405-
const firstRecord = dynamodbEventFactory('failure');
406-
const secondRecord = dynamodbEventFactory('failure');
407-
const thirdRecord = dynamodbEventFactory('fail');
405+
const firstRecord = dynamodbRecordFactory('failure');
406+
const secondRecord = dynamodbRecordFactory('failure');
407+
const thirdRecord = dynamodbRecordFactory('fail');
408408

409409
const records = [firstRecord, secondRecord, thirdRecord];
410410
const processor = new BatchProcessor(EventType.DynamoDBStreams);
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
/**
2+
* Test processPartialResponse function
3+
*
4+
* @group unit/batch/function/processpartialresponse
5+
*/
6+
7+
import {
8+
Context,
9+
DynamoDBStreamEvent,
10+
KinesisStreamEvent,
11+
SQSEvent,
12+
} from 'aws-lambda';
13+
import {
14+
BatchProcessor,
15+
EventType,
16+
PartialItemFailureResponse,
17+
processPartialResponse,
18+
} from '../../src';
19+
import {
20+
dynamodbRecordFactory,
21+
kinesisRecordFactory,
22+
sqsRecordFactory,
23+
} from '../../tests/helpers/factories';
24+
import {
25+
asyncSqsRecordHandler,
26+
dynamodbRecordHandler,
27+
kinesisRecordHandler,
28+
sqsRecordHandler,
29+
} from '../../tests/helpers/handlers';
30+
import { helloworldContext as dummyContext } from '../../../commons/src/samples/resources/contexts';
31+
import { Custom as dummyEvent } from '../../../commons/src/samples/resources/events';
32+
33+
describe('Function: processPartialResponse()', () => {
34+
const ENVIRONMENT_VARIABLES = process.env;
35+
const context = dummyContext;
36+
37+
beforeEach(() => {
38+
jest.clearAllMocks();
39+
jest.resetModules();
40+
process.env = { ...ENVIRONMENT_VARIABLES };
41+
});
42+
43+
afterAll(() => {
44+
process.env = ENVIRONMENT_VARIABLES;
45+
});
46+
47+
describe('Process partial response function call tests', () => {
48+
test('Process partial response function call with synchronous handler', async () => {
49+
// Prepare
50+
const records = [
51+
sqsRecordFactory('success'),
52+
sqsRecordFactory('success'),
53+
];
54+
const batch = { Records: records };
55+
const processor = new BatchProcessor(EventType.SQS);
56+
57+
// Act
58+
const ret = await processPartialResponse(
59+
batch,
60+
sqsRecordHandler,
61+
processor
62+
);
63+
64+
// Assess
65+
expect(ret).toStrictEqual({ batchItemFailures: [] });
66+
});
67+
68+
test('Process partial response function call with asynchronous handler', async () => {
69+
// Prepare
70+
const records = [
71+
sqsRecordFactory('success'),
72+
sqsRecordFactory('success'),
73+
];
74+
const batch = { Records: records };
75+
const processor = new BatchProcessor(EventType.SQS);
76+
77+
// Act
78+
const ret = await processPartialResponse(
79+
batch,
80+
asyncSqsRecordHandler,
81+
processor
82+
);
83+
84+
// Assess
85+
expect(ret).toStrictEqual({ batchItemFailures: [] });
86+
});
87+
});
88+
89+
describe('Process partial response function call through handler', () => {
90+
test('Process partial response through handler with SQS event', async () => {
91+
// Prepare
92+
const records = [
93+
sqsRecordFactory('success'),
94+
sqsRecordFactory('success'),
95+
];
96+
const processor = new BatchProcessor(EventType.SQS);
97+
const event: SQSEvent = { Records: records };
98+
99+
const handler = async (
100+
event: SQSEvent,
101+
_context: Context
102+
): Promise<PartialItemFailureResponse> => {
103+
return await processPartialResponse(event, sqsRecordHandler, processor);
104+
};
105+
106+
// Act
107+
const result = await handler(event, context);
108+
109+
// Assess
110+
expect(result).toStrictEqual({ batchItemFailures: [] });
111+
});
112+
113+
test('Process partial response through handler with Kinesis event', async () => {
114+
// Prepare
115+
const records = [
116+
kinesisRecordFactory('success'),
117+
kinesisRecordFactory('success'),
118+
];
119+
const processor = new BatchProcessor(EventType.KinesisDataStreams);
120+
const event: KinesisStreamEvent = { Records: records };
121+
122+
const handler = async (
123+
event: KinesisStreamEvent,
124+
_context: Context
125+
): Promise<PartialItemFailureResponse> => {
126+
return await processPartialResponse(
127+
event,
128+
kinesisRecordHandler,
129+
processor
130+
);
131+
};
132+
133+
// Act
134+
const result = await handler(event, context);
135+
136+
// Assess
137+
expect(result).toStrictEqual({ batchItemFailures: [] });
138+
});
139+
140+
test('Process partial response through handler with DynamoDB event', async () => {
141+
// Prepare
142+
const records = [
143+
dynamodbRecordFactory('success'),
144+
dynamodbRecordFactory('success'),
145+
];
146+
const processor = new BatchProcessor(EventType.DynamoDBStreams);
147+
const event: DynamoDBStreamEvent = { Records: records };
148+
149+
const handler = async (
150+
event: DynamoDBStreamEvent,
151+
_context: Context
152+
): Promise<PartialItemFailureResponse> => {
153+
return await processPartialResponse(
154+
event,
155+
dynamodbRecordHandler,
156+
processor
157+
);
158+
};
159+
160+
// Act
161+
const result = await handler(event, context);
162+
163+
// Assess
164+
expect(result).toStrictEqual({ batchItemFailures: [] });
165+
});
166+
167+
test('Process partial response through handler for SQS records with incorrect event type', async () => {
168+
// Prepare
169+
const processor = new BatchProcessor(EventType.SQS);
170+
const event = dummyEvent;
171+
const eventTypes: string = Object.values(EventType).toString();
172+
173+
const handler = async (
174+
event: SQSEvent,
175+
_context: Context
176+
): Promise<PartialItemFailureResponse> => {
177+
return await processPartialResponse(event, sqsRecordHandler, processor);
178+
};
179+
180+
// Act & Assess
181+
await expect(
182+
handler(event as unknown as SQSEvent, context)
183+
).rejects.toThrowError(
184+
new Error(
185+
'Failed to convert event to record batch for processing.\nPlease ensure batch event is a valid ' +
186+
eventTypes +
187+
' event.'
188+
)
189+
);
190+
});
191+
});
192+
});

0 commit comments

Comments
 (0)
Please sign in to comment.