test_hook_manager.py 3.4 KB

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