<s> import unittest <EOL> import supycache <EOL> class TestDictCache ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> from supycache . backends import DictCache <EOL> self . cache = DictCache ( ) <EOL> def tearDown ( self ) : <EOL> self . cache . clear ( ) <EOL> def test_init ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertTrue ( hasattr ( self . cache , '<STR_LIT>' ) ) <EOL> self . assertTrue ( hasattr ( self . cache , '<STR_LIT>' ) ) <EOL> self . assertTrue ( hasattr ( self . cache , '<STR_LIT>' ) ) <EOL> def test_methods ( self ) : <EOL> """<STR_LIT>""" <EOL> self . cache . set ( '<STR_LIT:key>' , '<STR_LIT:value>' ) <EOL> self . assertTrue ( self . cache . get ( '<STR_LIT:key>' ) == '<STR_LIT:value>' ) <EOL> self . assertTrue ( bool ( self . cache . get ( '<STR_LIT>' ) ) == False ) <EOL> self . assertTrue ( self . cache . clear ( ) == None ) <EOL> self . assertTrue ( len ( self . cache . _data ) == <NUM_LIT:0> ) <EOL> def test_get_set_default_backend ( ) : <EOL> """<STR_LIT>""" <EOL> reload ( supycache ) <EOL> from supycache . backends import DictCache <EOL> assert ( supycache . default_backend == None ) <EOL> assert ( isinstance ( supycache . get_default_backend ( ) , DictCache ) ) <EOL> assert ( isinstance ( supycache . default_backend , DictCache ) ) <EOL> new_backend = DictCache ( ) <EOL> supycache . set_default_backend ( new_backend ) <EOL> assert ( supycache . get_default_backend ( ) is new_backend ) <EOL> assert ( supycache . default_backend is new_backend ) <EOL> class TestCacheDecorators ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> from supycache . backends import DictCache <EOL> self . backend = DictCache ( ) <EOL> supycache . default_backend = self . backend <EOL> def tearDown ( self ) : <EOL> self . backend . clear ( ) <EOL> def test_missing_options ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertRaises ( KeyError ) as context : <EOL> @ supycache . supycache ( ) <EOL> def simple_function ( ) : <EOL> return '<STR_LIT>' <EOL> self . assertTrue ( '<STR_LIT>' in context . exception . message ) <EOL> def test_do_not_ignore_errors ( self ) : <EOL> """<STR_LIT>""" <EOL> from supycache . backends import DictCache <EOL> class TestException ( Exception ) : <EOL> pass <EOL> class DummyBackend : <EOL> config = { } <EOL> def raise_exc ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> raise TestException ( ) <EOL> backend = DummyBackend ( ) <EOL> supycache . set_default_backend ( backend ) <EOL> @ supycache . supycache ( cache_key = '<STR_LIT>' , ignore_errors = False ) <EOL> def simple_function ( ) : <EOL> return '<STR_LIT>' <EOL> with self . assertRaises ( TestException ) as context : <EOL> backend . get = backend . raise_exc <EOL> simple_function ( ) <EOL> with self . assertRaises ( TestException ) as context : <EOL> backend . get = lambda key : None <EOL> backend . set = backend . raise_exc <EOL> simple_function ( ) <EOL> with self . assertRaises ( TestException ) as context : <EOL> backend . delete = backend . raise_exc <EOL> simple_function ( ) <EOL> def test_decorator_for_cache_key_cache_miss ( self ) : <EOL> """<STR_LIT>""" <EOL> @ supycache . supycache ( cache_key = '<STR_LIT>' ) <EOL> def simple_function ( ) : <EOL> return '<STR_LIT>' <EOL> simple_function ( ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT>' ) == '<STR_LIT>' ) <EOL> def test_decorator_for_cache_key_cached ( self ) : <EOL> """<STR_LIT>""" <EOL> @ supycache . supycache ( cache_key = '<STR_LIT>' ) <EOL> def simple_function ( call_count ) : <EOL> return '<STR_LIT>' % call_count <EOL> simple_function ( <NUM_LIT:1> ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT>' ) == '<STR_LIT>' ) <EOL> simple_function ( <NUM_LIT:2> ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT>' ) == '<STR_LIT>' ) <EOL> simple_function ( <NUM_LIT:3> ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT>' ) == '<STR_LIT>' ) <EOL> def test_decorator_for_cache_key_positional_args ( self ) : <EOL> """<STR_LIT>""" <EOL> @ supycache . supycache ( cache_key = '<STR_LIT>' ) <EOL> def simple_function ( x , y ) : <EOL> return '<STR_LIT>' % y <EOL> simple_function ( '<STR_LIT:key>' , <NUM_LIT:1> ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT:key>' ) == '<STR_LIT>' ) <EOL> simple_function ( '<STR_LIT:key>' , <NUM_LIT:2> ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT:key>' ) == '<STR_LIT>' ) <EOL> simple_function ( '<STR_LIT>' , <NUM_LIT:3> ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT>' ) == '<STR_LIT>' ) <EOL> def test_decorator_for_cache_key_keyword_args ( self ) : <EOL> """<STR_LIT>""" <EOL> @ supycache . supycache ( cache_key = '<STR_LIT>' ) <EOL> def simple_function ( call_count , somearg ) : <EOL> return '<STR_LIT>' % call_count <EOL> simple_function ( <NUM_LIT:1> , somearg = '<STR_LIT:key>' ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT:key>' ) == '<STR_LIT>' ) <EOL> simple_function ( <NUM_LIT:2> , somearg = '<STR_LIT:key>' ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT:key>' ) == '<STR_LIT>' ) <EOL> simple_function ( <NUM_LIT:3> , somearg = '<STR_LIT>' ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT>' ) == '<STR_LIT>' ) <EOL> def test_decorator_for_cache_key_multi_args_simple ( self ) : <EOL> """<STR_LIT>""" <EOL> @ supycache . supycache ( cache_key = '<STR_LIT>' ) <EOL> def simple_function ( positional , call_count , keyword = '<STR_LIT>' ) : <EOL> return '<STR_LIT>' % call_count <EOL> simple_function ( '<STR_LIT>' , <NUM_LIT:1> , keyword = '<STR_LIT:key>' ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT>' ) == '<STR_LIT>' ) <EOL> simple_function ( '<STR_LIT>' , <NUM_LIT:2> , keyword = '<STR_LIT:key>' ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT>' ) == '<STR_LIT>' ) <EOL> simple_function ( '<STR_LIT>' , <NUM_LIT:1> , keyword = '<STR_LIT>' ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT>' ) == '<STR_LIT>' ) <EOL> def test_decorator_for_cache_key_multi_args_complex_list ( self ) : <EOL> """<STR_LIT>""" <EOL> @ supycache . supycache ( cache_key = '<STR_LIT>' ) <EOL> def simple_function ( positional , call_count , arglist = None ) : <EOL> return '<STR_LIT>' % call_count <EOL> simple_function ( '<STR_LIT>' , <NUM_LIT:1> , arglist = [ '<STR_LIT:key>' , '<STR_LIT>' ] ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT>' ) == '<STR_LIT>' ) <EOL> simple_function ( '<STR_LIT>' , <NUM_LIT:2> , arglist = [ '<STR_LIT:key>' , '<STR_LIT>' ] ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT>' ) == '<STR_LIT>' ) <EOL> simple_function ( '<STR_LIT>' , <NUM_LIT:1> , arglist = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT>' ) == '<STR_LIT>' ) <EOL> simple_function ( '<STR_LIT>' , <NUM_LIT:1> , arglist = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT>' ) == '<STR_LIT>' ) <EOL> def test_decorator_for_cache_key_multi_args_complex_dict ( self ) : <EOL> """<STR_LIT>""" <EOL> @ supycache . supycache ( cache_key = '<STR_LIT>' ) <EOL> def simple_function ( positional , call_count , argdict = None ) : <EOL> return '<STR_LIT>' % call_count <EOL> simple_function ( '<STR_LIT>' , <NUM_LIT:1> , argdict = { '<STR_LIT>' : '<STR_LIT:key>' } ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT>' ) == '<STR_LIT>' ) <EOL> simple_function ( '<STR_LIT>' , <NUM_LIT:2> , argdict = { '<STR_LIT>' : '<STR_LIT:key>' } ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT>' ) == '<STR_LIT>' ) <EOL> simple_function ( '<STR_LIT>' , <NUM_LIT:1> , argdict = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT>' ) == '<STR_LIT>' ) <EOL> def test_decorator_for_cache_key_multi_args_complex_object ( self ) : <EOL> """<STR_LIT>""" <EOL> @ supycache . supycache ( cache_key = '<STR_LIT>' ) <EOL> def simple_function ( positional , call_count , arg ) : <EOL> return '<STR_LIT>' % call_count <EOL> class DummyArg : <EOL> def __init__ ( self , value ) : <EOL> self . name = value <EOL> simple_function ( '<STR_LIT>' , <NUM_LIT:1> , arg = DummyArg ( '<STR_LIT:key>' ) ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT>' ) == '<STR_LIT>' ) <EOL> simple_function ( '<STR_LIT>' , <NUM_LIT:2> , arg = DummyArg ( '<STR_LIT:key>' ) ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT>' ) == '<STR_LIT>' ) <EOL> simple_function ( '<STR_LIT>' , <NUM_LIT:1> , arg = DummyArg ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT>' ) == '<STR_LIT>' ) <EOL> def test_decorator_for_expire_key_with_cached_key ( self ) : <EOL> """<STR_LIT>""" <EOL> @ supycache . supycache ( cache_key = '<STR_LIT>' ) <EOL> def simple_function ( ) : <EOL> return '<STR_LIT>' <EOL> @ supycache . supycache ( expire_key = '<STR_LIT>' ) <EOL> def simple_expiry ( ) : <EOL> return '<STR_LIT>' <EOL> simple_function ( ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT>' ) == '<STR_LIT>' ) <EOL> simple_expiry ( ) <EOL> self . assertFalse ( bool ( self . backend . get ( '<STR_LIT>' ) ) ) <EOL> def test_decorator_for_expire_key_with_non_cached_key ( self ) : <EOL> """<STR_LIT>""" <EOL> @ supycache . supycache ( cache_key = '<STR_LIT>' ) <EOL> def simple_function ( ) : <EOL> return '<STR_LIT>' <EOL> @ supycache . supycache ( expire_key = '<STR_LIT>' ) <EOL> def simple_expiry ( ) : <EOL> return '<STR_LIT>' <EOL> simple_function ( ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT>' ) == '<STR_LIT>' ) <EOL> simple_expiry ( ) <EOL> self . assertFalse ( bool ( self . backend . get ( '<STR_LIT>' ) ) ) <EOL> self . assertFalse ( bool ( self . backend . get ( '<STR_LIT>' ) ) ) <EOL> def test_decorator_for_expire_key_positional_args ( self ) : <EOL> """<STR_LIT>""" <EOL> @ supycache . supycache ( cache_key = '<STR_LIT>' ) <EOL> def simple_function ( x , y ) : <EOL> return '<STR_LIT>' <EOL> @ supycache . supycache ( expire_key = '<STR_LIT>' ) <EOL> def simple_expiry ( x , y ) : <EOL> return '<STR_LIT>' <EOL> simple_function ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> self . assertTrue ( self . backend . get ( '<STR_LIT>' ) == '<STR_LIT>' ) <EOL> simple_expiry ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertFalse ( bool ( self . backend . get ( '<STR_LIT>' ) ) ) </s>
