test_neuron_module.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import os
  2. import unittest
  3. import mock
  4. from kalliope.core.Models import Singleton
  5. from kalliope.core.Models.Tts import Tts
  6. from kalliope import SettingLoader
  7. from kalliope.core.NeuronModule import NeuronModule, TemplateFileNotFoundException, TTSModuleNotFound
  8. class TestNeuronModule(unittest.TestCase):
  9. def setUp(self):
  10. # kill singleton
  11. Singleton._instances = dict()
  12. self.expected_result = "hello, this is a replaced word"
  13. # this allow us to run the test from an IDE and from the root with python -m unittest tests.TestNeuronModule
  14. if "/Tests" in os.getcwd():
  15. self.file_template = "templates/template_test.j2"
  16. else:
  17. self.file_template = "Tests/templates/template_test.j2"
  18. self.say_template = "hello, this is a {{ test }}"
  19. self.message = {
  20. "test": "replaced word"
  21. }
  22. self.neuron_module_test = NeuronModule()
  23. if "/Tests" in os.getcwd():
  24. self.file_settings = "settings/settings_test.yml"
  25. else:
  26. self.file_settings = "Tests/settings/settings_test.yml"
  27. self.settings = SettingLoader(file_path=self.file_settings).settings
  28. def tearDown(self):
  29. del self.neuron_module_test
  30. def test_get_audio_from_stt(self):
  31. """
  32. Test the OrderListener thread is started
  33. """
  34. with mock.patch("kalliope.core.OrderListener.start") as mock_orderListener_start:
  35. with mock.patch("kalliope.core.OrderListener.join"):
  36. def callback():
  37. pass
  38. self.neuron_module_test.get_audio_from_stt(callback=callback())
  39. mock_orderListener_start.assert_called_once_with()
  40. mock_orderListener_start.reset_mock()
  41. def test_get_tts_object(self):
  42. # no TTS name provided. should return the default tts
  43. expected_tts = Tts(name="pico2wave", parameters={"language": "fr-FR", "cache": True})
  44. self.assertEqual(NeuronModule._get_tts_object(settings=self.settings), expected_tts)
  45. # TTS provided, only cache parameter updated
  46. expected_tts = Tts(name="pico2wave", parameters={"language": "fr-FR", "cache": False})
  47. self.assertEqual(NeuronModule._get_tts_object(tts_name="pico2wave",
  48. override_parameter={"cache": False},
  49. settings=self.settings), expected_tts)
  50. # TTS provided, all parameters updated
  51. expected_tts = Tts(name="pico2wave", parameters={"language": "es-ES", "cache": False})
  52. self.assertEqual(NeuronModule._get_tts_object(tts_name="pico2wave",
  53. override_parameter={"language": "es-ES", "cache": False},
  54. settings=self.settings), expected_tts)
  55. # TTS not existing in settings
  56. with self.assertRaises(TTSModuleNotFound):
  57. NeuronModule._get_tts_object(tts_name="no_existing_tts",
  58. override_parameter={"cache": False},
  59. settings=self.settings)
  60. def get_message_from_dict(self):
  61. # TODO not working in pycharm
  62. with mock.patch.object(NeuronModule, 'say', return_value=None) as mock_method:
  63. self.neuron_module_test.say_template = self.say_template
  64. self.assertEqual(self.neuron_module_test._get_message_from_dict(self.message), self.expected_result)
  65. del self.neuron_module_test
  66. self.neuron_module_test = NeuronModule()
  67. # test with file_template
  68. self.neuron_module_test.file_template = self.file_template
  69. self.assertEqual(self.neuron_module_test._get_message_from_dict(self.message), self.expected_result)
  70. del self.neuron_module_test
  71. # test with no say_template and no file_template
  72. self.neuron_module_test = NeuronModule()
  73. self.assertEqual(self.neuron_module_test._get_message_from_dict(self.message), None)
  74. def test_get_say_template(self):
  75. # test with a string
  76. self.assertEqual(NeuronModule._get_say_template(self.say_template, self.message), self.expected_result)
  77. # test with a list
  78. say_template = list()
  79. say_template.append("hello, this is a {{ test }} one")
  80. say_template.append("hello, this is a {{ test }} two")
  81. expected_result = list()
  82. expected_result.append("hello, this is a replaced word one")
  83. expected_result.append("hello, this is a replaced word two")
  84. self.assertTrue(NeuronModule._get_say_template(say_template, self.message) in expected_result)
  85. def test_get_file_template(self):
  86. # test with a valid template
  87. self.assertEqual(NeuronModule._get_file_template(self.file_template, self.message), self.expected_result)
  88. # test raise with a non existing template
  89. file_template = "does_not_exist.j2"
  90. with self.assertRaises(TemplateFileNotFoundException):
  91. NeuronModule._get_file_template(file_template, self.message)
  92. def test_get_content_of_file(self):
  93. expected_result = "hello, this is a {{ test }}"
  94. self.assertEqual(NeuronModule._get_content_of_file(self.file_template), expected_result)
  95. def test_serialize(self):
  96. """
  97. Test the serialisation of the neuron module
  98. """
  99. neuron_module = NeuronModule()
  100. neuron_module.neuron_name = "kalliope"
  101. neuron_module.tts_message = "I am french"
  102. expected_result = {
  103. 'neuron_name': "kalliope",
  104. 'generated_message': "I am french"
  105. }
  106. self.assertEqual(expected_result, neuron_module.serialize())
  107. if __name__ == '__main__':
  108. unittest.main()