Skip to content

Commit 2b696f6

Browse files
authored
fix: use pytest native method name (#305)
1 parent 1015337 commit 2b696f6

7 files changed

+67
-63
lines changed

README.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
:target: https://pypi.python.org/pypi/cachecontrol
1212
:alt: Latest Version
1313

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

1717
CacheControl is a port of the caching algorithms in httplib2_ for use with
1818
requests_ session object.

tests/test_cache_control.py

+39-27
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@
55
"""
66
Unit tests that verify our caching methods work correctly.
77
"""
8-
import pytest
9-
from unittest.mock import ANY, Mock
108
import time
119
from tempfile import mkdtemp
10+
from unittest.mock import ANY, Mock
11+
12+
import pytest
1213

1314
from cachecontrol import CacheController
1415
from cachecontrol.cache import DictCache
1516
from cachecontrol.caches import SeparateBodyFileCache
16-
from .utils import NullSerializer, DummyResponse, DummyRequest
1717

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

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

2122

2223
class TestCacheControllerResponse(object):
@@ -126,13 +127,16 @@ def test_update_cached_response_no_local_cache(self):
126127
cache = DictCache({})
127128
cc = CacheController(cache)
128129
req = DummyRequest(url="http://localhost/", headers={"if-match": "xyz"})
129-
resp = DummyResponse(status=304, headers={
130-
"ETag": "xyz",
131-
"x-value": "b",
132-
"Date": time.strftime(TIME_FMT, time.gmtime()),
133-
"Cache-Control": "max-age=60",
134-
"Content-Length": "200"
135-
})
130+
resp = DummyResponse(
131+
status=304,
132+
headers={
133+
"ETag": "xyz",
134+
"x-value": "b",
135+
"Date": time.strftime(TIME_FMT, time.gmtime()),
136+
"Cache-Control": "max-age=60",
137+
"Content-Length": "200",
138+
},
139+
)
136140
# First, ensure the response from update_cached_response() matches the
137141
# cached one:
138142
result = cc.update_cached_response(req, resp)
@@ -176,13 +180,16 @@ def update_cached_response_with_valid_headers_test(self, cache):
176180
cc = CacheController(cache)
177181
url = "http://localhost:123/x"
178182
req = DummyRequest(url=url, headers={})
179-
cached_resp = DummyResponse(status=200, headers={
180-
"ETag": etag,
181-
"x-value:": "a",
182-
"Content-Length": "100",
183-
"Cache-Control": "max-age=60",
184-
"Date": time.strftime(TIME_FMT, time.gmtime()),
185-
})
183+
cached_resp = DummyResponse(
184+
status=200,
185+
headers={
186+
"ETag": etag,
187+
"x-value:": "a",
188+
"Content-Length": "100",
189+
"Cache-Control": "max-age=60",
190+
"Date": time.strftime(TIME_FMT, time.gmtime()),
191+
},
192+
)
186193
cc._cache_set(url, req, cached_resp, b"my body")
187194

188195
# Now we get another request, and it's a 304, with new value for
@@ -191,13 +198,16 @@ def update_cached_response_with_valid_headers_test(self, cache):
191198
# Set our content length to 200. That would be a mistake in
192199
# the server, but we'll handle it gracefully... for now.
193200
req = DummyRequest(url=url, headers={"if-match": etag})
194-
resp = DummyResponse(status=304, headers={
195-
"ETag": etag,
196-
"x-value": "b",
197-
"Date": time.strftime(TIME_FMT, time.gmtime()),
198-
"Cache-Control": "max-age=60",
199-
"Content-Length": "200"
200-
})
201+
resp = DummyResponse(
202+
status=304,
203+
headers={
204+
"ETag": etag,
205+
"x-value": "b",
206+
"Date": time.strftime(TIME_FMT, time.gmtime()),
207+
"Cache-Control": "max-age=60",
208+
"Content-Length": "200",
209+
},
210+
)
201211
# First, ensure the response from update_cached_response() matches the
202212
# cached one:
203213
result = cc.update_cached_response(req, resp)
@@ -214,15 +224,17 @@ def update_cached_response_with_valid_headers_test(self, cache):
214224
class TestCacheControlRequest(object):
215225
url = "http://foo.com/bar"
216226

217-
def setup(self):
227+
def setup_method(self):
218228
self.c = CacheController(DictCache(), serializer=NullSerializer())
219229

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

224234
def test_cache_request_no_headers(self):
225-
cached_resp = Mock(headers={"ETag": "jfd9094r808", "Content-Length": 100}, status=200)
235+
cached_resp = Mock(
236+
headers={"ETag": "jfd9094r808", "Content-Length": 100}, status=200
237+
)
226238
self.c.cache = DictCache({self.url: cached_resp})
227239
resp = self.req({})
228240
assert not resp

tests/test_expires_heuristics.py

+18-25
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,27 @@
44

55
import calendar
66
import time
7-
8-
from email.utils import formatdate, parsedate
97
from datetime import datetime
10-
8+
from email.utils import formatdate, parsedate
9+
from pprint import pprint
1110
from unittest.mock import Mock
11+
1212
from requests import Session, get
1313

1414
from cachecontrol import CacheControl
15-
from cachecontrol.heuristics import LastModified, ExpiresAfter, OneDayCache
16-
from cachecontrol.heuristics import TIME_FMT
17-
from cachecontrol.heuristics import BaseHeuristic
18-
from .utils import DummyResponse
15+
from cachecontrol.heuristics import (
16+
TIME_FMT,
17+
BaseHeuristic,
18+
ExpiresAfter,
19+
LastModified,
20+
OneDayCache,
21+
)
1922

20-
from pprint import pprint
23+
from .utils import DummyResponse
2124

2225

2326
class TestHeuristicWithoutWarning(object):
24-
25-
def setup(self):
26-
27+
def setup_method(self):
2728
class NoopHeuristic(BaseHeuristic):
2829
warning = Mock()
2930

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

3637
def test_no_header_change_means_no_warning_header(self, url):
3738
the_url = url + "optional_cacheable_request"
38-
resp = self.sess.get(the_url)
39+
self.sess.get(the_url)
3940

4041
assert not self.heuristic.warning.called
4142

4243

4344
class TestHeuristicWith3xxResponse(object):
44-
45-
def setup(self):
46-
45+
def setup_method(self):
4746
class DummyHeuristic(BaseHeuristic):
48-
4947
def update_headers(self, resp):
5048
return {"x-dummy-header": "foobar"}
5149

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

6462

6563
class TestUseExpiresHeuristic(object):
66-
6764
def test_expires_heuristic_arg(self):
6865
sess = Session()
6966
cached_sess = CacheControl(sess, heuristic=Mock())
7067
assert cached_sess
7168

7269

7370
class TestOneDayCache(object):
74-
75-
def setup(self):
71+
def setup_method(self):
7672
self.sess = Session()
7773
self.cached_sess = CacheControl(self.sess, heuristic=OneDayCache())
7874

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

9288

9389
class TestExpiresAfter(object):
94-
95-
def setup(self):
90+
def setup_method(self):
9691
self.sess = Session()
9792
self.cache_sess = CacheControl(self.sess, heuristic=ExpiresAfter(days=1))
9893

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

113108

114109
class TestLastModified(object):
115-
116-
def setup(self):
110+
def setup_method(self):
117111
self.sess = Session()
118112
self.cached_sess = CacheControl(self.sess, heuristic=LastModified())
119113

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

137131

138132
class TestModifiedUnitTests(object):
139-
140133
def last_modified(self, period):
141134
return time.strftime(TIME_FMT, time.gmtime(self.time_now - period))
142135

143-
def setup(self):
136+
def setup_method(self):
144137
self.heuristic = LastModified()
145138
self.time_now = time.time()
146139
day_in_seconds = 86400

tests/test_redirects.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212

1313
class TestPermanentRedirects(object):
14-
15-
def setup(self):
14+
def setup_method(self):
1615
self.sess = CacheControl(requests.Session())
1716

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

3433

3534
class TestMultipleChoicesRedirects(object):
36-
37-
def setup(self):
35+
def setup_method(self):
3836
self.sess = CacheControl(requests.Session())
3937

4038
def test_multiple_choices_is_cacheable(self, url):

tests/test_serialization.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
class TestSerializer(object):
15-
def setup(self):
15+
def setup_method(self):
1616
self.serializer = Serializer()
1717
self.response_data = {
1818
"response": {

tests/test_storage_redis.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
from datetime import datetime
6-
76
from unittest.mock import Mock
7+
88
from cachecontrol.caches import RedisCache
99

1010

1111
class TestRedisCache(object):
12-
13-
def setup(self):
12+
def setup_method(self):
1413
self.conn = Mock()
1514
self.cache = RedisCache(self.conn)
1615

tests/utils.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
from requests.structures import CaseInsensitiveDict
66

7+
from cachecontrol.serialize import Serializer
78

8-
class NullSerializer(object):
9+
10+
class NullSerializer(Serializer):
911

1012
def dumps(self, request, response, body=None):
1113
return response

0 commit comments

Comments
 (0)