diff options
author | Jordan Cook <jordan.cook@pioneer.com> | 2022-04-19 21:34:49 -0500 |
---|---|---|
committer | Jordan Cook <jordan.cook@pioneer.com> | 2022-04-20 13:37:44 -0500 |
commit | a2a65250d61997935763cf958f16a914930f1b43 (patch) | |
tree | 4b2c37d54ea29a33dedbc0e61c610b1013b7231c /tests | |
parent | 3fb12461d847e04884f66dcf64ff5cabc79cce91 (diff) | |
download | requests-cache-a2a65250d61997935763cf958f16a914930f1b43.tar.gz |
Add support for DynamoDB TTL
Diffstat (limited to 'tests')
-rw-r--r-- | tests/conftest.py | 31 | ||||
-rw-r--r-- | tests/integration/base_storage_test.py | 5 | ||||
-rw-r--r-- | tests/integration/test_dynamodb.py | 70 | ||||
-rw-r--r-- | tests/integration/test_filesystem.py | 3 |
4 files changed, 76 insertions, 33 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index dc77368..96c35a7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -25,7 +25,16 @@ from timeout_decorator import timeout from requests_cache import ALL_METHODS, CachedSession, install_cache, uninstall_cache -CACHE_NAME = 'pytest_cache' +# Configure logging to show log output when tests fail (or with pytest -s) +basicConfig( + level='INFO', + format='%(message)s', + datefmt='[%m-%d %H:%M:%S]', + handlers=[RichHandler(rich_tracebacks=True, markup=True)], +) +# getLogger('requests_cache').setLevel('DEBUG') +logger = getLogger(__name__) + # Allow running longer stress tests with an environment variable STRESS_TEST_MULTIPLIER = int(os.getenv('STRESS_TEST_MULTIPLIER', '1')) @@ -49,7 +58,6 @@ HTTPBIN_FORMATS = [ 'robots.txt', 'xml', ] - HTTPDATE_STR = 'Fri, 16 APR 2021 21:13:00 GMT' HTTPDATE_DATETIME = datetime(2021, 4, 16, 21, 13) EXPIRED_DT = datetime.now() - timedelta(1) @@ -66,28 +74,11 @@ MOCKED_URL_404 = 'http+mock://requests-cache.com/nonexistent' MOCKED_URL_500 = 'http+mock://requests-cache.com/answer?q=this-statement-is-false' MOCK_PROTOCOLS = ['mock://', 'http+mock://', 'https+mock://'] +CACHE_NAME = 'pytest_cache' PROJECT_DIR = Path(__file__).parent.parent.absolute() SAMPLE_DATA_DIR = PROJECT_DIR / 'tests' / 'sample_data' SAMPLE_CACHE_FILES = list(SAMPLE_DATA_DIR.glob('sample.db.*')) -AWS_OPTIONS = { - 'endpoint_url': 'http://localhost:8000', - 'region_name': 'us-east-1', - 'aws_access_key_id': 'placeholder', - 'aws_secret_access_key': 'placeholder', -} - - -# Configure logging to show log output when tests fail (or with pytest -s) -basicConfig( - level='INFO', - format='%(message)s', - datefmt='[%m-%d %H:%M:%S]', - handlers=[RichHandler(rich_tracebacks=True, markup=True)], -) -# getLogger('requests_cache').setLevel('DEBUG') -logger = getLogger(__name__) - def httpbin(path): """Get the url for either a local or remote httpbin instance""" diff --git a/tests/integration/base_storage_test.py b/tests/integration/base_storage_test.py index 776d494..7d38643 100644 --- a/tests/integration/base_storage_test.py +++ b/tests/integration/base_storage_test.py @@ -18,8 +18,9 @@ class BaseStorageTest: num_instances: int = 10 # Max number of cache instances to test def init_cache(self, cache_name=CACHE_NAME, index=0, clear=True, **kwargs): + kwargs = {**self.init_kwargs, **kwargs} kwargs.setdefault('serializer', 'pickle') - cache = self.storage_class(cache_name, f'table_{index}', **self.init_kwargs, **kwargs) + cache = self.storage_class(cache_name, f'table_{index}', **kwargs) if clear: cache.clear() return cache @@ -98,7 +99,7 @@ class BaseStorageTest: def test_picklable_dict(self): if self.picklable: - cache = self.init_cache() + cache = self.init_cache(serializer='pickle') original_obj = BasicDataclass( bool_attr=True, datetime_attr=datetime(2022, 2, 2), diff --git a/tests/integration/test_dynamodb.py b/tests/integration/test_dynamodb.py index 857ab6c..84c1008 100644 --- a/tests/integration/test_dynamodb.py +++ b/tests/integration/test_dynamodb.py @@ -1,13 +1,27 @@ +from collections import OrderedDict +from decimal import Decimal from unittest.mock import patch import pytest +from botocore.exceptions import ClientError -from requests_cache.backends import DynamoDbCache, DynamoDbDict, DynamoDocumentDict +from requests_cache.backends import DynamoDbCache, DynamoDbDict, DynamoDbDocumentDict from requests_cache.serializers import dynamodb_document_serializer -from tests.conftest import AWS_OPTIONS, HTTPBIN_FORMATS, HTTPBIN_METHODS, fail_if_no_connection +from tests.conftest import HTTPBIN_FORMATS, HTTPBIN_METHODS, fail_if_no_connection from tests.integration.base_cache_test import TEST_SERIALIZERS, BaseCacheTest from tests.integration.base_storage_test import BaseStorageTest +AWS_OPTIONS = { + 'endpoint_url': 'http://localhost:8000', + 'region_name': 'us-east-1', + 'aws_access_key_id': 'placeholder', + 'aws_secret_access_key': 'placeholder', +} +DYNAMODB_OPTIONS = { + **AWS_OPTIONS, + 'serializer': None, # Use class default serializer +} + # Add extra DynamoDB-specific format to list of serializers to test against DYNAMODB_SERIALIZERS = [dynamodb_document_serializer] + list(TEST_SERIALIZERS.values()) @@ -24,7 +38,7 @@ def ensure_connection(): class TestDynamoDbDict(BaseStorageTest): storage_class = DynamoDbDict - init_kwargs = AWS_OPTIONS + init_kwargs = DYNAMODB_OPTIONS @patch('requests_cache.backends.dynamodb.boto3.resource') def test_connection_kwargs(self, mock_resource): @@ -32,19 +46,55 @@ class TestDynamoDbDict(BaseStorageTest): DynamoDbDict('test_table', 'namespace', region_name='us-east-2', invalid_kwarg='???') mock_resource.assert_called_with('dynamodb', region_name='us-east-2') + def test_create_table_error(self): + """An error other than 'table already exists' should be reraised""" + cache = self.init_cache() + error = ClientError({'Error': {'Code': 'NullPointerException'}}, 'CreateTable') + with patch.object(cache.connection.meta.client, 'update_time_to_live', side_effect=error): + with pytest.raises(ClientError): + cache._enable_ttl() + + def test_enable_ttl_error(self): + """An error other than 'ttl already enabled' should be reraised""" + cache = self.init_cache() + error = ClientError({'Error': {'Code': 'NullPointerException'}}, 'CreateTable') + with patch.object(cache.connection, 'create_table', side_effect=error): + with pytest.raises(ClientError): + cache._create_table() + + @pytest.mark.parametrize('ttl_enabled', [True, False]) + def test_ttl(self, ttl_enabled): + """DynamoDB's TTL removal process can take up to 48 hours to run, so just test if the + 'ttl' attribute is set correctly if enabled, and not set if disabled. + """ + cache = self.init_cache(ttl=ttl_enabled) + item = OrderedDict(foo='bar') + item.ttl = 60 + cache['key'] = item + + # 'ttl' is a reserved word, so to retrieve it we need to alias it + item = cache._table.get_item( + Key=cache._composite_key('key'), + ProjectionExpression='#t', + ExpressionAttributeNames={'#t': 'ttl'}, + ) + ttl_value = item['Item'].get('ttl') + + if ttl_enabled: + assert isinstance(ttl_value, Decimal) + else: + assert ttl_value is None + -class TestDynamoDocumentDict(BaseStorageTest): - storage_class = DynamoDocumentDict - init_kwargs = AWS_OPTIONS +class TestDynamoDbDocumentDict(BaseStorageTest): + storage_class = DynamoDbDocumentDict + init_kwargs = DYNAMODB_OPTIONS picklable = True class TestDynamoDbCache(BaseCacheTest): backend_class = DynamoDbCache - init_kwargs = { - 'serializer': None, - **AWS_OPTIONS, - } # Use class default serializer instead of pickle + init_kwargs = DYNAMODB_OPTIONS @pytest.mark.parametrize('serializer', DYNAMODB_SERIALIZERS) @pytest.mark.parametrize('method', HTTPBIN_METHODS) diff --git a/tests/integration/test_filesystem.py b/tests/integration/test_filesystem.py index 1b81dff..4181690 100644 --- a/tests/integration/test_filesystem.py +++ b/tests/integration/test_filesystem.py @@ -19,7 +19,8 @@ class TestFileDict(BaseStorageTest): rmtree(CACHE_NAME, ignore_errors=True) def init_cache(self, index=0, clear=True, **kwargs): - cache = FileDict(f'{CACHE_NAME}_{index}', serializer='pickle', use_temp=True, **kwargs) + kwargs.setdefault('serializer', 'pickle') + cache = FileDict(f'{CACHE_NAME}_{index}', use_temp=True, **kwargs) if clear: cache.clear() return cache |