63
63
]
64
64
65
65
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}'
69
73
70
74
71
75
def repeat (constant ):
@@ -74,7 +78,7 @@ def repeat(constant):
74
78
75
79
76
80
def 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 )))
78
82
79
83
80
84
@pytest .fixture
@@ -543,9 +547,23 @@ def get_all_views(conn):
543
547
results .append (view_name )
544
548
545
549
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
546
557
else :
558
+ with open ("get_all_views_dump.txt" , "a" ) as file :
559
+ file .write ('\n \n ' )
560
+ file .write (str (type (conn )))
547
561
from sqlalchemy import inspect
548
562
563
+ with open ("view_dump.txt" , "a" ) as file :
564
+ file .write ('\n \n ' )
565
+ file .write ((repr (conn )))
566
+
549
567
return inspect (conn ).get_view_names ()
550
568
551
569
def get_all_tables (conn ):
@@ -592,23 +610,23 @@ def filter_get_all_tables(conn, extract_this_value):
592
610
return return_val
593
611
594
612
def filter_get_all_views (conn , extract_this_value ):
595
- tables = get_all_views (conn )
613
+ views = get_all_views (conn )
596
614
with open ("views.txt" , "w" ) as file :
597
- file .write (str (tables ))
615
+ file .write (str (views ))
598
616
return_val = []
599
617
600
618
with open ("collected.txt" , "a" ) as file :
601
619
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 )
606
624
return return_val
607
625
608
626
609
627
# filter_get_all_tables(pytest.param("postgresql_psycopg2_engine", marks=pytest.mark.db),'')
610
628
611
-
629
+ #todo cleanup
612
630
def drop_table (
613
631
table_name : str ,
614
632
conn : sqlite3 .Connection | sqlalchemy .engine .Engine | sqlalchemy .engine .Connection ,
@@ -623,16 +641,26 @@ def drop_table(
623
641
with conn .cursor () as cur :
624
642
cur .execute (f'DROP TABLE IF EXISTS "{ table_name } "' )
625
643
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 )
629
655
630
656
657
+ #todo cleanup
631
658
def drop_view (
632
659
view_name : str ,
633
660
conn : sqlite3 .Connection | sqlalchemy .engine .Engine | sqlalchemy .engine .Connection ,
634
661
):
635
662
import sqlalchemy
663
+ from sqlalchemy import Engine
636
664
637
665
if isinstance (conn , sqlite3 .Connection ):
638
666
conn .execute (f"DROP VIEW IF EXISTS { sql ._get_valid_sqlite_name (view_name )} " )
@@ -647,8 +675,17 @@ def drop_view(
647
675
view_name
648
676
)
649
677
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 ()
652
689
653
690
654
691
@pytest .fixture
@@ -1031,11 +1068,65 @@ def sqlite_buildin_types(sqlite_buildin, types_data):
1031
1068
)
1032
1069
1033
1070
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 ))
1038
1087
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
1039
1130
test_frame1 .to_sql (name = table_uuid , con = conn , if_exists = "append" , index = False )
1040
1131
1041
1132
@@ -1050,7 +1141,7 @@ def test_dataframe_to_sql_empty(conn, test_frame1, request):
1050
1141
1051
1142
# GH 51086 if conn is sqlite_engine
1052
1143
conn = request .getfixturevalue (conn )
1053
- table_uuid = create_unique_table_name ("test" )
1144
+ table_uuid = "test_" + create_uuid ("test" )
1054
1145
empty_df = test_frame1 .iloc [:0 ]
1055
1146
empty_df .to_sql (name = table_uuid , con = conn , if_exists = "append" , index = False )
1056
1147
@@ -1516,56 +1607,21 @@ def insert_on_conflict(table, conn, keys, data_iter):
1516
1607
pandasSQL .drop_table (table_uuid )
1517
1608
1518
1609
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
1610
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
1611
1548
1612
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 ):
1552
1615
# GH 52969
1553
1616
# conn = request.getfixturevalue(conn)
1554
1617
1555
1618
# def run_test():
1556
1619
1557
1620
from sqlalchemy .engine import Engine
1558
1621
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
1569
1625
1570
1626
sql_stmt = text (
1571
1627
f"""
0 commit comments