-
Notifications
You must be signed in to change notification settings - Fork 481
Expand file tree
/
Copy pathtesting_your_code.py
More file actions
63 lines (54 loc) · 1.9 KB
/
Copy pathtesting_your_code.py
File metadata and controls
63 lines (54 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import base64
import json
from lambda_handler_test import lambda_handler
def test_process_json_message():
"""Test processing a simple JSON message"""
# Create a test Kafka event with JSON data
test_event = {
"eventSource": "aws:kafka",
"records": {
"orders-topic": [
{
"topic": "orders-topic",
"partition": 0,
"offset": 15,
"timestamp": 1545084650987,
"timestampType": "CREATE_TIME",
"key": None,
"value": base64.b64encode(json.dumps({"order_id": "12345", "amount": 99.95}).encode()).decode(),
},
],
},
}
# Invoke the Lambda handler
response = lambda_handler(test_event, {})
# Verify the response
assert response["statusCode"] == 200
assert response.get("processed") == 1
def test_process_multiple_records():
"""Test processing multiple records in a batch"""
# Create a test event with multiple records
test_event = {
"eventSource": "aws:kafka",
"records": {
"customers-topic": [
{
"topic": "customers-topic",
"partition": 0,
"offset": 10,
"value": base64.b64encode(json.dumps({"customer_id": "A1", "name": "Alice"}).encode()).decode(),
},
{
"topic": "customers-topic",
"partition": 0,
"offset": 11,
"value": base64.b64encode(json.dumps({"customer_id": "B2", "name": "Bob"}).encode()).decode(),
},
],
},
}
# Invoke the Lambda handler
response = lambda_handler(test_event, {})
# Verify the response
assert response["statusCode"] == 200
assert response.get("processed") == 2