6363]
6464
6565
66- @pytest .fixture
67- def create_uuid (request ) -> str :
68- return f'{ getattr (request , "param" , "None" )} _{ uuid .uuid4 ().hex } '
66+ # @pytest.fixture
67+ def create_uuid (uuid_value ):
68+ # def _return_uuid(content):
69+ return f'{ uuid_value } _{ uuid .uuid4 ().hex } '
70+ # return _return_uuid
71+
72+ # return f'{getattr(request, "param", "None")}_{uuid.uuid4().hex}'
6973
7074
7175def repeat (constant ):
@@ -74,7 +78,7 @@ def repeat(constant):
7478
7579
7680def setup (connection_variables , uuid_constant ):
77- return list (map (lambda * args : connection_variables , connection_variables , repeat (uuid_constant )))
81+ return list (map (lambda * args : args , connection_variables , repeat (uuid_constant )))
7882
7983
8084@pytest .fixture
@@ -543,9 +547,23 @@ def get_all_views(conn):
543547 results .append (view_name )
544548
545549 return results
550+ elif type (conn ) == type ("str" ):
551+ pytest .skip ("sqlite str does not support inspect" )
552+ with open ("get_all_views_dump.txt" , "a" ) as file :
553+ file .write ('\n \n ' )
554+ file .write ((repr (conn )))
555+ if "sqlite" in conn :
556+ return
546557 else :
558+ with open ("get_all_views_dump.txt" , "a" ) as file :
559+ file .write ('\n \n ' )
560+ file .write (str (type (conn )))
547561 from sqlalchemy import inspect
548562
563+ with open ("view_dump.txt" , "a" ) as file :
564+ file .write ('\n \n ' )
565+ file .write ((repr (conn )))
566+
549567 return inspect (conn ).get_view_names ()
550568
551569def get_all_tables (conn ):
@@ -592,23 +610,23 @@ def filter_get_all_tables(conn, extract_this_value):
592610 return return_val
593611
594612def filter_get_all_views (conn , extract_this_value ):
595- tables = get_all_views (conn )
613+ views = get_all_views (conn )
596614 with open ("views.txt" , "w" ) as file :
597- file .write (str (tables ))
615+ file .write (str (views ))
598616 return_val = []
599617
600618 with open ("collected.txt" , "a" ) as file :
601619 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 )
620+ for v in views :
621+ if v in extract_this_value :
622+ file .write (str (v ))
623+ return_val .append (v )
606624 return return_val
607625
608626
609627# filter_get_all_tables(pytest.param("postgresql_psycopg2_engine", marks=pytest.mark.db),'')
610628
611-
629+ #todo cleanup
612630def drop_table (
613631 table_name : str ,
614632 conn : sqlite3 .Connection | sqlalchemy .engine .Engine | sqlalchemy .engine .Connection ,
@@ -623,16 +641,26 @@ def drop_table(
623641 with conn .cursor () as cur :
624642 cur .execute (f'DROP TABLE IF EXISTS "{ table_name } "' )
625643 else :
626- with conn .begin () as con :
627- with sql .SQLDatabase (con ) as db :
628- db .drop_table (table_name )
644+ import sqlalchemy
645+ stmt = sqlalchemy .text (f"DROP TABLE IF EXISTS { table_name } " )
646+ # with conn.begin() as con:
647+ # with sql.SQLDatabase(con) as db:
648+ # db.drop_table(table_name)
649+ # conn.commit()
650+ if isinstance (conn , sqlalchemy .Engine ):
651+ conn = conn .connect ()
652+ conn .commit ()
653+ with conn .begin ():
654+ conn .execute (stmt )
629655
630656
657+ #todo cleanup
631658def drop_view (
632659 view_name : str ,
633660 conn : sqlite3 .Connection | sqlalchemy .engine .Engine | sqlalchemy .engine .Connection ,
634661):
635662 import sqlalchemy
663+ from sqlalchemy import Engine
636664
637665 if isinstance (conn , sqlite3 .Connection ):
638666 conn .execute (f"DROP VIEW IF EXISTS { sql ._get_valid_sqlite_name (view_name )} " )
@@ -647,8 +675,17 @@ def drop_view(
647675 view_name
648676 )
649677 stmt = sqlalchemy .text (f"DROP VIEW IF EXISTS { quoted_view } " )
650- with conn .begin () as con :
651- con .execute (stmt ) # type: ignore[union-attr]
678+ # conn.execution_options(isolation_level="AUTOCOMMIT")
679+ if isinstance (conn , Engine ):
680+ conn = conn .connect ()
681+ if conn .in_transaction ():
682+ conn .commit ()
683+ else :
684+ if conn .in_transaction ():
685+ conn .commit ()
686+ with conn .begin ():
687+ conn .execute (stmt ) # type: ignore[union-attr]
688+ conn .commit ()
652689
653690
654691@pytest .fixture
@@ -1031,11 +1068,65 @@ def sqlite_buildin_types(sqlite_buildin, types_data):
10311068)
10321069
10331070
1034- @pytest .mark .parametrize ("conn" , all_connectable )
1035- def test_dataframe_to_sql (conn , test_frame1 , request ):
1036- # GH 51086 if conn is sqlite_engine
1037- table_uuid = create_unique_table_name ("test" )
1071+ @pytest .fixture
1072+ def connect_and_uuid (request ):
1073+ conn = request .param [0 ]
1074+ while True :
1075+ try :
1076+ c = conn [0 ]
1077+ assert type (c ) != type ("string" )
1078+ conn = c
1079+
1080+ except AssertionError :
1081+ if type (conn ) != type ("string" ):
1082+ conn = conn [0 ]
1083+ break
1084+ with open ("raw_conn.txt" , "a" ) as file :
1085+ file .write ('\n \n ' )
1086+ file .write (repr (conn ))
10381087 conn = request .getfixturevalue (conn )
1088+ with open ("conn.txt" , "a" ) as file :
1089+ file .write ('\n \n ' )
1090+ file .write (repr (conn ))
1091+ # quit(1)
1092+ uuid_value = create_uuid (request .param [1 ])
1093+
1094+ import sqlalchemy
1095+ from sqlalchemy import Engine
1096+ # conn.execution_options(isolation_level="AUTOCOMMIT")
1097+ yield conn , uuid_value
1098+ # if isinstance(conn, Engine):
1099+ # conn = conn.connect()
1100+ # if conn.in_transaction():
1101+ # conn.commit()
1102+ # else:
1103+ # if conn.in_transaction():
1104+ # conn.commit()
1105+ with open ("a.txt" , "a" ) as file :
1106+ file .write ('\n \n ' )
1107+ file .write ("view_" + uuid_value )
1108+ file .write ('\n \n ' )
1109+ file .write ("table_" + uuid_value )
1110+
1111+ for view in filter_get_all_views (conn , "view_" + uuid_value ):
1112+ drop_view (view , conn )
1113+
1114+ for tbl in filter_get_all_tables (conn , "table_" + uuid_value ):
1115+ drop_table (tbl , conn )
1116+
1117+
1118+ @pytest .mark .parametrize ("connect_and_uuid" , setup (all_connectable , "test_dataframe_to_sql" ), indirect = True )
1119+ def test_all (connect_and_uuid ):
1120+ with open ("all.txt" , "a" ) as file :
1121+ file .write ('\n \n ' )
1122+ file .write (repr (setup (all_connectable , "test_dataframe_to_sql" )))
1123+
1124+
1125+ @pytest .mark .parametrize ("connect_and_uuid" , setup (all_connectable , "test_dataframe_to_sql" ), indirect = True )
1126+ def test_dataframe_to_sql (connect_and_uuid , test_frame1 , request ):
1127+ # GH 51086 if conn is sqlite_engine
1128+ conn , uuid = connect_and_uuid
1129+ table_uuid = "table_" + uuid
10391130 test_frame1 .to_sql (name = table_uuid , con = conn , if_exists = "append" , index = False )
10401131
10411132
@@ -1050,7 +1141,7 @@ def test_dataframe_to_sql_empty(conn, test_frame1, request):
10501141
10511142 # GH 51086 if conn is sqlite_engine
10521143 conn = request .getfixturevalue (conn )
1053- table_uuid = create_unique_table_name ("test" )
1144+ table_uuid = "test_" + create_uuid ("test" )
10541145 empty_df = test_frame1 .iloc [:0 ]
10551146 empty_df .to_sql (name = table_uuid , con = conn , if_exists = "append" , index = False )
10561147
@@ -1516,56 +1607,21 @@ def insert_on_conflict(table, conn, keys, data_iter):
15161607 pandasSQL .drop_table (table_uuid )
15171608
15181609
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 ))
15421610
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 )
15471611
15481612
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 ):
1613+ @pytest .mark .parametrize ("connect_and_uuid" , setup (postgresql_connectable , "test_read_view_postgres" ), indirect = True )
1614+ def test_read_view_postgres (connect_and_uuid , request ):
15521615 # GH 52969
15531616 # conn = request.getfixturevalue(conn)
15541617
15551618 # def run_test():
15561619
15571620 from sqlalchemy .engine import Engine
15581621 from sqlalchemy .sql import text
1559-
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 ))
1622+ conn , uuid = connect_and_uuid
1623+ table_name = "table_" + uuid
1624+ view_name = "view_" + uuid
15691625
15701626 sql_stmt = text (
15711627 f"""
0 commit comments