test_hook_manager.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import unittest
  2. import os
  3. import mock as mock
  4. import inspect
  5. import shutil
  6. from kalliope.core.Models import Singleton
  7. from kalliope.core.ConfigurationManager import SettingLoader
  8. from kalliope.core import HookManager
  9. from kalliope.core.Models.Settings import Settings
  10. class TestInit(unittest.TestCase):
  11. def setUp(self):
  12. # Init the folders, otherwise it raises an exceptions
  13. os.makedirs("/tmp/kalliope/tests/kalliope_resources_dir/neurons")
  14. os.makedirs("/tmp/kalliope/tests/kalliope_resources_dir/stt")
  15. os.makedirs("/tmp/kalliope/tests/kalliope_resources_dir/tts")
  16. os.makedirs("/tmp/kalliope/tests/kalliope_resources_dir/trigger")
  17. # get current script directory path. We are in /an/unknown/path/kalliope/core/tests
  18. cur_script_directory = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
  19. # get parent dir. Now we are in /an/unknown/path/kalliope
  20. root_dir = os.path.normpath(cur_script_directory + os.sep + os.pardir)
  21. self.settings_file_to_test = root_dir + os.sep + "Tests/settings/settings_test.yml"
  22. self.settings = SettingLoader(file_path=self.settings_file_to_test)
  23. def tearDown(self):
  24. # Cleanup
  25. shutil.rmtree('/tmp/kalliope/tests/kalliope_resources_dir')
  26. Singleton._instances = {}
  27. def test_on_start(self):
  28. """
  29. test list of synapse
  30. """
  31. with mock.patch("kalliope.core.SynapseLauncher.start_synapse_by_list_name") as mock_synapse_launcher:
  32. HookManager.on_start()
  33. mock_synapse_launcher.assert_called_with(["on-start-synapse", "bring-led-on"], new_lifo=True)
  34. mock_synapse_launcher.reset_mock()
  35. def test_on_waiting_for_trigger(self):
  36. """
  37. test with single synapse
  38. """
  39. with mock.patch("kalliope.core.SynapseLauncher.start_synapse_by_name") as mock_synapse_launcher:
  40. HookManager.on_waiting_for_trigger()
  41. mock_synapse_launcher.assert_called_with("test", new_lifo=True)
  42. mock_synapse_launcher.reset_mock()
  43. def test_on_triggered(self):
  44. with mock.patch("kalliope.core.SynapseLauncher.start_synapse_by_list_name") as mock_synapse_launcher:
  45. HookManager.on_triggered()
  46. mock_synapse_launcher.assert_called_with(["on-triggered-synapse"], new_lifo=True)
  47. mock_synapse_launcher.reset_mock()
  48. def test_on_start_listening(self):
  49. self.assertIsNone(HookManager.on_start_listening())
  50. def test_on_stop_listening(self):
  51. self.assertIsNone(HookManager.on_stop_listening())
  52. def test_on_order_found(self):
  53. self.assertIsNone(HookManager.on_order_found())
  54. def test_on_order_not_found(self):
  55. with mock.patch("kalliope.core.SynapseLauncher.start_synapse_by_list_name") as mock_synapse_launcher:
  56. HookManager.on_order_not_found()
  57. mock_synapse_launcher.assert_called_with(["order-not-found-synapse"], new_lifo=True)
  58. mock_synapse_launcher.reset_mock()
  59. def test_on_mute(self):
  60. """
  61. test that empty list of synapse return none
  62. """
  63. self.assertIsNone(HookManager.on_mute())
  64. if __name__ == '__main__':
  65. unittest.main()
  66. # suite = unittest.TestSuite()
  67. # suite.addTest(TestInit("test_main"))
  68. # runner = unittest.TextTestRunner()
  69. # runner.run(suite)