|
| 1 | +import pytest |
| 2 | +import compas |
| 3 | +from compas.datastructures import Datastructure |
| 4 | +from compas.datastructures import Mesh |
| 5 | +from compas.data import json_dumps, json_loads |
| 6 | + |
| 7 | + |
| 8 | +class Level1(Datastructure): |
| 9 | + def __init__(self, attributes=None, name=None): |
| 10 | + super(Level1, self).__init__(attributes=attributes, name=name) |
| 11 | + self.level1_attr = "level1" |
| 12 | + |
| 13 | + @property |
| 14 | + def __data__(self): |
| 15 | + return {"attributes": self.attributes, "level1_attr": self.level1_attr} |
| 16 | + |
| 17 | + @classmethod |
| 18 | + def __from_data__(cls, data): |
| 19 | + obj = cls(attributes=data.get("attributes")) |
| 20 | + obj.level1_attr = data.get("level1_attr", "") |
| 21 | + return obj |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def level2(): |
| 26 | + # Level2 is a custom class that is not available outside of this local scope |
| 27 | + class Level2(Level1): |
| 28 | + def __init__(self, attributes=None, name=None): |
| 29 | + super(Level2, self).__init__(attributes=attributes, name=name) |
| 30 | + self.level2_attr = "level2" |
| 31 | + |
| 32 | + @property |
| 33 | + def __data__(self): |
| 34 | + data = super(Level2, self).__data__ |
| 35 | + data["level2_attr"] = self.level2_attr |
| 36 | + return data |
| 37 | + |
| 38 | + @classmethod |
| 39 | + def __from_data__(cls, data): |
| 40 | + obj = super(Level2, cls).__from_data__(data) |
| 41 | + obj.level2_attr = data.get("level2_attr", "") |
| 42 | + return obj |
| 43 | + |
| 44 | + # return an instance of Level2 |
| 45 | + return Level2(name="test") |
| 46 | + |
| 47 | + |
| 48 | +@pytest.fixture |
| 49 | +def level3(): |
| 50 | + # Level2 and Level3 are custom classes that are not available outside of this local scope |
| 51 | + class Level2(Level1): |
| 52 | + def __init__(self, attributes=None, name=None): |
| 53 | + super(Level2, self).__init__(attributes=attributes, name=name) |
| 54 | + self.level2_attr = "level2" |
| 55 | + |
| 56 | + @property |
| 57 | + def __data__(self): |
| 58 | + data = super(Level2, self).__data__ |
| 59 | + data["level2_attr"] = self.level2_attr |
| 60 | + return data |
| 61 | + |
| 62 | + @classmethod |
| 63 | + def __from_data__(cls, data): |
| 64 | + obj = super(Level2, cls).__from_data__(data) |
| 65 | + obj.level2_attr = data.get("level2_attr", "") |
| 66 | + return obj |
| 67 | + |
| 68 | + class Level3(Level2): |
| 69 | + def __init__(self, attributes=None, name=None): |
| 70 | + super(Level3, self).__init__(attributes=attributes, name=name) |
| 71 | + self.level3_attr = "level3" |
| 72 | + |
| 73 | + @property |
| 74 | + def __data__(self): |
| 75 | + data = super(Level3, self).__data__ |
| 76 | + data["level3_attr"] = self.level3_attr |
| 77 | + return data |
| 78 | + |
| 79 | + @classmethod |
| 80 | + def __from_data__(cls, data): |
| 81 | + obj = super(Level3, cls).__from_data__(data) |
| 82 | + obj.level3_attr = data.get("level3_attr", "") |
| 83 | + return obj |
| 84 | + |
| 85 | + # return an instance of Level3 |
| 86 | + return Level3(name="test") |
| 87 | + |
| 88 | + |
| 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 | + |
| 111 | +def test_inheritance_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 | + |
| 117 | + assert level2.__jsondump__()["dtype"] == "test_datastructure/Level2" |
| 118 | + # Level2 should serialize Level1 into the inheritance |
| 119 | + assert level2.__jsondump__()["inheritance"] == ["test_datastructure/Level1"] |
| 120 | + |
| 121 | + dumped = json_dumps(level2) |
| 122 | + loaded = json_loads(dumped) |
| 123 | + |
| 124 | + # The loaded object should be deserialized as the closes available class: Level1 |
| 125 | + assert loaded.__class__ == Level1 |
| 126 | + assert loaded.__jsondump__()["dtype"] == "test_datastructure/Level1" |
| 127 | + assert loaded.__jsondump__()["inheritance"] == [] |
| 128 | + |
| 129 | + # level1 attributes should still be available |
| 130 | + assert loaded.level1_attr == "level1" |
| 131 | + # Meanwhile, level2 attributes will be discarded |
| 132 | + assert not hasattr(loaded, "level2_attr") |
| 133 | + |
| 134 | + |
| 135 | +def test_inheritance_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 | + |
| 141 | + assert level3.__jsondump__()["dtype"] == "test_datastructure/Level3" |
| 142 | + # Level3 should serialize Level2 and Level1 into the inheritance |
| 143 | + assert level3.__jsondump__()["inheritance"] == ["test_datastructure/Level2", "test_datastructure/Level1"] |
| 144 | + |
| 145 | + dumped = json_dumps(level3) |
| 146 | + loaded = json_loads(dumped) |
| 147 | + |
| 148 | + # The loaded object should be deserialized as the closes available class: Level1 |
| 149 | + assert loaded.__class__ == Level1 |
| 150 | + assert loaded.__jsondump__()["dtype"] == "test_datastructure/Level1" |
| 151 | + assert loaded.__jsondump__()["inheritance"] == [] |
| 152 | + |
| 153 | + # level1 attributes should still be available |
| 154 | + assert loaded.level1_attr == "level1" |
| 155 | + |
| 156 | + # level2 and 3 attributes will be discarded |
| 157 | + assert not hasattr(loaded, "level2_attr") |
| 158 | + 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__()["inheritance"] == ["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__()["inheritance"] == [] |
| 173 | + assert not hasattr(loaded, "custom_mesh_attr") |
0 commit comments