From 1e6cc7deabf55a1a1a9982e41652cded5588fd9d Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Sun, 26 Aug 2018 19:38:49 +0100 Subject: [PATCH 1/8] Avoid calling pytest fixtures directly --- pandas/tests/io/test_sql.py | 46 ++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index 824e5a2b23df3..fa80bb5c9cdf5 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -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]) @@ -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() @@ -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() @@ -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 @@ -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() @@ -2637,4 +2647,4 @@ def clean_up(test_table_to_drop): if_exists='append', index=False) assert (tquery(sql_select, con=self.conn) == [(1, 'A'), (2, 'B'), (3, 'C'), (4, 'D'), (5, 'E')]) - clean_up(table_name) + clean_up(table_name) \ No newline at end of file From 0f30be71db4ead3cc5983b83c634b64c28fd0dce Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Sun, 26 Aug 2018 19:43:38 +0100 Subject: [PATCH 2/8] Add newline --- pandas/tests/io/test_sql.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index fa80bb5c9cdf5..a6f89f26261e0 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -2647,4 +2647,4 @@ def clean_up(test_table_to_drop): if_exists='append', index=False) assert (tquery(sql_select, con=self.conn) == [(1, 'A'), (2, 'B'), (3, 'C'), (4, 'D'), (5, 'E')]) - clean_up(table_name) \ No newline at end of file + clean_up(table_name) From 84df6eadcf740b2ef7fc8cff58cb52fd5937f633 Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Tue, 28 Aug 2018 22:33:33 +0100 Subject: [PATCH 3/8] Rename method --- pandas/tests/io/test_sql.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index a6f89f26261e0..fc3ff983c95d1 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -254,7 +254,7 @@ def _get_exec(self): return self.conn.cursor() @pytest.fixture(params=[('io', 'data', 'iris.csv')]) - def _load_iris_data(self, datapath, request): + def load_iris_data(self, datapath, request): import io iris_csv_file = datapath(*request.param) @@ -511,7 +511,7 @@ def setup_connect(self): self.conn = self.connect() @pytest.fixture(autouse=True) - def setup_method(self, _load_iris_data): + def setup_method(self, load_iris_data): self.load_test_data_and_sql() def load_test_data_and_sql(self): @@ -1035,7 +1035,7 @@ class _EngineToConnMixin(object): """ @pytest.fixture(autouse=True) - def setup_method(self, _load_iris_data): + def setup_method(self, load_iris_data): super(_EngineToConnMixin, self).load_test_data_and_sql() engine = self.conn conn = engine.connect() @@ -1166,7 +1166,7 @@ def load_test_data_and_sql(self): self._load_test1_data() @pytest.fixture(autouse=True) - def setup_method(self, _load_iris_data): + def setup_method(self, load_iris_data): self.load_test_data_and_sql() @classmethod @@ -1941,7 +1941,7 @@ def load_test_data_and_sql(self): self._load_test1_data() @pytest.fixture(autouse=True) - def setup_method(self, _load_iris_data): + def setup_method(self, load_iris_data): self.load_test_data_and_sql() def test_read_sql(self): From d6df77fc6a4287fd1dfa3a8033137b207469d78a Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Sun, 26 Aug 2018 19:38:49 +0100 Subject: [PATCH 4/8] Avoid calling pytest fixtures directly --- pandas/tests/io/test_sql.py | 46 ++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index 824e5a2b23df3..fa80bb5c9cdf5 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -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]) @@ -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() @@ -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() @@ -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 @@ -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() @@ -2637,4 +2647,4 @@ def clean_up(test_table_to_drop): if_exists='append', index=False) assert (tquery(sql_select, con=self.conn) == [(1, 'A'), (2, 'B'), (3, 'C'), (4, 'D'), (5, 'E')]) - clean_up(table_name) + clean_up(table_name) \ No newline at end of file From bb905db6cb18e2bb8d3d5e1af066b5eba7a5390b Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Sun, 26 Aug 2018 19:43:38 +0100 Subject: [PATCH 5/8] Add newline --- pandas/tests/io/test_sql.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index fa80bb5c9cdf5..a6f89f26261e0 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -2647,4 +2647,4 @@ def clean_up(test_table_to_drop): if_exists='append', index=False) assert (tquery(sql_select, con=self.conn) == [(1, 'A'), (2, 'B'), (3, 'C'), (4, 'D'), (5, 'E')]) - clean_up(table_name) \ No newline at end of file + clean_up(table_name) From 816e855c517ae0cd488c5ad062ac2324249c7537 Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Tue, 28 Aug 2018 22:33:33 +0100 Subject: [PATCH 6/8] Rename method --- pandas/tests/io/test_sql.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index a6f89f26261e0..fc3ff983c95d1 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -254,7 +254,7 @@ def _get_exec(self): return self.conn.cursor() @pytest.fixture(params=[('io', 'data', 'iris.csv')]) - def _load_iris_data(self, datapath, request): + def load_iris_data(self, datapath, request): import io iris_csv_file = datapath(*request.param) @@ -511,7 +511,7 @@ def setup_connect(self): self.conn = self.connect() @pytest.fixture(autouse=True) - def setup_method(self, _load_iris_data): + def setup_method(self, load_iris_data): self.load_test_data_and_sql() def load_test_data_and_sql(self): @@ -1035,7 +1035,7 @@ class _EngineToConnMixin(object): """ @pytest.fixture(autouse=True) - def setup_method(self, _load_iris_data): + def setup_method(self, load_iris_data): super(_EngineToConnMixin, self).load_test_data_and_sql() engine = self.conn conn = engine.connect() @@ -1166,7 +1166,7 @@ def load_test_data_and_sql(self): self._load_test1_data() @pytest.fixture(autouse=True) - def setup_method(self, _load_iris_data): + def setup_method(self, load_iris_data): self.load_test_data_and_sql() @classmethod @@ -1941,7 +1941,7 @@ def load_test_data_and_sql(self): self._load_test1_data() @pytest.fixture(autouse=True) - def setup_method(self, _load_iris_data): + def setup_method(self, load_iris_data): self.load_test_data_and_sql() def test_read_sql(self): From 27c413330018fb442a9f7a52eff6ff6365b47469 Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Wed, 5 Sep 2018 01:27:29 +0100 Subject: [PATCH 7/8] Remove final fixture warning --- pandas/tests/io/test_sql.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index fc3ff983c95d1..e4df7043919ae 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -2161,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) @@ -2237,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 ( @@ -2256,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 From 0d8d239896a04b196bb826e02099484b135986ac Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Wed, 5 Sep 2018 01:30:04 +0100 Subject: [PATCH 8/8] Remove final fixture warning --- pandas/tests/io/test_sql.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index fc3ff983c95d1..e4df7043919ae 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -2161,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) @@ -2237,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 ( @@ -2256,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