summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJordan Cook <jordan.cook@pioneer.com>2022-04-22 14:43:12 -0500
committerJordan Cook <jordan.cook@pioneer.com>2022-04-22 14:44:25 -0500
commit5ff3f01ee93d249cc443af74cc310d43791645ea (patch)
tree0e8cb95acdfde88aaadf0ce7b0169d18ebee4915 /tests
parent00af46fc03b63d95f1f586e1ca7207a13bcb3b42 (diff)
downloadrequests-cache-5ff3f01ee93d249cc443af74cc310d43791645ea.tar.gz
Skip tests for optional serializer dependencies if not installed
Diffstat (limited to 'tests')
-rw-r--r--tests/conftest.py24
-rw-r--r--tests/unit/test_serializers.py9
2 files changed, 27 insertions, 6 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
index 96c35a7..b242946 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -11,6 +11,7 @@ https://requests-mock.readthedocs.io/en/latest/adapter.html
import os
from datetime import datetime, timedelta
from functools import wraps
+from importlib import import_module
from logging import basicConfig, getLogger
from pathlib import Path
from unittest.mock import MagicMock
@@ -224,6 +225,12 @@ def get_mock_response(
)
+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
+
+
def fail_if_no_connection(connect_timeout: float = 1.0) -> bool:
"""Decorator for testing a backend connection. This will intentionally cause a test failure if
the wrapped function doesn't have dependencies installed, doesn't connect after a short timeout,
@@ -247,7 +254,16 @@ def fail_if_no_connection(connect_timeout: float = 1.0) -> bool:
return decorator
-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
+def is_installed(module_name: str) -> bool:
+ """Check if a given dependency is installed"""
+ try:
+ import_module(module_name)
+ return True
+ except ImportError:
+ return False
+
+
+def skip_missing_deps(module_name: str) -> pytest.Mark:
+ return pytest.mark.skipif(
+ not is_installed(module_name), reason=f'{module_name} is not installed'
+ )
diff --git a/tests/unit/test_serializers.py b/tests/unit/test_serializers.py
index 6f6f61e..9a611ab 100644
--- a/tests/unit/test_serializers.py
+++ b/tests/unit/test_serializers.py
@@ -9,8 +9,6 @@ from unittest.mock import patch
from uuid import uuid4
import pytest
-from itsdangerous import Signer
-from itsdangerous.exc import BadSignature
from requests_cache import (
CachedResponse,
@@ -21,6 +19,7 @@ from requests_cache import (
safe_pickle_serializer,
utf8_encoder,
)
+from tests.conftest import skip_missing_deps
def test_stdlib_json():
@@ -35,6 +34,7 @@ def test_stdlib_json():
reload(requests_cache.serializers.preconf)
+@skip_missing_deps('ujson')
def test_ujson():
import ujson
@@ -43,6 +43,7 @@ def test_ujson():
assert module_json is ujson
+@skip_missing_deps('bson')
def test_standalone_bson():
"""Handle different method names for standalone bson codec vs pymongo"""
import requests_cache.serializers.preconf
@@ -80,7 +81,11 @@ def test_optional_dependencies():
reload(requests_cache.serializers.preconf)
+@skip_missing_deps('itsdangerous')
def test_cache_signing(tempfile_path):
+ from itsdangerous import Signer
+ from itsdangerous.exc import BadSignature
+
serializer = safe_pickle_serializer(secret_key=str(uuid4()))
session = CachedSession(tempfile_path, serializer=serializer)
assert isinstance(session.cache.responses.serializer.stages[-1].obj, Signer)