More be-nice-to-the-buildbot: be better about cleaning up files created by the cache/session tests.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9224 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jacob Kaplan-Moss 2008-10-10 21:41:12 +00:00
parent c185135068
commit b21ea0a836
2 changed files with 68 additions and 40 deletions

View File

@ -54,6 +54,11 @@ True
>>> db_session.save()
>>> DatabaseSession('1').get('cat')
# Do file session tests in an isolated directory, and kill it after we're done.
>>> original_session_file_path = settings.SESSION_FILE_PATH
>>> import tempfile
>>> temp_session_store = settings.SESSION_FILE_PATH = tempfile.mkdtemp()
>>> file_session = FileSession()
>>> file_session.modified
False
@ -105,6 +110,17 @@ Traceback (innermost last):
...
ImproperlyConfigured: The session storage path '/if/this/directory/exists/you/have/a/weird/computer' doesn't exist. Please set your SESSION_FILE_PATH setting to an existing directory in which Django can store session data.
# Clean up after the file tests
>>> settings.SESSION_FILE_PATH = original_session_file_path
>>> import shutil
>>> shutil.rmtree(temp_session_store)
#
# Cache-based tests
# NB: be careful to delete any sessions created; stale sessions fill up the
# /tmp and eventually overwhelm it after lots of runs (think buildbots)
#
>>> cache_session = CacheSession()
>>> cache_session.modified
False
@ -144,7 +160,7 @@ True
>>> Session.objects.filter(pk=cache_session.session_key).delete()
>>> cache_session = CacheSession(cache_session.session_key)
>>> cache_session.save()
>>> CacheSession('1').get('cat')
>>> cache_session.delete(cache_session.session_key)
>>> s = SessionBase()
>>> s._session['some key'] = 'exists' # Pre-populate the session with some data

View File

@ -9,7 +9,7 @@ import tempfile
import time
import unittest
from django.core.cache import cache
from django.core.cache import cache, get_cache
from django.core.cache.backends.filebased import CacheClass as FileCache
from django.http import HttpResponse
from django.utils.cache import patch_vary_headers
@ -23,51 +23,64 @@ class C:
return 24
class Cache(unittest.TestCase):
def setUp(self):
# Special-case the file cache so we can clean up after ourselves.
if isinstance(cache, FileCache):
self.cache_dir = tempfile.mkdtemp()
self.cache = get_cache("file:///%s" % self.cache_dir)
else:
self.cache_dir = None
self.cache = cache
def tearDown(self):
if self.cache_dir is not None:
shutil.rmtree(self.cache_dir)
def test_simple(self):
# simple set/get
cache.set("key", "value")
self.assertEqual(cache.get("key"), "value")
self.cache.set("key", "value")
self.assertEqual(self.cache.get("key"), "value")
def test_add(self):
# test add (only add if key isn't already in cache)
cache.add("addkey1", "value")
result = cache.add("addkey1", "newvalue")
self.cache.add("addkey1", "value")
result = self.cache.add("addkey1", "newvalue")
self.assertEqual(result, False)
self.assertEqual(cache.get("addkey1"), "value")
self.assertEqual(self.cache.get("addkey1"), "value")
def test_non_existent(self):
# get with non-existent keys
self.assertEqual(cache.get("does_not_exist"), None)
self.assertEqual(cache.get("does_not_exist", "bang!"), "bang!")
self.assertEqual(self.cache.get("does_not_exist"), None)
self.assertEqual(self.cache.get("does_not_exist", "bang!"), "bang!")
def test_get_many(self):
# get_many
cache.set('a', 'a')
cache.set('b', 'b')
cache.set('c', 'c')
cache.set('d', 'd')
self.assertEqual(cache.get_many(['a', 'c', 'd']), {'a' : 'a', 'c' : 'c', 'd' : 'd'})
self.assertEqual(cache.get_many(['a', 'b', 'e']), {'a' : 'a', 'b' : 'b'})
self.cache.set('a', 'a')
self.cache.set('b', 'b')
self.cache.set('c', 'c')
self.cache.set('d', 'd')
self.assertEqual(self.cache.get_many(['a', 'c', 'd']), {'a' : 'a', 'c' : 'c', 'd' : 'd'})
self.assertEqual(self.cache.get_many(['a', 'b', 'e']), {'a' : 'a', 'b' : 'b'})
def test_delete(self):
# delete
cache.set("key1", "spam")
cache.set("key2", "eggs")
self.assertEqual(cache.get("key1"), "spam")
cache.delete("key1")
self.assertEqual(cache.get("key1"), None)
self.assertEqual(cache.get("key2"), "eggs")
self.cache.set("key1", "spam")
self.cache.set("key2", "eggs")
self.assertEqual(self.cache.get("key1"), "spam")
self.cache.delete("key1")
self.assertEqual(self.cache.get("key1"), None)
self.assertEqual(self.cache.get("key2"), "eggs")
def test_has_key(self):
# has_key
cache.set("hello1", "goodbye1")
self.assertEqual(cache.has_key("hello1"), True)
self.assertEqual(cache.has_key("goodbye1"), False)
self.cache.set("hello1", "goodbye1")
self.assertEqual(self.cache.has_key("hello1"), True)
self.assertEqual(self.cache.has_key("goodbye1"), False)
def test_in(self):
cache.set("hello2", "goodbye2")
self.assertEqual("hello2" in cache, True)
self.assertEqual("goodbye2" in cache, False)
self.cache.set("hello2", "goodbye2")
self.assertEqual("hello2" in self.cache, True)
self.assertEqual("goodbye2" in self.cache, False)
def test_data_types(self):
stuff = {
@ -79,20 +92,20 @@ class Cache(unittest.TestCase):
'function' : f,
'class' : C,
}
cache.set("stuff", stuff)
self.assertEqual(cache.get("stuff"), stuff)
self.cache.set("stuff", stuff)
self.assertEqual(self.cache.get("stuff"), stuff)
def test_expiration(self):
cache.set('expire1', 'very quickly', 1)
cache.set('expire2', 'very quickly', 1)
cache.set('expire3', 'very quickly', 1)
self.cache.set('expire1', 'very quickly', 1)
self.cache.set('expire2', 'very quickly', 1)
self.cache.set('expire3', 'very quickly', 1)
time.sleep(2)
self.assertEqual(cache.get("expire1"), None)
self.assertEqual(self.cache.get("expire1"), None)
cache.add("expire2", "newvalue")
self.assertEqual(cache.get("expire2"), "newvalue")
self.assertEqual(cache.has_key("expire3"), False)
self.cache.add("expire2", "newvalue")
self.assertEqual(self.cache.get("expire2"), "newvalue")
self.assertEqual(self.cache.has_key("expire3"), False)
def test_unicode(self):
stuff = {
@ -102,8 +115,8 @@ class Cache(unittest.TestCase):
u'ascii': {u'x' : 1 }
}
for (key, value) in stuff.items():
cache.set(key, value)
self.assertEqual(cache.get(key), value)
self.cache.set(key, value)
self.assertEqual(self.cache.get(key), value)
class FileBasedCacheTests(unittest.TestCase):
@ -111,8 +124,7 @@ class FileBasedCacheTests(unittest.TestCase):
Specific test cases for the file-based cache.
"""
def setUp(self):
self.dirname = tempfile.mktemp()
os.mkdir(self.dirname)
self.dirname = tempfile.mkdtemp()
self.cache = FileCache(self.dirname, {})
def tearDown(self):