Skip to content

Commit b487f88

Browse files
author
bfolie
authored
Merge pull request #38 from CitrineInformatics/feature/test-unexpected-deserialization
Test unexpected deserialization
2 parents edb9e9d + 1fbe2dd commit b487f88

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

taurus/client/json_encoder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def _loado(d, index):
208208
obj = LinkByUID.from_dict(d)
209209
return obj
210210
else:
211-
raise ValueError("Unexpected base object type: {}".format(typ)) # pragma: no cover
211+
raise TypeError("Unexpected base object type: {}".format(typ))
212212

213213
if isinstance(obj, BaseEntity):
214214
for (scope, id) in obj.uids.items():

taurus/client/tests/test_json.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44

55
from taurus.client.json_encoder import dumps, loads, copy, thin_dumps
6+
from taurus.entity.dict_serializable import DictSerializable
67
from taurus.entity.case_insensitive_dict import CaseInsensitiveDict
78
from taurus.entity.attribute.condition import Condition
89
from taurus.entity.attribute.parameter import Parameter
@@ -103,6 +104,21 @@ def __init__(self, foo):
103104
dumps(ProcessRun("A process", notes=DummyClass("something")))
104105

105106

107+
def test_unexpected_deserialization():
108+
"""Trying to deserialize an unexpected class should throw a TypeError."""
109+
class DummyClass(DictSerializable):
110+
typ = 'dummy_class'
111+
112+
def __init__(self, foo):
113+
self.foo = foo
114+
115+
# DummyClass can be serialized because it is a DictSerializable, but cannot be
116+
# deserialized because it is not in the _clazzes list.
117+
serialized = dumps(ProcessRun("A process", notes=DummyClass("something")))
118+
with pytest.raises(TypeError):
119+
loads(serialized)
120+
121+
106122
def test_case_insensitive_rehydration():
107123
"""
108124

0 commit comments

Comments
 (0)