Skip to content

Commit ed4b512

Browse files
Debian Science Teamrebecca-palmer
Debian Science Team
authored andcommitted
pytest_fixtures
This is not allowed in recent pytest: https://tests.reproducible-builds.org/debian/rbuild/unstable/amd64/pandas_0.23.3+dfsg-3.rbuild.log.gz https://ci.debian.net/data/autopkgtest/testing/amd64/p/pandas/2954485/log.gz Origin: upstream b7e5704be86cb44707ae9be372129e521c35e0d0 + 1a12c41d201f56439510e683fadfed1218ea9067 Author: alimcmaster1, Tom Augspurger Bug: pandas-dev/pandas#22338 Gbp-Pq: Name pytest_fixtures.patch
1 parent ae860f5 commit ed4b512

File tree

4 files changed

+96
-35
lines changed

4 files changed

+96
-35
lines changed

pandas/tests/groupby/test_whitelist.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,15 @@ def df_letters():
123123
return df
124124

125125

126-
@pytest.mark.parametrize(
127-
"obj, whitelist", zip((df_letters(), df_letters().floats),
128-
(df_whitelist, s_whitelist)))
129-
def test_groupby_whitelist(df_letters, obj, whitelist):
126+
@pytest.mark.parametrize("whitelist", [df_whitelist, s_whitelist])
127+
def test_groupby_whitelist(df_letters, whitelist):
130128
df = df_letters
129+
if whitelist == df_whitelist:
130+
# dataframe
131+
obj = df_letters
132+
else:
133+
obj = df_letters['floats']
134+
131135

132136
# these are aliases so ok to have the alias __name__
133137
alias = {'bfill': 'backfill',

pandas/tests/indexes/datetimes/test_tools.py

+19-6
Original file line numberDiff line numberDiff line change
@@ -1491,12 +1491,25 @@ def units_from_epochs():
14911491
return list(range(5))
14921492

14931493

1494-
@pytest.fixture(params=[epoch_1960(),
1495-
epoch_1960().to_pydatetime(),
1496-
epoch_1960().to_datetime64(),
1497-
str(epoch_1960())])
1498-
def epochs(request):
1499-
return request.param
1494+
@pytest.fixture(params=['timestamp', 'pydatetime', 'datetime64', 'str_1960'])
1495+
def epochs(epoch_1960, request):
1496+
"""Timestamp at 1960-01-01 in various forms.
1497+
1498+
* pd.Timestamp
1499+
* datetime.datetime
1500+
* numpy.datetime64
1501+
* str
1502+
"""
1503+
assert request.param in {'timestamp', 'pydatetime', 'datetime64',
1504+
"str_1960"}
1505+
if request.param == 'timestamp':
1506+
return epoch_1960
1507+
elif request.param == 'pydatetime':
1508+
return epoch_1960.to_pydatetime()
1509+
elif request.param == "datetime64":
1510+
return epoch_1960.to_datetime64()
1511+
else:
1512+
return str(epoch_1960)
15001513

15011514

15021515
@pytest.fixture

pandas/tests/io/test_sql.py

+34-21
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,13 @@ def _get_exec(self):
254254
else:
255255
return self.conn.cursor()
256256

257-
def _load_iris_data(self, datapath):
257+
@pytest.fixture(params=[('io', 'data', 'iris.csv')])
258+
def load_iris_data(self, datapath, request):
258259
import io
259-
iris_csv_file = datapath('io', 'data', 'iris.csv')
260+
iris_csv_file = datapath(*request.param)
261+
262+
if not hasattr(self, 'conn'):
263+
self.setup_connect()
260264

261265
self.drop_table('iris')
262266
self._get_exec().execute(SQL_STRINGS['create_iris'][self.flavor])
@@ -504,10 +508,14 @@ class _TestSQLApi(PandasSQLTest):
504508
flavor = 'sqlite'
505509
mode = None
506510

507-
@pytest.fixture(autouse=True)
508-
def setup_method(self, datapath):
511+
def setup_connect(self):
509512
self.conn = self.connect()
510-
self._load_iris_data(datapath)
513+
514+
@pytest.fixture(autouse=True)
515+
def setup_method(self, load_iris_data):
516+
self.load_test_data_and_sql()
517+
518+
def load_test_data_and_sql(self):
511519
self._load_iris_view()
512520
self._load_test1_data()
513521
self._load_test2_data()
@@ -1028,8 +1036,8 @@ class _EngineToConnMixin(object):
10281036
"""
10291037

10301038
@pytest.fixture(autouse=True)
1031-
def setup_method(self, datapath):
1032-
super(_EngineToConnMixin, self).setup_method(datapath)
1039+
def setup_method(self, load_iris_data):
1040+
super(_EngineToConnMixin, self).load_test_data_and_sql()
10331041
engine = self.conn
10341042
conn = engine.connect()
10351043
self.__tx = conn.begin()
@@ -1154,14 +1162,14 @@ def setup_class(cls):
11541162
msg = "{0} - can't connect to {1} server".format(cls, cls.flavor)
11551163
pytest.skip(msg)
11561164

1157-
@pytest.fixture(autouse=True)
1158-
def setup_method(self, datapath):
1159-
self.setup_connect()
1160-
1161-
self._load_iris_data(datapath)
1165+
def load_test_data_and_sql(self):
11621166
self._load_raw_sql()
11631167
self._load_test1_data()
11641168

1169+
@pytest.fixture(autouse=True)
1170+
def setup_method(self, load_iris_data):
1171+
self.load_test_data_and_sql()
1172+
11651173
@classmethod
11661174
def setup_import(cls):
11671175
# Skip this test if SQLAlchemy not available
@@ -1926,15 +1934,17 @@ class TestSQLiteFallback(SQLiteMixIn, PandasSQLTest):
19261934
def connect(cls):
19271935
return sqlite3.connect(':memory:')
19281936

1929-
@pytest.fixture(autouse=True)
1930-
def setup_method(self, datapath):
1937+
def setup_connect(self):
19311938
self.conn = self.connect()
1932-
self.pandasSQL = sql.SQLiteDatabase(self.conn)
1933-
1934-
self._load_iris_data(datapath)
19351939

1940+
def load_test_data_and_sql(self):
1941+
self.pandasSQL = sql.SQLiteDatabase(self.conn)
19361942
self._load_test1_data()
19371943

1944+
@pytest.fixture(autouse=True)
1945+
def setup_method(self, load_iris_data):
1946+
self.load_test_data_and_sql()
1947+
19381948
def test_read_sql(self):
19391949
self._read_sql_iris()
19401950

@@ -2147,6 +2157,12 @@ def setup_method(self, request, datapath):
21472157
self.method = request.function
21482158
self.conn = sqlite3.connect(':memory:')
21492159

2160+
# In some test cases we may close db connection
2161+
# Re-open conn here so we can perform cleanup in teardown
2162+
yield
2163+
self.method = request.function
2164+
self.conn = sqlite3.connect(':memory:')
2165+
21502166
def test_basic(self):
21512167
frame = tm.makeTimeDataFrame()
21522168
self._check_roundtrip(frame)
@@ -2223,7 +2239,7 @@ def test_execute_fail(self):
22232239
with pytest.raises(Exception):
22242240
sql.execute('INSERT INTO test VALUES("foo", "bar", 7)', self.conn)
22252241

2226-
def test_execute_closed_connection(self, request, datapath):
2242+
def test_execute_closed_connection(self):
22272243
create_sql = """
22282244
CREATE TABLE test
22292245
(
@@ -2242,9 +2258,6 @@ def test_execute_closed_connection(self, request, datapath):
22422258
with pytest.raises(Exception):
22432259
tquery("select * from test", con=self.conn)
22442260

2245-
# Initialize connection again (needed for tearDown)
2246-
self.setup_method(request, datapath)
2247-
22482261
def test_na_roundtrip(self):
22492262
pass
22502263

pandas/tests/series/test_analytics.py

+35-4
Original file line numberDiff line numberDiff line change
@@ -1850,8 +1850,35 @@ def test_value_counts_categorical_not_ordered(self):
18501850
tm.assert_series_equal(idx.value_counts(normalize=True), exp)
18511851

18521852

1853+
main_dtypes = [
1854+
'datetime',
1855+
'datetimetz',
1856+
'timedelta',
1857+
'int8',
1858+
'int16',
1859+
'int32',
1860+
'int64',
1861+
'float32',
1862+
'float64',
1863+
'uint8',
1864+
'uint16',
1865+
'uint32',
1866+
'uint64'
1867+
]
1868+
1869+
18531870
@pytest.fixture
18541871
def s_main_dtypes():
1872+
"""A DataFrame with many dtypes
1873+
1874+
* datetime
1875+
* datetimetz
1876+
* timedelta
1877+
* [u]int{8,16,32,64}
1878+
* float{32,64}
1879+
1880+
The columns are the name of the dtype.
1881+
"""
18551882
df = pd.DataFrame(
18561883
{'datetime': pd.to_datetime(['2003', '2002',
18571884
'2001', '2002',
@@ -1871,6 +1898,12 @@ def s_main_dtypes():
18711898
return df
18721899

18731900

1901+
@pytest.fixture(params=main_dtypes)
1902+
def s_main_dtypes_split(request, s_main_dtypes):
1903+
"""Each series in s_main_dtypes."""
1904+
return s_main_dtypes[request.param]
1905+
1906+
18741907
def assert_check_nselect_boundary(vals, dtype, method):
18751908
# helper function for 'test_boundary_{dtype}' tests
18761909
s = Series(vals, dtype=dtype)
@@ -1900,12 +1933,10 @@ def test_error(self, r):
19001933
with tm.assert_raises_regex(TypeError, msg):
19011934
method(arg)
19021935

1903-
@pytest.mark.parametrize(
1904-
"s",
1905-
[v for k, v in s_main_dtypes().iteritems()])
1906-
def test_nsmallest_nlargest(self, s):
1936+
def test_nsmallest_nlargest(self, s_main_dtypes_split):
19071937
# float, int, datetime64 (use i8), timedelts64 (same),
19081938
# object that are numbers, object that are strings
1939+
s = s_main_dtypes_split
19091940

19101941
assert_series_equal(s.nsmallest(2), s.iloc[[2, 1]])
19111942
assert_series_equal(s.nsmallest(2, keep='last'), s.iloc[[2, 3]])

0 commit comments

Comments
 (0)