Skip to content

Commit

Permalink
Added tests to (hopefully) complete test coverage of serialization.py
Browse files Browse the repository at this point in the history
  • Loading branch information
lumip committed Jul 13, 2018
1 parent 2925c89 commit 9548742
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions tests/serialization_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,23 @@ def test_serialization_and_deserialization(self):
set_default_pulse_registry(None)

def test_duplication_error(self):
registry = dict()
import weakref
registry = weakref.WeakValueDictionary()

inst = self.make_instance('blub', registry=registry)

self.make_instance('blub', registry=registry)
# ensure that no two objects with same id can be created
with self.assertRaises(RuntimeError):
self.make_instance('blub', registry=registry)

# !!!! the following doesn't really seem to work in triggering the expected behavior... !!!!
# # ensure that pending deleted objects do not block new ones from being registered (i.e., gc invocation works)
# import gc
# gc.disable()
# del inst
# self.make_instance('blub', registry=registry)
# gc.enable()

def test_no_registration_before_correct_serialization(self) -> None:
class RegistryStub:
def __init__(self) -> None:
Expand Down Expand Up @@ -283,6 +294,10 @@ def test_get_contents(self) -> None:

self.assertEqual(expected, contents)

def test_get_contents_empty(self) -> None:
contents = self.backend.list_contents()
self.assertEqual(0, len(contents))


class ZipFileBackendTests(unittest.TestCase):
def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -664,6 +679,15 @@ def test_setitem_different_id(self) -> None:
with self.assertRaisesRegex(ValueError, "different than its own internal identifier"):
self.storage['a_totally_different_id'] = serializable

def test_setitem_duplicate_only_in_backend(self) -> None:
serializable = DummySerializable(identifier='my_id', registry=dict())
backend = DummyStorageBackend()
backend['my_id'] = 'data_in_storage'
storage = PulseStorage(backend)
with self.assertRaisesRegex(RuntimeError, "assigned in storage backend"):
storage['my_id'] = serializable
self.assertEqual({'my_id': 'data_in_storage'}, backend.stored_items)

def test_overwrite(self):

encode_mock = mock.Mock(return_value='asd')
Expand Down Expand Up @@ -975,6 +999,13 @@ def get_serialization_data(self):
self.assertIs(storage['new_id'], new_serializable)
self.assertIs(storage['existing_id'], existing_serializable)

def test_default_else_branch(self) -> None:
encoder = JSONSerializableEncoder(dict())
data = {'a': 'bc', 'b': [1, 2, 3]}

with self.assertRaises(TypeError):
encoder.default(data)

def test_encoding(self):
class A(AnonymousSerializable):
anonymous_serialization_data = [1, 2, 3]
Expand Down

0 comments on commit 9548742

Please sign in to comment.