1
1
"""Test serialization and deserialization of taurus objects."""
2
- from taurus .client .json_encoder import dumps , loads
2
+ import json
3
+ import pytest
4
+
5
+ from taurus .client .json_encoder import dumps , loads , copy , thin_dumps
6
+ from taurus .entity .dict_serializable import DictSerializable
3
7
from taurus .entity .case_insensitive_dict import CaseInsensitiveDict
4
8
from taurus .entity .attribute .condition import Condition
5
9
from taurus .entity .attribute .parameter import Parameter
6
10
from taurus .entity .link_by_uid import LinkByUID
7
- from taurus .entity .object import MeasurementRun , MaterialRun , ProcessRun
11
+ from taurus .entity .object import MeasurementRun , MaterialRun , ProcessRun , MeasurementSpec
8
12
from taurus .entity .object .ingredient_run import IngredientRun
9
13
from taurus .entity .object .ingredient_spec import IngredientSpec
10
14
from taurus .entity .value .nominal_real import NominalReal
11
15
from taurus .entity .value .normal_real import NormalReal
12
-
13
- import json
16
+ from taurus .enumeration .origin import Origin
14
17
15
18
16
19
def test_serialize ():
@@ -43,10 +46,33 @@ def test_deserialize():
43
46
parameter = Parameter (name = "A parameter" , value = NormalReal (mean = 17 , std = 1 , units = '' ))
44
47
measurement = MeasurementRun (tags = "A tag on a measurement" , conditions = condition ,
45
48
parameters = parameter )
46
- copy = loads (dumps (measurement ))
47
- assert (copy .conditions [0 ].value == measurement .conditions [0 ].value )
48
- assert (copy .parameters [0 ].value == measurement .parameters [0 ].value )
49
- assert (copy .uids ["auto" ] == measurement .uids ["auto" ])
49
+ copy_meas = copy (measurement )
50
+ assert (copy_meas .conditions [0 ].value == measurement .conditions [0 ].value )
51
+ assert (copy_meas .parameters [0 ].value == measurement .parameters [0 ].value )
52
+ assert (copy_meas .uids ["auto" ] == measurement .uids ["auto" ])
53
+
54
+
55
+ def test_enumeration_serde ():
56
+ """An enumeration should get serialized as a string."""
57
+ condition = Condition (name = "A condition" , notes = Origin .UNKNOWN )
58
+ copy_condition = copy (condition )
59
+ assert copy_condition .notes == Origin .get_value (condition .notes )
60
+
61
+
62
+ def test_thin_dumps ():
63
+ """Test that thin_dumps turns pointers into links and doesn't work on non-BaseEntity."""
64
+ mat = MaterialRun ("The actual material" )
65
+ meas_spec = MeasurementSpec ("measurement" , uids = {'my_scope' : '324324' })
66
+ meas = MeasurementRun ("The measurement" , spec = meas_spec , material = mat )
67
+
68
+ thin_copy = MeasurementRun .build (json .loads (thin_dumps (meas )))
69
+ assert isinstance (thin_copy , MeasurementRun )
70
+ assert isinstance (thin_copy .material , LinkByUID )
71
+ assert isinstance (thin_copy .spec , LinkByUID )
72
+ assert thin_copy .spec .id == meas_spec .uids ['my_scope' ]
73
+
74
+ with pytest .raises (TypeError ):
75
+ thin_dumps (LinkByUID ('scope' , 'id' ))
50
76
51
77
52
78
def test_uid_deser ():
@@ -59,6 +85,40 @@ def test_uid_deser():
59
85
assert ingredient_copy .material .uids ['sample id' ] == material .uids ['Sample ID' ]
60
86
61
87
88
+ def test_dict_serialization ():
89
+ """Test that a dictionary can be serialized and then deserialized as a taurus object."""
90
+ process = ProcessRun ("A process" )
91
+ mat = MaterialRun ("A material" , process = process )
92
+ meas = MeasurementRun ("A measurement" , material = mat )
93
+ copy = loads (dumps (meas .as_dict ()))
94
+ assert copy == meas
95
+
96
+
97
+ def test_unexpected_serialization ():
98
+ """Trying to serialize an unexpected class should throw a TypeError."""
99
+ class DummyClass :
100
+ def __init__ (self , foo ):
101
+ self .foo = foo
102
+
103
+ with pytest .raises (TypeError ):
104
+ dumps (ProcessRun ("A process" , notes = DummyClass ("something" )))
105
+
106
+
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
+
62
122
def test_case_insensitive_rehydration ():
63
123
"""
64
124
0 commit comments