Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test():
"""Test for the Cache class"""
# Create the Cache
my_cache = Cache(max_size=5, timeout=1)
my_cache.set('foo', 'bar')
# Test storage
assert my_cache.get('foo') == 'bar'
# Test timeout
time.sleep(1.1)
assert my_cache.get('foo') is None
# Test max_size
my_cache = Cache(max_size=5)
for i in range(6):
my_cache.set(str(i), i)
# So the '0' key should no longer be there FIFO
assert my_cache.get('0') is None
def test():
"""Test for the Cache class"""
# Create the Cache
my_cache = Cache(max_size=5, timeout=1)
my_cache.set('foo', 'bar')
# Test storage
assert my_cache.get('foo') == 'bar'
# Test timeout
time.sleep(1.1)
assert my_cache.get('foo') is None
# Test max_size
my_cache = Cache(max_size=5)
for i in range(6):
my_cache.set(str(i), i)
# So the '0' key should no longer be there FIFO
assert my_cache.get('0') is None
assert my_cache.get('5') is not None
# Make sure size is working
assert my_cache.size == 5
# Dump the cache
my_cache.dump()
# Test storing 'null' values
my_cache.set(0, 'foo')
my_cache.set(0, 'bar')
my_cache = Cache(max_size=5, timeout=1)
for i in range(5):
my_cache.set(str(i), i)
my_cache._compression_timer = 1
assert my_cache.size == 5
# Make sure compression is working
time.sleep(1.1)
my_cache._compress()
assert my_cache.size == 0
# Also make sure compression call is throttled
my_cache._compress() # Should not output a compression message
# Test persistance functionality
my_cache = Cache(load='my_test_cache')
for i in range(5):
my_cache.set(str(i), i)
assert my_cache.size == 5
my_cache.persist()
del my_cache
load_cache = Cache(load='my_test_cache')
assert load_cache.size == 5
load_cache.dump()