test_hook_manager.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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_list_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_processed_synapses(self):
  60. self.assertIsNone(HookManager.on_processed_synapses())
  61. def test_on_deaf(self):
  62. """
  63. test that empty list of synapse return none
  64. """
  65. self.assertIsNone(HookManager.on_deaf())
  66. if __name__ == '__main__':
  67. unittest.main()
  68. # suite = unittest.TestSuite()
  69. # suite.addTest(TestInit("test_main"))
  70. # runner = unittest.TextTestRunner()
  71. # runner.run(suite)