Skip to content
Permalink
Browse files

Replace hou.ui mocking with fixture.

  • Loading branch information...
captainhammy committed Sep 9, 2019
1 parent 9ceb7bc commit 830607a9a82ffe520cc9a35cf4658f39f3d1b487
Showing with 110 additions and 155 deletions.
  1. +31 −2 tests/conftest.py
  2. +14 −30 tests/logging/test_adapters.py
  3. +22 −31 tests/sohohooks/aovs/test_manager.py
  4. +19 −30 tests/ui/menus/test_opmenu.py
  5. +24 −62 tests/ui/menus/test_parmmenu.py
@@ -11,13 +11,20 @@
# Houdini Imports
import hou


# =============================================================================
# FIXTURES
# =============================================================================

@pytest.fixture
def patch_hou():
"""Mock importing of the hou module."""
"""Mock importing of the hou module.
The mocked module is available via the 'hou' key.
The original hou module is available via the 'original_hou' key.
"""
mock_hou = MagicMock()

modules = {
@@ -33,9 +40,25 @@ def patch_hou():
patcher.stop()


@pytest.fixture
def mock_hou_ui():
"""Mock the hou.ui module which isn't available when running tests via Hython."""
mock_ui = MagicMock()

hou.ui = mock_ui

yield mock_ui

del hou.ui


@pytest.fixture
def patch_soho():
"""Mock importing of soho modules."""
"""Mock importing of mantra/soho related modules.
Available mocked modules are available via their original names as keys.
"""
mock_api = MagicMock()
mock_hooks = MagicMock()
mock_mantra = MagicMock()
@@ -60,7 +83,13 @@ def patch_soho():

@pytest.fixture
def raise_hou_operationfailed():
"""Fixture to represent throwing a hou.OperationFailed exception.
We have to use these because setting a mock object's side_effect to a
hou.Error (or subclass) exception doesn't work properly compared to normal
Exceptions.
"""
def raise_error(*args, **kwargs):
raise hou.OperationFailed()

@@ -186,35 +186,32 @@ def test_process__node_property(self, mock_node_prop, mock_dialog_prop, mock_sta
@patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "node", new_callable=PropertyMock(return_value=None))
@patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "__init__", lambda x, y, z, w: None)
@patch.dict(ht.logging.adapters._TO_WRAP, {}, clear=True)
def test_process__ui_passed_no_severity_no_title(self, mock_node_prop, mock_dialog_prop, mock_status_prop, mock_available):
def test_process__ui_passed_no_severity_no_title(self, mock_node_prop, mock_dialog_prop, mock_status_prop,
mock_available, mock_hou_ui):
"""Test passing 'dialog' and 'status_bar' via extra dict with no severity or title."""
mock_message = MagicMock(spec=str)

kwargs = {
"extra": {"dialog": True, "status_bar": True}
}

mock_ui = MagicMock()
hou.ui = mock_ui

log = ht.logging.adapters.HoudiniLoggerAdapter(None, None, None)

result = log.process(mock_message, kwargs)

assert result == (mock_message, kwargs)

mock_ui.displayMessage.assert_called_with(mock_message, severity=hou.severityType.Message, title=None)
mock_ui.setStatusMessage.assert_called_with(mock_message, severity=hou.severityType.Message)

del hou.ui
mock_hou_ui.displayMessage.assert_called_with(mock_message, severity=hou.severityType.Message, title=None)
mock_hou_ui.setStatusMessage.assert_called_with(mock_message, severity=hou.severityType.Message)

@patch("ht.logging.adapters.hou.isUIAvailable", return_value=True)
@patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "status_bar", new_callable=PropertyMock(return_value=True))
@patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "dialog", new_callable=PropertyMock(return_value=True))
@patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "node", new_callable=PropertyMock(return_value=None))
@patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "__init__", lambda x, y, z, w: None)
@patch.dict(ht.logging.adapters._TO_WRAP, {}, clear=True)
def test_process__ui_properties_with_severity_and_title(self, mock_node_prop, mock_dialog_prop, mock_status_prop, mock_available):
def test_process__ui_properties_with_severity_and_title(self, mock_node_prop, mock_dialog_prop, mock_status_prop,
mock_available, mock_hou_ui):
"""Test passing 'dialog' and 'status_bar' via properties with a severity and title."""
mock_message = MagicMock(spec=str)
mock_title = MagicMock(spec=str)
@@ -223,19 +220,14 @@ def test_process__ui_properties_with_severity_and_title(self, mock_node_prop, mo
"extra": {"severity": hou.severityType.Error, "title": mock_title}
}

mock_ui = MagicMock()
hou.ui = mock_ui

log = ht.logging.adapters.HoudiniLoggerAdapter(None, None, None)

result = log.process(mock_message, kwargs)

assert result == (mock_message, kwargs)

mock_ui.displayMessage.assert_called_with(mock_message, severity=hou.severityType.Error, title=mock_title)
mock_ui.setStatusMessage.assert_called_with(mock_message, severity=hou.severityType.Error)

del hou.ui
mock_hou_ui.displayMessage.assert_called_with(mock_message, severity=hou.severityType.Error, title=mock_title)
mock_hou_ui.setStatusMessage.assert_called_with(mock_message, severity=hou.severityType.Error)

@patch("ht.logging.adapters.hou.hipFile.basename")
@patch("ht.logging.adapters.hou.isUIAvailable", return_value=True)
@@ -244,57 +236,49 @@ def test_process__ui_properties_with_severity_and_title(self, mock_node_prop, mo
@patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "node", new_callable=PropertyMock(return_value=None))
@patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "__init__", lambda x, y, z, w: None)
@patch.dict(ht.logging.adapters._TO_WRAP, {}, clear=True)
def test_process__message_args(self, mock_node_prop, mock_dialog_prop, mock_status_prop, mock_available, mock_basename):
def test_process__message_args(self, mock_node_prop, mock_dialog_prop, mock_status_prop, mock_available,
mock_basename, mock_hou_ui):
"""Test passing along 'message_args'."""
mock_message = MagicMock(spec=str)
mock_arg = MagicMock(spec=str)

kwargs = {"extra": {"message_args": (mock_arg, )}}

mock_ui = MagicMock()
hou.ui = mock_ui

log = ht.logging.adapters.HoudiniLoggerAdapter(None, None, None)

result = log.process(mock_message, kwargs)

assert result == (mock_message, kwargs)

mock_ui.displayMessage.assert_called_with(mock_message.__mod__.return_value, severity=hou.severityType.Message, title=None)
mock_hou_ui.displayMessage.assert_called_with(mock_message.__mod__.return_value, severity=hou.severityType.Message, title=None)

mock_message.__mod__.assert_called_with((mock_arg, ))

del hou.ui

@patch("ht.logging.adapters.hou.hipFile.basename")
@patch("ht.logging.adapters.hou.isUIAvailable", return_value=True)
@patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "status_bar", new_callable=PropertyMock(return_value=False))
@patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "dialog", new_callable=PropertyMock(return_value=False))
@patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "node", new_callable=PropertyMock(return_value=None))
@patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "__init__", lambda x, y, z, w: None)
@patch.dict(ht.logging.adapters._TO_WRAP, {}, clear=True)
def test_process__message_args_no_display(self, mock_node_prop, mock_dialog_prop, mock_status_prop, mock_available, mock_basename):
def test_process__message_args_no_display(self, mock_node_prop, mock_dialog_prop, mock_status_prop, mock_available,
mock_basename, mock_hou_ui):
"""Test passing along 'message_args' but not displaying them."""
mock_message = MagicMock(spec=str)
mock_arg = MagicMock(spec=str)

kwargs = {"extra": {"message_args": (mock_arg, )}}

mock_ui = MagicMock()
hou.ui = mock_ui

log = ht.logging.adapters.HoudiniLoggerAdapter(None, None, None)

result = log.process(mock_message, kwargs)

assert result == (mock_message, kwargs)

mock_ui.displayMessage.assert_not_called()
mock_hou_ui.displayMessage.assert_not_called()

mock_message.__mod__.assert_called_with((mock_arg, ))

del hou.ui

@patch("ht.logging.adapters.hou.hipFile.basename")
@patch("ht.logging.adapters.hou.isUIAvailable", return_value=False)
@patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "status_bar", new_callable=PropertyMock(return_value=False))
@@ -1314,48 +1314,39 @@ def test_with_groups(self, mock_manager):
assert result == expected


class Test_flatten_aov_items(object):
def test_flatten_aov_items():
"""Test ht.sohohooks.aovs.manager.flatten_aov_items."""
mock_aov = MagicMock(spec=manager.AOV)

def test(self):
mock_aov = MagicMock(spec=manager.AOV)

mock_group_aov = MagicMock(spec=manager.AOV)
mock_group_aov = MagicMock(spec=manager.AOV)

mock_group = MagicMock(spec=manager.AOVGroup)
type(mock_group).aovs = PropertyMock(return_value = [mock_group_aov])
mock_group = MagicMock(spec=manager.AOVGroup)
type(mock_group).aovs = PropertyMock(return_value = [mock_group_aov])

result = manager.flatten_aov_items((mock_aov, mock_group))
result = manager.flatten_aov_items((mock_aov, mock_group))

assert result == (mock_aov, mock_group_aov)
assert result == (mock_aov, mock_group_aov)


class Test_load_json_files(object):
@patch("ht.sohohooks.aovs.manager.MANAGER", autospec=True)
@patch("ht.sohohooks.aovs.manager.os.path.exists")
@patch("ht.sohohooks.aovs.manager.os.path.expandvars")
def test_load_json_files(mock_expand, mock_exists, mock_manager, mock_hou_ui):
"""Test ht.sohohooks.aovs.manager.load_json_files."""
mock_expand.side_effect = ("expanded1", "expanded2")

@patch("ht.sohohooks.aovs.manager.MANAGER", autospec=True)
@patch("ht.sohohooks.aovs.manager.os.path.exists")
@patch("ht.sohohooks.aovs.manager.os.path.expandvars")
def test(self, mock_expand, mock_exists, mock_manager):
mock_expand.side_effect = ("expanded1", "expanded2")

mock_exists.side_effect = (False, True)

path_str = "path1 ; path2"

mock_ui = MagicMock()
mock_ui.selectFile.return_value = path_str
mock_exists.side_effect = (False, True)

hou.ui = mock_ui
path_str = "path1 ; path2"

manager.load_json_files()
mock_hou_ui.selectFile.return_value = path_str

mock_ui.selectFile.assert_called_with(
pattern="*.json",
chooser_mode=hou.fileChooserMode.Read,
multiple_select=True
)
manager.load_json_files()

mock_manager.load.assert_called_with("expanded2")
mock_hou_ui.selectFile.assert_called_with(
pattern="*.json",
chooser_mode=hou.fileChooserMode.Read,
multiple_select=True
)

del hou.ui
mock_manager.load.assert_called_with("expanded2")
@@ -18,44 +18,33 @@
# CLASSES
# =============================================================================

class Test_create_absolute_reference_copy(object):
def test_create_absolute_reference_copy():
"""Test ht.ui.menus.opmenu.create_absolute_reference_copy."""

def test(self):
"""Test creating an absolute reference copy."""
mock_node = MagicMock(spec=hou.Node)
mock_node = MagicMock(spec=hou.Node)

scriptargs = {
"node": mock_node
}
scriptargs = {
"node": mock_node
}

mock_ui = MagicMock()
ht.ui.menus.opmenu.create_absolute_reference_copy(scriptargs)

hou.ui = mock_ui
mock_node.parent.return_value.copyItems.assert_called_with(
[mock_node],
channel_reference_originals=True,
relative_references=False
)

ht.ui.menus.opmenu.create_absolute_reference_copy(scriptargs)

mock_node.parent.return_value.copyItems.assert_called_with(
[mock_node],
channel_reference_originals=True,
relative_references=False
)

del hou.ui


class Test_save_item_to_file(object):
@patch("ht.ui.menus.opmenu.copy_item")
def test_save_item_to_file(mock_copy):
"""Test ht.ui.menus.opmenu.save_item_to_file."""
mock_node = MagicMock(spec=hou.Node)

@patch("ht.ui.menus.opmenu.copy_item")
def test(self, mock_copy):
"""Test saving an item to a file."""
mock_node = MagicMock(spec=hou.Node)

scriptargs = {
"node": mock_node
}
scriptargs = {
"node": mock_node
}

ht.ui.menus.opmenu.save_item_to_file(scriptargs)
ht.ui.menus.opmenu.save_item_to_file(scriptargs)

mock_copy.assert_called_with(mock_node)
mock_copy.assert_called_with(mock_node)

0 comments on commit 830607a

Please sign in to comment.
You can’t perform that action at this time.