Skip to content

Commit

Permalink
fix: use pytest native method name (#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming authored May 31, 2023
1 parent 1015337 commit 2b696f6
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 63 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
:target: https://pypi.python.org/pypi/cachecontrol
:alt: Latest Version

.. image:: https://travis-ci.org/ionrock/cachecontrol.png?branch=master
:target: https://travis-ci.org/ionrock/cachecontrol
.. image:: https://github.com/psf/cachecontrol/actions/workflows/tests.yml/badge.svg
:target: https://github.com/psf/cachecontrol/actions/workflows/tests.yml

CacheControl is a port of the caching algorithms in httplib2_ for use with
requests_ session object.
Expand Down
66 changes: 39 additions & 27 deletions tests/test_cache_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
"""
Unit tests that verify our caching methods work correctly.
"""
import pytest
from unittest.mock import ANY, Mock
import time
from tempfile import mkdtemp
from unittest.mock import ANY, Mock

import pytest

from cachecontrol import CacheController
from cachecontrol.cache import DictCache
from cachecontrol.caches import SeparateBodyFileCache
from .utils import NullSerializer, DummyResponse, DummyRequest

TIME_FMT = "%a, %d %b %Y %H:%M:%S GMT"
from .utils import DummyRequest, DummyResponse, NullSerializer

TIME_FMT = "%a, %d %b %Y %H:%M:%S GMT"


class TestCacheControllerResponse(object):
Expand Down Expand Up @@ -126,13 +127,16 @@ def test_update_cached_response_no_local_cache(self):
cache = DictCache({})
cc = CacheController(cache)
req = DummyRequest(url="http://localhost/", headers={"if-match": "xyz"})
resp = DummyResponse(status=304, headers={
"ETag": "xyz",
"x-value": "b",
"Date": time.strftime(TIME_FMT, time.gmtime()),
"Cache-Control": "max-age=60",
"Content-Length": "200"
})
resp = DummyResponse(
status=304,
headers={
"ETag": "xyz",
"x-value": "b",
"Date": time.strftime(TIME_FMT, time.gmtime()),
"Cache-Control": "max-age=60",
"Content-Length": "200",
},
)
# First, ensure the response from update_cached_response() matches the
# cached one:
result = cc.update_cached_response(req, resp)
Expand Down Expand Up @@ -176,13 +180,16 @@ def update_cached_response_with_valid_headers_test(self, cache):
cc = CacheController(cache)
url = "http://localhost:123/x"
req = DummyRequest(url=url, headers={})
cached_resp = DummyResponse(status=200, headers={
"ETag": etag,
"x-value:": "a",
"Content-Length": "100",
"Cache-Control": "max-age=60",
"Date": time.strftime(TIME_FMT, time.gmtime()),
})
cached_resp = DummyResponse(
status=200,
headers={
"ETag": etag,
"x-value:": "a",
"Content-Length": "100",
"Cache-Control": "max-age=60",
"Date": time.strftime(TIME_FMT, time.gmtime()),
},
)
cc._cache_set(url, req, cached_resp, b"my body")

# Now we get another request, and it's a 304, with new value for
Expand All @@ -191,13 +198,16 @@ def update_cached_response_with_valid_headers_test(self, cache):
# Set our content length to 200. That would be a mistake in
# the server, but we'll handle it gracefully... for now.
req = DummyRequest(url=url, headers={"if-match": etag})
resp = DummyResponse(status=304, headers={
"ETag": etag,
"x-value": "b",
"Date": time.strftime(TIME_FMT, time.gmtime()),
"Cache-Control": "max-age=60",
"Content-Length": "200"
})
resp = DummyResponse(
status=304,
headers={
"ETag": etag,
"x-value": "b",
"Date": time.strftime(TIME_FMT, time.gmtime()),
"Cache-Control": "max-age=60",
"Content-Length": "200",
},
)
# First, ensure the response from update_cached_response() matches the
# cached one:
result = cc.update_cached_response(req, resp)
Expand All @@ -214,15 +224,17 @@ def update_cached_response_with_valid_headers_test(self, cache):
class TestCacheControlRequest(object):
url = "http://foo.com/bar"

def setup(self):
def setup_method(self):
self.c = CacheController(DictCache(), serializer=NullSerializer())

def req(self, headers):
mock_request = Mock(url=self.url, headers=headers)
return self.c.cached_request(mock_request)

def test_cache_request_no_headers(self):
cached_resp = Mock(headers={"ETag": "jfd9094r808", "Content-Length": 100}, status=200)
cached_resp = Mock(
headers={"ETag": "jfd9094r808", "Content-Length": 100}, status=200
)
self.c.cache = DictCache({self.url: cached_resp})
resp = self.req({})
assert not resp
Expand Down
43 changes: 18 additions & 25 deletions tests/test_expires_heuristics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@

import calendar
import time

from email.utils import formatdate, parsedate
from datetime import datetime

from email.utils import formatdate, parsedate
from pprint import pprint
from unittest.mock import Mock

from requests import Session, get

from cachecontrol import CacheControl
from cachecontrol.heuristics import LastModified, ExpiresAfter, OneDayCache
from cachecontrol.heuristics import TIME_FMT
from cachecontrol.heuristics import BaseHeuristic
from .utils import DummyResponse
from cachecontrol.heuristics import (
TIME_FMT,
BaseHeuristic,
ExpiresAfter,
LastModified,
OneDayCache,
)

from pprint import pprint
from .utils import DummyResponse


class TestHeuristicWithoutWarning(object):

def setup(self):

def setup_method(self):
class NoopHeuristic(BaseHeuristic):
warning = Mock()

Expand All @@ -35,17 +36,14 @@ def update_headers(self, resp):

def test_no_header_change_means_no_warning_header(self, url):
the_url = url + "optional_cacheable_request"
resp = self.sess.get(the_url)
self.sess.get(the_url)

assert not self.heuristic.warning.called


class TestHeuristicWith3xxResponse(object):

def setup(self):

def setup_method(self):
class DummyHeuristic(BaseHeuristic):

def update_headers(self, resp):
return {"x-dummy-header": "foobar"}

Expand All @@ -63,16 +61,14 @@ def test_heuristic_applies_to_304(self, url):


class TestUseExpiresHeuristic(object):

def test_expires_heuristic_arg(self):
sess = Session()
cached_sess = CacheControl(sess, heuristic=Mock())
assert cached_sess


class TestOneDayCache(object):

def setup(self):
def setup_method(self):
self.sess = Session()
self.cached_sess = CacheControl(self.sess, heuristic=OneDayCache())

Expand All @@ -91,8 +87,7 @@ def test_cache_for_one_day(self, url):


class TestExpiresAfter(object):

def setup(self):
def setup_method(self):
self.sess = Session()
self.cache_sess = CacheControl(self.sess, heuristic=ExpiresAfter(days=1))

Expand All @@ -112,8 +107,7 @@ def test_expires_after_one_day(self, url):


class TestLastModified(object):

def setup(self):
def setup_method(self):
self.sess = Session()
self.cached_sess = CacheControl(self.sess, heuristic=LastModified())

Expand All @@ -136,11 +130,10 @@ def datetime_to_header(dt):


class TestModifiedUnitTests(object):

def last_modified(self, period):
return time.strftime(TIME_FMT, time.gmtime(self.time_now - period))

def setup(self):
def setup_method(self):
self.heuristic = LastModified()
self.time_now = time.time()
day_in_seconds = 86400
Expand Down
6 changes: 2 additions & 4 deletions tests/test_redirects.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@


class TestPermanentRedirects(object):

def setup(self):
def setup_method(self):
self.sess = CacheControl(requests.Session())

def test_redirect_response_is_cached(self, url):
Expand All @@ -33,8 +32,7 @@ def test_bust_cache_on_redirect(self, url):


class TestMultipleChoicesRedirects(object):

def setup(self):
def setup_method(self):
self.sess = CacheControl(requests.Session())

def test_multiple_choices_is_cacheable(self, url):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


class TestSerializer(object):
def setup(self):
def setup_method(self):
self.serializer = Serializer()
self.response_data = {
"response": {
Expand Down
5 changes: 2 additions & 3 deletions tests/test_storage_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
# SPDX-License-Identifier: Apache-2.0

from datetime import datetime

from unittest.mock import Mock

from cachecontrol.caches import RedisCache


class TestRedisCache(object):

def setup(self):
def setup_method(self):
self.conn = Mock()
self.cache = RedisCache(self.conn)

Expand Down
4 changes: 3 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

from requests.structures import CaseInsensitiveDict

from cachecontrol.serialize import Serializer

class NullSerializer(object):

class NullSerializer(Serializer):

def dumps(self, request, response, body=None):
return response
Expand Down

0 comments on commit 2b696f6

Please sign in to comment.