summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.github/workflows/build.yml5
-rw-r--r--noxfile.py16
-rw-r--r--requests_cache/backends/base.py2
-rw-r--r--requests_cache/backends/gridfs.py5
-rw-r--r--tests/integration/base_storage_test.py6
-rw-r--r--tests/unit/test_serializers.py24
6 files changed, 30 insertions, 28 deletions
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 1d81cb1..29c85c7 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -10,7 +10,7 @@ on:
env:
LATEST_PY_VERSION: 3.9
COVERAGE_ARGS: '--cov --cov-report=term --cov-report=html'
- COMPLEXITY_ARGS: '--show-complexity --average --order SCORE'
+ XDIST_ARGS: '--numprocesses=auto --dist=loadfile'
jobs:
# Run tests for each supported python version
@@ -66,7 +66,7 @@ jobs:
- name: Run tests
run: |
source $VENV
- pytest -rs -x tests/unit --numprocesses=auto ${{ env.COVERAGE_ARGS }}
+ pytest -rs -x tests/unit ${{ env.XDIST_ARGS }} ${{ env.COVERAGE_ARGS }}
pytest -rs -x tests/integration --cov-append ${{ env.COVERAGE_ARGS }}
# Latest python version: send coverage report to coveralls
@@ -79,7 +79,6 @@ jobs:
pip install coveralls
coveralls --service=github
-
# Run code analysis checks via pre-commit hooks
analyze:
runs-on: ubuntu-18.04
diff --git a/noxfile.py b/noxfile.py
index 30e08fa..5f1765c 100644
--- a/noxfile.py
+++ b/noxfile.py
@@ -18,7 +18,9 @@ LIVE_DOCS_WATCH = ['requests_cache', 'examples']
CLEAN_DIRS = ['dist', 'build', join('docs', '_build'), join('docs', 'modules')]
UNIT_TESTS = join('tests', 'unit')
-INT_TESTS = join('tests', 'integration')
+INTEGRATION_TESTS = join('tests', 'integration')
+COVERAGE_ARGS = '--cov --cov-report=term --cov-report=html' # Generate HTML + stdout coverage report
+XDIST_ARGS = '--numprocesses=auto --dist=loadfile' # Run tests in parallel, grouped by test module
@session(python=['3.6', '3.7', '3.8', '3.9', '3.10'])
@@ -26,7 +28,9 @@ def test(session):
"""Run tests for a specific python version"""
test_paths = session.posargs or [UNIT_TESTS]
session.install('.', 'pytest', 'pytest-order', 'pytest-xdist', 'requests-mock', 'timeout-decorator')
- session.run('pytest', '-vv', '-n', 'auto', *test_paths)
+
+ cmd = f'pytest -rs {XDIST_ARGS}'
+ session.run(*cmd.split(' '), *test_paths)
@session(python=False)
@@ -37,13 +41,11 @@ def clean(session):
rmtree(dir, ignore_errors=True)
-@session(python=False)
@session(python=False, name='cov')
def coverage(session):
"""Run tests and generate coverage report"""
- coverage_args = '--cov --cov-report=term --cov-report=html'
- cmd_1 = f'pytest {UNIT_TESTS} --numprocesses=auto {coverage_args}'
- cmd_2 = f'pytest {INT_TESTS} --cov-append {coverage_args}'
+ cmd_1 = f'pytest {UNIT_TESTS} -rs {XDIST_ARGS} {COVERAGE_ARGS}'
+ cmd_2 = f'pytest {INTEGRATION_TESTS} -rs {COVERAGE_ARGS} --cov-append'
session.run(*cmd_1.split(' '))
session.run(*cmd_2.split(' '))
@@ -58,7 +60,7 @@ def docs(session):
@session(python=False)
def livedocs(session):
"""Auto-build docs with live reload in browser.
- Add `-- open` to also open the browser after starting.
+ Add `--open` to also open the browser after starting.
"""
args = ['-a']
args += [f'--watch {pattern}' for pattern in LIVE_DOCS_WATCH]
diff --git a/requests_cache/backends/base.py b/requests_cache/backends/base.py
index 87e5d49..3e4b045 100644
--- a/requests_cache/backends/base.py
+++ b/requests_cache/backends/base.py
@@ -32,7 +32,7 @@ class BaseCache:
ignored_parameters: Iterable[str] = None,
**kwargs,
):
- self.name: str = ''
+ self.name: str = kwargs.get('cache_name', '')
self.redirects: BaseStorage = DictStorage()
self.responses: BaseStorage = DictStorage()
self.include_get_headers = include_get_headers
diff --git a/requests_cache/backends/gridfs.py b/requests_cache/backends/gridfs.py
index cd13d9e..8ac8e6a 100644
--- a/requests_cache/backends/gridfs.py
+++ b/requests_cache/backends/gridfs.py
@@ -27,8 +27,9 @@ class GridFSCache(BaseCache):
def __init__(self, db_name: str, **kwargs):
super().__init__(**kwargs)
self.responses = GridFSPickleDict(db_name, **kwargs)
- kwargs['connection'] = self.responses.connection
- self.redirects = MongoDict(db_name, collection_name='redirects', **kwargs)
+ self.redirects = MongoDict(
+ db_name, collection_name='redirects', connection=self.responses.connection, **kwargs
+ )
class GridFSPickleDict(BaseStorage):
diff --git a/tests/integration/base_storage_test.py b/tests/integration/base_storage_test.py
index f3755ca..c4ec32b 100644
--- a/tests/integration/base_storage_test.py
+++ b/tests/integration/base_storage_test.py
@@ -1,5 +1,4 @@
"""Common tests to run for all backends (BaseStorage subclasses)"""
-import pickle
from typing import Dict, Type
import pytest
@@ -18,9 +17,8 @@ class BaseStorageTest:
num_instances: int = 10 # Max number of cache instances to test
def init_cache(self, index=0, clear=True, **kwargs):
- cache = self.storage_class(
- CACHE_NAME, f'table_{index}', serializer=pickle, **self.init_kwargs, **kwargs
- )
+ kwargs.setdefault('serializer', 'pickle')
+ cache = self.storage_class(CACHE_NAME, f'table_{index}', **self.init_kwargs, **kwargs)
if clear:
cache.clear()
return cache
diff --git a/tests/unit/test_serializers.py b/tests/unit/test_serializers.py
index f4ea86f..aeac0a8 100644
--- a/tests/unit/test_serializers.py
+++ b/tests/unit/test_serializers.py
@@ -37,24 +37,26 @@ def test_ujson():
assert module_json is ujson
-@patch.dict(sys.modules, {'bson': None, 'itsdangerous': None, 'yaml': None})
def test_optional_dependencies():
import requests_cache.serializers.preconf
- reload(requests_cache.serializers.preconf)
+ with patch.dict(sys.modules, {'bson': None, 'itsdangerous': None, 'yaml': None}):
+ reload(requests_cache.serializers.preconf)
- from requests_cache.serializers.preconf import (
- bson_serializer,
- safe_pickle_serializer,
- yaml_serializer,
- )
+ from requests_cache.serializers.preconf import (
+ bson_serializer,
+ safe_pickle_serializer,
+ yaml_serializer,
+ )
+
+ for obj in [bson_serializer, yaml_serializer]:
+ with pytest.raises(ImportError):
+ obj.dumps('')
- for obj in [bson_serializer, yaml_serializer]:
with pytest.raises(ImportError):
- obj.dumps('')
+ safe_pickle_serializer('')
- with pytest.raises(ImportError):
- safe_pickle_serializer('')
+ reload(requests_cache.serializers.preconf)
# TODO: This usage is deprecated. Keep this test for backwards-compatibility until removed in a future release.