Skip to content

Commit

Permalink
add pickle tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maximlt committed Jul 3, 2023
1 parent c51969d commit de38270
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ tests-full = [
"ipython",
"jsonschema",
"gmpy",
"cloudpickle",
]
lint = [
"flake8",
Expand Down Expand Up @@ -122,6 +123,7 @@ dependencies = [
"jsonschema",
"numpy",
"pandas",
"cloudpickle",
# To keep __version__ up-to-date in editable installs
"setuptools_scm",
]
Expand Down Expand Up @@ -181,6 +183,7 @@ name."^(?!pypy).*".dependencies = [
"odfpy",
"feather-format",
"pyarrow",
"cloudpickle",
]
# Only install gmpy on Linux on these version
# Only install tables (deser HDF5) on Linux on these version
Expand Down
171 changes: 171 additions & 0 deletions tests/testpickle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import pickle

import cloudpickle
import param
import pytest


def eq(o1, o2):
if not sorted(o1.param) == sorted(o2.param):
return False

for pname in o1.param:
if getattr(o1, pname) != getattr(o2, pname):
return False
return True


class P1(param.Parameterized):
x = param.Parameter()

@pytest.mark.parametrize('pickler', [cloudpickle, pickle])
def test_pickle_simple_class(pickler):
s = pickler.dumps(P1)
cls = pickler.loads(s)
assert cls is P1


@pytest.mark.parametrize('pickler', [cloudpickle, pickle])
def test_pickle_simple_instance(pickler):
p1 = P1()
s = pickler.dumps(p1)
inst = pickler.loads(s)
assert eq(p1, inst)


@pytest.mark.parametrize('pickler', [cloudpickle, pickle])
def test_pickle_simple_instance_modif_after(pickler):
p1 = P1()
s = pickler.dumps(p1)
p1.x = 'modified'
inst = pickler.loads(s)
assert not eq(p1, inst)
assert inst.x is None


class P2(param.Parameterized):
a = param.Parameter()
b = param.String()
c = param.Dynamic()
d = param.Number()
e = param.Integer()
f = param.Action()
g = param.Event()
h = param.Callable()
i = param.Tuple()
k = param.NumericTuple()
l = param.Range()
m = param.XYCoordinates()
n = param.CalendarDateRange()
o = param.DateRange()
p = param.List()
q = param.HookList()
r = param.Path()
s = param.Filename()
t = param.Foldername()
u = param.Date()
v = param.CalendarDate()
w = param.Selector()
x = param.ObjectSelector()
y = param.FileSelector()
z = param.ListSelector()
aa = param.MultiFileSelector()
ab = param.ClassSelector(class_=type(None))
ac = param.Series()
ad = param.Dict()
ae = param.DataFrame()
af = param.Array()


@pytest.mark.parametrize('pickler', [cloudpickle, pickle])
def test_pickle_all_parameters_class(pickler):
s = pickler.dumps(P2)
cls = pickler.loads(s)
assert cls is P2


@pytest.mark.parametrize('pickler', [cloudpickle, pickle])
def test_pickle_all_parameters_instance(pickler):
p = P2()
s = pickler.dumps(p)
inst = pickler.loads(s)
assert eq(p, inst)


class P3(param.Parameterized):
a = param.Integer(0)
count = param.Integer(0)

@param.depends("a", watch=True)
def cb(self):
self.count += 1


@pytest.mark.parametrize('pickler', [cloudpickle, pickle])
def test_pickle_depends_watch_class(pickler):
s = pickler.dumps(P3)
cls = pickler.loads(s)
assert cls is P3


@pytest.mark.parametrize('pickler', [cloudpickle, pickle])
def test_pickle_depends_watch_instance(pickler):
# https://github.com/holoviz/param/issues/757
p = P3()
s = pickler.dumps(p)
inst = pickler.loads(s)
assert eq(p, inst)

inst.a += 1
assert inst.count == 1


class P4(param.Parameterized):
a = param.Parameter()
b = param.Parameter()

single_count = param.Integer()
attr_count = param.Integer()
single_nested_count = param.Integer()
double_nested_count = param.Integer()
nested_attr_count = param.Integer()
nested_count = param.Integer()

@param.depends('a', watch=True)
def single_parameter(self):
self.single_count += 1

@param.depends('a:constant', watch=True)
def constant(self):
self.attr_count += 1

@param.depends('b.a', watch=True)
def single_nested(self):
self.single_nested_count += 1

@param.depends('b.b.a', watch=True)
def double_nested(self):
self.double_nested_count += 1

@param.depends('b.a:constant', watch=True)
def nested_attribute(self):
self.nested_attr_count += 1

@param.depends('b.param', watch=True)
def nested(self):
self.nested_count += 1


@pytest.mark.parametrize('pickler', [cloudpickle, pickle])
def test_pickle_complex_depends_class(pickler):
s = pickler.dumps(P4)
cls = pickler.loads(s)
assert cls is P4


@pytest.mark.parametrize('pickler', [cloudpickle, pickle])
def test_pickle_complex_depends_instance(pickler):
p = P4()
s = pickler.dumps(p)
inst = pickler.loads(s)
assert eq(p, inst)

0 comments on commit de38270

Please sign in to comment.