From c29569892f3d91c3ad6686a5cb7a0a1e2d39c5ec Mon Sep 17 00:00:00 2001 From: Jordan Cook Date: Wed, 21 Apr 2021 20:18:02 -0500 Subject: Reorganize backend integration tests and add some more thorough tests --- tests/integration/test_dynamodb.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'tests/integration/test_dynamodb.py') diff --git a/tests/integration/test_dynamodb.py b/tests/integration/test_dynamodb.py index 3a4bd23..c78baad 100644 --- a/tests/integration/test_dynamodb.py +++ b/tests/integration/test_dynamodb.py @@ -4,7 +4,7 @@ from unittest.mock import patch from requests_cache.backends import DynamoDbDict from tests.conftest import AWS_OPTIONS, fail_if_no_connection -from tests.integration.test_backends import BaseStorageTestCase +from tests.integration.test_backends import CACHE_NAME, BaseStorageTestCase # Run this test module last, since the DynamoDB container takes the longest to initialize pytestmark = pytest.mark.order(-1) @@ -20,16 +20,18 @@ def ensure_connection(): client.describe_limits() -class DynamoDbDictWrapper(DynamoDbDict): - def __init__(self, namespace, collection_name='dynamodb_dict_data', **options): - super().__init__(namespace, collection_name, **options, **AWS_OPTIONS) - - class DynamoDbTestCase(BaseStorageTestCase, unittest.TestCase): + def init_cache(self, index=0, clear=True, **kwargs): + kwargs['suppress_warnings'] = True + cache = self.storage_class(CACHE_NAME, f'table_{index}', **kwargs, **AWS_OPTIONS) + if clear: + cache.clear() + return cache + def __init__(self, *args, **kwargs): super().__init__( *args, - storage_class=DynamoDbDictWrapper, + storage_class=DynamoDbDict, picklable=True, **kwargs, ) -- cgit v1.2.1 From b1070a950e0365085b7edf8ef2e4a65c77630ac9 Mon Sep 17 00:00:00 2001 From: Jordan Cook Date: Wed, 21 Apr 2021 23:33:18 -0500 Subject: Turn remaining unittest.TestCase classes into pytest-style test classes --- tests/integration/test_dynamodb.py | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) (limited to 'tests/integration/test_dynamodb.py') diff --git a/tests/integration/test_dynamodb.py b/tests/integration/test_dynamodb.py index c78baad..12da135 100644 --- a/tests/integration/test_dynamodb.py +++ b/tests/integration/test_dynamodb.py @@ -1,10 +1,9 @@ import pytest -import unittest from unittest.mock import patch from requests_cache.backends import DynamoDbDict from tests.conftest import AWS_OPTIONS, fail_if_no_connection -from tests.integration.test_backends import CACHE_NAME, BaseStorageTestCase +from tests.integration.test_backends import BaseStorageTest # Run this test module last, since the DynamoDB container takes the longest to initialize pytestmark = pytest.mark.order(-1) @@ -20,25 +19,13 @@ def ensure_connection(): client.describe_limits() -class DynamoDbTestCase(BaseStorageTestCase, unittest.TestCase): - def init_cache(self, index=0, clear=True, **kwargs): - kwargs['suppress_warnings'] = True - cache = self.storage_class(CACHE_NAME, f'table_{index}', **kwargs, **AWS_OPTIONS) - if clear: - cache.clear() - return cache - - def __init__(self, *args, **kwargs): - super().__init__( - *args, - storage_class=DynamoDbDict, - picklable=True, - **kwargs, - ) - - -@patch('requests_cache.backends.dynamodb.boto3.resource') -def test_connection_kwargs(mock_resource): - """A spot check to make sure optional connection kwargs gets passed to connection""" - DynamoDbDict('test', region_name='us-east-2', invalid_kwarg='???') - mock_resource.assert_called_with('dynamodb', region_name='us-east-2') +class TestDynamoDbDict(BaseStorageTest): + storage_class = DynamoDbDict + init_kwargs = AWS_OPTIONS + picklable = True + + @patch('requests_cache.backends.dynamodb.boto3.resource') + def test_connection_kwargs(self, mock_resource): + """A spot check to make sure optional connection kwargs gets passed to connection""" + DynamoDbDict('test', region_name='us-east-2', invalid_kwarg='???') + mock_resource.assert_called_with('dynamodb', region_name='us-east-2') -- cgit v1.2.1 From 3ec85f0107caedbe3b3bd8080bd3dac81b7baa17 Mon Sep 17 00:00:00 2001 From: Jordan Cook Date: Thu, 22 Apr 2021 00:02:08 -0500 Subject: Turn multi-threaded stress tests into test (sub)classes This is mainly to take advantage of fail-fast connection tests; otherwise, these tests may just hang if backend dependenices are installed but backend services are not set up. See issue #221 for details. --- tests/integration/test_dynamodb.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'tests/integration/test_dynamodb.py') diff --git a/tests/integration/test_dynamodb.py b/tests/integration/test_dynamodb.py index 12da135..951d9c8 100644 --- a/tests/integration/test_dynamodb.py +++ b/tests/integration/test_dynamodb.py @@ -1,9 +1,9 @@ import pytest from unittest.mock import patch -from requests_cache.backends import DynamoDbDict +from requests_cache.backends import DynamoDbCache, DynamoDbDict from tests.conftest import AWS_OPTIONS, fail_if_no_connection -from tests.integration.test_backends import BaseStorageTest +from tests.integration.test_backends import BaseCacheTest, BaseStorageTest # Run this test module last, since the DynamoDB container takes the longest to initialize pytestmark = pytest.mark.order(-1) @@ -29,3 +29,8 @@ class TestDynamoDbDict(BaseStorageTest): """A spot check to make sure optional connection kwargs gets passed to connection""" DynamoDbDict('test', region_name='us-east-2', invalid_kwarg='???') mock_resource.assert_called_with('dynamodb', region_name='us-east-2') + + +class TestDynamoDbCache(BaseCacheTest): + backend_class = DynamoDbCache + init_kwargs = AWS_OPTIONS -- cgit v1.2.1