Skip to content

Commit 4618a72

Browse files
committed
add test for scene serialisation
1 parent b812aea commit 4618a72

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import pytest # noqa: F401
2+
3+
from compas.data import Data
4+
from compas.scene import Scene
5+
from compas.geometry import Box
6+
from compas.geometry import Capsule
7+
from compas.geometry import Circle
8+
from compas.geometry import Cone
9+
from compas.geometry import Cylinder
10+
from compas.geometry import Ellipse
11+
from compas.geometry import Frame
12+
from compas.geometry import Line
13+
from compas.geometry import Point
14+
from compas.geometry import Polygon
15+
from compas.geometry import Polyhedron
16+
from compas.geometry import Polyline
17+
from compas.geometry import Sphere
18+
from compas.geometry import Torus
19+
from compas.geometry import Vector
20+
from compas.geometry import Plane
21+
from compas.datastructures import Mesh
22+
from compas.datastructures import Graph
23+
from compas.datastructures import VolMesh
24+
25+
26+
@pytest.fixture
27+
def items():
28+
box = Box.from_width_height_depth(1, 1, 1)
29+
capsule = Capsule(0.5, 1, Frame.worldXY())
30+
circle = Circle(1, Frame.worldXY())
31+
cone = Cone(1, 1, Frame.worldXY())
32+
cylinder = Cylinder(1, 1, Frame.worldXY())
33+
line = Line(Point(0, 0, 0), Point(1, 1, 1))
34+
point = Point(0, 0, 0)
35+
polygon = Polygon.from_sides_and_radius_xy(5, 1)
36+
polyhedron = Polyhedron.from_platonicsolid(4)
37+
polyline = Polyline([[0, 0, 0], [1, 0, 0], [1, 0, 1]])
38+
sphere = Sphere(1)
39+
torus = Torus(1, 0.3, Frame.worldXY())
40+
vector = Vector(0, 0, 1)
41+
ellipse = Ellipse(1, 0.5, Frame.worldXY())
42+
frame = Frame.worldXY()
43+
plane = Plane(Point(0, 0, 0), Vector(0, 0, 1))
44+
mesh = Mesh.from_polyhedron(8)
45+
graph = Graph.from_nodes_and_edges([(0, 0, 0), (0, -1.5, 0), (-1, 1, 0), (1, 1, 0)], [(0, 1), (0, 2), (0, 3)])
46+
volmesh = VolMesh.from_meshgrid(1, 1, 1, 2, 2, 2)
47+
48+
return [
49+
box,
50+
capsule,
51+
circle,
52+
cone,
53+
cylinder,
54+
line,
55+
point,
56+
polygon,
57+
polyhedron,
58+
polyline,
59+
sphere,
60+
torus,
61+
vector,
62+
ellipse,
63+
frame,
64+
plane,
65+
mesh,
66+
graph,
67+
volmesh,
68+
]
69+
70+
71+
def assert_is_data_equal(obj1, obj2, path=""):
72+
73+
if type(obj1) is not type(obj2):
74+
print(f"Type mismatch: {type(obj1)} != {type(obj2)} for {path}:{obj1} and {path}:{obj2}")
75+
return False
76+
77+
if isinstance(obj1, (list, tuple)):
78+
if len(obj1) != len(obj2):
79+
print(f"Length mismatch: {len(obj1)} != {len(obj2)} for {path} and {path}")
80+
return False
81+
82+
for i, (item1, item2) in enumerate(zip(obj1, obj2)):
83+
if not assert_is_data_equal(item1, item2, path=f"{path}[{i}]"):
84+
return False
85+
86+
return True
87+
88+
elif isinstance(obj1, dict):
89+
if len(obj1) != len(obj2):
90+
print(f"Length mismatch: {len(obj1)} != {len(obj2)} for {path} and {path}")
91+
return False
92+
93+
for key in obj1:
94+
if not assert_is_data_equal(obj1[key], obj2[key], path=f'{path}["{key}"]'):
95+
return False
96+
97+
return True
98+
99+
elif isinstance(obj1, Data):
100+
return assert_is_data_equal(obj1.__data__, obj2.__data__, path=f"{path}.__data__")
101+
102+
else:
103+
if obj1 != obj2:
104+
print(f"Value mismatch: {obj1} != {obj2} for {path}:{obj1} and {path}:{obj2}")
105+
return False
106+
else:
107+
return True
108+
109+
110+
def test_scene_serialisation(items):
111+
scene1 = Scene()
112+
for item in items:
113+
scene1.add(item)
114+
115+
scene2 = Scene.from_jsonstring(scene1.to_jsonstring())
116+
assert assert_is_data_equal(scene1, scene2)

0 commit comments

Comments
 (0)