diff options
author | Jordan Cook <jordan.cook@pioneer.com> | 2021-04-29 13:31:39 -0500 |
---|---|---|
committer | Jordan Cook <jordan.cook@pioneer.com> | 2021-04-29 15:06:33 -0500 |
commit | 993d85cdb943caf565336eac9218a2290bc8c66b (patch) | |
tree | 5a2da9bb958625bea456763f53043bc99b37bb79 | |
parent | a07b1e4178e30d0402d4ac77b1cc28b418816147 (diff) | |
download | requests-cache-993d85cdb943caf565336eac9218a2290bc8c66b.tar.gz |
Make sure all tests with SQLite dbs clean up after themselves
-rw-r--r-- | tests/conftest.py | 7 | ||||
-rw-r--r-- | tests/integration/test_sqlite.py | 4 | ||||
-rw-r--r-- | tests/unit/test_cache.py | 79 | ||||
-rw-r--r-- | tests/unit/test_patcher.py | 15 |
4 files changed, 52 insertions, 53 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index 7f155d4..12cca1d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -131,6 +131,13 @@ def tempfile_session() -> CachedSession: @pytest.fixture(scope='function') +def tempfile_path() -> CachedSession: + """Get a tempfile path that will be deleted after the test finished""" + with NamedTemporaryFile(suffix='.db') as temp: + yield temp.name + + +@pytest.fixture(scope='function') def installed_session() -> CachedSession: """Get a CachedSession using a temporary SQLite db, with global patching. Installs cache before test and uninstalls after. diff --git a/tests/integration/test_sqlite.py b/tests/integration/test_sqlite.py index 6efba0b..703a9d1 100644 --- a/tests/integration/test_sqlite.py +++ b/tests/integration/test_sqlite.py @@ -14,7 +14,7 @@ class SQLiteTestCase(BaseStorageTest): @classmethod def teardown_class(cls): try: - os.unlink(CACHE_NAME) + os.unlink(f'{CACHE_NAME}.sqlite') except Exception: pass @@ -79,7 +79,7 @@ class SQLiteTestCase(BaseStorageTest): @patch('requests_cache.backends.sqlite.sqlite3') def test_connection_kwargs(self, mock_sqlite): """A spot check to make sure optional connection kwargs gets passed to connection""" - cache = self.storage_class('test', timeout=0.5, invalid_kwarg='???') + cache = self.storage_class('test', use_temp=True, timeout=0.5, invalid_kwarg='???') mock_sqlite.connect.assert_called_with(cache.db_path, timeout=0.5) diff --git a/tests/unit/test_cache.py b/tests/unit/test_cache.py index bdcfc70..2b94033 100644 --- a/tests/unit/test_cache.py +++ b/tests/unit/test_cache.py @@ -123,16 +123,15 @@ def test_response_history(mock_session): assert len(mock_session.cache.redirects) == 1 -def test_repr(): +def test_repr(mock_session): """Test session and cache string representations""" - cache_name = 'requests_cache_test' - session = CachedSession(cache_name=cache_name, backend='memory', expire_after=10) - session.cache.responses['key'] = 'value' - session.cache.redirects['key'] = 'value' - session.cache.redirects['key_2'] = 'value' + mock_session.expire_after = 10.5 + mock_session.cache.responses['key'] = 'value' + mock_session.cache.redirects['key'] = 'value' + mock_session.cache.redirects['key_2'] = 'value' - assert cache_name in repr(session) and '10' in repr(session) - assert 'redirects: 2' in str(session.cache) and 'responses: 1' in str(session.cache) + assert mock_session._cache_name in repr(mock_session) and '10.5' in repr(mock_session) + assert 'redirects: 2' in str(mock_session.cache) and 'responses: 1' in str(mock_session.cache) def test_urls(mock_session): @@ -432,16 +431,14 @@ def test_cache_disabled__nested(mock_session): ('some_other_site.com', None), ], ) -def test_urls_expire_after(url, expected_expire_after): - session = CachedSession( - urls_expire_after={ - '*.site_1.com': 60 * 60, - 'site_2.com/resource_1': 60 * 60 * 2, - 'site_2.com/resource_2': 60 * 60 * 24, - 'site_2.com/static': -1, - }, - ) - assert session._url_expire_after(url) == expected_expire_after +def test_urls_expire_after(url, expected_expire_after, mock_session): + mock_session.urls_expire_after = { + '*.site_1.com': 60 * 60, + 'site_2.com/resource_1': 60 * 60 * 2, + 'site_2.com/resource_2': 60 * 60 * 24, + 'site_2.com/static': -1, + } + assert mock_session._url_expire_after(url) == expected_expire_after @pytest.mark.parametrize( @@ -453,26 +450,25 @@ def test_urls_expire_after(url, expected_expire_after): ('https://any_other_site.com', 1), ], ) -def test_urls_expire_after__evaluation_order(url, expected_expire_after): +def test_urls_expire_after__evaluation_order(url, expected_expire_after, mock_session): """If there are multiple matches, the first match should be used in the order defined""" - session = CachedSession( - urls_expire_after={ - '*.site_1.com/resource': 60 * 60 * 2, - '*.site_1.com': 60 * 60, - '*': 1, - }, - ) - assert session._url_expire_after(url) == expected_expire_after - - -def test_get_expiration_precedence(): - session = CachedSession(expire_after=1, urls_expire_after={'*.site_1.com': 60 * 60}) - assert session._get_expiration() == 1 - assert session._get_expiration('site_2.com') == 1 - assert session._get_expiration('img.site_1.com/image.jpg') == 60 * 60 - with session.request_expire_after(30): - assert session._get_expiration() == 30 - assert session._get_expiration('img.site_1.com/image.jpg') == 30 + mock_session.urls_expire_after = { + '*.site_1.com/resource': 60 * 60 * 2, + '*.site_1.com': 60 * 60, + '*': 1, + } + assert mock_session._url_expire_after(url) == expected_expire_after + + +def test_get_expiration_precedence(mock_session): + mock_session.expire_after = 1 + mock_session.urls_expire_after = {'*.site_1.com': 60 * 60} + assert mock_session._get_expiration() == 1 + assert mock_session._get_expiration('site_2.com') == 1 + assert mock_session._get_expiration('img.site_1.com/image.jpg') == 60 * 60 + with mock_session.request_expire_after(30): + assert mock_session._get_expiration() == 30 + assert mock_session._get_expiration('img.site_1.com/image.jpg') == 30 def test_remove_expired_responses(mock_session): @@ -591,14 +587,13 @@ def test_unpickle_errors(mock_session): assert resp.json()['message'] == 'mock json response' -def test_cache_signing(): - # Without a secret key, plain pickle should be used - session = CachedSession() +def test_cache_signing(tempfile_path): + session = CachedSession(tempfile_path) assert session.cache.responses._serializer == pickle # With a secret key, itsdangerous should be used secret_key = str(uuid4()) - session = CachedSession(secret_key=secret_key) + session = CachedSession(tempfile_path, secret_key=secret_key) assert isinstance(session.cache.responses._serializer, Serializer) # Simple serialize/deserialize round trip @@ -606,6 +601,6 @@ def test_cache_signing(): assert session.cache.responses['key'] == 'value' # Without the same signing key, the item shouldn't be considered safe to deserialize - session = CachedSession(secret_key='a different key') + session = CachedSession(tempfile_path, secret_key='a different key') with pytest.raises(BadSignature): session.cache.responses['key'] diff --git a/tests/unit/test_patcher.py b/tests/unit/test_patcher.py index 4a3e417..fb1c25a 100644 --- a/tests/unit/test_patcher.py +++ b/tests/unit/test_patcher.py @@ -6,15 +6,12 @@ from requests.sessions import Session as OriginalSession import requests_cache from requests_cache import CachedSession from requests_cache.backends import BaseCache - -CACHE_NAME = 'requests_cache_test' -CACHE_BACKEND = 'sqlite' -FAST_SAVE = False +from tests.conftest import CACHE_NAME def test_install_uninstall(): for _ in range(2): - requests_cache.install_cache(name=CACHE_NAME, backend=CACHE_BACKEND) + requests_cache.install_cache(name=CACHE_NAME, use_temp=True) assert isinstance(requests.Session(), CachedSession) assert isinstance(requests.sessions.Session(), CachedSession) requests_cache.uninstall_cache() @@ -61,8 +58,8 @@ def test_disabled(cached_request, original_request, installed_session): @patch.object(OriginalSession, 'request') @patch.object(CachedSession, 'request') -def test_enabled(cached_request, original_request): - with requests_cache.enabled(): +def test_enabled(cached_request, original_request, tempfile_path): + with requests_cache.enabled(tempfile_path): for i in range(3): requests.get('some_url') assert cached_request.call_count == 3 @@ -70,8 +67,8 @@ def test_enabled(cached_request, original_request): @patch.object(BaseCache, 'remove_expired_responses') -def test_remove_expired_responses(remove_expired_responses): - requests_cache.install_cache(expire_after=360) +def test_remove_expired_responses(remove_expired_responses, tempfile_path): + requests_cache.install_cache(tempfile_path, expire_after=360) requests_cache.remove_expired_responses() assert remove_expired_responses.called is True requests_cache.uninstall_cache() |