Skip to content

Commit 4147a77

Browse files
committed
Make test_sql.py other connectable tests parallelizable
1 parent 17cf723 commit 4147a77

File tree

1 file changed

+44
-32
lines changed

1 file changed

+44
-32
lines changed

pandas/tests/io/test_sql.py

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3967,33 +3967,37 @@ def test_psycopg2_schema_support(postgresql_psycopg2_engine):
39673967
con.exec_driver_sql("DROP SCHEMA IF EXISTS other CASCADE;")
39683968
con.exec_driver_sql("CREATE SCHEMA other;")
39693969

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+
39703974
# 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
39723976
assert (
39733977
df.to_sql(
3974-
name="test_schema_public_explicit",
3978+
name=schema_public_explicit_uuid,
39753979
con=conn,
39763980
index=False,
39773981
schema="public",
39783982
)
39793983
== 2
39803984
)
39813985
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
39833987
)
39843988

39853989
# read dataframes back in
3986-
res1 = sql.read_sql_table("test_schema_public", conn)
3990+
res1 = sql.read_sql_table(schema_public_uuid, conn)
39873991
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)
39893993
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")
39913995
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")
39933997
tm.assert_frame_equal(df, res4)
3994-
msg = "Table test_schema_other not found"
3998+
msg = f"Table {schema_other_uuid} not found"
39953999
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")
39974001

39984002
# different if_exists options
39994003

@@ -4005,26 +4009,26 @@ def test_psycopg2_schema_support(postgresql_psycopg2_engine):
40054009

40064010
# write dataframe with different if_exists options
40074011
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
40094013
)
40104014
df.to_sql(
4011-
name="test_schema_other",
4015+
name=schema_other_uuid,
40124016
con=conn,
40134017
schema="other",
40144018
index=False,
40154019
if_exists="replace",
40164020
)
40174021
assert (
40184022
df.to_sql(
4019-
name="test_schema_other",
4023+
name=schema_other_uuid,
40204024
con=conn,
40214025
schema="other",
40224026
index=False,
40234027
if_exists="append",
40244028
)
40254029
== 2
40264030
)
4027-
res = sql.read_sql_table("test_schema_other", conn, schema="other")
4031+
res = sql.read_sql_table(schema_other_uuid, conn, schema="other")
40284032
tm.assert_frame_equal(concat([df, df], ignore_index=True), res)
40294033

40304034

@@ -4034,15 +4038,17 @@ def test_self_join_date_columns(postgresql_psycopg2_engine):
40344038
conn = postgresql_psycopg2_engine
40354039
from sqlalchemy.sql import text
40364040

4041+
table_uuid = table_uuid_gen("person")
4042+
40374043
create_table = text(
4038-
"""
4039-
CREATE TABLE person
4044+
f"""
4045+
CREATE TABLE {table_uuid}
40404046
(
4041-
id serial constraint person_pkey primary key,
4047+
id serial constraint {table_uuid}_pkey primary key,
40424048
created_dt timestamp with time zone
40434049
);
40444050
4045-
INSERT INTO person
4051+
INSERT INTO {table_uuid}
40464052
VALUES (1, '2021-01-01T00:00:00Z');
40474053
"""
40484054
)
@@ -4051,7 +4057,7 @@ def test_self_join_date_columns(postgresql_psycopg2_engine):
40514057
con.execute(create_table)
40524058

40534059
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;'
40554061
)
40564062
result = pd.read_sql(sql_query, conn)
40574063
expected = DataFrame(
@@ -4062,7 +4068,7 @@ def test_self_join_date_columns(postgresql_psycopg2_engine):
40624068

40634069
# Cleanup
40644070
with sql.SQLDatabase(conn, need_transaction=True) as pandasSQL:
4065-
pandasSQL.drop_table("person")
4071+
pandasSQL.drop_table(table_uuid)
40664072

40674073

40684074
def test_create_and_drop_table(sqlite_engine):
@@ -4083,16 +4089,18 @@ def test_create_and_drop_table(sqlite_engine):
40834089

40844090
def test_sqlite_datetime_date(sqlite_buildin):
40854091
conn = sqlite_buildin
4092+
table_uuid = table_uuid_gen("test_date")
40864093
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)
40894096
# comes back as strings
40904097
tm.assert_frame_equal(res, df.astype(str))
40914098

40924099

40934100
@pytest.mark.parametrize("tz_aware", [False, True])
40944101
def test_sqlite_datetime_time(tz_aware, sqlite_buildin):
40954102
conn = sqlite_buildin
4103+
table_uuid = table_uuid_gen("test_time")
40964104
# test support for datetime.time, GH #8341
40974105
if not tz_aware:
40984106
tz_times = [time(9, 0, 0), time(9, 1, 30)]
@@ -4102,8 +4110,8 @@ def test_sqlite_datetime_time(tz_aware, sqlite_buildin):
41024110

41034111
df = DataFrame(tz_times, columns=["a"])
41044112

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)
41074115
# comes back as strings
41084116
expected = df.map(lambda _: _.strftime("%H:%M:%S.%f"))
41094117
tm.assert_frame_equal(res, expected)
@@ -4119,24 +4127,28 @@ def get_sqlite_column_type(conn, table, column):
41194127

41204128
def test_sqlite_test_dtype(sqlite_buildin):
41214129
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")
41224134
cols = ["A", "B"]
41234135
data = [(0.8, True), (0.9, None)]
41244136
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
41274139

41284140
# 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"
41304142

4131-
assert get_sqlite_column_type(conn, "dtype_test2", "B") == "STRING"
4143+
assert get_sqlite_column_type(conn, table_uuid2, "B") == "STRING"
41324144
msg = r"B \(<class 'bool'>\) not a string"
41334145
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})
41354147

41364148
# 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"
41404152

41414153

41424154
def test_sqlite_notna_dtype(sqlite_buildin):
@@ -4149,7 +4161,7 @@ def test_sqlite_notna_dtype(sqlite_buildin):
41494161
}
41504162
df = DataFrame(cols)
41514163

4152-
tbl = "notna_dtype_test"
4164+
tbl = table_uuid_gen("notna_dtype_test")
41534165
assert df.to_sql(name=tbl, con=conn) == 2
41544166

41554167
assert get_sqlite_column_type(conn, tbl, "Bool") == "INTEGER"

0 commit comments

Comments
 (0)