Skip to content

Instantly share code, notes, and snippets.

@vincentclaes
Last active October 21, 2020 16:50
Show Gist options
  • Save vincentclaes/41111665eb9e4c229405f3b0c5feb90e to your computer and use it in GitHub Desktop.
Save vincentclaes/41111665eb9e4c229405f3b0c5feb90e to your computer and use it in GitHub Desktop.
example of how to use moto
import boto3
import json
import os
import unittest
from moto import mock_s3
from my_project.lambda_functions import lambda_handler
class TestLambda(unittest.TestCase):
@mock_s3
def test_my_lambda_happy_flow(self):
# arrange
bucket = "my-bucket"
source_key = "source-file.json"
destination_key = "destination-file.json"
boto3.resource("s3").create_bucket(Bucket=bucket)
boto3.client('s3').put_object(Body='{"foo":"bar"}', Bucket=bucket, Key=source_key)
os.environ["DESTINATION_KEY"] = destination_key
event = {"Records": [{"s3": {"bucket": {"name": bucket}, "object": {"key": source_key}}}]}
# act
lambda_handler.handler(event=event, context=None)
# assert
result = boto3.client('s3').get_object(Bucket=bucket, Key=destination_key)['Body'].read()
self.assertDictEqual(json.loads(result), {"foo": "baz"})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment