Skip to content

Commit

Permalink
ipy can't deal with trailing commas in function signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvanmele committed Jun 5, 2024
1 parent 59c8a55 commit 58e2a0f
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 164 deletions.
110 changes: 49 additions & 61 deletions tests/compas/scene/test_scene.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import pytest # noqa: F401

import compas
from compas.scene import context
from compas.scene import register
from compas.scene import SceneObject
from compas.scene import SceneObjectNotRegisteredError
from compas.data import Data


if not compas.IPY:
import pytest # noqa: F401
from compas.scene import context
from compas.scene import register
from compas.scene import SceneObject
from compas.scene import SceneObjectNotRegisteredError
from compas.data import Data

@pytest.fixture(autouse=True)
def reset_sceneobjects_registration():
Expand All @@ -17,73 +15,63 @@ def reset_sceneobjects_registration():
# after each test, reset scene objects
context.ITEM_SCENEOBJECT.clear()

def register_fake_context():
register(FakeItem, FakeSceneObject, context="fake")

def register_fake_context():
register(FakeItem, FakeSceneObject, context="fake")
class FakeSceneObject(SceneObject):
def draw(self):
pass

def clear(self):
pass

class FakeSceneObject(SceneObject):
def draw(self):
pass

def clear(self):
pass
class FakeSubSceneObject(SceneObject):
def draw(self):
pass

def clear(self):
pass

class FakeSubSceneObject(SceneObject):
def draw(self):
class FakeItem(Data):
pass

def clear(self):
class FakeSubItem(FakeItem):
pass

def test_get_sceneobject_cls_with_orderly_registration():
register(FakeItem, FakeSceneObject, context="fake")
register(FakeSubItem, FakeSubSceneObject, context="fake")
item = FakeItem()
sceneobject = SceneObject(item, context="fake")
assert isinstance(sceneobject, FakeSceneObject)

class FakeItem(Data):
pass


class FakeSubItem(FakeItem):
pass


def test_get_sceneobject_cls_with_orderly_registration():
register(FakeItem, FakeSceneObject, context="fake")
register(FakeSubItem, FakeSubSceneObject, context="fake")
item = FakeItem()
sceneobject = SceneObject(item, context="fake")
assert isinstance(sceneobject, FakeSceneObject)

item = FakeSubItem()
sceneobject = SceneObject(item, context="fake")
assert isinstance(sceneobject, FakeSubSceneObject)


def test_get_sceneobject_cls_with_out_of_order_registration():
register(FakeSubItem, FakeSubSceneObject, context="fake")
register(FakeItem, FakeSceneObject, context="fake")
item = FakeItem()
sceneobject = SceneObject(item, context="fake")
assert isinstance(sceneobject, FakeSceneObject)

item = FakeSubItem()
sceneobject = SceneObject(item, context="fake")
assert isinstance(sceneobject, FakeSubSceneObject)
item = FakeSubItem()
sceneobject = SceneObject(item, context="fake")
assert isinstance(sceneobject, FakeSubSceneObject)

def test_get_sceneobject_cls_with_out_of_order_registration():
register(FakeSubItem, FakeSubSceneObject, context="fake")
register(FakeItem, FakeSceneObject, context="fake")
item = FakeItem()
sceneobject = SceneObject(item, context="fake")
assert isinstance(sceneobject, FakeSceneObject)

if not compas.IPY:
item = FakeSubItem()
sceneobject = SceneObject(item, context="fake")
assert isinstance(sceneobject, FakeSubSceneObject)

def test_sceneobject_auto_context_discovery(mocker):
register_fake_context()
def test_sceneobject_auto_context_discovery(mocker):
register_fake_context()

item = FakeItem()
sceneobject = SceneObject(item)
item = FakeItem()
sceneobject = SceneObject(item)

assert isinstance(sceneobject, FakeSceneObject)
assert isinstance(sceneobject, FakeSceneObject)

def test_sceneobject_auto_context_discovery_no_context(mocker):
mocker.patch("compas.scene.context.compas.is_grasshopper", return_value=False)
mocker.patch("compas.scene.context.compas.is_rhino", return_value=False)
def test_sceneobject_auto_context_discovery_no_context(mocker):
mocker.patch("compas.scene.context.compas.is_grasshopper", return_value=False)
mocker.patch("compas.scene.context.compas.is_rhino", return_value=False)

with pytest.raises(SceneObjectNotRegisteredError):
item = FakeSubItem()
_ = SceneObject(item)
with pytest.raises(SceneObjectNotRegisteredError):
item = FakeSubItem()
_ = SceneObject(item)
204 changes: 101 additions & 103 deletions tests/compas/scene/test_scene_serialisation.py
Original file line number Diff line number Diff line change
@@ -1,119 +1,117 @@
import pytest # noqa: F401
import compas

from compas.data import Data
from compas.scene import Scene
from compas.geometry import Box
from compas.geometry import Capsule
from compas.geometry import Circle
from compas.geometry import Cone
from compas.geometry import Cylinder
from compas.geometry import Ellipse
from compas.geometry import Frame
from compas.geometry import Line
from compas.geometry import Point
from compas.geometry import Polygon
from compas.geometry import Polyhedron
from compas.geometry import Polyline
from compas.geometry import Sphere
from compas.geometry import Torus
from compas.geometry import Vector
from compas.geometry import Plane
from compas.datastructures import Mesh
from compas.datastructures import Graph
from compas.datastructures import VolMesh


@pytest.fixture
def items():
box = Box.from_width_height_depth(1, 1, 1)
capsule = Capsule(0.5, 1, Frame.worldXY())
circle = Circle(1, Frame.worldXY())
cone = Cone(1, 1, Frame.worldXY())
cylinder = Cylinder(1, 1, Frame.worldXY())
line = Line(Point(0, 0, 0), Point(1, 1, 1))
point = Point(0, 0, 0)
polygon = Polygon.from_sides_and_radius_xy(5, 1)
polyhedron = Polyhedron.from_platonicsolid(4)
polyline = Polyline([[0, 0, 0], [1, 0, 0], [1, 0, 1]])
sphere = Sphere(1)
torus = Torus(1, 0.3, Frame.worldXY())
vector = Vector(0, 0, 1)
ellipse = Ellipse(1, 0.5, Frame.worldXY())
frame = Frame.worldXY()
plane = Plane(Point(0, 0, 0), Vector(0, 0, 1))
mesh = Mesh.from_polyhedron(8)
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)])
volmesh = VolMesh.from_meshgrid(1, 1, 1, 2, 2, 2)

return [
box,
capsule,
circle,
cone,
cylinder,
line,
point,
polygon,
polyhedron,
polyline,
sphere,
torus,
vector,
ellipse,
frame,
plane,
mesh,
graph,
volmesh,
]


def assert_is_data_equal(obj1, obj2, path=""):
if type(obj1) is not type(obj2):
print("Type mismatch: {} != {} for {}:{} and {}:{}".format(type(obj1), type(obj2), path, obj1, path, obj2))
return False

if isinstance(obj1, (list, tuple)):
if len(obj1) != len(obj2):
print("Length mismatch: {} != {} for {} and {}".format(len(obj1), len(obj2), path, path))
if not compas.IPY:
import pytest # noqa: F401
from compas.data import Data
from compas.scene import Scene
from compas.geometry import Box
from compas.geometry import Capsule
from compas.geometry import Circle
from compas.geometry import Cone
from compas.geometry import Cylinder
from compas.geometry import Ellipse
from compas.geometry import Frame
from compas.geometry import Line
from compas.geometry import Point
from compas.geometry import Polygon
from compas.geometry import Polyhedron
from compas.geometry import Polyline
from compas.geometry import Sphere
from compas.geometry import Torus
from compas.geometry import Vector
from compas.geometry import Plane
from compas.datastructures import Mesh
from compas.datastructures import Graph
from compas.datastructures import VolMesh

@pytest.fixture
def items():
box = Box.from_width_height_depth(1, 1, 1)
capsule = Capsule(0.5, 1, Frame.worldXY())
circle = Circle(1, Frame.worldXY())
cone = Cone(1, 1, Frame.worldXY())
cylinder = Cylinder(1, 1, Frame.worldXY())
line = Line(Point(0, 0, 0), Point(1, 1, 1))
point = Point(0, 0, 0)
polygon = Polygon.from_sides_and_radius_xy(5, 1)
polyhedron = Polyhedron.from_platonicsolid(4)
polyline = Polyline([[0, 0, 0], [1, 0, 0], [1, 0, 1]])
sphere = Sphere(1)
torus = Torus(1, 0.3, Frame.worldXY())
vector = Vector(0, 0, 1)
ellipse = Ellipse(1, 0.5, Frame.worldXY())
frame = Frame.worldXY()
plane = Plane(Point(0, 0, 0), Vector(0, 0, 1))
mesh = Mesh.from_polyhedron(8)
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)])
volmesh = VolMesh.from_meshgrid(1, 1, 1, 2, 2, 2)

return [
box,
capsule,
circle,
cone,
cylinder,
line,
point,
polygon,
polyhedron,
polyline,
sphere,
torus,
vector,
ellipse,
frame,
plane,
mesh,
graph,
volmesh,
]

def assert_is_data_equal(obj1, obj2, path=""):
if type(obj1) is not type(obj2):
print("Type mismatch: {} != {} for {}:{} and {}:{}".format(type(obj1), type(obj2), path, obj1, path, obj2))
return False

for i, (item1, item2) in enumerate(zip(obj1, obj2)):
if not assert_is_data_equal(item1, item2, path="{}[{}]".format(path, i)):
if isinstance(obj1, (list, tuple)):
if len(obj1) != len(obj2):
print("Length mismatch: {} != {} for {} and {}".format(len(obj1), len(obj2), path, path))
return False

return True
for i, (item1, item2) in enumerate(zip(obj1, obj2)):
if not assert_is_data_equal(item1, item2, path="{}[{}]".format(path, i)):
return False

elif isinstance(obj1, dict):
if set(obj1.keys()) != set(obj2.keys()):
print("Key mismatch: {} != {} for {} and {}".format(set(obj1.keys()), set(obj2.keys()), path, path))
return False
return True

for key in obj1:
if not assert_is_data_equal(obj1[key], obj2[key], path='{}["{}"]'.format(path, key)):
elif isinstance(obj1, dict):
if set(obj1.keys()) != set(obj2.keys()):
print("Key mismatch: {} != {} for {} and {}".format(set(obj1.keys()), set(obj2.keys()), path, path))
return False

return True

elif isinstance(obj1, Data):
return assert_is_data_equal(obj1.__data__, obj2.__data__, path="{}.__data__".format(path))
for key in obj1:
if not assert_is_data_equal(obj1[key], obj2[key], path='{}["{}"]'.format(path, key)):
return False

else:
if obj1 != obj2:
print("Value mismatch: {} != {} for {}:{} and {}:{}".format(obj1, obj2, path, obj1, path, obj2))
return False
else:
return True

elif isinstance(obj1, Data):
return assert_is_data_equal(obj1.__data__, obj2.__data__, path="{}.__data__".format(path))

else:
if obj1 != obj2:
print("Value mismatch: {} != {} for {}:{} and {}:{}".format(obj1, obj2, path, obj1, path, obj2))
return False
else:
return True

def test_scene_serialisation(items, mocker):
if compas.IPY:
mocker.patch("compas.is_rhino", return_value=False)
def test_scene_serialisation(items, mocker):
if compas.IPY:
mocker.patch("compas.is_rhino", return_value=False)

scene1 = Scene()
for item in items:
scene1.add(item)
scene1 = Scene()
for item in items:
scene1.add(item)

scene2 = Scene.from_jsonstring(scene1.to_jsonstring())
assert assert_is_data_equal(scene1, scene2)
scene2 = Scene.from_jsonstring(scene1.to_jsonstring())
assert assert_is_data_equal(scene1, scene2)

0 comments on commit 58e2a0f

Please sign in to comment.