<s> import datetime <EOL> from walrus import * <EOL> class TestHelper ( object ) : <EOL> def test_simple_string_ops ( self ) : <EOL> self . assertTrue ( self . db . set ( '<STR_LIT:name>' , '<STR_LIT>' ) ) <EOL> self . assertEqual ( self . db . get ( '<STR_LIT:name>' ) , '<STR_LIT>' ) <EOL> self . assertIsNone ( self . db . get ( '<STR_LIT>' ) ) <EOL> self . assertFalse ( self . db . setnx ( '<STR_LIT:name>' , '<STR_LIT>' ) ) <EOL> self . db . setnx ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( self . db [ '<STR_LIT:name>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( self . db [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertTrue ( self . db . mset ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) ) <EOL> res = self . db . mget ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( res , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . db . append ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( self . db [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> del self . db [ '<STR_LIT>' ] <EOL> self . assertEqual ( self . db . incr ( '<STR_LIT>' ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( self . db . incr ( '<STR_LIT>' , <NUM_LIT:5> ) , <NUM_LIT:6> ) <EOL> self . assertEqual ( self . db . decr ( '<STR_LIT>' , <NUM_LIT:2> ) , <NUM_LIT:4> ) <EOL> self . assertEqual ( self . db . getrange ( '<STR_LIT:name>' , <NUM_LIT:3> , <NUM_LIT:5> ) , '<STR_LIT>' ) <EOL> self . assertEqual ( self . db . getset ( '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( self . db [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( self . db . strlen ( '<STR_LIT:name>' ) , <NUM_LIT:7> ) <EOL> self . db [ '<STR_LIT:data>' ] = '<STR_LIT>' <EOL> self . assertEqual ( self . db . bitcount ( '<STR_LIT:data>' ) , <NUM_LIT:3> ) <EOL> del self . db [ '<STR_LIT:name>' ] <EOL> self . assertIsNone ( self . db . get ( '<STR_LIT:name>' ) ) <EOL> self . assertRaises ( KeyError , lambda : self . db [ '<STR_LIT:name>' ] ) <EOL> self . assertFalse ( '<STR_LIT:name>' in self . db ) <EOL> self . assertTrue ( '<STR_LIT>' in self . db ) <EOL> def test_simple_hash ( self ) : <EOL> h = self . db . Hash ( '<STR_LIT>' ) <EOL> h . clear ( ) <EOL> h [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> h . update ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( h . as_dict ( ) , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( h [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertIsNone ( h [ '<STR_LIT>' ] ) <EOL> self . assertTrue ( '<STR_LIT>' in h ) <EOL> self . assertFalse ( '<STR_LIT>' in h ) <EOL> del h [ '<STR_LIT>' ] <EOL> del h [ '<STR_LIT>' ] <EOL> self . assertEqual ( sorted ( h . keys ( ) ) , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( sorted ( h . values ( ) ) , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( len ( h ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( h [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , [ '<STR_LIT>' , None , '<STR_LIT>' ] ) <EOL> self . assertEqual ( h . incr ( '<STR_LIT>' ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( h . incr ( '<STR_LIT>' , <NUM_LIT:3> ) , <NUM_LIT:4> ) <EOL> def test_simple_list ( self ) : <EOL> l = self . db . List ( '<STR_LIT>' ) <EOL> l . clear ( ) <EOL> l . append ( '<STR_LIT>' ) <EOL> l . extend ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( l [ <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> self . assertEqual ( l [ - <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> self . assertEqual ( l [ : ] , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( l [ <NUM_LIT:1> : - <NUM_LIT:1> ] , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( l [ <NUM_LIT:2> : ] , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( len ( l ) , <NUM_LIT:4> ) <EOL> l . prepend ( '<STR_LIT>' ) <EOL> l . popright ( ) <EOL> l . popright ( ) <EOL> self . assertEqual ( [ item for item in l ] , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( l . popleft ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( l . popright ( ) , '<STR_LIT>' ) <EOL> l . clear ( ) <EOL> self . assertEqual ( list ( l ) , [ ] ) <EOL> self . assertIsNone ( l . popleft ( ) ) <EOL> def test_simple_set ( self ) : <EOL> s = self . db . Set ( '<STR_LIT>' ) <EOL> s . clear ( ) <EOL> self . assertTrue ( s . add ( '<STR_LIT>' ) ) <EOL> self . assertFalse ( s . add ( '<STR_LIT>' ) ) <EOL> s . add ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( len ( s ) , <NUM_LIT:3> ) <EOL> self . assertTrue ( '<STR_LIT>' in s ) <EOL> self . assertFalse ( '<STR_LIT>' in s ) <EOL> self . assertEqual ( s . members ( ) , set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> del s [ '<STR_LIT>' ] <EOL> del s [ '<STR_LIT>' ] <EOL> self . assertEqual ( s . members ( ) , set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> n1 = self . db . Set ( '<STR_LIT>' ) <EOL> n2 = self . db . Set ( '<STR_LIT>' ) <EOL> n1 . add ( * range ( <NUM_LIT:5> ) ) <EOL> n2 . add ( * range ( <NUM_LIT:3> , <NUM_LIT:7> ) ) <EOL> self . assertEqual ( n1 - n2 , set ( [ '<STR_LIT:0>' , '<STR_LIT:1>' , '<STR_LIT:2>' ] ) ) <EOL> self . assertEqual ( n2 - n1 , set ( [ '<STR_LIT:5>' , '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( n1 | n2 , set ( map ( str , range ( <NUM_LIT:7> ) ) ) ) <EOL> self . assertEqual ( n1 & n2 , set ( [ '<STR_LIT:3>' , '<STR_LIT:4>' ] ) ) <EOL> n1 . diffstore ( '<STR_LIT>' , n2 ) <EOL> ndiff = self . db . Set ( '<STR_LIT>' ) <EOL> self . assertEqual ( ndiff . members ( ) , set ( [ '<STR_LIT:0>' , '<STR_LIT:1>' , '<STR_LIT:2>' ] ) ) <EOL> n1 . interstore ( '<STR_LIT>' , n2 ) <EOL> ninter = self . db . Set ( '<STR_LIT>' ) <EOL> self . assertEqual ( ninter . members ( ) , set ( [ '<STR_LIT:3>' , '<STR_LIT:4>' ] ) ) <EOL> def test_zset ( self ) : <EOL> zs = self . db . ZSet ( '<STR_LIT>' ) <EOL> zs . clear ( ) <EOL> zs . add ( '<STR_LIT>' , <NUM_LIT> , '<STR_LIT>' , <NUM_LIT:3> , '<STR_LIT>' , <NUM_LIT:6> , '<STR_LIT>' , <NUM_LIT:3> , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . assertEqual ( zs [ <NUM_LIT:1> ] , [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( zs [ <NUM_LIT:1> , True ] , [ ( '<STR_LIT>' , <NUM_LIT:3> ) ] ) <EOL> self . assertEqual ( <EOL> zs [ : ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( zs [ : <NUM_LIT:2> ] , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( zs [ <NUM_LIT:1> : <NUM_LIT:3> , True ] , [ ( '<STR_LIT>' , <NUM_LIT:3> ) , ( '<STR_LIT>' , <NUM_LIT:3> ) ] ) <EOL> self . assertEqual ( zs [ '<STR_LIT>' : '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( len ( zs ) , <NUM_LIT:5> ) <EOL> self . assertTrue ( '<STR_LIT>' in zs ) <EOL> self . assertFalse ( '<STR_LIT>' in zs ) <EOL> self . assertEqual ( zs . score ( '<STR_LIT>' ) , <NUM_LIT> ) <EOL> self . assertIsNone ( zs . score ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( zs . rank ( '<STR_LIT>' ) , <NUM_LIT:3> ) <EOL> self . assertIsNone ( zs . rank ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( zs . count ( <NUM_LIT:0> , <NUM_LIT:5> ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( zs . count ( <NUM_LIT:6> , <NUM_LIT> ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( zs . count ( <NUM_LIT:6> , <NUM_LIT:30> ) , <NUM_LIT:1> ) <EOL> zs . incr ( '<STR_LIT>' ) <EOL> self . assertEqual ( zs . score ( '<STR_LIT>' ) , <NUM_LIT> ) <EOL> self . assertEqual ( zs . range_by_score ( <NUM_LIT:0> , <NUM_LIT:5> ) , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> zs . remove ( '<STR_LIT>' ) <EOL> self . assertEqual ( zs [ : <NUM_LIT:2> ] , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> del zs [ '<STR_LIT>' ] <EOL> self . assertEqual ( zs [ : ] , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( len ( zs ) , <NUM_LIT:3> ) <EOL> zs . remove_by_score ( <NUM_LIT:2> , <NUM_LIT:4> ) <EOL> self . assertEqual ( zs [ : ] , [ '<STR_LIT>' ] ) <EOL> zs . add ( '<STR_LIT>' , <NUM_LIT:4> , '<STR_LIT>' , <NUM_LIT:3> , '<STR_LIT>' , <NUM_LIT:8> ) <EOL> zs . remove_by_rank ( <NUM_LIT:2> ) <EOL> self . assertEqual ( zs [ : ] , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertRaises ( KeyError , lambda : zs [ '<STR_LIT>' : ] ) <EOL> z1 = self . db . ZSet ( '<STR_LIT>' ) <EOL> z2 = self . db . ZSet ( '<STR_LIT>' ) <EOL> z1 . add ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> z2 . add ( <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:5> ) <EOL> z3 = z1 . unionstore ( '<STR_LIT>' , z2 ) <EOL> self . assertEqual ( z3 [ : ] , [ '<STR_LIT:1>' , '<STR_LIT:2>' , '<STR_LIT:4>' , '<STR_LIT:5>' , '<STR_LIT:3>' ] ) <EOL> z3 = z1 . interstore ( '<STR_LIT>' , z2 ) <EOL> self . assertEqual ( z3 [ : ] , [ '<STR_LIT:3>' ] ) <EOL> def test_models ( self ) : <EOL> class User ( Model ) : <EOL> database = self . db <EOL> username = TextField ( primary_key = True ) <EOL> value = IntegerField ( index = True ) <EOL> for i , username in enumerate ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> User . create ( username = username , value = i ) <EOL> charlie = User . load ( '<STR_LIT>' ) <EOL> self . assertEqual ( charlie . username , '<STR_LIT>' ) <EOL> self . assertEqual ( charlie . value , <NUM_LIT:0> ) <EOL> query = User . query ( <EOL> ( User . username == '<STR_LIT>' ) | <EOL> ( User . username == '<STR_LIT>' ) ) <EOL> users = [ user . username for user in query ] <EOL> self . assertEqual ( sorted ( users ) , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_cache ( self ) : <EOL> cache = self . db . cache ( name = '<STR_LIT>' ) <EOL> @ cache . cached ( timeout = <NUM_LIT:10> ) <EOL> def now ( seed = None ) : <EOL> return datetime . datetime . now ( ) <EOL> dt1 = now ( ) <EOL> self . assertEqual ( now ( ) , dt1 ) <EOL> self . assertNotEqual ( now ( <NUM_LIT:1> ) , dt1 ) <EOL> self . assertEqual ( now ( <NUM_LIT:1> ) , now ( <NUM_LIT:1> ) ) </s>
