@@ -3895,6 +3895,7 @@ def test_get_schema_create_table(connect_and_uuid, request, test_frame3):
3895
3895
3896
3896
@pytest .mark .parametrize ("connect_and_uuid" , setup (sqlalchemy_connectable , uuid_tables = "test_dataframe_to_sql" ), indirect = True )
3897
3897
def test_dtype (connect_and_uuid ):
3898
+
3898
3899
conn = connect_and_uuid ["conn" ]
3899
3900
table_uuid = connect_and_uuid ["table_uuid" ]
3900
3901
conn_name = connect_and_uuid ["conn_name" ]
@@ -3905,14 +3906,15 @@ def test_dtype(connect_and_uuid):
3905
3906
from sqlalchemy import (
3906
3907
TEXT ,
3907
3908
String ,
3909
+ Table
3908
3910
)
3909
3911
from sqlalchemy .schema import MetaData
3910
3912
3911
3913
cols = ["A" , "B" ]
3912
3914
data = [(0.8 , True ), (0.9 , None )]
3913
3915
df = DataFrame (data , columns = cols )
3914
3916
3915
- table_uuid1 = table_uuid
3917
+ table_uuid1 = 'a' + table_uuid
3916
3918
table_uuid2 = 'b' + table_uuid
3917
3919
table_uuid3 = 'c' + table_uuid
3918
3920
table_uuid_single = 's' + table_uuid
@@ -3921,34 +3923,29 @@ def test_dtype(connect_and_uuid):
3921
3923
assert df .to_sql (name = table_uuid1 , con = conn ) == 2
3922
3924
assert df .to_sql (name = table_uuid2 , con = conn , dtype = {"B" : TEXT }) == 2
3923
3925
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
3926
3928
assert isinstance (sqltype , TEXT )
3927
3929
msg = "The type of B is not a SQLAlchemy type"
3928
3930
with pytest .raises (ValueError , match = msg ):
3929
3931
df .to_sql (name = error_table , con = conn , dtype = {"B" : str })
3930
3932
3931
3933
# GH9083
3932
3934
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
3935
3938
assert isinstance (sqltype , String )
3936
3939
assert sqltype .length == 10
3937
3940
3938
3941
# single dtype
3939
3942
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
3943
3947
assert isinstance (sqltypea , TEXT )
3944
3948
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 " )
3952
3949
3953
3950
3954
3951
@@ -3966,6 +3963,7 @@ def test_notna_dtype(connect_and_uuid):
3966
3963
DateTime ,
3967
3964
Float ,
3968
3965
Integer ,
3966
+ Table
3969
3967
)
3970
3968
from sqlalchemy .schema import MetaData
3971
3969
@@ -3980,20 +3978,14 @@ def test_notna_dtype(connect_and_uuid):
3980
3978
assert df .to_sql (name = table_uuid , con = conn ) == 2
3981
3979
_ = sql .read_sql_table (table_uuid , conn )
3982
3980
meta = MetaData ()
3983
- meta .reflect (bind = conn )
3981
+ table_with_datatypes = Table (table_uuid , meta , autoload_with = conn )
3982
+
3984
3983
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
3986
3985
assert isinstance (col_dict ["Bool" ].type , my_type )
3987
3986
assert isinstance (col_dict ["Date" ].type , DateTime )
3988
3987
assert isinstance (col_dict ["Int" ].type , Integer )
3989
3988
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 " )
3997
3989
3998
3990
3999
3991
@@ -4011,6 +4003,7 @@ def test_double_precision(connect_and_uuid):
4011
4003
BigInteger ,
4012
4004
Float ,
4013
4005
Integer ,
4006
+ Table
4014
4007
)
4015
4008
from sqlalchemy .schema import MetaData
4016
4009
@@ -4043,20 +4036,13 @@ def test_double_precision(connect_and_uuid):
4043
4036
4044
4037
# check sql types
4045
4038
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
4048
4041
assert str (col_dict ["f32" ].type ) == str (col_dict ["f64_as_f32" ].type )
4049
4042
assert isinstance (col_dict ["f32" ].type , Float )
4050
4043
assert isinstance (col_dict ["f64" ].type , Float )
4051
4044
assert isinstance (col_dict ["i32" ].type , Integer )
4052
4045
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 " )
4060
4046
4061
4047
4062
4048
@@ -4575,11 +4561,13 @@ def test_roundtripping_datetimes(connect_and_uuid):
4575
4561
4576
4562
@pytest .fixture
4577
4563
def 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
4583
4571
4584
4572
4585
4573
@pytest .mark .parametrize ("connect_and_uuid" , setup (['sqlite_builtin_detect_types' ], uuid_tables = "test_dataframe_to_sql_empty" ), indirect = True )
0 commit comments