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.set(None, 'foo')
    my_cache.set('', None)
    assert my_cache.get('') is None
    assert my_cache.get(None) == 'foo'
    assert my_cache.get(0) == 'bar'

    # Test the cache compression
    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()
def test ( ) : my_cache = Cache ( max_size = 5 , timeout = 1 ) my_cache . set ( 'foo' , 'bar' ) assert my_cache . get ( 'foo' ) == 'bar' time . sleep ( 1.1 ) assert my_cache . get ( 'foo' ) is None my_cache = Cache ( max_size = 5 ) for i in range ( 6 ) : my_cache . set ( str ( i ) , i ) assert my_cache . get ( '0' ) is None assert my_cache . get ( '5' ) is not None assert my_cache . size == 5 my_cache . dump ( ) my_cache . set ( 0 , 'foo' ) my_cache . set ( 0 , 'bar' ) my_cache . set ( None , 'foo' ) my_cache . set ( '' , None ) assert my_cache . get ( '' ) is None assert my_cache . get ( None ) == 'foo' assert my_cache . get ( 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 time . sleep ( 1.1 ) my_cache . _compress ( ) assert my_cache . size == 0 my_cache . _compress ( )
Test for the Cache class