MCPcopy Create free account
hub / github.com/psf/cachecontrol / FileCacheTestsMixin

Class FileCacheTestsMixin

tests/test_storage_filecache.py:28–118  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

26
27
28class FileCacheTestsMixin:
29 FileCacheClass = None # Either FileCache or SeparateBodyFileCache
30
31 @pytest.fixture()
32 def sess(self, url, tmpdir):
33 self.url = url
34 self.cache = self.FileCacheClass(str(tmpdir))
35 sess = CacheControl(requests.Session(), cache=self.cache)
36 yield sess
37
38 # closing session object
39 sess.close()
40
41 def test_filecache_from_cache(self, sess):
42 response = sess.get(self.url)
43 assert not response.from_cache
44 response = sess.get(self.url)
45 assert response.from_cache
46
47 def test_filecache_directory_not_exists(self, tmpdir, sess):
48 url = self.url + "".join(sample(string.ascii_lowercase, randint(2, 4)))
49
50 # Make sure our cache dir doesn't exist
51 tmp_cache = tmpdir.join("missing", "folder", "name").strpath
52 assert not os.path.exists(tmp_cache)
53
54 self.cache.directory = tmp_cache
55
56 # trigger a cache save
57 sess.get(url)
58
59 # Now our cache dir does exist
60 assert os.path.exists(tmp_cache)
61
62 def test_filecache_directory_already_exists(self, tmpdir, sess):
63 """
64 Assert no errors are raised when using a cache directory
65 that already exists on the filesystem.
66 """
67 url = self.url + "".join(sample(string.ascii_lowercase, randint(2, 4)))
68
69 # Make sure our cache dir DOES exist
70 tmp_cache = tmpdir.join("missing", "folder", "name").strpath
71 os.makedirs(tmp_cache, self.cache.dirmode)
72
73 assert os.path.exists(tmp_cache)
74
75 self.cache.directory = tmp_cache
76
77 # trigger a cache save
78 sess.get(url)
79
80 assert True # b/c no exceptions were raised
81
82 def test_key_length(self, sess):
83 """
84 Hash table keys:
85 Most file systems have a 255 characters path limitation.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…