summaryrefslogtreecommitdiff
path: root/tests/regressiontests/cache/tests.py
diff options
context:
space:
mode:
authorDerek Anderson <public@kered.org>2006-10-26 19:09:51 +0000
committerDerek Anderson <public@kered.org>2006-10-26 19:09:51 +0000
commit42851d90dadbf62f5d342ce5c4f496ba1eeba987 (patch)
treea5d0e5c178afb2d7dbb7bf5ab37db9ced42f4b52 /tests/regressiontests/cache/tests.py
parent450889c9a6f7da3c2fce77a0ccf4c4cea9e29710 (diff)
committing to schema-evolution
merge from HEAD git-svn-id: http://code.djangoproject.com/svn/django/branches/schema-evolution@3937 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/regressiontests/cache/tests.py')
-rw-r--r--tests/regressiontests/cache/tests.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py
new file mode 100644
index 0000000000..cf58ab321a
--- /dev/null
+++ b/tests/regressiontests/cache/tests.py
@@ -0,0 +1,71 @@
+# Unit tests for cache framework
+# Uses whatever cache backend is set in the test settings file.
+
+from django.core.cache import cache
+import time, unittest
+
+# functions/classes for complex data type tests
+def f():
+ return 42
+class C:
+ def m(n):
+ return 24
+
+class Cache(unittest.TestCase):
+ def test_simple(self):
+ # simple set/get
+ cache.set("key", "value")
+ self.assertEqual(cache.get("key"), "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!")
+
+ 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'})
+
+ 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")
+
+ def test_has_key(self):
+ # has_key
+ cache.set("hello", "goodbye")
+ self.assertEqual(cache.has_key("hello"), True)
+ self.assertEqual(cache.has_key("goodbye"), False)
+
+ def test_data_types(self):
+ # test data types
+ stuff = {
+ 'string' : 'this is a string',
+ 'int' : 42,
+ 'list' : [1, 2, 3, 4],
+ 'tuple' : (1, 2, 3, 4),
+ 'dict' : {'A': 1, 'B' : 2},
+ 'function' : f,
+ 'class' : C,
+ }
+ for (key, value) in stuff.items():
+ cache.set(key, value)
+ self.assertEqual(cache.get(key), value)
+
+ def test_expiration(self):
+ # expiration
+ cache.set('expire', 'very quickly', 1)
+ time.sleep(2)
+ self.assertEqual(cache.get("expire"), None)
+
+if __name__ == '__main__':
+ unittest.main() \ No newline at end of file