Skip to content

Commit 54af661

Browse files
authored
TST: Use more pytest fixtures (#53679)
* TST: Use more pytest fixtures * Fix sql fixture * More test fixtures
1 parent 8cf7ac1 commit 54af661

File tree

12 files changed

+100
-125
lines changed

12 files changed

+100
-125
lines changed

pandas/tests/interchange/conftest.py

-12
This file was deleted.

pandas/tests/interchange/test_impl.py

+35-35
Original file line numberDiff line numberDiff line change
@@ -16,47 +16,31 @@
1616
)
1717
from pandas.core.interchange.from_dataframe import from_dataframe
1818

19-
test_data_categorical = {
20-
"ordered": pd.Categorical(list("testdata") * 30, ordered=True),
21-
"unordered": pd.Categorical(list("testdata") * 30, ordered=False),
22-
}
2319

24-
NCOLS, NROWS = 100, 200
25-
26-
27-
def _make_data(make_one):
20+
@pytest.fixture
21+
def data_categorical():
2822
return {
29-
f"col{int((i - NCOLS / 2) % NCOLS + 1)}": [make_one() for _ in range(NROWS)]
30-
for i in range(NCOLS)
23+
"ordered": pd.Categorical(list("testdata") * 30, ordered=True),
24+
"unordered": pd.Categorical(list("testdata") * 30, ordered=False),
3125
}
3226

3327

34-
int_data = _make_data(lambda: random.randint(-100, 100))
35-
uint_data = _make_data(lambda: random.randint(1, 100))
36-
bool_data = _make_data(lambda: random.choice([True, False]))
37-
float_data = _make_data(lambda: random.random())
38-
datetime_data = _make_data(
39-
lambda: datetime(
40-
year=random.randint(1900, 2100),
41-
month=random.randint(1, 12),
42-
day=random.randint(1, 20),
43-
)
44-
)
45-
46-
string_data = {
47-
"separator data": [
48-
"abC|DeF,Hik",
49-
"234,3245.67",
50-
"gSaf,qWer|Gre",
51-
"asd3,4sad|",
52-
np.NaN,
53-
]
54-
}
28+
@pytest.fixture
29+
def string_data():
30+
return {
31+
"separator data": [
32+
"abC|DeF,Hik",
33+
"234,3245.67",
34+
"gSaf,qWer|Gre",
35+
"asd3,4sad|",
36+
np.NaN,
37+
]
38+
}
5539

5640

5741
@pytest.mark.parametrize("data", [("ordered", True), ("unordered", False)])
58-
def test_categorical_dtype(data):
59-
df = pd.DataFrame({"A": (test_data_categorical[data[0]])})
42+
def test_categorical_dtype(data, data_categorical):
43+
df = pd.DataFrame({"A": (data_categorical[data[0]])})
6044

6145
col = df.__dataframe__().get_column_by_name("A")
6246
assert col.dtype[0] == DtypeKind.CATEGORICAL
@@ -143,9 +127,25 @@ def test_bitmasks_pyarrow(offset, length, expected_values):
143127

144128

145129
@pytest.mark.parametrize(
146-
"data", [int_data, uint_data, float_data, bool_data, datetime_data]
130+
"data",
131+
[
132+
lambda: random.randint(-100, 100),
133+
lambda: random.randint(1, 100),
134+
lambda: random.random(),
135+
lambda: random.choice([True, False]),
136+
lambda: datetime(
137+
year=random.randint(1900, 2100),
138+
month=random.randint(1, 12),
139+
day=random.randint(1, 20),
140+
),
141+
],
147142
)
148143
def test_dataframe(data):
144+
NCOLS, NROWS = 10, 20
145+
data = {
146+
f"col{int((i - NCOLS / 2) % NCOLS + 1)}": [data() for _ in range(NROWS)]
147+
for i in range(NCOLS)
148+
}
149149
df = pd.DataFrame(data)
150150

151151
df2 = df.__dataframe__()
@@ -227,7 +227,7 @@ def test_mixed_missing():
227227
assert df2.get_column_by_name(col_name).null_count == 2
228228

229229

230-
def test_string():
230+
def test_string(string_data):
231231
test_str_data = string_data["separator data"] + [""]
232232
df = pd.DataFrame({"A": test_str_data})
233233
col = df.__dataframe__().get_column_by_name("A")

pandas/tests/interchange/test_spec_conformance.py

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@
77

88
import pytest
99

10+
import pandas as pd
11+
12+
13+
@pytest.fixture
14+
def df_from_dict():
15+
def maker(dct, is_categorical=False):
16+
df = pd.DataFrame(dct)
17+
return df.astype("category") if is_categorical else df
18+
19+
return maker
20+
1021

1122
@pytest.mark.parametrize(
1223
"test_data",

pandas/tests/io/excel/test_xlrd.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010

1111
xlrd = pytest.importorskip("xlrd")
1212

13-
exts = [".xls"]
1413

15-
16-
@pytest.fixture(params=exts)
14+
@pytest.fixture(params=[".xls"])
1715
def read_ext_xlrd(request):
1816
"""
1917
Valid extensions for reading Excel files with xlrd.

pandas/tests/io/formats/style/test_html.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
jinja2 = pytest.importorskip("jinja2")
1616
from pandas.io.formats.style import Styler
1717

18-
loader = jinja2.PackageLoader("pandas", "io/formats/templates")
19-
env = jinja2.Environment(loader=loader, trim_blocks=True)
18+
19+
@pytest.fixture
20+
def env():
21+
loader = jinja2.PackageLoader("pandas", "io/formats/templates")
22+
env = jinja2.Environment(loader=loader, trim_blocks=True)
23+
return env
2024

2125

2226
@pytest.fixture
@@ -31,12 +35,12 @@ def styler_mi():
3135

3236

3337
@pytest.fixture
34-
def tpl_style():
38+
def tpl_style(env):
3539
return env.get_template("html_style.tpl")
3640

3741

3842
@pytest.fixture
39-
def tpl_table():
43+
def tpl_table(env):
4044
return env.get_template("html_table.tpl")
4145

4246

pandas/tests/io/formats/test_format.py

-7
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@
2424

2525
from pandas._config import config
2626

27-
from pandas.compat import (
28-
IS64,
29-
is_platform_windows,
30-
)
31-
3227
import pandas as pd
3328
from pandas import (
3429
DataFrame,
@@ -48,8 +43,6 @@
4843
from pandas.io.formats import printing
4944
import pandas.io.formats.format as fmt
5045

51-
use_32bit_repr = is_platform_windows() or not IS64
52-
5346

5447
def get_local_am_pm():
5548
"""Return the AM and PM strings returned by strftime in current locale."""

pandas/tests/io/parser/usecols/test_parse_dates.py

-9
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@
1313
)
1414
import pandas._testing as tm
1515

16-
_msg_validate_usecols_arg = (
17-
"'usecols' must either be list-like "
18-
"of all strings, all unicode, all "
19-
"integers or a callable."
20-
)
21-
_msg_validate_usecols_names = (
22-
"Usecols do not match columns, columns expected but not found: {0}"
23-
)
24-
2516
# TODO(1.4): Change these to xfails whenever parse_dates support(which was
2617
# intentionally disable to keep small PR sizes) is added back
2718
pytestmark = pytest.mark.usefixtures("pyarrow_skip")

pandas/tests/io/parser/usecols/test_strings.py

+5-10
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@
99
from pandas import DataFrame
1010
import pandas._testing as tm
1111

12-
_msg_validate_usecols_arg = (
13-
"'usecols' must either be list-like "
14-
"of all strings, all unicode, all "
15-
"integers or a callable."
16-
)
17-
_msg_validate_usecols_names = (
18-
"Usecols do not match columns, columns expected but not found: {0}"
19-
)
20-
2112

2213
def test_usecols_with_unicode_strings(all_parsers):
2314
# see gh-13219
@@ -70,7 +61,11 @@ def test_usecols_with_mixed_encoding_strings(all_parsers, usecols):
7061
2.613230982,2,False,b
7162
3.568935038,7,False,a"""
7263
parser = all_parsers
73-
64+
_msg_validate_usecols_arg = (
65+
"'usecols' must either be list-like "
66+
"of all strings, all unicode, all "
67+
"integers or a callable."
68+
)
7469
with pytest.raises(ValueError, match=_msg_validate_usecols_arg):
7570
parser.read_csv(StringIO(data), usecols=usecols)
7671

pandas/tests/io/pytables/test_round_trip.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
)
2727
from pandas.util import _test_decorators as td
2828

29-
_default_compressor = "blosc"
30-
31-
3229
pytestmark = pytest.mark.single_cpu
3330

3431

@@ -479,7 +476,7 @@ def _make_one():
479476
def _check_roundtrip(obj, comparator, path, compression=False, **kwargs):
480477
options = {}
481478
if compression:
482-
options["complib"] = _default_compressor
479+
options["complib"] = "blosc"
483480

484481
with ensure_clean_store(path, "w", **options) as store:
485482
store["obj"] = obj
@@ -490,7 +487,7 @@ def _check_roundtrip(obj, comparator, path, compression=False, **kwargs):
490487
def _check_roundtrip_table(obj, comparator, path, compression=False):
491488
options = {}
492489
if compression:
493-
options["complib"] = _default_compressor
490+
options["complib"] = "blosc"
494491

495492
with ensure_clean_store(path, "w", **options) as store:
496493
store.put("obj", obj, format="table")

pandas/tests/io/pytables/test_store.py

-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
safe_close,
3131
)
3232

33-
_default_compressor = "blosc"
34-
3533
from pandas.io.pytables import (
3634
HDFStore,
3735
read_hdf,

0 commit comments

Comments
 (0)