58
58
SQLAlchemyEngine ,
59
59
SQLDatabase ,
60
60
SQLiteDatabase ,
61
- _gt14 ,
62
61
get_engine ,
63
62
pandasSQL_builder ,
64
63
read_sql_query ,
@@ -385,10 +384,10 @@ def mysql_pymysql_engine(iris_path, types_data):
385
384
"mysql+pymysql://root@localhost:3306/pandas" ,
386
385
connect_args = {"client_flag" : pymysql .constants .CLIENT .MULTI_STATEMENTS },
387
386
)
388
- check_target = sqlalchemy .inspect (engine ) if _gt14 () else engine
389
- if not check_target .has_table ("iris" ):
387
+ insp = sqlalchemy .inspect (engine )
388
+ if not insp .has_table ("iris" ):
390
389
create_and_load_iris (engine , iris_path , "mysql" )
391
- if not check_target .has_table ("types" ):
390
+ if not insp .has_table ("types" ):
392
391
for entry in types_data :
393
392
entry .pop ("DateColWithTz" )
394
393
create_and_load_types (engine , types_data , "mysql" )
@@ -412,10 +411,10 @@ def postgresql_psycopg2_engine(iris_path, types_data):
412
411
engine = sqlalchemy .create_engine (
413
412
"postgresql+psycopg2://postgres:postgres@localhost:5432/pandas"
414
413
)
415
- check_target = sqlalchemy .inspect (engine ) if _gt14 () else engine
416
- if not check_target .has_table ("iris" ):
414
+ insp = sqlalchemy .inspect (engine )
415
+ if not insp .has_table ("iris" ):
417
416
create_and_load_iris (engine , iris_path , "postgresql" )
418
- if not check_target .has_table ("types" ):
417
+ if not insp .has_table ("types" ):
419
418
create_and_load_types (engine , types_data , "postgresql" )
420
419
yield engine
421
420
with engine .connect () as conn :
@@ -1450,8 +1449,7 @@ def test_query_by_select_obj(self):
1450
1449
)
1451
1450
1452
1451
iris = iris_table_metadata (self .flavor )
1453
- iris_select = iris if _gt14 () else [iris ]
1454
- name_select = select (iris_select ).where (iris .c .Name == bindparam ("name" ))
1452
+ name_select = select (iris ).where (iris .c .Name == bindparam ("name" ))
1455
1453
iris_df = sql .read_sql (name_select , self .conn , params = {"name" : "Iris-setosa" })
1456
1454
all_names = set (iris_df ["Name" ])
1457
1455
assert all_names == {"Iris-setosa" }
@@ -1624,46 +1622,33 @@ def test_to_sql_empty(self, test_frame1):
1624
1622
self ._to_sql_empty (test_frame1 )
1625
1623
1626
1624
def test_create_table (self ):
1625
+ from sqlalchemy import inspect
1626
+
1627
1627
temp_conn = self .connect ()
1628
1628
temp_frame = DataFrame (
1629
1629
{"one" : [1.0 , 2.0 , 3.0 , 4.0 ], "two" : [4.0 , 3.0 , 2.0 , 1.0 ]}
1630
1630
)
1631
-
1632
1631
pandasSQL = sql .SQLDatabase (temp_conn )
1633
1632
assert pandasSQL .to_sql (temp_frame , "temp_frame" ) == 4
1634
1633
1635
- if _gt14 ():
1636
- from sqlalchemy import inspect
1637
-
1638
- insp = inspect (temp_conn )
1639
- assert insp .has_table ("temp_frame" )
1640
- else :
1641
- assert temp_conn .has_table ("temp_frame" )
1634
+ insp = inspect (temp_conn )
1635
+ assert insp .has_table ("temp_frame" )
1642
1636
1643
1637
def test_drop_table (self ):
1644
- temp_conn = self . connect ()
1638
+ from sqlalchemy import inspect
1645
1639
1640
+ temp_conn = self .connect ()
1646
1641
temp_frame = DataFrame (
1647
1642
{"one" : [1.0 , 2.0 , 3.0 , 4.0 ], "two" : [4.0 , 3.0 , 2.0 , 1.0 ]}
1648
1643
)
1649
-
1650
1644
pandasSQL = sql .SQLDatabase (temp_conn )
1651
1645
assert pandasSQL .to_sql (temp_frame , "temp_frame" ) == 4
1652
1646
1653
- if _gt14 ():
1654
- from sqlalchemy import inspect
1655
-
1656
- insp = inspect (temp_conn )
1657
- assert insp .has_table ("temp_frame" )
1658
- else :
1659
- assert temp_conn .has_table ("temp_frame" )
1647
+ insp = inspect (temp_conn )
1648
+ assert insp .has_table ("temp_frame" )
1660
1649
1661
1650
pandasSQL .drop_table ("temp_frame" )
1662
-
1663
- if _gt14 ():
1664
- assert not insp .has_table ("temp_frame" )
1665
- else :
1666
- assert not temp_conn .has_table ("temp_frame" )
1651
+ assert not insp .has_table ("temp_frame" )
1667
1652
1668
1653
def test_roundtrip (self , test_frame1 ):
1669
1654
self ._roundtrip (test_frame1 )
@@ -2156,14 +2141,10 @@ def bar(connection, data):
2156
2141
data .to_sql (name = "test_foo_data" , con = connection , if_exists = "append" )
2157
2142
2158
2143
def baz (conn ):
2159
- if _gt14 ():
2160
- # https://github.com/sqlalchemy/sqlalchemy/commit/
2161
- # 00b5c10846e800304caa86549ab9da373b42fa5d#r48323973
2162
- foo_data = foo (conn )
2163
- bar (conn , foo_data )
2164
- else :
2165
- foo_data = conn .run_callable (foo )
2166
- conn .run_callable (bar , foo_data )
2144
+ # https://github.com/sqlalchemy/sqlalchemy/commit/
2145
+ # 00b5c10846e800304caa86549ab9da373b42fa5d#r48323973
2146
+ foo_data = foo (conn )
2147
+ bar (conn , foo_data )
2167
2148
2168
2149
def main (connectable ):
2169
2150
if isinstance (connectable , Engine ):
@@ -2216,14 +2197,9 @@ def test_temporary_table(self):
2216
2197
)
2217
2198
from sqlalchemy .orm import (
2218
2199
Session ,
2219
- sessionmaker ,
2200
+ declarative_base ,
2220
2201
)
2221
2202
2222
- if _gt14 ():
2223
- from sqlalchemy .orm import declarative_base
2224
- else :
2225
- from sqlalchemy .ext .declarative import declarative_base
2226
-
2227
2203
test_data = "Hello, World!"
2228
2204
expected = DataFrame ({"spam" : [test_data ]})
2229
2205
Base = declarative_base ()
@@ -2234,24 +2210,13 @@ class Temporary(Base):
2234
2210
id = Column (Integer , primary_key = True )
2235
2211
spam = Column (Unicode (30 ), nullable = False )
2236
2212
2237
- if _gt14 ():
2238
- with Session (self .conn ) as session :
2239
- with session .begin ():
2240
- conn = session .connection ()
2241
- Temporary .__table__ .create (conn )
2242
- session .add (Temporary (spam = test_data ))
2243
- session .flush ()
2244
- df = sql .read_sql_query (sql = select (Temporary .spam ), con = conn )
2245
- else :
2246
- Session = sessionmaker ()
2247
- session = Session (bind = self .conn )
2248
- with session .transaction :
2213
+ with Session (self .conn ) as session :
2214
+ with session .begin ():
2249
2215
conn = session .connection ()
2250
2216
Temporary .__table__ .create (conn )
2251
2217
session .add (Temporary (spam = test_data ))
2252
2218
session .flush ()
2253
- df = sql .read_sql_query (sql = select ([Temporary .spam ]), con = conn )
2254
-
2219
+ df = sql .read_sql_query (sql = select (Temporary .spam ), con = conn )
2255
2220
tm .assert_frame_equal (df , expected )
2256
2221
2257
2222
# -- SQL Engine tests (in the base class for now)
@@ -2349,12 +2314,10 @@ def test_row_object_is_named_tuple(self):
2349
2314
Integer ,
2350
2315
String ,
2351
2316
)
2352
- from sqlalchemy .orm import sessionmaker
2353
-
2354
- if _gt14 ():
2355
- from sqlalchemy .orm import declarative_base
2356
- else :
2357
- from sqlalchemy .ext .declarative import declarative_base
2317
+ from sqlalchemy .orm import (
2318
+ declarative_base ,
2319
+ sessionmaker ,
2320
+ )
2358
2321
2359
2322
BaseModel = declarative_base ()
2360
2323
0 commit comments