diff options
author | Jordan Cook <jordan.cook@pioneer.com> | 2021-04-30 13:00:25 -0500 |
---|---|---|
committer | Jordan Cook <jordan.cook@pioneer.com> | 2021-05-07 09:36:07 -0500 |
commit | 6257a5061e159559705b22ee0b4fab011bd25dda (patch) | |
tree | e5e45205e0fde7d02da75845218d1f59431ebef7 /tests/conftest.py | |
parent | 6ccf39c92691724f3010240c7c93422af382a8f7 (diff) | |
download | requests-cache-6257a5061e159559705b22ee0b4fab011bd25dda.tar.gz |
Add support for using request and response cache headers to set cache expiration
Diffstat (limited to 'tests/conftest.py')
-rw-r--r-- | tests/conftest.py | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index 12cca1d..774d2de 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,7 +2,7 @@ Short description: * The ``mock_session`` fixture uses pre-configured mock requests, and should be used for unit tests. -* The ``tempfile_session`` fixture makes real HTTP requests, and should be used for integration test. +* The ``tempfile_session`` fixture makes real HTTP requests, and should be used for integration tests. Note: The protocol ``http(s)+mock://`` helps :py:class:`requests_mock.Adapter` play nicely with :py:class:`requests.PreparedRequest`. More info here: @@ -10,6 +10,7 @@ https://requests-mock.readthedocs.io/en/latest/adapter.html """ import os import pytest +from datetime import datetime, timezone from functools import wraps from logging import basicConfig, getLogger from tempfile import NamedTemporaryFile @@ -46,6 +47,9 @@ HTTPBIN_FORMATS = [ 'xml', ] +HTTPDATE_STR = 'Fri, 16 APR 2021 21:13:00 GMT' +HTTPDATE_DATETIME = datetime(2021, 4, 16, 21, 13, tzinfo=timezone.utc) + MOCKED_URL = 'http+mock://requests-cache.com/text' MOCKED_URL_HTTPS = 'https+mock://requests-cache.com/text' MOCKED_URL_JSON = 'http+mock://requests-cache.com/json' @@ -132,7 +136,7 @@ def tempfile_session() -> CachedSession: @pytest.fixture(scope='function') def tempfile_path() -> CachedSession: - """Get a tempfile path that will be deleted after the test finished""" + """Get a tempfile path that will be deleted after the test finishes""" with NamedTemporaryFile(suffix='.db') as temp: yield temp.name @@ -225,3 +229,9 @@ def fail_if_no_connection(func) -> bool: pytest.fail('Could not connect to backend') return wrapper + + +def assert_delta_approx_equal(dt1: datetime, dt2: datetime, target_delta, threshold_seconds=2): + """Assert that the given datetimes are approximately ``target_delta`` seconds apart""" + diff_in_seconds = (dt2 - dt1).total_seconds() + assert abs(diff_in_seconds - target_delta) <= threshold_seconds |