django-new / tests / othertests / cache.py

The default branch has multiple heads

# Unit tests for cache framework
# Uses whatever cache backend is set in the test settings file.

from django.core.cache import cache
import time

# functions/classes for complex data type tests        
def f():
    return 42
class C:
    def m(n):
        return 24

# simple set/get
cache.set("key", "value")
assert cache.get("key") == "value"

# get with non-existent keys
assert cache.get("does not exist") is None
assert cache.get("does not exist", "bang!") == "bang!"

# get_many
cache.set('a', 'a')
cache.set('b', 'b')
cache.set('c', 'c')
cache.set('d', 'd')
assert cache.get_many(['a', 'c', 'd']) == {'a' : 'a', 'c' : 'c', 'd' : 'd'}
assert cache.get_many(['a', 'b', 'e']) == {'a' : 'a', 'b' : 'b'}

# delete
cache.set("key1", "spam")
cache.set("key2", "eggs")
assert cache.get("key1") == "spam"
cache.delete("key1")
assert cache.get("key1") is None
assert cache.get("key2") == "eggs"

# has_key
cache.set("hello", "goodbye")
assert cache.has_key("hello") == True
assert cache.has_key("goodbye") == False

# 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)
    assert cache.get(key) == value
    
# expiration
cache.set('expire', 'very quickly', 1)
time.sleep(2)
assert cache.get("expire") == None
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.