Skip to content
Permalink
Browse files

When using mock_hou_ui now mock hou.isUIAvailable to return True.

patch_hou now returns an object with properties instead of a dictionary.
  • Loading branch information
captainhammy committed Jan 9, 2020
1 parent f280e0e commit f5e8729c2d9ac7dd296ec3aaa6cac702e37b8100
@@ -20,7 +20,7 @@ def mock_hou_exceptions(mocker):
"""Fixture to support dealing with hou.Error exceptions.
Since hou.Error and all derived exceptions are not actual Exception objects
pytest will not list this. If your code throws an uncaught exception it will
pytest will not like this. If your code throws an uncaught exception it will
confuse pytest and it will basically error, but not error. The test execution
will halt but your test will not fail.
@@ -146,9 +146,17 @@ class HouExceptions(object):

@pytest.fixture
def mock_hou_ui(mocker):
"""Mock the hou.ui module which isn't available when running tests via Hython."""
"""Mock the hou.ui module which isn't available when running tests via Hython.
This fixture will also mock hou.isUIAvailable() to return True.
"""
mock_ui = mocker.MagicMock()

# If we are going to be mocking the hou.ui module we should patch
# hou.isUIAvailable so that it reflects we have hou.ui.
mocker.patch("hou.isUIAvailable", return_value=True)

hou.ui = mock_ui

yield mock_ui
@@ -160,23 +168,27 @@ def mock_hou_ui(mocker):
def patch_hou(mocker):
"""Mock importing of the hou module.
The mocked module is available via the 'hou' key.
The mocked module is available via the 'hou' attribute on the fixture object.
The original hou module is available via the 'original_hou' key.
The original hou module is available via the 'original_hou' attribute.
This is useful for dealing with tucked hou imports.
"""
mock_hou = mocker.MagicMock()

class MockHou(object):
original_hou = hou
hou = mock_hou

modules = {
"hou": mock_hou,
"original_hou": hou
}

mocker.patch.dict("sys.modules", modules)

yield modules
yield MockHou


@pytest.fixture
@@ -192,7 +192,6 @@ def test_process__ui_passed_no_severity_no_title(self, init_adapter, mocker, moc
mocker.patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "node", new_callable=mocker.PropertyMock(return_value=None))
mocker.patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "dialog", new_callable=mocker.PropertyMock(return_value=False))
mocker.patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "status_bar", new_callable=mocker.PropertyMock(return_value=False))
mocker.patch("ht.logging.adapters.hou.isUIAvailable", return_value=True)

mock_message = mocker.MagicMock(spec=str)

@@ -215,7 +214,6 @@ def test_process__ui_properties_with_severity_and_title(self, init_adapter, mock
mocker.patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "node", new_callable=mocker.PropertyMock(return_value=None))
mocker.patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "dialog", new_callable=mocker.PropertyMock(return_value=True))
mocker.patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "status_bar", new_callable=mocker.PropertyMock(return_value=True))
mocker.patch("ht.logging.adapters.hou.isUIAvailable", return_value=True)

mock_message = mocker.MagicMock(spec=str)
mock_title = mocker.MagicMock(spec=str)
@@ -239,7 +237,6 @@ def test_process__message_args(self, init_adapter, mocker, mock_hou_ui):
mocker.patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "node", new_callable=mocker.PropertyMock(return_value=None))
mocker.patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "dialog", new_callable=mocker.PropertyMock(return_value=True))
mocker.patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "status_bar", new_callable=mocker.PropertyMock(return_value=False))
mocker.patch("ht.logging.adapters.hou.isUIAvailable", return_value=True)

mock_message = mocker.MagicMock(spec=str)
mock_arg = mocker.MagicMock(spec=str)
@@ -262,7 +259,6 @@ def test_process__message_args_no_display(self, init_adapter, mocker, mock_hou_u
mocker.patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "node", new_callable=mocker.PropertyMock(return_value=None))
mocker.patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "dialog", new_callable=mocker.PropertyMock(return_value=False))
mocker.patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "status_bar", new_callable=mocker.PropertyMock(return_value=False))
mocker.patch("ht.logging.adapters.hou.isUIAvailable", return_value=True)
mocker.patch("ht.logging.adapters.hou.hipFile.basename")

mock_message = mocker.MagicMock(spec=str)
@@ -286,7 +282,6 @@ def test_process__no_extra(self, init_adapter, mocker):
mock_node_prop = mocker.patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "node", new_callable=mocker.PropertyMock)
mocker.patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "dialog", new_callable=mocker.PropertyMock(return_value=False))
mocker.patch.object(ht.logging.adapters.HoudiniLoggerAdapter, "status_bar", new_callable=mocker.PropertyMock(return_value=False))
mocker.patch("ht.logging.adapters.hou.isUIAvailable", return_value=True)
mocker.patch("ht.logging.adapters.hou.hipFile.basename")

mock_message = mocker.MagicMock(spec=str)
@@ -273,11 +273,11 @@ def test___init___findfile(self, patch_hou, mocker):
op = setproperties.PropertySetter(mock_name, block)

assert op._name == mock_name
assert op._value == patch_hou["hou"].findFile.return_value
assert op._value == patch_hou.hou.findFile.return_value
assert op._find_file
assert op._rendertype is None

patch_hou["hou"].findFile.assert_called_with(mock_value)
patch_hou.hou.findFile.assert_called_with(mock_value)

# Properties

@@ -320,13 +320,13 @@ def test_value(self, init_setter, mocker):
def test_set_property__rendertype_no_match(self, init_setter, properties, mocker, patch_hou):
mock_rendertype = mocker.patch.object(setproperties.PropertySetter, "rendertype", new_callable=mocker.PropertyMock)

patch_hou["hou"].patternMatch.return_value = False
patch_hou.hou.patternMatch.return_value = False

op = init_setter()
op.set_property()

properties.mock_get.assert_called_with("renderer:rendertype")
patch_hou["hou"].patternMatch.assert_called_with(mock_rendertype.return_value, properties.mock_get.return_value)
patch_hou.hou.patternMatch.assert_called_with(mock_rendertype.return_value, properties.mock_get.return_value)

properties.mock_set.assert_not_called()

@@ -335,13 +335,13 @@ def test_set_property__rendertype_match(self, init_setter, properties, mocker, p
mock_name = mocker.patch.object(setproperties.PropertySetter, "name", new_callable=mocker.PropertyMock)
mock_value = mocker.patch.object(setproperties.PropertySetter, "value", new_callable=mocker.PropertyMock)

patch_hou["hou"].patternMatch.return_value = True
patch_hou.hou.patternMatch.return_value = True

op = init_setter()
op.set_property()

properties.mock_get.assert_called_with("renderer:rendertype")
patch_hou["hou"].patternMatch.assert_called_with(mock_rendertype.return_value, properties.mock_get.return_value)
patch_hou.hou.patternMatch.assert_called_with(mock_rendertype.return_value, properties.mock_get.return_value)

properties.mock_set.assert_called_with(mock_name.return_value, mock_value.return_value)

@@ -350,7 +350,7 @@ def test_set_property__no_rendertype(self, init_setter, properties, mocker, patc
mock_name = mocker.patch.object(setproperties.PropertySetter, "name", new_callable=mocker.PropertyMock)
mock_value = mocker.patch.object(setproperties.PropertySetter, "value", new_callable=mocker.PropertyMock)

patch_hou["hou"].patternMatch.return_value = True
patch_hou.hou.patternMatch.return_value = True

op = init_setter()
op.set_property()
@@ -407,13 +407,13 @@ def test_set_property__mask_no_match(self, init_masked_setter, mocker, patch_hou

mock_get = mocker.patch("ht.pyfilter.operations.setproperties.get_property")

patch_hou["hou"].patternMatch.return_value = False
patch_hou.hou.patternMatch.return_value = False

op = init_masked_setter()
op.set_property()

mock_get.assert_called_with(mock_mask_name.return_value)
patch_hou["hou"].patternMatch.assert_called_with(mock_mask.return_value, mock_get.return_value)
patch_hou.hou.patternMatch.assert_called_with(mock_mask.return_value, mock_get.return_value)

mock_super_set.assert_not_called()

@@ -424,13 +424,13 @@ def test_set_property__mask_match(self, init_masked_setter, mocker, patch_hou):

mock_get = mocker.patch("ht.pyfilter.operations.setproperties.get_property")

patch_hou["hou"].patternMatch.return_value = True
patch_hou.hou.patternMatch.return_value = True

op = init_masked_setter()
op.set_property()

mock_get.assert_called_with(mock_mask_name.return_value)
patch_hou["hou"].patternMatch.assert_called_with(mock_mask.return_value, mock_get.return_value)
patch_hou.hou.patternMatch.assert_called_with(mock_mask.return_value, mock_get.return_value)

mock_super_set.assert_called()

@@ -281,10 +281,7 @@ class Test__find_operation_files(object):
"""Test ht.pyfilter.manager._find_operation_files."""

def test_no_files(self, patch_hou, mock_hou_exceptions):
# Because we are tucking the hou import in the function we need to patch in the
# original hou.OperationFailed so that the test will execute correctly.
# patch_hou["hou"].OperationFailed = patch_hou["original_hou"].OperationFailed
patch_hou["hou"].findFiles.side_effect = mock_hou_exceptions.OperationFailed
patch_hou.hou.findFiles.side_effect = mock_hou_exceptions.OperationFailed

result = manager._find_operation_files()

@@ -293,7 +290,7 @@ def test_no_files(self, patch_hou, mock_hou_exceptions):
def test(self, patch_hou):
result = manager._find_operation_files()

assert result == patch_hou["hou"].findFiles.return_value
assert result == patch_hou.hou.findFiles.return_value


class Test__get_class(object):
@@ -22,11 +22,7 @@ def test_no_found_script(self, mocker, patch_hou, mock_hou_exceptions):
mock_logger = mocker.patch("ht.pyfilter.utils._logger")
mock_exists = mocker.patch("os.path.exists")

# Because we are tucking the hou import in the function we need to patch in the
# original hou.OperationFailed so that the test will execute correctly.
# patch_hou["hou"].OperationFailed = patch_hou["original_hou"].OperationFailed

patch_hou["hou"].findFile.side_effect = mock_hou_exceptions.OperationFailed
patch_hou.hou.findFile.side_effect = mock_hou_exceptions.OperationFailed

result = utils.build_pyfilter_command()

@@ -42,16 +38,16 @@ def test_found_script_does_not_exists(self, mocker, patch_hou):
with pytest.raises(OSError):
utils.build_pyfilter_command()

mock_exists.assert_called_with(patch_hou["hou"].findFile.return_value)
mock_exists.assert_called_with(patch_hou.hou.findFile.return_value)

def test_found_script_no_args(self, mocker, patch_hou):
mock_exists = mocker.patch("os.path.exists", return_value=True)

result = utils.build_pyfilter_command()

assert result == '-P "{} "'.format(patch_hou["hou"].findFile.return_value)
assert result == '-P "{} "'.format(patch_hou.hou.findFile.return_value)

mock_exists.assert_called_with(patch_hou["hou"].findFile.return_value)
mock_exists.assert_called_with(patch_hou.hou.findFile.return_value)

def test_manual_path(self, mocker, patch_hou):
mock_exists = mocker.patch("os.path.exists", return_value=True)
@@ -62,7 +58,7 @@ def test_manual_path(self, mocker, patch_hou):

assert result == '-P "{} "'.format(mock_path)

patch_hou["hou"].findFile.assert_not_called()
patch_hou.hou.findFile.assert_not_called()
mock_exists.assert_called_with(mock_path)

def test_manual_path_args(self, mocker, patch_hou):
@@ -76,5 +72,5 @@ def test_manual_path_args(self, mocker, patch_hou):

assert result == '-P "{} {}"'.format(mock_path, " ".join(args))

patch_hou["hou"].findFile.assert_not_called()
patch_hou.hou.findFile.assert_not_called()
mock_exists.assert_called_with(mock_path)

0 comments on commit f5e8729

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