Refs #29983 -- Added tests for FileBasedCache pathlib support.

This commit is contained in:
Jon Dufresne 2019-11-06 01:01:01 -08:00 committed by Carlton Gibson
parent fbbff7f808
commit 74f2a58b3a
1 changed files with 12 additions and 2 deletions

14
tests/cache/tests.py vendored
View File

@ -10,6 +10,7 @@ import tempfile
import threading import threading
import time import time
import unittest import unittest
from pathlib import Path
from unittest import mock from unittest import mock
from django.conf import settings from django.conf import settings
@ -1422,12 +1423,12 @@ class FileBasedCacheTests(BaseCacheTests, TestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.dirname = tempfile.mkdtemp() self.dirname = self.mkdtemp()
# Caches location cannot be modified through override_settings / modify_settings, # Caches location cannot be modified through override_settings / modify_settings,
# hence settings are manipulated directly here and the setting_changed signal # hence settings are manipulated directly here and the setting_changed signal
# is triggered manually. # is triggered manually.
for cache_params in settings.CACHES.values(): for cache_params in settings.CACHES.values():
cache_params.update({'LOCATION': self.dirname}) cache_params['LOCATION'] = self.dirname
setting_changed.send(self.__class__, setting='CACHES', enter=False) setting_changed.send(self.__class__, setting='CACHES', enter=False)
def tearDown(self): def tearDown(self):
@ -1435,6 +1436,9 @@ class FileBasedCacheTests(BaseCacheTests, TestCase):
# Call parent first, as cache.clear() may recreate cache base directory # Call parent first, as cache.clear() may recreate cache base directory
shutil.rmtree(self.dirname) shutil.rmtree(self.dirname)
def mkdtemp(self):
return tempfile.mkdtemp()
def test_ignores_non_cache_files(self): def test_ignores_non_cache_files(self):
fname = os.path.join(self.dirname, 'not-a-cache-file') fname = os.path.join(self.dirname, 'not-a-cache-file')
with open(fname, 'w'): with open(fname, 'w'):
@ -1473,6 +1477,12 @@ class FileBasedCacheTests(BaseCacheTests, TestCase):
self.assertIs(cache._is_expired(fh), True) self.assertIs(cache._is_expired(fh), True)
class FileBasedCachePathLibTests(FileBasedCacheTests):
def mkdtemp(self):
tmp_dir = super().mkdtemp()
return Path(tmp_dir)
@override_settings(CACHES={ @override_settings(CACHES={
'default': { 'default': {
'BACKEND': 'cache.liberal_backend.CacheClass', 'BACKEND': 'cache.liberal_backend.CacheClass',