summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJordan Cook <jordan.cook@pioneer.com>2021-04-18 21:46:08 -0500
committerJordan Cook <jordan.cook@pioneer.com>2021-04-19 16:45:34 -0500
commitf0916900e11778f9ff0a33b148edbf2f51f6f600 (patch)
tree36627a6f9c46657bac58d28d2c17ff311c1d679c /tests
parent714814d0c23271fb357e9ee094e8754ef9818029 (diff)
downloadrequests-cache-f0916900e11778f9ff0a33b148edbf2f51f6f600.tar.gz
Allow passing any valid backend connection kwargs via BaseCache
* Pass `**kwargs` to backend storage classes, split out any that are valid for the backend-specific connection function/class, and pass them to the connection * Add intersphinx links to docs for dependencies * Update and format some more backend class docstrings * Remove 'Unrecognized keyword arguments' warning from `BaseStorage` * Turn `warnings.warn` about using secret keys into a `logging.warning` (due to complaints about too many messages)
Diffstat (limited to 'tests')
-rw-r--r--tests/integration/test_dynamodb.py8
-rw-r--r--tests/integration/test_gridfs.py17
-rw-r--r--tests/integration/test_mongodb.py16
-rw-r--r--tests/integration/test_redis.py11
-rw-r--r--tests/integration/test_sqlite.py6
5 files changed, 52 insertions, 6 deletions
diff --git a/tests/integration/test_dynamodb.py b/tests/integration/test_dynamodb.py
index a79118b..13e40dd 100644
--- a/tests/integration/test_dynamodb.py
+++ b/tests/integration/test_dynamodb.py
@@ -1,5 +1,6 @@
import pytest
import unittest
+from unittest.mock import patch
from requests_cache.backends import DynamoDbDict
from tests.conftest import fail_if_no_connection
@@ -39,3 +40,10 @@ class DynamoDbTestCase(BaseStorageTestCase, unittest.TestCase):
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')
diff --git a/tests/integration/test_gridfs.py b/tests/integration/test_gridfs.py
index 128356b..aae9948 100644
--- a/tests/integration/test_gridfs.py
+++ b/tests/integration/test_gridfs.py
@@ -1,7 +1,10 @@
import pytest
import unittest
+from unittest.mock import patch
-from requests_cache.backends import GridFSPickleDict
+from pymongo import MongoClient
+
+from requests_cache.backends import GridFSPickleDict, get_valid_kwargs
from tests.conftest import fail_if_no_connection
from tests.integration.test_backends import BaseStorageTestCase
@@ -29,3 +32,15 @@ class GridFSPickleDictTestCase(BaseStorageTestCase, unittest.TestCase):
with pytest.raises(KeyError):
d1[4]
+
+
+@patch('requests_cache.backends.gridfs.GridFS')
+@patch('requests_cache.backends.gridfs.MongoClient')
+@patch(
+ 'requests_cache.backends.gridfs.get_valid_kwargs',
+ side_effect=lambda cls, kwargs: get_valid_kwargs(MongoClient, kwargs),
+)
+def test_connection_kwargs(mock_get_valid_kwargs, mock_client, mock_gridfs):
+ """A spot check to make sure optional connection kwargs gets passed to connection"""
+ GridFSPickleDict('test', host='http://0.0.0.0', port=1234, invalid_kwarg='???')
+ mock_client.assert_called_with(host='http://0.0.0.0', port=1234)
diff --git a/tests/integration/test_mongodb.py b/tests/integration/test_mongodb.py
index c781b52..05e61a2 100644
--- a/tests/integration/test_mongodb.py
+++ b/tests/integration/test_mongodb.py
@@ -1,7 +1,10 @@
import pytest
import unittest
+from unittest.mock import patch
-from requests_cache.backends import MongoDict, MongoPickleDict
+from pymongo import MongoClient
+
+from requests_cache.backends import MongoDict, MongoPickleDict, get_valid_kwargs
from tests.conftest import fail_if_no_connection
from tests.integration.test_backends import BaseStorageTestCase
@@ -24,3 +27,14 @@ class MongoDictTestCase(BaseStorageTestCase, unittest.TestCase):
class MongoPickleDictTestCase(BaseStorageTestCase, unittest.TestCase):
def __init__(self, *args, **kwargs):
super().__init__(*args, storage_class=MongoPickleDict, picklable=True, **kwargs)
+
+
+@patch('requests_cache.backends.mongo.MongoClient')
+@patch(
+ 'requests_cache.backends.mongo.get_valid_kwargs',
+ side_effect=lambda cls, kwargs: get_valid_kwargs(MongoClient, kwargs),
+)
+def test_connection_kwargs(mock_get_valid_kwargs, mock_client):
+ """A spot check to make sure optional connection kwargs gets passed to connection"""
+ MongoDict('test', host='http://0.0.0.0', port=1234, invalid_kwarg='???')
+ mock_client.assert_called_with(host='http://0.0.0.0', port=1234)
diff --git a/tests/integration/test_redis.py b/tests/integration/test_redis.py
index 4bf5fd8..5edf559 100644
--- a/tests/integration/test_redis.py
+++ b/tests/integration/test_redis.py
@@ -1,7 +1,8 @@
import pytest
import unittest
+from unittest.mock import patch
-from requests_cache.backends.redis import RedisDict
+from requests_cache.backends.redis import RedisCache, RedisDict
from tests.conftest import fail_if_no_connection
from tests.integration.test_backends import BaseStorageTestCase
@@ -18,3 +19,11 @@ def ensure_connection():
class RedisTestCase(BaseStorageTestCase, unittest.TestCase):
def __init__(self, *args, **kwargs):
super().__init__(*args, storage_class=RedisDict, picklable=True, **kwargs)
+
+
+# @patch.object(Redis, '__init__', Redis.__init__)
+@patch('requests_cache.backends.redis.StrictRedis')
+def test_connection_kwargs(mock_redis):
+ """A spot check to make sure optional connection kwargs gets passed to connection"""
+ RedisCache('test', username='user', password='pass', invalid_kwarg='???')
+ mock_redis.assert_called_with(username='user', password='pass')
diff --git a/tests/integration/test_sqlite.py b/tests/integration/test_sqlite.py
index bc883bf..18ddefd 100644
--- a/tests/integration/test_sqlite.py
+++ b/tests/integration/test_sqlite.py
@@ -89,7 +89,7 @@ class DbPickleDictTestCase(SQLiteTestCase, unittest.TestCase):
@patch('requests_cache.backends.sqlite.sqlite3')
-def test_timeout(mock_sqlite):
- """Just make sure the optional 'timeout' param gets passed to sqlite3.connect"""
- DbDict('test', timeout=0.5)
+def test_connection_kwargs(mock_sqlite):
+ """A spot check to make sure optional connection kwargs gets passed to connection"""
+ DbDict('test', timeout=0.5, invalid_kwarg='???')
mock_sqlite.connect.assert_called_with('test', timeout=0.5)