Skip to content

Commit

Permalink
Added test for detecting the problem raised in telegraphic#125
Browse files Browse the repository at this point in the history
  • Loading branch information
1313e committed Jun 6, 2020
1 parent 101ec05 commit ada053d
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions hickle/tests/test_hickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,53 @@
}
}


# Define a test function that must be serialized and unpacked again
def func(a, b, c=0):
return(a, b, c)


# Define a class that must always be pickled
class with_state(object):
def __init__(self):
self.a = 12
self.b = {
'love': np.ones([12, 7]),
'hatred': np.zeros([4, 9])}

def __getstate__(self):
return({
'a': self.a,
'b': self.b})

def __setstate__(self, state):
self.a = state['a']
self.b = state['b']

def __getitem__(self, index):
if(index == 0):
return(self.a)
if(index < 2):
return(self.b['hatred'])
if(index > 2):
raise ValueError("index unknown")
return(self.b['love'])


DUMP_CACHE = [] # Used in test_track_times()


def test_state_obj():
""" Dumping and loading a class object with pickle states """
filename, mode = 'test.h5', 'w'
obj = with_state()
with pytest.warns(SerializedWarning):
dump(obj, filename, mode)
obj_hkl = load(filename)
assert type(obj) == type(obj_hkl)
assert np.allclose(obj[1], obj_hkl[1])


def test_local_func():
""" Dumping and loading a local function """
filename, mode = 'test.h5', 'w'
Expand Down

0 comments on commit ada053d

Please sign in to comment.