@@ -253,9 +253,13 @@ def _get_exec(self):
253253 else :
254254 return self .conn .cursor ()
255255
256- def _load_iris_data (self , datapath ):
256+ @pytest .fixture (params = [('io' , 'data' , 'iris.csv' )])
257+ def load_iris_data (self , datapath , request ):
257258 import io
258- iris_csv_file = datapath ('io' , 'data' , 'iris.csv' )
259+ iris_csv_file = datapath (* request .param )
260+
261+ if not hasattr (self , 'conn' ):
262+ self .setup_connect ()
259263
260264 self .drop_table ('iris' )
261265 self ._get_exec ().execute (SQL_STRINGS ['create_iris' ][self .flavor ])
@@ -503,10 +507,14 @@ class _TestSQLApi(PandasSQLTest):
503507 flavor = 'sqlite'
504508 mode = None
505509
506- @pytest .fixture (autouse = True )
507- def setup_method (self , datapath ):
510+ def setup_connect (self ):
508511 self .conn = self .connect ()
509- self ._load_iris_data (datapath )
512+
513+ @pytest .fixture (autouse = True )
514+ def setup_method (self , load_iris_data ):
515+ self .load_test_data_and_sql ()
516+
517+ def load_test_data_and_sql (self ):
510518 self ._load_iris_view ()
511519 self ._load_test1_data ()
512520 self ._load_test2_data ()
@@ -1027,8 +1035,8 @@ class _EngineToConnMixin(object):
10271035 """
10281036
10291037 @pytest .fixture (autouse = True )
1030- def setup_method (self , datapath ):
1031- super (_EngineToConnMixin , self ).setup_method ( datapath )
1038+ def setup_method (self , load_iris_data ):
1039+ super (_EngineToConnMixin , self ).load_test_data_and_sql ( )
10321040 engine = self .conn
10331041 conn = engine .connect ()
10341042 self .__tx = conn .begin ()
@@ -1153,14 +1161,14 @@ def setup_class(cls):
11531161 msg = "{0} - can't connect to {1} server" .format (cls , cls .flavor )
11541162 pytest .skip (msg )
11551163
1156- @pytest .fixture (autouse = True )
1157- def setup_method (self , datapath ):
1158- self .setup_connect ()
1159-
1160- self ._load_iris_data (datapath )
1164+ def load_test_data_and_sql (self ):
11611165 self ._load_raw_sql ()
11621166 self ._load_test1_data ()
11631167
1168+ @pytest .fixture (autouse = True )
1169+ def setup_method (self , load_iris_data ):
1170+ self .load_test_data_and_sql ()
1171+
11641172 @classmethod
11651173 def setup_import (cls ):
11661174 # Skip this test if SQLAlchemy not available
@@ -1925,15 +1933,17 @@ class TestSQLiteFallback(SQLiteMixIn, PandasSQLTest):
19251933 def connect (cls ):
19261934 return sqlite3 .connect (':memory:' )
19271935
1928- @pytest .fixture (autouse = True )
1929- def setup_method (self , datapath ):
1936+ def setup_connect (self ):
19301937 self .conn = self .connect ()
1931- self .pandasSQL = sql .SQLiteDatabase (self .conn )
1932-
1933- self ._load_iris_data (datapath )
19341938
1939+ def load_test_data_and_sql (self ):
1940+ self .pandasSQL = sql .SQLiteDatabase (self .conn )
19351941 self ._load_test1_data ()
19361942
1943+ @pytest .fixture (autouse = True )
1944+ def setup_method (self , load_iris_data ):
1945+ self .load_test_data_and_sql ()
1946+
19371947 def test_read_sql (self ):
19381948 self ._read_sql_iris ()
19391949
@@ -2151,6 +2161,12 @@ def setup_method(self, request, datapath):
21512161 self .method = request .function
21522162 self .conn = sqlite3 .connect (':memory:' )
21532163
2164+ # In some test cases we may close db connection
2165+ # Re-open conn here so we can perform cleanup in teardown
2166+ yield
2167+ self .method = request .function
2168+ self .conn = sqlite3 .connect (':memory:' )
2169+
21542170 def test_basic (self ):
21552171 frame = tm .makeTimeDataFrame ()
21562172 self ._check_roundtrip (frame )
@@ -2227,7 +2243,7 @@ def test_execute_fail(self):
22272243 with pytest .raises (Exception ):
22282244 sql .execute ('INSERT INTO test VALUES("foo", "bar", 7)' , self .conn )
22292245
2230- def test_execute_closed_connection (self , request , datapath ):
2246+ def test_execute_closed_connection (self ):
22312247 create_sql = """
22322248 CREATE TABLE test
22332249 (
@@ -2246,9 +2262,6 @@ def test_execute_closed_connection(self, request, datapath):
22462262 with pytest .raises (Exception ):
22472263 tquery ("select * from test" , con = self .conn )
22482264
2249- # Initialize connection again (needed for tearDown)
2250- self .setup_method (request , datapath )
2251-
22522265 def test_na_roundtrip (self ):
22532266 pass
22542267
0 commit comments