Skip to content

Commit

Permalink
Move test setup out into its own method
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaballoch committed Feb 7, 2018
1 parent 2f67dea commit 0be6d8c
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,40 @@
from moto import mock_s3
from handler import call

BUCKET = "some-bucket"
KEY = "incoming/transaction-0001.txt"
BODY = "Hello World!"

## Test Setup Functions

def test_handler_moves_incoming_object_to_processed():
from contextlib import contextmanager

@contextmanager
def do_test_setup():
with mock_s3():
# Create the bucket
conn = boto3.resource('s3', region_name='us-east-1')
conn.create_bucket(Bucket="some-bucket")
# Add a file
boto3.client('s3', region_name='us-east-1').put_object(Bucket="some-bucket", Key="incoming/transaction-0001.txt", Body="Hello World!")
set_up_s3()
yield

def set_up_s3():
conn = boto3.resource('s3', region_name='us-east-1')
conn.create_bucket(Bucket=BUCKET)
boto3.client('s3', region_name='us-east-1').put_object(Bucket=BUCKET, Key=KEY, Body=BODY)

## Tests

def test_handler_moves_incoming_object_to_processed():
with do_test_setup():
# Run call with an event describing the file:
call(s3_object_created_event("some-bucket", "incoming/transaction-0001.txt"), None)
call(s3_object_created_event(BUCKET, KEY), None)

conn = boto3.resource('s3', region_name='us-east-1')
# Assert the original file doesn't exist
with pytest.raises(ClientError) as e_info:
conn.Object("some-bucket", "incoming/transaction-0001.txt").get()
conn.Object(BUCKET, KEY).get()
assert e_info.response['Error']['Code'] == 'NoSuchKey'

# Check that it exists in `processed/`
obj = conn.Object("some-bucket", "processed/transaction-0001.txt").get()
obj = conn.Object(BUCKET, "processed/transaction-0001.txt").get()
assert obj['Body'].read() == b'Hello World!'

def s3_object_created_event(bucket_name, key):
Expand All @@ -42,5 +56,3 @@ def s3_object_created_event(bucket_name, key):
}
]
}


0 comments on commit 0be6d8c

Please sign in to comment.