Last active
May 29, 2019 18:52
-
-
Save jg75/0bf6b19476f771c3f934c7d8324dd597 to your computer and use it in GitHub Desktop.
PyTest ExampleMemoized function embedded in a handler function
This file contains hidden or 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
from unittest import mock | |
import thing | |
import pytest | |
@pytest.fixture() | |
def mock_client(monkeypatch, request): | |
"""Mock the client.""" | |
mock_client = mock.create_autospec(thing.client) | |
monkeypatch.setattr(thing, "client", mock_client) | |
def _teardown(): | |
thing.exists.cache_clear() | |
request.addfinalizer(_teardown) | |
return mock_client | |
@pytest.mark.parametrize( | |
"does_use_cache, does_key_exist", | |
[(True, True), (True, False), (False, True), (False, False)] | |
) | |
def test_key_exists(mock_client, does_use_cache, does_key_exist): | |
if not does_key_exist: | |
mock_client.head_object.side_effect = thing.Client.ClientException | |
count = 2 | |
bucket = thing.s3_bucket | |
key = "key" | |
call_args_list = list() | |
for i in range(count): | |
exists = thing.exists(key) | |
assert exists if does_key_exist else not exists | |
if i == 0 or not does_use_cache: | |
call_args_list.append(mock.call(Bucket=bucket, Key=key)) | |
if not does_use_cache: | |
thing.exists.cache_clear() | |
assert mock_client.head_object.call_count == 1 if does_use_cache else count | |
assert mock_client.head_object.call_args_list == call_args_list | |
@pytest.mark.parametrize("status_code", [(200), (404)]) | |
def test_the_handler(mock_client, status_code): | |
"""Test the handler.""" | |
if status_code == 404: | |
mock_client.head_object.side_effect = thing.Client.ClientException | |
key = "key" | |
response = thing.handler(key) | |
assert response["statusCode"] == status_code |
This file contains hidden or 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
"""Example python source.""" | |
import functools | |
import json | |
import os | |
class Client: | |
"""This is a client.""" | |
class ClientException(Exception): | |
pass | |
def head_object(self, Bucket=None, Key=None): | |
"""This is a dummy head_object. | |
No. You are a dummy head.""" | |
pass | |
@functools.lru_cache(maxsize=2) | |
def exists(key): | |
"""Return True.""" | |
try: | |
client.head_object(Bucket=s3_bucket, Key=key) | |
except Client.ClientException: | |
return False | |
return True | |
def handler(key, times=1): | |
"""The handler.""" | |
for _ in range(times): | |
if not exists(key): | |
return { | |
"statusCode": 404, | |
"body": json.dumps({"body": "Not Found"}) | |
} | |
return { | |
"statusCode": 200, | |
"body": json.dumps({"body": "Found"}) | |
} | |
client = Client() | |
s3_bucket = os.getenv("BUCKET", "bucket") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment