Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test helper to generate DynamoDB event #1945

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 24 additions & 1 deletion chalice/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import contextlib
from types import TracebackType

from typing import Optional, Type, Generator, Dict, Any, List # noqa
from typing import Optional, Type, Generator, Dict, Any, List, Tuple # noqa

from chalice import Chalice # noqa
from chalice.config import Config
Expand Down Expand Up @@ -323,6 +323,29 @@ def generate_kinesis_event(self, message_bodies,
} for body in message_bodies]
return {'Records': records}

def generate_dynamodb_event(self, images):
# type: (List[Tuple[Dict, Dict]]) -> Dict[str, Any]
records = [{
"dynamodb": {
"ApproximateCreationDateTime": 1545084650.987,
"Keys": list(new_image.keys()),
"NewImage": new_image,
IdrisMiles marked this conversation as resolved.
Show resolved Hide resolved
"OldImage": old_image,
"SequenceNumber": "12345",
"SizeBytes": 12345,
"StreamViewType": "NEW_AND_OLD_IMAGES",
},
"awsRegion": "us-west-2",
"eventID": "da037887f71a88a1f6f4cfd149709d5a",
"eventName": "INSERT",
"eventSource": "aws:dynamodb",
"eventSourceARN": (
"arn:aws:dynamodb:us-west-2:12345:table/MyTable/stream/"
"2015-05-11T21:21:33.291"
)
} for new_image, old_image in images]
return {'Records': records}


class TestLambdaClient(BaseClient):
def __init__(self, app, config):
Expand Down
4 changes: 4 additions & 0 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1800,6 +1800,10 @@ Testing

Generates a Kinesis event.

.. method:: generate_dynamodb_event(images)

Generates a DynamoDB Stream event.


.. class:: HTTPResponse()

Expand Down
23 changes: 23 additions & 0 deletions tests/unit/test_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,29 @@ def foo(event):
assert response.payload == [b'foo', b'bar', b'baz']


def test_can_generate_dynamodb_event():
app = Chalice('dynamodb')

@app.on_dynamodb_record(
stream_arn=('arn:aws:dynamodb:us-west-2:12345:table/MyTable/stream/'
'2015-05-11T21:21:33.291')
)
def foo(event):
return [(record.new_image, record.old_image) for record in event]

old_image = {'PK': {'S': 'foo'}, 'SK': {'S': 'bar'}}
new_image = {'PK': {'S': 'hello'}, 'SK': {'S': 'world'}}

with Client(app) as client:
event = client.events.generate_dynamodb_event(
images=[(old_image, new_image)]
)
response = client.lambda_.invoke('foo', event)
assert len(response.payload) == 1
assert response.payload[0][0] == old_image
assert response.payload[0][1] == new_image


def test_can_mix_pure_lambda_and_event_handlers():
app = Chalice('lambda-only')

Expand Down