summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJordan Cook <jordan.cook@pioneer.com>2022-03-19 11:55:20 -0500
committerJordan Cook <jordan.cook@pioneer.com>2022-03-29 12:17:43 -0500
commitb3646b03bc05f5b05f4384eb9a2b4796fa59cb34 (patch)
tree77d3dfbcb5213deb4d59972301f7b6302343748b /tests
parent4b524f7298093bb4c27cb3419d4ddafc7e42891e (diff)
downloadrequests-cache-b3646b03bc05f5b05f4384eb9a2b4796fa59cb34.tar.gz
Split datetime-related utility functions into a separate module
Diffstat (limited to 'tests')
-rw-r--r--tests/unit/test_cache_control.py93
-rw-r--r--tests/unit/test_expiration.py85
2 files changed, 90 insertions, 88 deletions
diff --git a/tests/unit/test_cache_control.py b/tests/unit/test_cache_control.py
index c942891..1d9f417 100644
--- a/tests/unit/test_cache_control.py
+++ b/tests/unit/test_cache_control.py
@@ -1,17 +1,12 @@
-from datetime import datetime, timedelta, timezone
+from datetime import datetime, timedelta
from unittest.mock import MagicMock, patch
import pytest
from requests import PreparedRequest
-from requests_cache.cache_control import (
- DO_NOT_CACHE,
- CacheActions,
- get_expiration_datetime,
- get_url_expiration,
-)
-from requests_cache.models.response import CachedResponse, CacheSettings, RequestSettings
-from tests.conftest import ETAG, HTTPDATE_DATETIME, HTTPDATE_STR, LAST_MODIFIED
+from requests_cache.cache_control import DO_NOT_CACHE, CacheActions
+from requests_cache.models import CachedResponse, CacheSettings, RequestSettings
+from tests.conftest import ETAG, HTTPDATE_STR, LAST_MODIFIED
IGNORED_DIRECTIVES = [
'no-transform',
@@ -261,7 +256,7 @@ def test_update_from_response__ignored():
@pytest.mark.parametrize('validator_headers', [{'ETag': ETAG}, {'Last-Modified': LAST_MODIFIED}])
@pytest.mark.parametrize('cache_headers', [{'Cache-Control': 'max-age=0'}, {'Expires': '0'}])
-@patch('requests_cache.cache_control.datetime')
+@patch('requests_cache.expiration.datetime')
def test_update_from_response__revalidate(mock_datetime, cache_headers, validator_headers):
"""If expiration is 0 and there's a validator, the response should be cached, but with immediate
expiration
@@ -289,81 +284,3 @@ def test_ignored_headers(directive):
assert actions.revalidate is False
assert actions.skip_read is False
assert actions.skip_write is False
-
-
-@patch('requests_cache.cache_control.datetime')
-def test_get_expiration_datetime__no_expiration(mock_datetime):
- assert get_expiration_datetime(None) is None
- assert get_expiration_datetime(-1) is None
- assert get_expiration_datetime(DO_NOT_CACHE) == mock_datetime.utcnow()
-
-
-@pytest.mark.parametrize(
- 'expire_after, expected_expiration_delta',
- [
- (timedelta(seconds=60), timedelta(seconds=60)),
- (60, timedelta(seconds=60)),
- (33.3, timedelta(seconds=33.3)),
- ],
-)
-def test_get_expiration_datetime__relative(expire_after, expected_expiration_delta):
- expires = get_expiration_datetime(expire_after)
- expected_expiration = datetime.utcnow() + expected_expiration_delta
- # Instead of mocking datetime (which adds some complications), check for approximate value
- assert abs((expires - expected_expiration).total_seconds()) <= 1
-
-
-def test_get_expiration_datetime__tzinfo():
- tz = timezone(-timedelta(hours=5))
- dt = datetime(2021, 2, 1, 7, 0, tzinfo=tz)
- assert get_expiration_datetime(dt) == datetime(2021, 2, 1, 12, 0)
-
-
-def test_get_expiration_datetime__httpdate():
- assert get_expiration_datetime(HTTPDATE_STR) == HTTPDATE_DATETIME
- assert get_expiration_datetime('P12Y34M56DT78H90M12.345S') is None
-
-
-@pytest.mark.parametrize(
- 'url, expected_expire_after',
- [
- ('img.site_1.com', 60 * 60),
- ('http://img.site_1.com/base/img.jpg', 60 * 60),
- ('https://img.site_2.com/base/img.jpg', None),
- ('site_2.com/resource_1', 60 * 60 * 2),
- ('http://site_2.com/resource_1/index.html', 60 * 60 * 2),
- ('http://site_2.com/resource_2/', 60 * 60 * 24),
- ('http://site_2.com/static/', -1),
- ('http://site_2.com/static/img.jpg', -1),
- ('site_2.com', None),
- ('some_other_site.com', None),
- (None, None),
- ],
-)
-def test_get_url_expiration(url, expected_expire_after, 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 get_url_expiration(url, urls_expire_after) == expected_expire_after
-
-
-@pytest.mark.parametrize(
- 'url, expected_expire_after',
- [
- ('https://img.site_1.com/image.jpeg', 60 * 60),
- ('https://img.site_1.com/resource/1', 60 * 60 * 2),
- ('https://site_2.com', 1),
- ('https://any_other_site.com', 1),
- ],
-)
-def test_get_url_expiration__evaluation_order(url, expected_expire_after):
- """If there are multiple matches, the first match should be used in the order defined"""
- urls_expire_after = {
- '*.site_1.com/resource': 60 * 60 * 2,
- '*.site_1.com': 60 * 60,
- '*': 1,
- }
- assert get_url_expiration(url, urls_expire_after) == expected_expire_after
diff --git a/tests/unit/test_expiration.py b/tests/unit/test_expiration.py
new file mode 100644
index 0000000..05490dd
--- /dev/null
+++ b/tests/unit/test_expiration.py
@@ -0,0 +1,85 @@
+from datetime import datetime, timedelta, timezone
+from unittest.mock import patch
+
+import pytest
+
+from requests_cache.expiration import DO_NOT_CACHE, get_expiration_datetime, get_url_expiration
+from tests.conftest import HTTPDATE_DATETIME, HTTPDATE_STR
+
+
+@patch('requests_cache.expiration.datetime')
+def test_get_expiration_datetime__no_expiration(mock_datetime):
+ assert get_expiration_datetime(None) is None
+ assert get_expiration_datetime(-1) is None
+ assert get_expiration_datetime(DO_NOT_CACHE) == mock_datetime.utcnow()
+
+
+@pytest.mark.parametrize(
+ 'expire_after, expected_expiration_delta',
+ [
+ (timedelta(seconds=60), timedelta(seconds=60)),
+ (60, timedelta(seconds=60)),
+ (33.3, timedelta(seconds=33.3)),
+ ],
+)
+def test_get_expiration_datetime__relative(expire_after, expected_expiration_delta):
+ expires = get_expiration_datetime(expire_after)
+ expected_expiration = datetime.utcnow() + expected_expiration_delta
+ # Instead of mocking datetime (which adds some complications), check for approximate value
+ assert abs((expires - expected_expiration).total_seconds()) <= 1
+
+
+def test_get_expiration_datetime__tzinfo():
+ tz = timezone(-timedelta(hours=5))
+ dt = datetime(2021, 2, 1, 7, 0, tzinfo=tz)
+ assert get_expiration_datetime(dt) == datetime(2021, 2, 1, 12, 0)
+
+
+def test_get_expiration_datetime__httpdate():
+ assert get_expiration_datetime(HTTPDATE_STR) == HTTPDATE_DATETIME
+ assert get_expiration_datetime('P12Y34M56DT78H90M12.345S') is None
+
+
+@pytest.mark.parametrize(
+ 'url, expected_expire_after',
+ [
+ ('img.site_1.com', 60 * 60),
+ ('http://img.site_1.com/base/img.jpg', 60 * 60),
+ ('https://img.site_2.com/base/img.jpg', None),
+ ('site_2.com/resource_1', 60 * 60 * 2),
+ ('http://site_2.com/resource_1/index.html', 60 * 60 * 2),
+ ('http://site_2.com/resource_2/', 60 * 60 * 24),
+ ('http://site_2.com/static/', -1),
+ ('http://site_2.com/static/img.jpg', -1),
+ ('site_2.com', None),
+ ('some_other_site.com', None),
+ (None, None),
+ ],
+)
+def test_get_url_expiration(url, expected_expire_after, 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 get_url_expiration(url, urls_expire_after) == expected_expire_after
+
+
+@pytest.mark.parametrize(
+ 'url, expected_expire_after',
+ [
+ ('https://img.site_1.com/image.jpeg', 60 * 60),
+ ('https://img.site_1.com/resource/1', 60 * 60 * 2),
+ ('https://site_2.com', 1),
+ ('https://any_other_site.com', 1),
+ ],
+)
+def test_get_url_expiration__evaluation_order(url, expected_expire_after):
+ """If there are multiple matches, the first match should be used in the order defined"""
+ urls_expire_after = {
+ '*.site_1.com/resource': 60 * 60 * 2,
+ '*.site_1.com': 60 * 60,
+ '*': 1,
+ }
+ assert get_url_expiration(url, urls_expire_after) == expected_expire_after