6363 pytest .mark .single_cpu ,
6464]
6565
66+
6667def table_uuid_gen (prefix : str ) -> str :
6768 """Generate a unique table name with context prefix."""
6869 return f"{ prefix } _{ uuid .uuid4 ().hex } "
@@ -1370,9 +1371,7 @@ def insert_on_conflict(table, conn, keys, data_iter):
13701371 conn .execute (create_sql )
13711372
13721373 expected = DataFrame ([[1 , 2.1 , "a" ]], columns = list ("abc" ))
1373- expected .to_sql (
1374- name = table_uuid , con = conn , if_exists = "append" , index = False
1375- )
1374+ expected .to_sql (name = table_uuid , con = conn , if_exists = "append" , index = False )
13761375
13771376 df_insert = DataFrame ([[1 , 3.2 , "b" ]], columns = list ("abc" ))
13781377 inserted = df_insert .to_sql (
@@ -2029,7 +2028,7 @@ def test_api_to_sql_index_label_multiindex(conn, request):
20292028 # no index name, defaults to 'level_0' and 'level_1'
20302029 result = sql .to_sql (temp_frame , table_uuid , conn )
20312030 assert result == expected_row_count
2032- frame = sql .read_sql_query (f "SELECT * FROM table_uuid" , conn )
2031+ frame = sql .read_sql_query ("SELECT * FROM table_uuid" , conn )
20332032 assert frame .columns [0 ] == "level_0"
20342033 assert frame .columns [1 ] == "level_1"
20352034
@@ -2061,7 +2060,7 @@ def test_api_to_sql_index_label_multiindex(conn, request):
20612060 index_label = ["C" , "D" ],
20622061 )
20632062 assert result == expected_row_count
2064- frame = sql .read_sql_query (f "SELECT * FROM table_uuid" , conn )
2063+ frame = sql .read_sql_query ("SELECT * FROM table_uuid" , conn )
20652064 assert frame .columns [:2 ].tolist () == ["C" , "D" ]
20662065
20672066 msg = "Length of 'index_label' should match number of levels, which is 2"
@@ -2562,7 +2561,9 @@ def test_database_uri_string(conn, request, test_frame1):
25622561 with tm .ensure_clean () as name :
25632562 db_uri = "sqlite:///" + name
25642563 table_uuid = table_uuid_gen ("iris" )
2565- test_frame1 .to_sql (name = table_uuid , con = db_uri , if_exists = "replace" , index = False )
2564+ test_frame1 .to_sql (
2565+ name = table_uuid , con = db_uri , if_exists = "replace" , index = False
2566+ )
25662567 test_frame2 = sql .read_sql (table_uuid , db_uri )
25672568 test_frame3 = sql .read_sql_table (table_uuid , db_uri )
25682569 query = f"SELECT * FROM { table_uuid } "
@@ -3318,7 +3319,7 @@ def test_dtype(conn, request):
33183319 df = DataFrame (data , columns = cols )
33193320
33203321 table_uuid1 = table_uuid_gen ("dtype_test" )
3321- table_uuid2 = table_uuid_gen ("dtype_test2" )
3322+ table_uuid2 = table_uuid_gen ("dtype_test2" )
33223323 table_uuid3 = table_uuid_gen ("dtype_test3" )
33233324 table_uuid_single = table_uuid_gen ("single_dtype_test" )
33243325 error_table = table_uuid_gen ("error" )
@@ -3470,8 +3471,7 @@ def main(connectable):
34703471 test_connectable (connectable )
34713472
34723473 assert (
3473- DataFrame ({"test_foo_data" : [0 , 1 , 2 ]}).to_sql (name = table_uuid , con = conn )
3474- == 3
3474+ DataFrame ({"test_foo_data" : [0 , 1 , 2 ]}).to_sql (name = table_uuid , con = conn ) == 3
34753475 )
34763476 main (conn )
34773477
@@ -3900,8 +3900,7 @@ class Test(BaseModel):
39003900 with Session () as session :
39013901 df = DataFrame ({"id" : [0 , 1 ], "string_column" : ["hello" , "world" ]})
39023902 assert (
3903- df .to_sql (name = table_uuid , con = conn , index = False , if_exists = "replace" )
3904- == 2
3903+ df .to_sql (name = table_uuid , con = conn , index = False , if_exists = "replace" ) == 2
39053904 )
39063905 session .commit ()
39073906 test_query = session .query (Test .id , Test .string_column )
@@ -3986,9 +3985,7 @@ def test_psycopg2_schema_support(postgresql_psycopg2_engine):
39863985 )
39873986 == 2
39883987 )
3989- assert (
3990- df .to_sql (name = schema_other_uuid , con = conn , index = False , schema = "other" ) == 2
3991- )
3988+ assert df .to_sql (name = schema_other_uuid , con = conn , index = False , schema = "other" ) == 2
39923989
39933990 # read dataframes back in
39943991 res1 = sql .read_sql_table (schema_public_uuid , conn )
@@ -4012,9 +4009,7 @@ def test_psycopg2_schema_support(postgresql_psycopg2_engine):
40124009 con .exec_driver_sql ("CREATE SCHEMA other;" )
40134010
40144011 # write dataframe with different if_exists options
4015- assert (
4016- df .to_sql (name = schema_other_uuid , con = conn , schema = "other" , index = False ) == 2
4017- )
4012+ assert df .to_sql (name = schema_other_uuid , con = conn , schema = "other" , index = False ) == 2
40184013 df .to_sql (
40194014 name = schema_other_uuid ,
40204015 con = conn ,
@@ -4042,27 +4037,25 @@ def test_self_join_date_columns(postgresql_psycopg2_engine):
40424037 conn = postgresql_psycopg2_engine
40434038 from sqlalchemy .sql import text
40444039
4045- table_uuid = table_uuid_gen ("person" )
4040+ tb = table_uuid_gen ("person" )
40464041
40474042 create_table = text (
40484043 f"""
4049- CREATE TABLE { table_uuid }
4044+ CREATE TABLE { tb }
40504045 (
4051- id serial constraint { table_uuid } _pkey primary key,
4046+ id serial constraint { tb } _pkey primary key,
40524047 created_dt timestamp with time zone
40534048 );
40544049
4055- INSERT INTO { table_uuid }
4050+ INSERT INTO { tb }
40564051 VALUES (1, '2021-01-01T00:00:00Z');
40574052 """
40584053 )
40594054 with conn .connect () as con :
40604055 with con .begin ():
40614056 con .execute (create_table )
40624057
4063- sql_query = (
4064- f'SELECT * FROM "{ table_uuid } " AS p1 INNER JOIN "{ table_uuid } " AS p2 ON p1.id = p2.id;'
4065- )
4058+ sql_query = f'SELECT * FROM "{ tb } " AS p1 INNER JOIN "{ tb } " AS p2 ON p1.id = p2.id;'
40664059 result = pd .read_sql (sql_query , conn )
40674060 expected = DataFrame (
40684061 [[1 , Timestamp ("2021" , tz = "UTC" )] * 2 ], columns = ["id" , "created_dt" ] * 2
@@ -4072,7 +4065,7 @@ def test_self_join_date_columns(postgresql_psycopg2_engine):
40724065
40734066 # Cleanup
40744067 with sql .SQLDatabase (conn , need_transaction = True ) as pandasSQL :
4075- pandasSQL .drop_table (table_uuid )
4068+ pandasSQL .drop_table (tb )
40764069
40774070
40784071def test_create_and_drop_table (sqlite_engine ):
@@ -4258,7 +4251,9 @@ def test_xsqlite_basic(sqlite_buildin):
42584251 new_idx = Index (np .arange (len (frame2 )), dtype = np .int64 ) + 10
42594252 frame2 ["Idx" ] = new_idx .copy ()
42604253 assert sql .to_sql (frame2 , name = table_uuid2 , con = sqlite_buildin , index = False ) == 10
4261- result = sql .read_sql (f"select * from { table_uuid2 } " , sqlite_buildin , index_col = "Idx" )
4254+ result = sql .read_sql (
4255+ f"select * from { table_uuid2 } " , sqlite_buildin , index_col = "Idx"
4256+ )
42624257 expected = frame .copy ()
42634258 expected .index = new_idx
42644259 expected .index .name = "Idx"
@@ -4271,19 +4266,20 @@ def test_xsqlite_write_row_by_row(sqlite_buildin):
42714266 columns = Index (list ("ABCD" )),
42724267 index = date_range ("2000-01-01" , periods = 10 , freq = "B" ),
42734268 )
4269+ table_uuid = table_uuid_gen ("test" )
42744270 frame .iloc [0 , 0 ] = np .nan
4275- create_sql = sql .get_schema (frame , "test" )
4271+ create_sql = sql .get_schema (frame , table_uuid )
42764272 cur = sqlite_buildin .cursor ()
42774273 cur .execute (create_sql )
42784274
4279- ins = "INSERT INTO test VALUES (%s, %s, %s, %s)"
4275+ ins = f "INSERT INTO { table_uuid } VALUES (%s, %s, %s, %s)"
42804276 for _ , row in frame .iterrows ():
42814277 fmt_sql = format_query (ins , * row )
42824278 tquery (fmt_sql , con = sqlite_buildin )
42834279
42844280 sqlite_buildin .commit ()
42854281
4286- result = sql .read_sql ("select * from test " , con = sqlite_buildin )
4282+ result = sql .read_sql (f "select * from { table_uuid } " , con = sqlite_buildin )
42874283 result .index = frame .index
42884284 tm .assert_frame_equal (result , frame , rtol = 1e-3 )
42894285
@@ -4294,17 +4290,18 @@ def test_xsqlite_execute(sqlite_buildin):
42944290 columns = Index (list ("ABCD" )),
42954291 index = date_range ("2000-01-01" , periods = 10 , freq = "B" ),
42964292 )
4297- create_sql = sql .get_schema (frame , "test" )
4293+ table_uuid = table_uuid_gen ("test" )
4294+ create_sql = sql .get_schema (frame , table_uuid )
42984295 cur = sqlite_buildin .cursor ()
42994296 cur .execute (create_sql )
4300- ins = "INSERT INTO test VALUES (?, ?, ?, ?)"
4297+ ins = f "INSERT INTO { table_uuid } VALUES (?, ?, ?, ?)"
43014298
43024299 row = frame .iloc [0 ]
43034300 with sql .pandasSQL_builder (sqlite_buildin ) as pandas_sql :
43044301 pandas_sql .execute (ins , tuple (row ))
43054302 sqlite_buildin .commit ()
43064303
4307- result = sql .read_sql ("select * from test " , sqlite_buildin )
4304+ result = sql .read_sql (f "select * from { table_uuid } " , sqlite_buildin )
43084305 result .index = frame .index [:1 ]
43094306 tm .assert_frame_equal (result , frame [:1 ])
43104307
@@ -4315,23 +4312,25 @@ def test_xsqlite_schema(sqlite_buildin):
43154312 columns = Index (list ("ABCD" )),
43164313 index = date_range ("2000-01-01" , periods = 10 , freq = "B" ),
43174314 )
4318- create_sql = sql .get_schema (frame , "test" )
4315+ table_uuid = table_uuid_gen ("test" )
4316+ create_sql = sql .get_schema (frame , table_uuid )
43194317 lines = create_sql .splitlines ()
43204318 for line in lines :
43214319 tokens = line .split (" " )
43224320 if len (tokens ) == 2 and tokens [0 ] == "A" :
43234321 assert tokens [1 ] == "DATETIME"
43244322
4325- create_sql = sql .get_schema (frame , "test" , keys = ["A" , "B" ])
4323+ create_sql = sql .get_schema (frame , table_uuid , keys = ["A" , "B" ])
43264324 lines = create_sql .splitlines ()
43274325 assert 'PRIMARY KEY ("A", "B")' in create_sql
43284326 cur = sqlite_buildin .cursor ()
43294327 cur .execute (create_sql )
43304328
43314329
43324330def test_xsqlite_execute_fail (sqlite_buildin ):
4333- create_sql = """
4334- CREATE TABLE test
4331+ table_uuid = table_uuid_gen ("test" )
4332+ create_sql = f"""
4333+ CREATE TABLE { table_uuid }
43354334 (
43364335 a TEXT,
43374336 b TEXT,
@@ -4343,16 +4342,17 @@ def test_xsqlite_execute_fail(sqlite_buildin):
43434342 cur .execute (create_sql )
43444343
43454344 with sql .pandasSQL_builder (sqlite_buildin ) as pandas_sql :
4346- pandas_sql .execute ('INSERT INTO test VALUES("foo", "bar", 1.234)' )
4347- pandas_sql .execute ('INSERT INTO test VALUES("foo", "baz", 2.567)' )
4345+ pandas_sql .execute (f 'INSERT INTO { table_uuid } VALUES("foo", "bar", 1.234)' )
4346+ pandas_sql .execute (f 'INSERT INTO { table_uuid } VALUES("foo", "baz", 2.567)' )
43484347
43494348 with pytest .raises (sql .DatabaseError , match = "Execution failed on sql" ):
4350- pandas_sql .execute ('INSERT INTO test VALUES("foo", "bar", 7)' )
4349+ pandas_sql .execute (f 'INSERT INTO { table_uuid } VALUES("foo", "bar", 7)' )
43514350
43524351
43534352def test_xsqlite_execute_closed_connection ():
4354- create_sql = """
4355- CREATE TABLE test
4353+ table_uuid = table_uuid_gen ("test" )
4354+ create_sql = f"""
4355+ CREATE TABLE { table_uuid }
43564356 (
43574357 a TEXT,
43584358 b TEXT,
@@ -4365,38 +4365,39 @@ def test_xsqlite_execute_closed_connection():
43654365 cur .execute (create_sql )
43664366
43674367 with sql .pandasSQL_builder (conn ) as pandas_sql :
4368- pandas_sql .execute ('INSERT INTO test VALUES("foo", "bar", 1.234)' )
4368+ pandas_sql .execute (f 'INSERT INTO { table_uuid } VALUES("foo", "bar", 1.234)' )
43694369
43704370 msg = "Cannot operate on a closed database."
43714371 with pytest .raises (sqlite3 .ProgrammingError , match = msg ):
4372- tquery ("select * from test " , con = conn )
4372+ tquery (f "select * from { table_uuid } " , con = conn )
43734373
43744374
43754375def test_xsqlite_keyword_as_column_names (sqlite_buildin ):
4376+ table_uuid = table_uuid_gen ("testkeywords" )
43764377 df = DataFrame ({"From" : np .ones (5 )})
4377- assert sql .to_sql (df , con = sqlite_buildin , name = "testkeywords" , index = False ) == 5
4378+ assert sql .to_sql (df , con = sqlite_buildin , name = table_uuid , index = False ) == 5
43784379
43794380
43804381def test_xsqlite_onecolumn_of_integer (sqlite_buildin ):
43814382 # GH 3628
43824383 # a column_of_integers dataframe should transfer well to sql
4383-
4384+ table_uuid = table_uuid_gen ( "mono_df" )
43844385 mono_df = DataFrame ([1 , 2 ], columns = ["c0" ])
4385- assert sql .to_sql (mono_df , con = sqlite_buildin , name = "mono_df" , index = False ) == 2
4386+ assert sql .to_sql (mono_df , con = sqlite_buildin , name = table_uuid , index = False ) == 2
43864387 # computing the sum via sql
43874388 con_x = sqlite_buildin
4388- the_sum = sum (my_c0 [0 ] for my_c0 in con_x .execute ("select * from mono_df " ))
4389+ the_sum = sum (my_c0 [0 ] for my_c0 in con_x .execute (f "select * from { table_uuid } " ))
43894390 # it should not fail, and gives 3 ( Issue #3628 )
43904391 assert the_sum == 3
43914392
4392- result = sql .read_sql ("select * from mono_df " , con_x )
4393+ result = sql .read_sql (f "select * from { table_uuid } " , con_x )
43934394 tm .assert_frame_equal (result , mono_df )
43944395
43954396
43964397def test_xsqlite_if_exists (sqlite_buildin ):
43974398 df_if_exists_1 = DataFrame ({"col1" : [1 , 2 ], "col2" : ["A" , "B" ]})
43984399 df_if_exists_2 = DataFrame ({"col1" : [3 , 4 , 5 ], "col2" : ["C" , "D" , "E" ]})
4399- table_name = "table_if_exists"
4400+ table_name = table_uuid_gen ( "table_if_exists" )
44004401 sql_select = f"SELECT * FROM { table_name } "
44014402
44024403 msg = "'notvalidvalue' is not valid for if_exists"
0 commit comments