<s> """<STR_LIT>""" <EOL> import os <EOL> import time <EOL> import unittest <EOL> import tempfile <EOL> import shutil <EOL> from werkzeug . testsuite import WerkzeugTestCase <EOL> from werkzeug . contrib import cache <EOL> try : <EOL> import redis <EOL> except ImportError : <EOL> redis = None <EOL> class SimpleCacheTestCase ( WerkzeugTestCase ) : <EOL> def test_get_dict ( self ) : <EOL> c = cache . SimpleCache ( ) <EOL> c . set ( '<STR_LIT:a>' , '<STR_LIT:a>' ) <EOL> c . set ( '<STR_LIT:b>' , '<STR_LIT:b>' ) <EOL> d = c . get_dict ( '<STR_LIT:a>' , '<STR_LIT:b>' ) <EOL> assert '<STR_LIT:a>' in d <EOL> assert '<STR_LIT:a>' == d [ '<STR_LIT:a>' ] <EOL> assert '<STR_LIT:b>' in d <EOL> assert '<STR_LIT:b>' == d [ '<STR_LIT:b>' ] <EOL> def test_set_many ( self ) : <EOL> c = cache . SimpleCache ( ) <EOL> c . set_many ( { <NUM_LIT:0> : <NUM_LIT:0> , <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:4> } ) <EOL> assert c . get ( <NUM_LIT:2> ) == <NUM_LIT:4> <EOL> c . set_many ( ( i , i * i ) for i in xrange ( <NUM_LIT:3> ) ) <EOL> assert c . get ( <NUM_LIT:2> ) == <NUM_LIT:4> <EOL> class FileSystemCacheTestCase ( WerkzeugTestCase ) : <EOL> def test_set_get ( self ) : <EOL> tmp_dir = tempfile . mkdtemp ( ) <EOL> try : <EOL> c = cache . FileSystemCache ( cache_dir = tmp_dir ) <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> c . set ( str ( i ) , i * i ) <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> result = c . get ( str ( i ) ) <EOL> assert result == i * i <EOL> finally : <EOL> shutil . rmtree ( tmp_dir ) <EOL> def test_filesystemcache_prune ( self ) : <EOL> THRESHOLD = <NUM_LIT> <EOL> tmp_dir = tempfile . mkdtemp ( ) <EOL> c = cache . FileSystemCache ( cache_dir = tmp_dir , threshold = THRESHOLD ) <EOL> for i in range ( <NUM_LIT:2> * THRESHOLD ) : <EOL> c . set ( str ( i ) , i ) <EOL> cache_files = os . listdir ( tmp_dir ) <EOL> shutil . rmtree ( tmp_dir ) <EOL> assert len ( cache_files ) <= THRESHOLD <EOL> def test_filesystemcache_clear ( self ) : <EOL> tmp_dir = tempfile . mkdtemp ( ) <EOL> c = cache . FileSystemCache ( cache_dir = tmp_dir ) <EOL> c . set ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) <EOL> cache_files = os . listdir ( tmp_dir ) <EOL> assert len ( cache_files ) == <NUM_LIT:1> <EOL> c . clear ( ) <EOL> cache_files = os . listdir ( tmp_dir ) <EOL> assert len ( cache_files ) == <NUM_LIT:0> <EOL> shutil . rmtree ( tmp_dir ) <EOL> class RedisCacheTestCase ( WerkzeugTestCase ) : <EOL> def make_cache ( self ) : <EOL> return cache . RedisCache ( key_prefix = '<STR_LIT>' ) <EOL> def teardown ( self ) : <EOL> self . make_cache ( ) . clear ( ) <EOL> def test_compat ( self ) : <EOL> c = self . make_cache ( ) <EOL> c . _client . set ( c . key_prefix + '<STR_LIT:foo>' , '<STR_LIT>' ) <EOL> self . assert_equal ( c . get ( '<STR_LIT:foo>' ) , '<STR_LIT>' ) <EOL> c . _client . set ( c . key_prefix + '<STR_LIT:foo>' , '<STR_LIT>' ) <EOL> self . assert_equal ( c . get ( '<STR_LIT:foo>' ) , <NUM_LIT> ) <EOL> def test_get_set ( self ) : <EOL> c = self . make_cache ( ) <EOL> c . set ( '<STR_LIT:foo>' , [ '<STR_LIT:bar>' ] ) <EOL> assert c . get ( '<STR_LIT:foo>' ) == [ '<STR_LIT:bar>' ] <EOL> def test_get_many ( self ) : <EOL> c = self . make_cache ( ) <EOL> c . set ( '<STR_LIT:foo>' , [ '<STR_LIT:bar>' ] ) <EOL> c . set ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert c . get_many ( '<STR_LIT:foo>' , '<STR_LIT>' ) == [ [ '<STR_LIT:bar>' ] , '<STR_LIT>' ] <EOL> def test_set_many ( self ) : <EOL> c = self . make_cache ( ) <EOL> c . set_many ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' , '<STR_LIT>' : [ '<STR_LIT>' ] } ) <EOL> assert c . get ( '<STR_LIT:foo>' ) == '<STR_LIT:bar>' <EOL> assert c . get ( '<STR_LIT>' ) == [ '<STR_LIT>' ] <EOL> def test_expire ( self ) : <EOL> c = self . make_cache ( ) <EOL> c . set ( '<STR_LIT:foo>' , '<STR_LIT:bar>' , <NUM_LIT:1> ) <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> assert c . get ( '<STR_LIT:foo>' ) is None <EOL> def test_add ( self ) : <EOL> c = self . make_cache ( ) <EOL> c . add ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) <EOL> assert c . get ( '<STR_LIT:foo>' ) == '<STR_LIT:bar>' <EOL> c . add ( '<STR_LIT:foo>' , '<STR_LIT>' ) <EOL> assert c . get ( '<STR_LIT:foo>' ) == '<STR_LIT:bar>' <EOL> def test_delete ( self ) : <EOL> c = self . make_cache ( ) <EOL> c . add ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) <EOL> assert c . get ( '<STR_LIT:foo>' ) == '<STR_LIT:bar>' <EOL> c . delete ( '<STR_LIT:foo>' ) <EOL> assert c . get ( '<STR_LIT:foo>' ) is None <EOL> def test_delete_many ( self ) : <EOL> c = self . make_cache ( ) <EOL> c . add ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) <EOL> c . add ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> c . delete_many ( '<STR_LIT:foo>' , '<STR_LIT>' ) <EOL> assert c . get ( '<STR_LIT:foo>' ) is None <EOL> assert c . get ( '<STR_LIT>' ) is None <EOL> def test_inc_dec ( self ) : <EOL> c = self . make_cache ( ) <EOL> c . set ( '<STR_LIT:foo>' , <NUM_LIT:1> ) <EOL> assert c . inc ( '<STR_LIT:foo>' ) == <NUM_LIT:2> <EOL> assert c . dec ( '<STR_LIT:foo>' ) == <NUM_LIT:1> <EOL> c . delete ( '<STR_LIT:foo>' ) <EOL> def test_true_false ( self ) : <EOL> c = self . make_cache ( ) <EOL> c . set ( '<STR_LIT:foo>' , True ) <EOL> assert c . get ( '<STR_LIT:foo>' ) == True <EOL> c . set ( '<STR_LIT:bar>' , False ) <EOL> assert c . get ( '<STR_LIT:bar>' ) == False <EOL> def suite ( ) : <EOL> suite = unittest . TestSuite ( ) <EOL> suite . addTest ( unittest . makeSuite ( SimpleCacheTestCase ) ) <EOL> suite . addTest ( unittest . makeSuite ( FileSystemCacheTestCase ) ) <EOL> if redis is not None : <EOL> suite . addTest ( unittest . makeSuite ( RedisCacheTestCase ) ) <EOL> return suite </s>
