Skip to content

Commit

Permalink
Fix test_sql pytest fixture warnings (pandas-dev#22515)
Browse files Browse the repository at this point in the history
* Avoid calling pytest fixtures directly
  • Loading branch information
alimcmaster1 authored and victor committed Sep 30, 2018
1 parent 9d069ba commit 45283e0
Showing 1 changed file with 34 additions and 21 deletions.
55 changes: 34 additions & 21 deletions pandas/tests/io/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,13 @@ def _get_exec(self):
else:
return self.conn.cursor()

def _load_iris_data(self, datapath):
@pytest.fixture(params=[('io', 'data', 'iris.csv')])
def load_iris_data(self, datapath, request):
import io
iris_csv_file = datapath('io', 'data', 'iris.csv')
iris_csv_file = datapath(*request.param)

if not hasattr(self, 'conn'):
self.setup_connect()

self.drop_table('iris')
self._get_exec().execute(SQL_STRINGS['create_iris'][self.flavor])
Expand Down Expand Up @@ -503,10 +507,14 @@ class _TestSQLApi(PandasSQLTest):
flavor = 'sqlite'
mode = None

@pytest.fixture(autouse=True)
def setup_method(self, datapath):
def setup_connect(self):
self.conn = self.connect()
self._load_iris_data(datapath)

@pytest.fixture(autouse=True)
def setup_method(self, load_iris_data):
self.load_test_data_and_sql()

def load_test_data_and_sql(self):
self._load_iris_view()
self._load_test1_data()
self._load_test2_data()
Expand Down Expand Up @@ -1027,8 +1035,8 @@ class _EngineToConnMixin(object):
"""

@pytest.fixture(autouse=True)
def setup_method(self, datapath):
super(_EngineToConnMixin, self).setup_method(datapath)
def setup_method(self, load_iris_data):
super(_EngineToConnMixin, self).load_test_data_and_sql()
engine = self.conn
conn = engine.connect()
self.__tx = conn.begin()
Expand Down Expand Up @@ -1153,14 +1161,14 @@ def setup_class(cls):
msg = "{0} - can't connect to {1} server".format(cls, cls.flavor)
pytest.skip(msg)

@pytest.fixture(autouse=True)
def setup_method(self, datapath):
self.setup_connect()

self._load_iris_data(datapath)
def load_test_data_and_sql(self):
self._load_raw_sql()
self._load_test1_data()

@pytest.fixture(autouse=True)
def setup_method(self, load_iris_data):
self.load_test_data_and_sql()

@classmethod
def setup_import(cls):
# Skip this test if SQLAlchemy not available
Expand Down Expand Up @@ -1925,15 +1933,17 @@ class TestSQLiteFallback(SQLiteMixIn, PandasSQLTest):
def connect(cls):
return sqlite3.connect(':memory:')

@pytest.fixture(autouse=True)
def setup_method(self, datapath):
def setup_connect(self):
self.conn = self.connect()
self.pandasSQL = sql.SQLiteDatabase(self.conn)

self._load_iris_data(datapath)

def load_test_data_and_sql(self):
self.pandasSQL = sql.SQLiteDatabase(self.conn)
self._load_test1_data()

@pytest.fixture(autouse=True)
def setup_method(self, load_iris_data):
self.load_test_data_and_sql()

def test_read_sql(self):
self._read_sql_iris()

Expand Down Expand Up @@ -2151,6 +2161,12 @@ def setup_method(self, request, datapath):
self.method = request.function
self.conn = sqlite3.connect(':memory:')

# In some test cases we may close db connection
# Re-open conn here so we can perform cleanup in teardown
yield
self.method = request.function
self.conn = sqlite3.connect(':memory:')

def test_basic(self):
frame = tm.makeTimeDataFrame()
self._check_roundtrip(frame)
Expand Down Expand Up @@ -2227,7 +2243,7 @@ def test_execute_fail(self):
with pytest.raises(Exception):
sql.execute('INSERT INTO test VALUES("foo", "bar", 7)', self.conn)

def test_execute_closed_connection(self, request, datapath):
def test_execute_closed_connection(self):
create_sql = """
CREATE TABLE test
(
Expand All @@ -2246,9 +2262,6 @@ def test_execute_closed_connection(self, request, datapath):
with pytest.raises(Exception):
tquery("select * from test", con=self.conn)

# Initialize connection again (needed for tearDown)
self.setup_method(request, datapath)

def test_na_roundtrip(self):
pass

Expand Down

0 comments on commit 45283e0

Please sign in to comment.