Created
April 8, 2016 13:26
-
-
Save seajoshc/8f9339d067f44e553b5ca77dfbc186f8 to your computer and use it in GitHub Desktop.
test walkthrough
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@patch('boto3.client') # Mock the boto3.client call to prevent using the boto3 library | |
def test_find_newest_artifact(mock_client): # Because we used the @patch decorator we need to add an argument for our mock'd client call | |
""" | |
test find_newest_artifact returns a properly formatted string | |
""" | |
# The normal "list_objects" function will return a dict. | |
# One key in the dict is "Contents" which is an list of the bucket objects. | |
# Each item in the "Contents" list has several keys, but if you look at the "find_newest_artifact" | |
# there are only two we are interested in: "Key" and "LastModified". So we need to fake a | |
# "list_objects" call and return a slimmed down version of the response we expect. | |
bucket_objects = { | |
"Contents": [ | |
{ | |
"Key": "blah", | |
"LastModified": "datetime.datetime(2016, 3, 18, 19, 20, 29, tzinfo=tzutc())" | |
} | |
] | |
} | |
# Create a MagicMock object to return instead of a normal boto3.client call | |
aws_s3 = MagicMock() | |
mock_client.return_value = aws_s3 | |
# Using our fake object, we will now fake a list_objects call and return our | |
# slimmed down response that we created above. We don't need to fake anything | |
# else now because our slimmed down response contains everything the rest of | |
# the normal logic in the function needs in order to work. | |
aws_s3.list_objects.return_value = bucket_objects | |
# Now we come to the test. We'll pass in a fake bucket name to the function | |
# and since we set "Key" above to "blah" we should expect that our function | |
# will return s3://blah/blah as the full name of the artifact. | |
assert find_newest_artifact('blah') == 's3://blah/blah' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment