73 lines
2.0 KiB
Python
73 lines
2.0 KiB
Python
"""
|
|
module containing a parametrized tests testing cross-python
|
|
serialization via the pickle module.
|
|
"""
|
|
import distutils.spawn
|
|
import subprocess
|
|
import textwrap
|
|
|
|
import pytest
|
|
|
|
pythonlist = ["python2.7", "python3.4", "python3.5"]
|
|
|
|
|
|
@pytest.fixture(params=pythonlist)
|
|
def python1(request, tmpdir):
|
|
picklefile = tmpdir.join("data.pickle")
|
|
return Python(request.param, picklefile)
|
|
|
|
|
|
@pytest.fixture(params=pythonlist)
|
|
def python2(request, python1):
|
|
return Python(request.param, python1.picklefile)
|
|
|
|
|
|
class Python(object):
|
|
def __init__(self, version, picklefile):
|
|
self.pythonpath = distutils.spawn.find_executable(version)
|
|
if not self.pythonpath:
|
|
pytest.skip("{!r} not found".format(version))
|
|
self.picklefile = picklefile
|
|
|
|
def dumps(self, obj):
|
|
dumpfile = self.picklefile.dirpath("dump.py")
|
|
dumpfile.write(
|
|
textwrap.dedent(
|
|
r"""
|
|
import pickle
|
|
f = open({!r}, 'wb')
|
|
s = pickle.dump({!r}, f, protocol=2)
|
|
f.close()
|
|
""".format(
|
|
str(self.picklefile), obj
|
|
)
|
|
)
|
|
)
|
|
subprocess.check_call((self.pythonpath, str(dumpfile)))
|
|
|
|
def load_and_is_true(self, expression):
|
|
loadfile = self.picklefile.dirpath("load.py")
|
|
loadfile.write(
|
|
textwrap.dedent(
|
|
r"""
|
|
import pickle
|
|
f = open({!r}, 'rb')
|
|
obj = pickle.load(f)
|
|
f.close()
|
|
res = eval({!r})
|
|
if not res:
|
|
raise SystemExit(1)
|
|
""".format(
|
|
str(self.picklefile), expression
|
|
)
|
|
)
|
|
)
|
|
print(loadfile)
|
|
subprocess.check_call((self.pythonpath, str(loadfile)))
|
|
|
|
|
|
@pytest.mark.parametrize("obj", [42, {}, {1: 3}])
|
|
def test_basic_objects(python1, python2, obj):
|
|
python1.dumps(obj)
|
|
python2.load_and_is_true("obj == %s" % obj)
|