99import pandas .testing as tm
1010import pytest
1111
12- from dataframe_sql import query , register_temp_table , remove_temp_table
12+ from dataframe_sql import query
1313from dataframe_sql .tests .utils import (
1414 AVOCADO ,
1515 DIGIMON_MON_LIST ,
@@ -428,12 +428,18 @@ def test_select_star_from_multiple_tables():
428428 """
429429 my_frame = query ("""select * from forest_fires, digimon_mon_list""" )
430430 forest_fires = FOREST_FIRES .copy ()
431- digimon_mon_list_new = DIGIMON_MON_LIST .copy ()
432- forest_fires ["_temp_id" ] = 1
433- digimon_mon_list_new ["_temp_id" ] = 1
434- pandas_frame = merge (forest_fires , digimon_mon_list_new , on = "_temp_id" ).drop (
435- columns = ["_temp_id" ]
431+ digimon_mon_list = DIGIMON_MON_LIST .copy ()
432+ pandas_frame = merge (
433+ forest_fires .assign (__ = 1 ), digimon_mon_list .assign (__ = 1 ), on = "__" , how = "inner"
436434 )
435+ del pandas_frame ["__" ]
436+ renamed = {}
437+ for column in pandas_frame .columns :
438+ if "_x" in column :
439+ renamed [column ] = "forest_fires." + column .replace ("_x" , "" )
440+ if "_y" in column :
441+ renamed [column ] = "digimon_mon_list." + column .replace ("_y" , "" )
442+ pandas_frame .rename (columns = renamed , inplace = True )
437443 tm .assert_frame_equal (pandas_frame , my_frame )
438444
439445
@@ -445,9 +451,17 @@ def test_select_columns_from_two_tables_with_same_column_name():
445451 my_frame = query ("""select * from forest_fires table1, forest_fires table2""" )
446452 table1 = FOREST_FIRES .copy ()
447453 table2 = FOREST_FIRES .copy ()
448- table1 ["_temp_id" ] = 1
449- table2 ["_temp_id" ] = 1
450- pandas_frame = merge (table1 , table2 , on = "_temp_id" ).drop (columns = ["_temp_id" ])
454+ pandas_frame = merge (table1 .assign (__ = 1 ), table2 .assign (__ = 1 ), on = "__" , how = "inner" )
455+ del pandas_frame ["__" ]
456+
457+ renamed = {}
458+ for column in pandas_frame .columns :
459+ if "_x" in column :
460+ renamed [column ] = "table1." + column .replace ("_x" , "" )
461+ if "_y" in column :
462+ renamed [column ] = "table2." + column .replace ("_y" , "" )
463+ pandas_frame .rename (columns = renamed , inplace = True )
464+
451465 tm .assert_frame_equal (pandas_frame , my_frame )
452466
453467
@@ -1130,5 +1144,6 @@ def test_boolean_order_of_operations_with_parens():
11301144 # print("my first\n", query("select * from forest_fires order by wind desc limit 5"))
11311145 # print("pandas second\n", frame2)
11321146 # print("my second\n", query("select * from forest_fires order by wind asc limit 5"))
1147+ test_select_star_from_multiple_tables ()
11331148
11341149 remove_env_tables ()
0 commit comments