<s> from __future__ import print_function <EOL> import sys , os , re <EOL> import unittest <EOL> from decimal import Decimal <EOL> from testutils import * <EOL> _TESTSTR = '<STR_LIT>' <EOL> def _generate_test_string ( length ) : <EOL> """<STR_LIT>""" <EOL> if length <= len ( _TESTSTR ) : <EOL> return _TESTSTR [ : length ] <EOL> c = int ( ( length + len ( _TESTSTR ) - <NUM_LIT:1> ) / len ( _TESTSTR ) ) <EOL> v = _TESTSTR * c <EOL> return v [ : length ] <EOL> class PGTestCase ( unittest . TestCase ) : <EOL> SMALL_READ = <NUM_LIT> <EOL> LARGE_READ = <NUM_LIT> <EOL> SMALL_STRING = _generate_test_string ( SMALL_READ ) <EOL> LARGE_STRING = _generate_test_string ( LARGE_READ ) <EOL> def __init__ ( self , connection_string , ansi , method_name ) : <EOL> unittest . TestCase . __init__ ( self , method_name ) <EOL> self . connection_string = connection_string <EOL> self . ansi = ansi <EOL> def setUp ( self ) : <EOL> self . cnxn = pyodbc . connect ( self . connection_string , ansi = self . ansi ) <EOL> self . cursor = self . cnxn . cursor ( ) <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> try : <EOL> self . cursor . execute ( "<STR_LIT>" % i ) <EOL> self . cnxn . commit ( ) <EOL> except : <EOL> pass <EOL> self . cnxn . rollback ( ) <EOL> def tearDown ( self ) : <EOL> try : <EOL> self . cursor . close ( ) <EOL> self . cnxn . close ( ) <EOL> except : <EOL> pass <EOL> def test_datasources ( self ) : <EOL> p = pyodbc . dataSources ( ) <EOL> self . assert_ ( isinstance ( p , dict ) ) <EOL> def test_getinfo_string ( self ) : <EOL> value = self . cnxn . getinfo ( pyodbc . SQL_CATALOG_NAME_SEPARATOR ) <EOL> self . assert_ ( isinstance ( value , str ) ) <EOL> def test_getinfo_bool ( self ) : <EOL> value = self . cnxn . getinfo ( pyodbc . SQL_ACCESSIBLE_TABLES ) <EOL> self . assert_ ( isinstance ( value , bool ) ) <EOL> def test_getinfo_int ( self ) : <EOL> value = self . cnxn . getinfo ( pyodbc . SQL_DEFAULT_TXN_ISOLATION ) <EOL> self . assert_ ( isinstance ( value , int ) ) <EOL> def test_getinfo_smallint ( self ) : <EOL> value = self . cnxn . getinfo ( pyodbc . SQL_CONCAT_NULL_BEHAVIOR ) <EOL> self . assert_ ( isinstance ( value , int ) ) <EOL> def test_negative_float ( self ) : <EOL> value = - <NUM_LIT:200> <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> self . cursor . execute ( "<STR_LIT>" , value ) <EOL> result = self . cursor . execute ( "<STR_LIT>" ) . fetchone ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( value , result ) <EOL> def _test_strtype ( self , sqltype , value , colsize = None ) : <EOL> """<STR_LIT>""" <EOL> assert colsize is None or ( value is None or colsize >= len ( value ) ) <EOL> if colsize : <EOL> sql = "<STR_LIT>" % ( sqltype , colsize ) <EOL> else : <EOL> sql = "<STR_LIT>" % sqltype <EOL> self . cursor . execute ( sql ) <EOL> self . cursor . execute ( "<STR_LIT>" , value ) <EOL> v = self . cursor . execute ( "<STR_LIT>" ) . fetchone ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( type ( v ) , type ( value ) ) <EOL> if value is not None : <EOL> self . assertEqual ( len ( v ) , len ( value ) ) <EOL> self . assertEqual ( v , value ) <EOL> def test_empty_varchar ( self ) : <EOL> self . _test_strtype ( '<STR_LIT>' , '<STR_LIT>' , self . SMALL_READ ) <EOL> def test_null_varchar ( self ) : <EOL> self . _test_strtype ( '<STR_LIT>' , None , self . SMALL_READ ) <EOL> def test_large_null_varchar ( self ) : <EOL> self . _test_strtype ( '<STR_LIT>' , None , self . LARGE_READ ) <EOL> def test_small_varchar ( self ) : <EOL> self . _test_strtype ( '<STR_LIT>' , self . SMALL_STRING , self . SMALL_READ ) <EOL> def test_large_varchar ( self ) : <EOL> self . _test_strtype ( '<STR_LIT>' , self . LARGE_STRING , self . LARGE_READ ) <EOL> def test_varchar_many ( self ) : <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> v1 = '<STR_LIT>' * <NUM_LIT:30> <EOL> v2 = '<STR_LIT>' * <NUM_LIT:30> <EOL> v3 = '<STR_LIT>' * <NUM_LIT:30> <EOL> self . cursor . execute ( "<STR_LIT>" , v1 , v2 , v3 ) ; <EOL> row = self . cursor . execute ( "<STR_LIT>" ) . fetchone ( ) <EOL> self . assertEqual ( v1 , row . c1 ) <EOL> self . assertEqual ( v2 , row . c2 ) <EOL> self . assertEqual ( v3 , row . c3 ) <EOL> def test_small_decimal ( self ) : <EOL> value = Decimal ( '<STR_LIT>' ) <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> self . cursor . execute ( "<STR_LIT>" , value ) <EOL> v = self . cursor . execute ( "<STR_LIT>" ) . fetchone ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( type ( v ) , Decimal ) <EOL> self . assertEqual ( v , value ) <EOL> def test_small_decimal_scale ( self ) : <EOL> value = '<STR_LIT>' <EOL> value = Decimal ( value ) <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> self . cursor . execute ( "<STR_LIT>" , value ) <EOL> v = self . cursor . execute ( "<STR_LIT>" ) . fetchone ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( type ( v ) , Decimal ) <EOL> self . assertEqual ( v , value ) <EOL> def test_negative_decimal_scale ( self ) : <EOL> value = Decimal ( '<STR_LIT>' ) <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> self . cursor . execute ( "<STR_LIT>" , value ) <EOL> v = self . cursor . execute ( "<STR_LIT>" ) . fetchone ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( type ( v ) , Decimal ) <EOL> self . assertEqual ( v , value ) <EOL> def _exec ( self ) : <EOL> self . cursor . execute ( self . sql ) <EOL> def test_close_cnxn ( self ) : <EOL> """<STR_LIT>""" <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> self . cursor . execute ( "<STR_LIT>" , <NUM_LIT:1> , '<STR_LIT:test>' ) <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> self . cnxn . close ( ) <EOL> self . sql = "<STR_LIT>" <EOL> self . assertRaises ( pyodbc . ProgrammingError , self . _exec ) <EOL> def test_empty_string ( self ) : <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> self . cursor . execute ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def test_fixed_str ( self ) : <EOL> value = "<STR_LIT>" <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> self . cursor . execute ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> v = self . cursor . execute ( "<STR_LIT>" ) . fetchone ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( type ( v ) , str ) <EOL> self . assertEqual ( len ( v ) , len ( value ) ) <EOL> self . assertEqual ( v , value ) <EOL> def test_negative_row_index ( self ) : <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> self . cursor . execute ( "<STR_LIT>" , "<STR_LIT:1>" ) <EOL> row = self . cursor . execute ( "<STR_LIT>" ) . fetchone ( ) <EOL> self . assertEquals ( row [ <NUM_LIT:0> ] , "<STR_LIT:1>" ) <EOL> self . assertEquals ( row [ - <NUM_LIT:1> ] , "<STR_LIT:1>" ) <EOL> def test_version ( self ) : <EOL> self . assertEquals ( <NUM_LIT:3> , len ( pyodbc . version . split ( '<STR_LIT:.>' ) ) ) <EOL> def test_rowcount_delete ( self ) : <EOL> self . assertEquals ( self . cursor . rowcount , - <NUM_LIT:1> ) <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> count = <NUM_LIT:4> <EOL> for i in range ( count ) : <EOL> self . cursor . execute ( "<STR_LIT>" , i ) <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> self . assertEquals ( self . cursor . rowcount , count ) <EOL> def test_rowcount_nodata ( self ) : <EOL> """<STR_LIT>""" <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> self . assertEquals ( self . cursor . rowcount , <NUM_LIT:0> ) <EOL> def test_rowcount_select ( self ) : <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> count = <NUM_LIT:4> <EOL> for i in range ( count ) : <EOL> self . cursor . execute ( "<STR_LIT>" , i ) <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> self . assertEquals ( self . cursor . rowcount , <NUM_LIT:4> ) <EOL> def test_lower_case ( self ) : <EOL> "<STR_LIT>" <EOL> pyodbc . lowercase = True <EOL> self . cursor = self . cnxn . cursor ( ) <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> names = [ t [ <NUM_LIT:0> ] for t in self . cursor . description ] <EOL> names . sort ( ) <EOL> self . assertEquals ( names , [ "<STR_LIT:abc>" , "<STR_LIT>" ] ) <EOL> pyodbc . lowercase = False <EOL> def test_row_description ( self ) : <EOL> """<STR_LIT>""" <EOL> self . cursor = self . cnxn . cursor ( ) <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> self . cnxn . commit ( ) <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> row = self . cursor . execute ( "<STR_LIT>" ) . fetchone ( ) <EOL> self . assertEquals ( self . cursor . description , row . cursor_description ) <EOL> def test_executemany ( self ) : <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> params = [ ( i , str ( i ) ) for i in range ( <NUM_LIT:1> , <NUM_LIT:6> ) ] <EOL> self . cursor . executemany ( "<STR_LIT>" , params ) <EOL> count = self . cursor . execute ( "<STR_LIT>" ) . fetchone ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( count , len ( params ) ) <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> rows = self . cursor . fetchall ( ) <EOL> self . assertEqual ( count , len ( rows ) ) <EOL> for param , row in zip ( params , rows ) : <EOL> self . assertEqual ( param [ <NUM_LIT:0> ] , row [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( param [ <NUM_LIT:1> ] , row [ <NUM_LIT:1> ] ) <EOL> def test_executemany_failure ( self ) : <EOL> """<STR_LIT>""" <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> params = [ ( <NUM_LIT:1> , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:error>' , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:3> , '<STR_LIT>' ) ] <EOL> self . failUnlessRaises ( pyodbc . Error , self . cursor . executemany , "<STR_LIT>" , params ) <EOL> def test_row_slicing ( self ) : <EOL> self . cursor . execute ( "<STR_LIT>" ) ; <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> row = self . cursor . execute ( "<STR_LIT>" ) . fetchone ( ) <EOL> result = row [ : ] <EOL> self . failUnless ( result is row ) <EOL> result = row [ : - <NUM_LIT:1> ] <EOL> self . assertEqual ( result , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> result = row [ <NUM_LIT:0> : <NUM_LIT:4> ] <EOL> self . failUnless ( result is row ) <EOL> def test_cnxn_execute_error ( self ) : <EOL> """<STR_LIT>""" <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> self . failUnlessRaises ( pyodbc . Error , self . cnxn . execute , "<STR_LIT>" ) <EOL> def test_row_repr ( self ) : <EOL> self . cursor . execute ( "<STR_LIT>" ) ; <EOL> self . cursor . execute ( "<STR_LIT>" ) <EOL> row = self . cursor . execute ( "<STR_LIT>" ) . fetchone ( ) <EOL> result = str ( row ) <EOL> self . assertEqual ( result , "<STR_LIT>" ) <EOL> result = str ( row [ : - <NUM_LIT:1> ] ) <EOL> self . assertEqual ( result , "<STR_LIT>" ) <EOL> result = str ( row [ : <NUM_LIT:1> ] ) <EOL> self . assertEqual ( result , "<STR_LIT>" ) <EOL> def test_autocommit ( self ) : <EOL> self . assertEqual ( self . cnxn . autocommit , False ) <EOL> othercnxn = pyodbc . connect ( self . connection_string , autocommit = True ) <EOL> self . assertEqual ( othercnxn . autocommit , True ) <EOL> othercnxn . autocommit = False <EOL> self . assertEqual ( othercnxn . autocommit , False ) <EOL> def test_cnxn_set_attr_before ( self ) : <EOL> SQL_ATTR_PACKET_SIZE = <NUM_LIT> <EOL> othercnxn = pyodbc . connect ( self . connection_string , attrs_before = { SQL_ATTR_PACKET_SIZE : <NUM_LIT> * <NUM_LIT:32> } ) <EOL> def test_cnxn_set_attr ( self ) : <EOL> SQL_ATTR_ACCESS_MODE = <NUM_LIT> <EOL> SQL_MODE_READ_ONLY = <NUM_LIT:1> <EOL> self . cnxn . set_attr ( SQL_ATTR_ACCESS_MODE , SQL_MODE_READ_ONLY ) <EOL> def main ( ) : <EOL> from optparse import OptionParser <EOL> parser = OptionParser ( usage = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , default = <NUM_LIT:0> , action = "<STR_LIT:count>" , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , default = False , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , default = False , action = '<STR_LIT:store_true>' ) <EOL> ( options , args ) = parser . parse_args ( ) <EOL> if len ( args ) > <NUM_LIT:1> : <EOL> parser . error ( '<STR_LIT>' ) <EOL> if not args : <EOL> connection_string = load_setup_connection_string ( '<STR_LIT>' ) <EOL> if not connection_string : <EOL> parser . print_help ( ) <EOL> raise SystemExit ( ) <EOL> else : <EOL> connection_string = args [ <NUM_LIT:0> ] <EOL> if options . verbose : <EOL> cnxn = pyodbc . connect ( connection_string , ansi = options . ansi ) <EOL> print_library_info ( cnxn ) <EOL> cnxn . close ( ) <EOL> if options . test : <EOL> if not options . test . startswith ( '<STR_LIT>' ) : <EOL> options . test = '<STR_LIT>' % ( options . test ) <EOL> s = unittest . TestSuite ( [ PGTestCase ( connection_string , options . ansi , options . test ) ] ) <EOL> else : <EOL> methods = [ m for m in dir ( PGTestCase ) if m . startswith ( '<STR_LIT>' ) ] <EOL> methods . sort ( ) <EOL> s = unittest . TestSuite ( [ PGTestCase ( connection_string , options . ansi , m ) for m in methods ] ) <EOL> testRunner = unittest . TextTestRunner ( verbosity = options . verbose ) <EOL> result = testRunner . run ( s ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> add_to_path ( ) <EOL> import pyodbc <EOL> main ( ) </s>
