Skip to content

Commit

Permalink
Use only integers for expire_after values in tests and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
JWCook committed Feb 15, 2022
1 parent 261bda4 commit 1f5d6f6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions docs/user_guide/expiration.md
Expand Up @@ -75,10 +75,10 @@ retrieving a new response. If you would like to use expired response data in the

For example:
```python
>>> # Cache a test response that will expire immediately
>>> # Cache a test response and wait until it's expired
>>> session = CachedSession(stale_if_error=True)
>>> session.get('https://httpbin.org/get', expire_after=0.0001)
>>> time.sleep(0.0001)
>>> session.get('https://httpbin.org/get', expire_after=1)
>>> time.sleep(1)
```

Afterward, let's say the page has moved and you get a 404, or the site is experiencing downtime and
Expand Down
20 changes: 10 additions & 10 deletions tests/unit/test_session.py
Expand Up @@ -10,7 +10,7 @@

import pytest
import requests
from requests import Request
from requests import Request, RequestException
from requests.structures import CaseInsensitiveDict

from requests_cache import ALL_METHODS, CachedResponse, CachedSession
Expand Down Expand Up @@ -154,12 +154,12 @@ def test_response_history(mock_session):

def test_repr(mock_session):
"""Test session and cache string representations"""
mock_session.expire_after = 10.5
mock_session.expire_after = 11
mock_session.cache.responses['key'] = 'value'
mock_session.cache.redirects['key'] = 'value'
mock_session.cache.redirects['key_2'] = 'value'

assert mock_session.cache.cache_name in repr(mock_session) and '10.5' in repr(mock_session)
assert mock_session.cache.cache_name in repr(mock_session) and '11' in repr(mock_session)
assert '2 redirects' in str(mock_session.cache) and '1 responses' in str(mock_session.cache)


Expand Down Expand Up @@ -515,9 +515,9 @@ def test_expired_request_error(mock_session):
"""Without stale_if_error (default), if there is an error while re-fetching an expired
response, the request should be re-raised and the expired item deleted"""
mock_session.stale_if_error = False
mock_session.expire_after = 0.01
mock_session.expire_after = 1
mock_session.get(MOCKED_URL)
time.sleep(0.01)
time.sleep(1)

with patch.object(mock_session.cache, 'save_response', side_effect=ValueError):
with pytest.raises(ValueError):
Expand All @@ -528,25 +528,25 @@ def test_expired_request_error(mock_session):
def test_stale_if_error__exception(mock_session):
"""With stale_if_error, expect to get old cache data if there is an exception during a request"""
mock_session.stale_if_error = True
mock_session.expire_after = 0.2
mock_session.expire_after = 1

assert mock_session.get(MOCKED_URL).from_cache is False
assert mock_session.get(MOCKED_URL).from_cache is True
time.sleep(0.2)
with patch.object(mock_session.cache, 'save_response', side_effect=ValueError):
time.sleep(1)
with patch.object(mock_session.cache, 'save_response', side_effect=RequestException):
response = mock_session.get(MOCKED_URL)
assert response.from_cache is True and response.is_expired is True


def test_stale_if_error__error_code(mock_session):
"""With stale_if_error, expect to get old cache data if a response has an error status code"""
mock_session.stale_if_error = True
mock_session.expire_after = 0.2
mock_session.expire_after = 1
mock_session.allowable_codes = (200, 404)

assert mock_session.get(MOCKED_URL_404).from_cache is False

time.sleep(0.2)
time.sleep(1)
response = mock_session.get(MOCKED_URL_404)
assert response.from_cache is True and response.is_expired is True

Expand Down

0 comments on commit 1f5d6f6

Please sign in to comment.