Permalink
Browse files

Add test for sohohooks.manager.

Fix up docstrings in tests for sohohooks.aovs.manager.
  • Loading branch information...
captainhammy committed Jan 17, 2019
1 parent 0a66f58 commit 43c39596b9c9c1ab56cb3711e22991839368e325
Showing with 175 additions and 11 deletions.
  1. +1 −0 .coveragerc
  2. +4 −3 python/ht/sohohooks/manager.py
  3. +8 −8 tests/sohohooks/aovs/test_manager.py
  4. +162 −0 tests/sohohooks/test_manager.py
@@ -29,6 +29,7 @@ source =
ht.pyfilter.operations.zdepth
ht.sohohooks.aovs.aov
ht.sohohooks.aovs.manager
ht.sohohooks.manager

omit =
*tests*
@@ -39,6 +39,8 @@ def call_hook(self, name, *args, **kwargs):

hooks = self.hooks.get(name, ())

return_value = False

for hook in hooks:
try:
result = hook(*args, **kwargs)
@@ -56,9 +58,9 @@ def call_hook(self, name, *args, **kwargs):

else:
if result:
return True
return_value = True

return False
return return_value

def register_hook(self, name, hook):
"""Register a hook function for a given soho hook name."""
@@ -69,4 +71,3 @@ def register_hook(self, name, hook):
# =============================================================================

MANAGER = SohoHookManager()

@@ -23,7 +23,7 @@
# =============================================================================

class Test_AOVManager(unittest.TestCase):
"""Test ht.sohohooks.manager.AOVManager object."""
"""Test ht.sohohooks.aovs.manager.AOVManager object."""

def setUp(self):
super(Test_AOVManager, self).setUp()
@@ -789,7 +789,7 @@ def test_remove_group__match_interface(self, mock_groups, mock_interface):


class Test_AOVFile(unittest.TestCase):
"""Test ht.sohohooks.manager.AOVFile object."""
"""Test ht.sohohooks.aovs.manager.AOVFile object."""

@patch.object(manager.AOVFile, "_init_from_file")
@patch.object(manager.AOVFile, "exists", new_callable=PropertyMock(return_value=True))
@@ -1185,7 +1185,7 @@ def test_write_to_file(self, mock_groups, mock_aovs, mock_path, mock_dump):


class Test__find_aov_files(unittest.TestCase):
"""Test ht.sohohooks.manager._find_aov_files."""
"""Test ht.sohohooks.aovs.manager._find_aov_files."""

@patch("ht.sohohooks.aovs.manager.glob.glob")
@patch("ht.sohohooks.aovs.manager._get_aov_path_folders")
@@ -1229,7 +1229,7 @@ def test_houdini_path(self, mock_find, mock_glob):


class Test__find_houdinipath_aov_folders(unittest.TestCase):
"""Test ht.sohohooks.manager._find_houdinipath_aov_folders."""
"""Test ht.sohohooks.aovs.manager._find_houdinipath_aov_folders."""

@patch("ht.sohohooks.aovs.manager.hou.findDirectories")
def test_no_dirs(self, mock_find):
@@ -1250,7 +1250,7 @@ def test_dirs(self, mock_find):


class Test__get_aov_path_folders(unittest.TestCase):
"""Test ht.sohohooks.manager._get_aov_path_folders."""
"""Test ht.sohohooks.aovs.manager._get_aov_path_folders."""

def test_no_hpath(self):

@@ -1291,7 +1291,7 @@ def test_hpath_folders(self, mock_find):


class Test_build_menu_script(unittest.TestCase):
"""Test ht.sohohooks.manager.build_menu_script."""
"""Test ht.sohohooks.aovs.manager.build_menu_script."""

@patch("ht.sohohooks.aovs.manager.MANAGER", autospec=True)
def test_no_groups(self, mock_manager):
@@ -1333,7 +1333,7 @@ def test_with_groups(self, mock_manager):


class Test_flatten_aov_items(unittest.TestCase):
"""Test ht.sohohooks.manager.flatten_aov_items."""
"""Test ht.sohohooks.aovs.manager.flatten_aov_items."""

def test(self):
mock_aov = MagicMock(spec=manager.AOV)
@@ -1349,7 +1349,7 @@ def test(self):


class Test_load_json_files(unittest.TestCase):
"""Test ht.sohohooks.manager.load_json_files."""
"""Test ht.sohohooks.aovs.manager.load_json_files."""

@patch("ht.sohohooks.aovs.manager.MANAGER", autospec=True)
@patch("ht.sohohooks.aovs.manager.os.path.exists")
@@ -0,0 +1,162 @@
"""Test the ht.sohohooks.aovs.manager module."""

# =============================================================================
# IMPORTS
# =============================================================================

# Python Imports
from mock import MagicMock, PropertyMock, patch
import unittest

# Houdini Toolbox Imports
from ht.sohohooks import manager

reload(manager)

# =============================================================================
# CLASSES
# =============================================================================

class Test_SohoHookManager(unittest.TestCase):
"""Test ht.sohohooks.manager.SohoHookManager object."""

def setUp(self):
super(Test_SohoHookManager, self).setUp()

self.mock_api = MagicMock()

modules = {
"IFDapi": self.mock_api,
}

self.patcher = patch.dict("sys.modules", modules)
self.patcher.start()

def tearDown(self):
super(Test_SohoHookManager, self).tearDown()

self.patcher.stop()

def test___init__(self):
"""Test the __init__ method."""
mgr = manager.SohoHookManager()

self.assertEqual(mgr._hooks, {})

# Properties

# hooks

@patch.object(manager.SohoHookManager, "__init__", lambda x: None)
def test_hooks(self):
"""Test the 'hooks' property."""
mock_hooks = MagicMock(spec=dict)

mgr = manager.SohoHookManager()
mgr._hooks = mock_hooks

self.assertEqual(mgr.hooks, mock_hooks)

# Methods

# call_hook

@patch.object(manager.SohoHookManager, "hooks", new_callable=PropertyMock)
@patch.object(manager.SohoHookManager, "__init__", lambda x: None)
def test_call_hook__func_result_true(self, mock_hooks):
"""Test when a function returns a value that is equivalent to bool(value) == True."""
mock_hook_name = MagicMock(spec=str)
mock_hook = MagicMock()
mock_hook.return_value = True

mock_hooks.return_value = {mock_hook_name: [mock_hook]}

mock_arg = MagicMock()
mock_kwarg = MagicMock()

mgr = manager.SohoHookManager()

result = mgr.call_hook(mock_hook_name, mock_arg, foo=mock_kwarg)

self.assertTrue(result)

mock_hook.assert_called_with(mock_arg, foo=mock_kwarg)

@patch.object(manager.SohoHookManager, "hooks", new_callable=PropertyMock)
@patch.object(manager.SohoHookManager, "__init__", lambda x: None)
def test_call_hook__func_no_result(self, mock_hooks):
"""Test when a function returns no value."""
mock_hook_name = MagicMock(spec=str)
mock_hook = MagicMock()
mock_hook.return_value = None

mock_hooks.return_value = {mock_hook_name: [mock_hook]}

mock_arg = MagicMock()
mock_kwarg = MagicMock()

mgr = manager.SohoHookManager()

result = mgr.call_hook(mock_hook_name, mock_arg, foo=mock_kwarg)

self.assertFalse(result)

mock_hook.assert_called_with(mock_arg, foo=mock_kwarg)

@patch.object(manager.SohoHookManager, "hooks", new_callable=PropertyMock)
@patch.object(manager.SohoHookManager, "__init__", lambda x: None)
def test_call_hook__error(self, mock_hooks):
"""Test when calling a hook generates an exception."""
mock_hook_name = MagicMock(spec=str)
mock_hook = MagicMock()
mock_hook.side_effect = Exception

mock_hooks.return_value = {mock_hook_name: [mock_hook]}

mock_arg = MagicMock()
mock_kwarg = MagicMock()

mgr = manager.SohoHookManager()

result = mgr.call_hook(mock_hook_name, mock_arg, foo=mock_kwarg)

self.assertFalse(result)

mock_hook.assert_called_with(mock_arg, foo=mock_kwarg)

self.assertEqual(self.mock_api.ray_comment.call_count, 2)

# register_hook

@patch.object(manager.SohoHookManager, "hooks", new_callable=PropertyMock)
@patch.object(manager.SohoHookManager, "__init__", lambda x: None)
def test_register_hook(self, mock_hooks):
"""Test registering hooks."""
hooks = {}
mock_hooks.return_value = hooks

mock_hook_name1 = MagicMock(spec=str)
mock_hook_name3 = MagicMock(spec=str)

mock_hook1 = MagicMock()
mock_hook2 = MagicMock()
mock_hook3 = MagicMock()

mgr = manager.SohoHookManager()

mgr.register_hook(mock_hook_name1, mock_hook1)
mgr.register_hook(mock_hook_name1, mock_hook2)
mgr.register_hook(mock_hook_name3, mock_hook3)

expected = {
mock_hook_name1: [mock_hook1, mock_hook2],
mock_hook_name3: [mock_hook3]
}

self.assertEqual(hooks, expected)

# =============================================================================

if __name__ == '__main__':
# Run the tests.
unittest.main()

0 comments on commit 43c3959

Please sign in to comment.