Skip to content

Commit 1456634

Browse files
committed
Parametrize DB connections and table/view UUIDs for test_read_view_postgres
1 parent e62a848 commit 1456634

File tree

1 file changed

+101
-13
lines changed

1 file changed

+101
-13
lines changed

pandas/tests/io/test_sql.py

Lines changed: 101 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,18 @@
6363
]
6464

6565

66-
def create_unique_table_name(prefix: str) -> str:
67-
return f"{prefix}_{uuid.uuid4().hex}"
66+
@pytest.fixture
67+
def create_uuid(request) -> str:
68+
return f'{getattr(request, "param", "None")}_{uuid.uuid4().hex}'
69+
70+
71+
def repeat(constant):
72+
while True:
73+
yield constant
74+
75+
76+
def setup(connection_variables, uuid_constant):
77+
return list(map(lambda *args: connection_variables, connection_variables, repeat(uuid_constant)))
6878

6979

7080
@pytest.fixture
@@ -538,7 +548,6 @@ def get_all_views(conn):
538548

539549
return inspect(conn).get_view_names()
540550

541-
542551
def get_all_tables(conn):
543552
if isinstance(conn, sqlite3.Connection):
544553
c = conn.execute("SELECT name FROM sqlite_master WHERE type='table'")
@@ -562,6 +571,43 @@ def get_all_tables(conn):
562571

563572
return inspect(conn).get_table_names()
564573

574+
#TODO
575+
# test1.py has a parameter that runs in a subrequest as a fixture.
576+
# I need to modify this behaviour to create a subrequest in a fixture
577+
# so I need to see if I can modify the request inside the test, and then
578+
# run it with the fixture.
579+
580+
#TODO See if I can inject something in the request object
581+
def filter_get_all_tables(conn, extract_this_value):
582+
tables = get_all_tables(conn)
583+
with open("tables.txt", "w") as file:
584+
file.write(str(tables))
585+
return_val = []
586+
with open("collected.txt", "w") as file:
587+
file.write('\n')
588+
for t in tables:
589+
if t in extract_this_value:
590+
file.write(str(t))
591+
return_val.append(t)
592+
return return_val
593+
594+
def filter_get_all_views(conn, extract_this_value):
595+
tables = get_all_views(conn)
596+
with open("views.txt", "w") as file:
597+
file.write(str(tables))
598+
return_val = []
599+
600+
with open("collected.txt", "a") as file:
601+
file.write('\n')
602+
for t in tables:
603+
if t in extract_this_value:
604+
file.write(str(t))
605+
return_val.append(t)
606+
return return_val
607+
608+
609+
# filter_get_all_tables(pytest.param("postgresql_psycopg2_engine", marks=pytest.mark.db),'')
610+
565611

566612
def drop_table(
567613
table_name: str,
@@ -654,18 +700,19 @@ def mysql_pymysql_conn_types(mysql_pymysql_engine_types):
654700

655701

656702
@pytest.fixture
657-
def postgresql_psycopg2_engine():
703+
def postgresql_psycopg2_engine(request):
658704
sqlalchemy = pytest.importorskip("sqlalchemy")
659705
pytest.importorskip("psycopg2")
660706
engine = sqlalchemy.create_engine(
661707
"postgresql+psycopg2://postgres:postgres@localhost:5432/pandas",
662708
poolclass=sqlalchemy.pool.NullPool,
663709
)
664710
yield engine
665-
for view in get_all_views(engine):
666-
drop_view(view, engine)
667-
for tbl in get_all_tables(engine):
668-
drop_table(tbl, engine)
711+
712+
# for view in filter_get_all_tables(engine, getattr(request, "create_uuid")):
713+
# drop_view(view, engine)
714+
# for tbl in filter_get_all_tables(engine, getattr(request, "create_uuid")):
715+
# drop_table(tbl, engine)
669716
engine.dispose()
670717

671718

@@ -1469,16 +1516,56 @@ def insert_on_conflict(table, conn, keys, data_iter):
14691516
pandasSQL.drop_table(table_uuid)
14701517

14711518

1472-
@pytest.mark.parametrize("conn", postgresql_connectable)
1473-
def test_read_view_postgres(conn, request):
1519+
@pytest.fixture
1520+
def conn(request):
1521+
with open("aaaa.txt", "w") as file:
1522+
file.write('\n\n')
1523+
file.write(repr(request.__dict__))
1524+
file.write('\n\n')
1525+
file.write(repr(getattr(request, "param", None)))
1526+
file.write('\n\n')
1527+
#Parameterset
1528+
file.write('\n\n')
1529+
file.write(str(getattr(request, "param", None)[0][0][0]))
1530+
file.write('\n\n')
1531+
file.write('\n\n')
1532+
#String
1533+
file.write(repr(getattr(request, "param", None)[1]))
1534+
conn_type = getattr(request, "param", None)[0]
1535+
conn = request.getfixturevalue(str(getattr(request, "param", None)[0][0][0]))
1536+
uuid_value = getattr(request, "param", None)[1]
1537+
yield conn
1538+
# clean_tables()
1539+
with open("a.txt", "w") as file:
1540+
file.write('\n\n')
1541+
file.write(repr(conn))
1542+
1543+
for view in filter_get_all_tables(conn, uuid_value):
1544+
drop_view(view, conn)
1545+
for tbl in filter_get_all_tables(conn, uuid_value):
1546+
drop_table(tbl, conn)
1547+
1548+
1549+
@pytest.mark.parametrize("conn", setup(postgresql_connectable, "test_read_view_postgres"), indirect = True)
1550+
@pytest.mark.parametrize("create_uuid", ["test_read_view_postgres"], indirect = True)
1551+
def test_read_view_postgres(conn, create_uuid, request):
14741552
# GH 52969
1475-
conn = request.getfixturevalue(conn)
1553+
# conn = request.getfixturevalue(conn)
1554+
1555+
# def run_test():
14761556

14771557
from sqlalchemy.engine import Engine
14781558
from sqlalchemy.sql import text
14791559

1480-
table_name = create_unique_table_name("group")
1481-
view_name = create_unique_table_name("group_view")
1560+
view_name = table_name = create_uuid
1561+
# view_name = "view_"+create_uuid
1562+
1563+
with open("blah.txt", "w") as file:
1564+
file.write('\n')
1565+
file.write(repr(table_name))
1566+
table_name = "table_"+create_uuid
1567+
file.write('\n')
1568+
file.write(repr(table_name))
14821569

14831570
sql_stmt = text(
14841571
f"""
@@ -1493,6 +1580,7 @@ def test_read_view_postgres(conn, request):
14931580
SELECT * FROM {table_name};
14941581
"""
14951582
)
1583+
request
14961584
if isinstance(conn, Engine):
14971585
with conn.connect() as con:
14981586
with con.begin():

0 commit comments

Comments
 (0)