@@ -3895,6 +3895,7 @@ def test_get_schema_create_table(connect_and_uuid, request, test_frame3):
38953895
38963896@pytest .mark .parametrize ("connect_and_uuid" , setup (sqlalchemy_connectable , uuid_tables = "test_dataframe_to_sql" ), indirect = True )
38973897def test_dtype (connect_and_uuid ):
3898+
38983899 conn = connect_and_uuid ["conn" ]
38993900 table_uuid = connect_and_uuid ["table_uuid" ]
39003901 conn_name = connect_and_uuid ["conn_name" ]
@@ -3905,14 +3906,15 @@ def test_dtype(connect_and_uuid):
39053906 from sqlalchemy import (
39063907 TEXT ,
39073908 String ,
3909+ Table
39083910 )
39093911 from sqlalchemy .schema import MetaData
39103912
39113913 cols = ["A" , "B" ]
39123914 data = [(0.8 , True ), (0.9 , None )]
39133915 df = DataFrame (data , columns = cols )
39143916
3915- table_uuid1 = table_uuid
3917+ table_uuid1 = 'a' + table_uuid
39163918 table_uuid2 = 'b' + table_uuid
39173919 table_uuid3 = 'c' + table_uuid
39183920 table_uuid_single = 's' + table_uuid
@@ -3921,34 +3923,29 @@ def test_dtype(connect_and_uuid):
39213923 assert df .to_sql (name = table_uuid1 , con = conn ) == 2
39223924 assert df .to_sql (name = table_uuid2 , con = conn , dtype = {"B" : TEXT }) == 2
39233925 meta = MetaData ()
3924- meta . reflect ( bind = conn )
3925- sqltype = meta . tables [ table_uuid2 ] .columns ["B" ].type
3926+ table_with_strings = Table ( table_uuid2 , meta , autoload_with = conn )
3927+ sqltype = table_with_strings .columns ["B" ].type
39263928 assert isinstance (sqltype , TEXT )
39273929 msg = "The type of B is not a SQLAlchemy type"
39283930 with pytest .raises (ValueError , match = msg ):
39293931 df .to_sql (name = error_table , con = conn , dtype = {"B" : str })
39303932
39313933 # GH9083
39323934 assert df .to_sql (name = table_uuid3 , con = conn , dtype = {"B" : String (10 )}) == 2
3933- meta .reflect (bind = conn )
3934- sqltype = meta .tables [table_uuid3 ].columns ["B" ].type
3935+ meta = MetaData ()
3936+ table_with_sql_strings = Table (table_uuid3 , meta , autoload_with = conn )
3937+ sqltype = table_with_sql_strings .columns ["B" ].type
39353938 assert isinstance (sqltype , String )
39363939 assert sqltype .length == 10
39373940
39383941 # single dtype
39393942 assert df .to_sql (name = table_uuid_single , con = conn , dtype = TEXT ) == 2
3940- meta .reflect (bind = conn )
3941- sqltypea = meta .tables [table_uuid_single ].columns ["A" ].type
3942- sqltypeb = meta .tables [table_uuid_single ].columns ["B" ].type
3943+ meta = MetaData ()
3944+ table_with_sql_dtype_text = Table (table_uuid_single , meta , autoload_with = conn )
3945+ sqltypea = table_with_sql_dtype_text .columns ["A" ].type
3946+ sqltypeb = table_with_sql_dtype_text .columns ["B" ].type
39433947 assert isinstance (sqltypea , TEXT )
39443948 assert isinstance (sqltypeb , TEXT )
3945- with open ("test_dtype.txt" , "a" ) as file :
3946- file .write (conn_name )
3947- file .write ("\n " )
3948- file .write ("\n " )
3949- file .write (repr (setup (sqlalchemy_connectable )))
3950- file .write ("\n " )
3951- file .write ("\n " )
39523949
39533950
39543951
@@ -3966,6 +3963,7 @@ def test_notna_dtype(connect_and_uuid):
39663963 DateTime ,
39673964 Float ,
39683965 Integer ,
3966+ Table
39693967 )
39703968 from sqlalchemy .schema import MetaData
39713969
@@ -3980,20 +3978,14 @@ def test_notna_dtype(connect_and_uuid):
39803978 assert df .to_sql (name = table_uuid , con = conn ) == 2
39813979 _ = sql .read_sql_table (table_uuid , conn )
39823980 meta = MetaData ()
3983- meta .reflect (bind = conn )
3981+ table_with_datatypes = Table (table_uuid , meta , autoload_with = conn )
3982+
39843983 my_type = Integer if "mysql" in conn_name else Boolean
3985- col_dict = meta . tables [ table_uuid ] .columns
3984+ col_dict = table_with_datatypes .columns
39863985 assert isinstance (col_dict ["Bool" ].type , my_type )
39873986 assert isinstance (col_dict ["Date" ].type , DateTime )
39883987 assert isinstance (col_dict ["Int" ].type , Integer )
39893988 assert isinstance (col_dict ["Float" ].type , Float )
3990- with open ("test_notna_dtype.txt" , "a" ) as file :
3991- file .write (conn_name )
3992- file .write ("\n " )
3993- file .write ("\n " )
3994- file .write (repr (setup (sqlalchemy_connectable )))
3995- file .write ("\n " )
3996- file .write ("\n " )
39973989
39983990
39993991
@@ -4011,6 +4003,7 @@ def test_double_precision(connect_and_uuid):
40114003 BigInteger ,
40124004 Float ,
40134005 Integer ,
4006+ Table
40144007 )
40154008 from sqlalchemy .schema import MetaData
40164009
@@ -4043,20 +4036,13 @@ def test_double_precision(connect_and_uuid):
40434036
40444037 # check sql types
40454038 meta = MetaData ()
4046- meta . reflect ( bind = conn )
4047- col_dict = meta . tables [ table_uuid ] .columns
4039+ table_with_datatypes = Table ( table_uuid , meta , autoload_with = conn )
4040+ col_dict = table_with_datatypes .columns
40484041 assert str (col_dict ["f32" ].type ) == str (col_dict ["f64_as_f32" ].type )
40494042 assert isinstance (col_dict ["f32" ].type , Float )
40504043 assert isinstance (col_dict ["f64" ].type , Float )
40514044 assert isinstance (col_dict ["i32" ].type , Integer )
40524045 assert isinstance (col_dict ["i64" ].type , BigInteger )
4053- with open ("test_double_precision.txt" , "a" ) as file :
4054- file .write (conn_name )
4055- file .write ("\n " )
4056- file .write ("\n " )
4057- file .write (repr (setup (sqlalchemy_connectable )))
4058- file .write ("\n " )
4059- file .write ("\n " )
40604046
40614047
40624048
@@ -4575,11 +4561,13 @@ def test_roundtripping_datetimes(connect_and_uuid):
45754561
45764562@pytest .fixture
45774563def sqlite_builtin_detect_types ():
4578- with contextlib .closing (
4579- sqlite3 .connect (":memory:" , detect_types = sqlite3 .PARSE_DECLTYPES )
4580- ) as closing_conn :
4581- with closing_conn as conn :
4582- yield conn
4564+ yield sqlite3 .connect (":memory:" , detect_types = sqlite3 .PARSE_DECLTYPES )
4565+
4566+ # with contextlib.closing(
4567+ # sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES)
4568+ # ) as closing_conn:
4569+ # with closing_conn as conn:
4570+ # yield conn
45834571
45844572
45854573@pytest .mark .parametrize ("connect_and_uuid" , setup (['sqlite_builtin_detect_types' ], uuid_tables = "test_dataframe_to_sql_empty" ), indirect = True )
0 commit comments