Skip to content

Commit

Permalink
refactor artist test for sceneobject
Browse files Browse the repository at this point in the history
  • Loading branch information
Licini committed Nov 13, 2023
1 parent 4f58e0f commit 635215a
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 110 deletions.
110 changes: 0 additions & 110 deletions tests/compas/artists/test_artists.py

This file was deleted.

110 changes: 110 additions & 0 deletions tests/compas/scene/test_scene.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import pytest # noqa: F401

import compas
from compas.scene import SceneObject
from compas.scene import NoSceneObjectContextError


if not compas.IPY:

@pytest.fixture(autouse=True)
def reset_sceneobjects():
# before each test
yield
# after each test, reset sceneobjects
SceneObject.ITEM_SCENEOBJECT.clear()
SceneObject._SceneObject__SCENEOBJECTS_REGISTERED = False # type: ignore


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


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


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


class FakeItem(object):
pass


class FakeSubItem(FakeItem):
pass


def test_get_sceneobject_cls_with_orderly_registration():
SceneObject.register(FakeItem, FakeSceneObject, context="fake")
SceneObject.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():
SceneObject.register(FakeSubItem, FakeSubSceneObject, context="fake")
SceneObject.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)


if not compas.IPY:

def test_sceneobject_auto_context_discovery(mocker):
mocker.patch("compas.scene.SceneObject.register_sceneobjects")
SceneObject.register_sceneobjects.side_effect = register_fake_context
SceneObject._SceneObject__SCENEOBJECTS_REGISTERED = False # type: ignore

item = FakeItem()
sceneobject = SceneObject(item)

assert isinstance(sceneobject, FakeSceneObject)

def test_sceneobject_auto_context_discovery_viewer(mocker):
mocker.patch("compas.scene.sceneobject.is_viewer_open", return_value=True)
SceneObject.ITEM_SCENEOBJECT["Viewer"] = {FakeItem: FakeSceneObject}

item = FakeSubItem()
sceneobject = SceneObject(item)

assert isinstance(sceneobject, FakeSceneObject)

def test_sceneobject_auto_context_discovery_viewer_priority(mocker):
mocker.patch("compas.scene.sceneobject.is_viewer_open", return_value=True)

class FakeViewerSceneObject(FakeSceneObject):
pass

class FakePlotterSceneObject(FakeSceneObject):
pass

SceneObject.ITEM_SCENEOBJECT["Viewer"] = {FakeItem: FakeViewerSceneObject}
SceneObject.ITEM_SCENEOBJECT["Plotter"] = {FakeItem: FakePlotterSceneObject}

item = FakeSubItem()
sceneobject = SceneObject(item)

assert isinstance(sceneobject, FakeViewerSceneObject)

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

with pytest.raises(NoSceneObjectContextError):
item = FakeSubItem()
_ = SceneObject(item)

0 comments on commit 635215a

Please sign in to comment.