Skip to content

Commit 3cf4583

Browse files
committed
cache pickles instead of using pickle files
1 parent c5f7a43 commit 3cf4583

File tree

4 files changed

+44
-45
lines changed

4 files changed

+44
-45
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ repos:
128128
- lxml # dep of `--txt-report`, `--cobertura-xml-report` & `--html-report`
129129
- pytest >= 8.4.0
130130
- pytest_codspeed
131+
- pytest-harvest >= 1.10.5
131132
- Sphinx >= 5.3.0
132133
- sphinxcontrib-spelling
133134
- types-psutil
@@ -145,6 +146,7 @@ repos:
145146
- types-docutils
146147
- lxml # dep of `--txt-report`, `--cobertura-xml-report` & `--html-report`
147148
- pytest >= 8.4.0
149+
- pytest-harvest >= 1.10.5
148150
- pytest_codspeed
149151
- Sphinx >= 5.3.0
150152
- sphinxcontrib-spelling

requirements/pytest.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ pytest==8.4.2
44
pytest-codspeed==4.2.0
55
pytest-cov==6.1.0
66
psutil==7.1.1
7+
pytest-harvest==1.10.5

tests/conftest.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
from typing import Callable, Type, Union
1010

1111
import pytest
12+
from pytest_harvest import saved_fixture
1213

1314
from multidict import (
1415
CIMultiDict,
1516
MultiDict,
1617
MultiDictProxy,
1718
MultiMapping,
1819
MutableMultiMapping,
20+
istr,
1921
)
2022

2123
C_EXT_MARK = pytest.mark.c_extension
@@ -156,6 +158,33 @@ def multidict_getversion_callable(
156158
return multidict_module.getversion # type: ignore[no-any-return]
157159

158160

161+
@pytest.fixture(scope="session", params=range(pickle.HIGHEST_PROTOCOL + 1), ids=str)
162+
def pickle_protocol(request: pytest.FixtureRequest) -> int:
163+
return request.param # type: ignore[no-any-return]
164+
165+
166+
@pytest.fixture(scope="session")
167+
@saved_fixture
168+
def pickle_protocol_multidict(
169+
pickle_protocol: int,
170+
any_multidict_class: type[MultiDict[int]],
171+
) -> tuple[bytes, type[MultiDict[int]]]:
172+
return pickle.dumps(
173+
any_multidict_class([("a", 1), ("a", 2)]), pickle_protocol
174+
), any_multidict_class
175+
176+
177+
@pytest.fixture(scope="session")
178+
@saved_fixture
179+
def pickle_protocol_istr(
180+
pickle_protocol: int,
181+
case_insensitive_str_class: type[istr],
182+
) -> tuple[bytes, type[istr]]:
183+
return pickle.dumps(
184+
case_insensitive_str_class("str"), pickle_protocol
185+
), case_insensitive_str_class
186+
187+
159188
def pytest_addoption(
160189
parser: pytest.Parser,
161190
pluginmanager: pytest.PytestPluginManager,
@@ -206,10 +235,3 @@ def pytest_configure(config: pytest.Config) -> None:
206235
"markers",
207236
f"{C_EXT_MARK.name}: tests running against the C-extension implementation.",
208237
)
209-
210-
211-
def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
212-
if "pickle_protocol" in metafunc.fixturenames:
213-
metafunc.parametrize(
214-
"pickle_protocol", list(range(pickle.HIGHEST_PROTOCOL + 1)), scope="session"
215-
)

tests/test_pickle.py

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
import pickle
22
from pathlib import Path
3-
from typing import TYPE_CHECKING
4-
53
import pytest
64

75
from multidict import MultiDict, MultiDictProxy, istr
86

9-
if TYPE_CHECKING:
10-
from conftest import MultidictImplementation
11-
127
here = Path(__file__).resolve().parent
138

149

@@ -43,42 +38,21 @@ def test_pickle_istr(
4338

4439

4540
def test_load_from_file(
46-
any_multidict_class: type[MultiDict[int]],
47-
multidict_implementation: "MultidictImplementation",
48-
pickle_protocol: int,
41+
# any_multidict_class: type[MultiDict[int]],
42+
# multidict_implementation: "MultidictImplementation",
43+
# pickle_protocol: int,
44+
pickle_protocol_multidict: tuple[bytes, type[MultiDict[int]]],
4945
) -> None:
50-
multidict_class_name = any_multidict_class.__name__
51-
pickle_file_basename = "-".join(
52-
(
53-
multidict_class_name.lower(),
54-
multidict_implementation.tag,
55-
)
56-
)
46+
buf, any_multidict_class = pickle_protocol_multidict
5747
d = any_multidict_class([("a", 1), ("a", 2)])
58-
fname = f"{pickle_file_basename}.pickle.{pickle_protocol}"
59-
p = here / fname
60-
with p.open("rb") as f:
61-
obj = pickle.load(f)
48+
obj = pickle.loads(buf)
6249
assert d == obj
6350
assert isinstance(obj, any_multidict_class)
6451

6552

66-
def test_load_istr_from_file(
67-
case_insensitive_str_class: type[istr],
68-
multidict_implementation: "MultidictImplementation",
69-
pickle_protocol: int,
70-
) -> None:
71-
istr_class_name = case_insensitive_str_class.__name__
72-
pickle_file_basename = "-".join(
73-
(
74-
istr_class_name.lower(),
75-
multidict_implementation.tag,
76-
)
77-
)
78-
s = case_insensitive_str_class("str")
79-
fname = f"{pickle_file_basename}.pickle.{pickle_protocol}"
80-
p = here / fname
81-
with p.open("rb") as f:
82-
obj = pickle.load(f)
83-
assert s == obj
84-
assert isinstance(obj, case_insensitive_str_class)
53+
def test_load_istr_from_file(pickle_protocol_istr: tuple[bytes, type[istr]]) -> None:
54+
buf, case_incasive_str = pickle_protocol_istr
55+
d = case_incasive_str("str")
56+
obj = pickle.loads(buf)
57+
assert d == obj
58+
assert isinstance(obj, case_incasive_str)

0 commit comments

Comments
 (0)