<s> import os <EOL> import datetime <EOL> import numbers <EOL> import pytest <EOL> from redis . client import StrictRedis , ConnectionError <EOL> from sider . session import Session <EOL> from sider . types import Integer <EOL> def get_client ( cls = StrictRedis ) : <EOL> host = os . environ . get ( '<STR_LIT>' , '<STR_LIT:localhost>' ) <EOL> port = int ( os . environ . get ( '<STR_LIT>' , <NUM_LIT> ) ) <EOL> db = int ( os . environ . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> try : <EOL> client = cls ( host = host , port = port , db = db ) <EOL> client . ping ( ) <EOL> return client <EOL> except ConnectionError as e : <EOL> pytest . fail ( str ( e ) , pytrace = False ) <EOL> def get_session ( client = None ) : <EOL> if client is None : <EOL> client = get_client ( ) <EOL> session = Session ( client ) <EOL> session . verbose_transaction_error = True <EOL> return session <EOL> prefix = '<STR_LIT>' . format ( datetime . datetime . now ( ) ) <EOL> def key ( key ) : <EOL> global prefix <EOL> return prefix + str ( key ) <EOL> @ pytest . fixture <EOL> def session ( request ) : <EOL> client = get_client ( ) <EOL> session = get_session ( client ) <EOL> @ request . addfinalizer <EOL> def fin ( ) : <EOL> used_keys = client . keys ( prefix + '<STR_LIT:*>' ) <EOL> if used_keys : <EOL> client . delete ( * used_keys ) <EOL> return session <EOL> class NInt ( Integer ) : <EOL> """<STR_LIT>""" <EOL> def encode ( self , value ) : <EOL> if not isinstance ( value , numbers . Integral ) : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( value ) ) <EOL> return Integer . encode ( self , - value + <NUM_LIT:6> ) <EOL> def decode ( self , bulk ) : <EOL> return - ( Integer . decode ( self , bulk ) - <NUM_LIT:6> ) </s>
