-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The main goal of these changes is to make test_pickle.py more readable and eliminate get_pickles.py. These changes: - add dict_data, pickled_data, and pickle_file_path fixtures - rename test_pickle to test_unpickle - add test_pickle_format_stability - replace test_load_from_file with test_pickle_backward_compatibility - replace get_pickles.py with test_write_pickle_file
- Loading branch information
Showing
6 changed files
with
77 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Pickle tests have been refactored in order | ||
to make ``test_pickle.py`` more readable | ||
and eliminate ``get_pickles.py``. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,50 @@ | ||
import os | ||
import pickle | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
here = Path(__file__).resolve().parent | ||
WRITE_PICKLE_FILES = bool(os.environ.get("WRITE_PICKLE_FILES")) | ||
|
||
|
||
def test_pickle(any_multidict_class, pickle_protocol): | ||
d = any_multidict_class([("a", 1), ("a", 2)]) | ||
pbytes = pickle.dumps(d, pickle_protocol) | ||
obj = pickle.loads(pbytes) | ||
assert d == obj | ||
assert isinstance(obj, any_multidict_class) | ||
def test_unpickle(any_multidict_class, dict_data, pickled_data): | ||
expected = any_multidict_class(dict_data) | ||
actual = pickle.loads(pickled_data) | ||
assert actual == expected | ||
assert isinstance(actual, any_multidict_class) | ||
|
||
|
||
def test_pickle_proxy(any_multidict_class, any_multidict_proxy_class): | ||
d = any_multidict_class([("a", 1), ("a", 2)]) | ||
def test_pickle_proxy(any_multidict_class, any_multidict_proxy_class, dict_data): | ||
d = any_multidict_class(dict_data) | ||
proxy = any_multidict_proxy_class(d) | ||
with pytest.raises(TypeError): | ||
pickle.dumps(proxy) | ||
|
||
|
||
def test_load_from_file(any_multidict_class, multidict_implementation, pickle_protocol): | ||
multidict_class_name = any_multidict_class.__name__ | ||
pickle_file_basename = "-".join( | ||
( | ||
multidict_class_name.lower(), | ||
multidict_implementation.tag, | ||
) | ||
) | ||
d = any_multidict_class([("a", 1), ("a", 2)]) | ||
fname = f"{pickle_file_basename}.pickle.{pickle_protocol}" | ||
p = here / fname | ||
with p.open("rb") as f: | ||
obj = pickle.load(f) | ||
assert d == obj | ||
assert isinstance(obj, any_multidict_class) | ||
def test_pickle_format_stability(pickled_data, pickle_file_path, pickle_protocol): | ||
if pickle_protocol == 0: | ||
# TODO: consider updating pickle files | ||
pytest.skip(reason="Format for pickle protocol 0 is changed, it's a known fact") | ||
expected = pickle_file_path.read_bytes() | ||
assert pickled_data == expected | ||
|
||
|
||
def test_pickle_backward_compatibility( | ||
any_multidict_class, | ||
dict_data, | ||
pickle_file_path, | ||
): | ||
expected = any_multidict_class(dict_data) | ||
with pickle_file_path.open("rb") as f: | ||
actual = pickle.load(f) | ||
|
||
assert actual == expected | ||
assert isinstance(actual, any_multidict_class) | ||
|
||
|
||
@pytest.mark.skipif( | ||
not WRITE_PICKLE_FILES, | ||
reason="This is a helper that writes pickle test files", | ||
) | ||
def test_write_pickle_file(pickled_data: bytes, pickle_file_path: Path) -> None: | ||
pickle_file_path.write_bytes(pickled_data) |