@@ -3967,33 +3967,37 @@ def test_psycopg2_schema_support(postgresql_psycopg2_engine):
3967
3967
con .exec_driver_sql ("DROP SCHEMA IF EXISTS other CASCADE;" )
3968
3968
con .exec_driver_sql ("CREATE SCHEMA other;" )
3969
3969
3970
+ schema_public_uuid = table_uuid_gen ("test_schema_public" )
3971
+ schema_public_explicit_uuid = table_uuid_gen ("test_schema_public_explicit" )
3972
+ schema_other_uuid = table_uuid_gen ("test_schema_other" )
3973
+
3970
3974
# write dataframe to different schema's
3971
- assert df .to_sql (name = "test_schema_public" , con = conn , index = False ) == 2
3975
+ assert df .to_sql (name = schema_public_uuid , con = conn , index = False ) == 2
3972
3976
assert (
3973
3977
df .to_sql (
3974
- name = "test_schema_public_explicit" ,
3978
+ name = schema_public_explicit_uuid ,
3975
3979
con = conn ,
3976
3980
index = False ,
3977
3981
schema = "public" ,
3978
3982
)
3979
3983
== 2
3980
3984
)
3981
3985
assert (
3982
- df .to_sql (name = "test_schema_other" , con = conn , index = False , schema = "other" ) == 2
3986
+ df .to_sql (name = schema_other_uuid , con = conn , index = False , schema = "other" ) == 2
3983
3987
)
3984
3988
3985
3989
# read dataframes back in
3986
- res1 = sql .read_sql_table ("test_schema_public" , conn )
3990
+ res1 = sql .read_sql_table (schema_public_uuid , conn )
3987
3991
tm .assert_frame_equal (df , res1 )
3988
- res2 = sql .read_sql_table ("test_schema_public_explicit" , conn )
3992
+ res2 = sql .read_sql_table (schema_public_explicit_uuid , conn )
3989
3993
tm .assert_frame_equal (df , res2 )
3990
- res3 = sql .read_sql_table ("test_schema_public_explicit" , conn , schema = "public" )
3994
+ res3 = sql .read_sql_table (schema_public_explicit_uuid , conn , schema = "public" )
3991
3995
tm .assert_frame_equal (df , res3 )
3992
- res4 = sql .read_sql_table ("test_schema_other" , conn , schema = "other" )
3996
+ res4 = sql .read_sql_table (schema_other_uuid , conn , schema = "other" )
3993
3997
tm .assert_frame_equal (df , res4 )
3994
- msg = "Table test_schema_other not found"
3998
+ msg = f "Table { schema_other_uuid } not found"
3995
3999
with pytest .raises (ValueError , match = msg ):
3996
- sql .read_sql_table ("test_schema_other" , conn , schema = "public" )
4000
+ sql .read_sql_table (schema_other_uuid , conn , schema = "public" )
3997
4001
3998
4002
# different if_exists options
3999
4003
@@ -4005,26 +4009,26 @@ def test_psycopg2_schema_support(postgresql_psycopg2_engine):
4005
4009
4006
4010
# write dataframe with different if_exists options
4007
4011
assert (
4008
- df .to_sql (name = "test_schema_other" , con = conn , schema = "other" , index = False ) == 2
4012
+ df .to_sql (name = schema_other_uuid , con = conn , schema = "other" , index = False ) == 2
4009
4013
)
4010
4014
df .to_sql (
4011
- name = "test_schema_other" ,
4015
+ name = schema_other_uuid ,
4012
4016
con = conn ,
4013
4017
schema = "other" ,
4014
4018
index = False ,
4015
4019
if_exists = "replace" ,
4016
4020
)
4017
4021
assert (
4018
4022
df .to_sql (
4019
- name = "test_schema_other" ,
4023
+ name = schema_other_uuid ,
4020
4024
con = conn ,
4021
4025
schema = "other" ,
4022
4026
index = False ,
4023
4027
if_exists = "append" ,
4024
4028
)
4025
4029
== 2
4026
4030
)
4027
- res = sql .read_sql_table ("test_schema_other" , conn , schema = "other" )
4031
+ res = sql .read_sql_table (schema_other_uuid , conn , schema = "other" )
4028
4032
tm .assert_frame_equal (concat ([df , df ], ignore_index = True ), res )
4029
4033
4030
4034
@@ -4034,15 +4038,17 @@ def test_self_join_date_columns(postgresql_psycopg2_engine):
4034
4038
conn = postgresql_psycopg2_engine
4035
4039
from sqlalchemy .sql import text
4036
4040
4041
+ table_uuid = table_uuid_gen ("person" )
4042
+
4037
4043
create_table = text (
4038
- """
4039
- CREATE TABLE person
4044
+ f """
4045
+ CREATE TABLE { table_uuid }
4040
4046
(
4041
- id serial constraint person_pkey primary key,
4047
+ id serial constraint { table_uuid } _pkey primary key,
4042
4048
created_dt timestamp with time zone
4043
4049
);
4044
4050
4045
- INSERT INTO person
4051
+ INSERT INTO { table_uuid }
4046
4052
VALUES (1, '2021-01-01T00:00:00Z');
4047
4053
"""
4048
4054
)
@@ -4051,7 +4057,7 @@ def test_self_join_date_columns(postgresql_psycopg2_engine):
4051
4057
con .execute (create_table )
4052
4058
4053
4059
sql_query = (
4054
- 'SELECT * FROM "person " AS p1 INNER JOIN "person " AS p2 ON p1.id = p2.id;'
4060
+ f 'SELECT * FROM "{ table_uuid } " AS p1 INNER JOIN "{ table_uuid } " AS p2 ON p1.id = p2.id;'
4055
4061
)
4056
4062
result = pd .read_sql (sql_query , conn )
4057
4063
expected = DataFrame (
@@ -4062,7 +4068,7 @@ def test_self_join_date_columns(postgresql_psycopg2_engine):
4062
4068
4063
4069
# Cleanup
4064
4070
with sql .SQLDatabase (conn , need_transaction = True ) as pandasSQL :
4065
- pandasSQL .drop_table ("person" )
4071
+ pandasSQL .drop_table (table_uuid )
4066
4072
4067
4073
4068
4074
def test_create_and_drop_table (sqlite_engine ):
@@ -4083,16 +4089,18 @@ def test_create_and_drop_table(sqlite_engine):
4083
4089
4084
4090
def test_sqlite_datetime_date (sqlite_buildin ):
4085
4091
conn = sqlite_buildin
4092
+ table_uuid = table_uuid_gen ("test_date" )
4086
4093
df = DataFrame ([date (2014 , 1 , 1 ), date (2014 , 1 , 2 )], columns = ["a" ])
4087
- assert df .to_sql (name = "test_date" , con = conn , index = False ) == 2
4088
- res = read_sql_query ("SELECT * FROM test_date " , conn )
4094
+ assert df .to_sql (name = table_uuid , con = conn , index = False ) == 2
4095
+ res = read_sql_query (f "SELECT * FROM { table_uuid } " , conn )
4089
4096
# comes back as strings
4090
4097
tm .assert_frame_equal (res , df .astype (str ))
4091
4098
4092
4099
4093
4100
@pytest .mark .parametrize ("tz_aware" , [False , True ])
4094
4101
def test_sqlite_datetime_time (tz_aware , sqlite_buildin ):
4095
4102
conn = sqlite_buildin
4103
+ table_uuid = table_uuid_gen ("test_time" )
4096
4104
# test support for datetime.time, GH #8341
4097
4105
if not tz_aware :
4098
4106
tz_times = [time (9 , 0 , 0 ), time (9 , 1 , 30 )]
@@ -4102,8 +4110,8 @@ def test_sqlite_datetime_time(tz_aware, sqlite_buildin):
4102
4110
4103
4111
df = DataFrame (tz_times , columns = ["a" ])
4104
4112
4105
- assert df .to_sql (name = "test_time" , con = conn , index = False ) == 2
4106
- res = read_sql_query ("SELECT * FROM test_time " , conn )
4113
+ assert df .to_sql (name = table_uuid , con = conn , index = False ) == 2
4114
+ res = read_sql_query (f "SELECT * FROM { table_uuid } " , conn )
4107
4115
# comes back as strings
4108
4116
expected = df .map (lambda _ : _ .strftime ("%H:%M:%S.%f" ))
4109
4117
tm .assert_frame_equal (res , expected )
@@ -4119,24 +4127,28 @@ def get_sqlite_column_type(conn, table, column):
4119
4127
4120
4128
def test_sqlite_test_dtype (sqlite_buildin ):
4121
4129
conn = sqlite_buildin
4130
+ table_uuid = table_uuid_gen ("dtype_test" )
4131
+ table_uuid2 = table_uuid_gen ("dtype_test2" )
4132
+ table_error = table_uuid_gen ("error" )
4133
+ table_single = table_uuid_gen ("single_dtype_test" )
4122
4134
cols = ["A" , "B" ]
4123
4135
data = [(0.8 , True ), (0.9 , None )]
4124
4136
df = DataFrame (data , columns = cols )
4125
- assert df .to_sql (name = "dtype_test" , con = conn ) == 2
4126
- assert df .to_sql (name = "dtype_test2" , con = conn , dtype = {"B" : "STRING" }) == 2
4137
+ assert df .to_sql (name = table_uuid , con = conn ) == 2
4138
+ assert df .to_sql (name = table_uuid2 , con = conn , dtype = {"B" : "STRING" }) == 2
4127
4139
4128
4140
# sqlite stores Boolean values as INTEGER
4129
- assert get_sqlite_column_type (conn , "dtype_test" , "B" ) == "INTEGER"
4141
+ assert get_sqlite_column_type (conn , table_uuid , "B" ) == "INTEGER"
4130
4142
4131
- assert get_sqlite_column_type (conn , "dtype_test2" , "B" ) == "STRING"
4143
+ assert get_sqlite_column_type (conn , table_uuid2 , "B" ) == "STRING"
4132
4144
msg = r"B \(<class 'bool'>\) not a string"
4133
4145
with pytest .raises (ValueError , match = msg ):
4134
- df .to_sql (name = "error" , con = conn , dtype = {"B" : bool })
4146
+ df .to_sql (name = table_error , con = conn , dtype = {"B" : bool })
4135
4147
4136
4148
# single dtype
4137
- assert df .to_sql (name = "single_dtype_test" , con = conn , dtype = "STRING" ) == 2
4138
- assert get_sqlite_column_type (conn , "single_dtype_test" , "A" ) == "STRING"
4139
- assert get_sqlite_column_type (conn , "single_dtype_test" , "B" ) == "STRING"
4149
+ assert df .to_sql (name = table_single , con = conn , dtype = "STRING" ) == 2
4150
+ assert get_sqlite_column_type (conn , table_single , "A" ) == "STRING"
4151
+ assert get_sqlite_column_type (conn , table_single , "B" ) == "STRING"
4140
4152
4141
4153
4142
4154
def test_sqlite_notna_dtype (sqlite_buildin ):
@@ -4149,7 +4161,7 @@ def test_sqlite_notna_dtype(sqlite_buildin):
4149
4161
}
4150
4162
df = DataFrame (cols )
4151
4163
4152
- tbl = "notna_dtype_test"
4164
+ tbl = table_uuid_gen ( "notna_dtype_test" )
4153
4165
assert df .to_sql (name = tbl , con = conn ) == 2
4154
4166
4155
4167
assert get_sqlite_column_type (conn , tbl , "Bool" ) == "INTEGER"
0 commit comments