Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor pytests to make pickle testing in-memory #924

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
14 changes: 0 additions & 14 deletions tests/cimultidict-c-extension.pickle.0

This file was deleted.

Binary file removed tests/cimultidict-c-extension.pickle.1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this pickle file hasn't been removed. It needs to be removed and probably added to tests/.gitignore.

There's more files missed like this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I think the file was removed the first time around. But I think Github shows the diffs for binary files that don't end with the extension .0 as a bit different for the other files. I tried adding them back, checked that there were no diffs and then deleted them again using git rm and I do see the files being shown in the delete mode

 delete mode 100644 tests/multidict-pure-python.pickle.1
 delete mode 100644 tests/multidict-pure-python.pickle.2
 delete mode 100644 tests/multidict-pure-python.pickle.3
 delete mode 100644 tests/multidict-pure-python.pickle.4
 delete mode 100644 tests/multidict-pure-python.pickle.5

But the UI shows the same. Let me know if you have any other ideas on how to address this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vrdn-23 your console snippet shows a multidict file, not cimultidict.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try git rm -f tests/*.pickle.*.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess, it might be GitHub's poor data representation, after all (although, I haven't checked in detail).

Anyway, per https://github.com/aio-libs/multidict/pull/924/files#r1463633143, we might need to keep the files, after all.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. I'm following the different threads but I'm not sure what the final resolution is. What is the final specification/requirements we want from this change? Could you update #922 with the new changes/ set of requirements that we need?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I'll try to circle back later.

Binary file not shown.
Binary file removed tests/cimultidict-c-extension.pickle.2
Binary file not shown.
Binary file removed tests/cimultidict-c-extension.pickle.3
Binary file not shown.
Binary file removed tests/cimultidict-c-extension.pickle.4
Binary file not shown.
Binary file removed tests/cimultidict-c-extension.pickle.5
Binary file not shown.
14 changes: 0 additions & 14 deletions tests/cimultidict-pure-python.pickle.0

This file was deleted.

Binary file removed tests/cimultidict-pure-python.pickle.1
Binary file not shown.
Binary file removed tests/cimultidict-pure-python.pickle.2
Binary file not shown.
Binary file removed tests/cimultidict-pure-python.pickle.3
Binary file not shown.
Binary file removed tests/cimultidict-pure-python.pickle.4
Binary file not shown.
Binary file removed tests/cimultidict-pure-python.pickle.5
Binary file not shown.
24 changes: 23 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from importlib import import_module
from sys import version_info as _version_info
from types import ModuleType
from typing import Callable, Type
from typing import Any, Callable, Type
vrdn-23 marked this conversation as resolved.
Show resolved Hide resolved

try:
from functools import cached_property # Python 3.8+
Expand Down Expand Up @@ -158,6 +158,28 @@ def multidict_getversion_callable(multidict_module: ModuleType) -> Callable:
return multidict_module.getversion


def write(cls: Any, proto: int) -> bytes:
vrdn-23 marked this conversation as resolved.
Show resolved Hide resolved
d = cls([("a", 1), ("a", 2)])
return pickle.dumps(d, proto)


@pytest.fixture
def in_memory_pickle_classes(multidict_module: ModuleType) -> dict[str, bytes]:
vrdn-23 marked this conversation as resolved.
Show resolved Hide resolved
"""Generates a dict for in-memory storage of pickled classes"""
vrdn-23 marked this conversation as resolved.
Show resolved Hide resolved
pickle_dict = {}
_impl_map = {
"c-extension": "_multidict",
"pure-python": "_multidict_py",
}
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
for tag, impl_name in _impl_map.items():
impl = import_module(f"multidict.{impl_name}")
for cls in impl.CIMultiDict, impl.MultiDict:
file_key = f"{cls.__name__.lower()}-{tag}.pickle.{proto}"
pickle_dict[file_key] = write(cls, proto)
return pickle_dict


def pytest_addoption(
parser: pytest.Parser,
pluginmanager: pytest.PytestPluginManager,
Expand Down
28 changes: 0 additions & 28 deletions tests/gen_pickles.py

This file was deleted.

14 changes: 0 additions & 14 deletions tests/multidict-c-extension.pickle.0
webknjaz marked this conversation as resolved.
Show resolved Hide resolved

This file was deleted.

Binary file removed tests/multidict-c-extension.pickle.1
Binary file not shown.
Binary file removed tests/multidict-c-extension.pickle.2
Binary file not shown.
Binary file removed tests/multidict-c-extension.pickle.3
Binary file not shown.
Binary file removed tests/multidict-c-extension.pickle.4
Binary file not shown.
Binary file removed tests/multidict-c-extension.pickle.5
Binary file not shown.
14 changes: 0 additions & 14 deletions tests/multidict-pure-python.pickle.0

This file was deleted.

Binary file removed tests/multidict-pure-python.pickle.1
Binary file not shown.
Binary file removed tests/multidict-pure-python.pickle.2
Binary file not shown.
Binary file removed tests/multidict-pure-python.pickle.3
Binary file not shown.
Binary file removed tests/multidict-pure-python.pickle.4
Binary file not shown.
Binary file removed tests/multidict-pure-python.pickle.5
Binary file not shown.
11 changes: 7 additions & 4 deletions tests/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ def test_pickle_proxy(any_multidict_class, any_multidict_proxy_class):
pickle.dumps(proxy)


def test_load_from_file(any_multidict_class, multidict_implementation, pickle_protocol):
def test_load_from_file(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test might need renaming since it no longer uses a file on disk.
Also, it seems like test_pickle might cover this case already.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OTOH, upon further reflection, I think I might know why the actual files are committed in Git — this is to ensure that newer multidict versions can load pickles of the older ones.
So we might be treating this incorrectly.

We might need to have a CLI interface for making the files in pytest but keep them. A fixture w/o an in-memory strucutre would be useful, though.

any_multidict_class,
multidict_implementation,
in_memory_pickle_classes,
pickle_protocol,
):
multidict_class_name = any_multidict_class.__name__
pickle_file_basename = "-".join(
(
Expand All @@ -31,8 +36,6 @@ def test_load_from_file(any_multidict_class, multidict_implementation, pickle_pr
)
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)
obj = pickle.loads(in_memory_pickle_classes[fname])
assert d == obj
assert isinstance(obj, any_multidict_class)
Loading