11import pytest
2+ import compas
23from compas .datastructures import Datastructure
4+ from compas .datastructures import Mesh
35from compas .data import json_dumps , json_loads
46
57
@@ -84,7 +86,34 @@ def __from_data__(cls, data):
8486 return Level3 (name = "test" )
8587
8688
89+ @pytest .fixture
90+ def custom_mesh ():
91+ class CustomMesh (Mesh ):
92+ def __init__ (self , * args , ** kwargs ):
93+ super (CustomMesh , self ).__init__ (* args , ** kwargs )
94+ self .custom_mesh_attr = "custom_mesh"
95+
96+ @property
97+ def __data__ (self ):
98+ data = super (CustomMesh , self ).__data__
99+ data ["custom_mesh_attr" ] = self .custom_mesh_attr
100+ return data
101+
102+ @classmethod
103+ def __from_data__ (cls , data ):
104+ obj = super (CustomMesh , cls ).__from_data__ (data )
105+ obj .custom_mesh_attr = data .get ("custom_mesh_attr" , "" )
106+ return obj
107+
108+ return CustomMesh (name = "test" )
109+
110+
87111def test_mro_fallback (level2 ):
112+ if compas .IPY :
113+ # IronPython is not able to deserialize a class that is defined in a local scope like Level1.
114+ # We skip this tests for IronPython.
115+ return
116+
88117 assert level2 .__jsondump__ ()["dtype" ] == "test_datastructure/Level2"
89118 # Level2 should serialize Level1 into the mro
90119 assert level2 .__jsondump__ ()["mro" ] == ["test_datastructure/Level1" ]
@@ -104,6 +133,11 @@ def test_mro_fallback(level2):
104133
105134
106135def test_mro_fallback_multi_level (level3 ):
136+ if compas .IPY :
137+ # IronPython is not able to deserialize a class that is defined in a local scope like Level1.
138+ # We skip this tests for IronPython.
139+ return
140+
107141 assert level3 .__jsondump__ ()["dtype" ] == "test_datastructure/Level3"
108142 # Level3 should serialize Level2 and Level1 into the mro
109143 assert level3 .__jsondump__ ()["mro" ] == ["test_datastructure/Level2" , "test_datastructure/Level1" ]
@@ -122,3 +156,18 @@ def test_mro_fallback_multi_level(level3):
122156 # level2 and 3 attributes will be discarded
123157 assert not hasattr (loaded , "level2_attr" )
124158 assert not hasattr (loaded , "level3_attr" )
159+
160+
161+ def test_custom_mesh (custom_mesh ):
162+ # This test should pass both Python and IronPython
163+ assert custom_mesh .__jsondump__ ()["dtype" ].endswith ("CustomMesh" )
164+ assert custom_mesh .__jsondump__ ()["mro" ] == ["compas.datastructures/Mesh" ]
165+ assert custom_mesh .__jsondump__ ()["data" ]["custom_mesh_attr" ] == "custom_mesh"
166+
167+ dumped = json_dumps (custom_mesh )
168+ loaded = json_loads (dumped )
169+
170+ assert loaded .__class__ == Mesh
171+ assert loaded .__jsondump__ ()["dtype" ] == "compas.datastructures/Mesh"
172+ assert loaded .__jsondump__ ()["mro" ] == []
173+ assert not hasattr (loaded , "custom_mesh_attr" )
0 commit comments