test_neuron_module.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import os
  2. import unittest
  3. import mock
  4. from kalliope.core.NeuronModule import NeuronModule, TemplateFileNotFoundException
  5. class TestNeuronModule(unittest.TestCase):
  6. def setUp(self):
  7. self.expected_result = "hello, this is a replaced word"
  8. # this allow us to run the test from an IDE and from the root with python -m unittest Tests.TestNeuronModule
  9. if "/Tests" in os.getcwd():
  10. self.file_template = "templates/template_test.j2"
  11. else:
  12. self.file_template = "Tests/templates/template_test.j2"
  13. self.say_template = "hello, this is a {{ test }}"
  14. self.message = {
  15. "test": "replaced word"
  16. }
  17. self.neuron_module_test = NeuronModule()
  18. def tearDown(self):
  19. del self.neuron_module_test
  20. def test_get_audio_from_stt(self):
  21. """
  22. Test the OrderListener thread is started
  23. """
  24. with mock.patch("kalliope.core.OrderListener.start") as mock_orderListener_start:
  25. with mock.patch("kalliope.core.OrderListener.join") as mock_orderListener_join:
  26. def callback():
  27. pass
  28. self.neuron_module_test.get_audio_from_stt(callback=callback())
  29. mock_orderListener_start.assert_called_once_with()
  30. mock_orderListener_start.reset_mock()
  31. def test_update_cache_var(self):
  32. """
  33. Test Update the value of the cache in the provided arg list
  34. """
  35. # True -> False
  36. args_dict = {
  37. "cache": True
  38. }
  39. expected_dict = {
  40. "cache": False
  41. }
  42. self.assertEquals(NeuronModule._update_cache_var(False, args_dict=args_dict),
  43. expected_dict,
  44. "Fail to update the cache value from True to False")
  45. self.assertFalse(args_dict["cache"])
  46. # False -> True
  47. args_dict = {
  48. "cache": False
  49. }
  50. expected_dict = {
  51. "cache": True
  52. }
  53. self.assertEquals(NeuronModule._update_cache_var(True, args_dict=args_dict),
  54. expected_dict,
  55. "Fail to update the cache value from False to True")
  56. self.assertTrue(args_dict["cache"])
  57. def test_get_message_from_dict(self):
  58. self.neuron_module_test.say_template = self.say_template
  59. self.assertEqual(self.neuron_module_test._get_message_from_dict(self.message), self.expected_result)
  60. del self.neuron_module_test
  61. self.neuron_module_test = NeuronModule()
  62. # test with file_template
  63. self.neuron_module_test.file_template = self.file_template
  64. self.assertEqual(self.neuron_module_test._get_message_from_dict(self.message), self.expected_result)
  65. del self.neuron_module_test
  66. # test with no say_template and no file_template
  67. self.neuron_module_test = NeuronModule()
  68. self.assertEqual(self.neuron_module_test._get_message_from_dict(self.message), None)
  69. def test_get_say_template(self):
  70. # test with a string
  71. self.assertEqual(NeuronModule._get_say_template(self.say_template, self.message), self.expected_result)
  72. # test with a list
  73. say_template = list()
  74. say_template.append("hello, this is a {{ test }} one")
  75. say_template.append("hello, this is a {{ test }} two")
  76. expected_result = list()
  77. expected_result.append("hello, this is a replaced word one")
  78. expected_result.append("hello, this is a replaced word two")
  79. self.assertTrue(NeuronModule._get_say_template(say_template, self.message) in expected_result)
  80. def test_get_file_template(self):
  81. # test with a valid template
  82. self.assertEqual(NeuronModule._get_file_template(self.file_template, self.message), self.expected_result)
  83. # test raise with a non existing template
  84. file_template = "does_not_exist.j2"
  85. with self.assertRaises(TemplateFileNotFoundException):
  86. NeuronModule._get_file_template(file_template, self.message)
  87. def test_get_content_of_file(self):
  88. expected_result = "hello, this is a {{ test }}"
  89. self.assertEqual(NeuronModule._get_content_of_file(self.file_template), expected_result)
  90. def test_serialize(self):
  91. """
  92. Test the serialisation of the neuron module
  93. """
  94. neuron_module = NeuronModule()
  95. neuron_module.neuron_name = "kalliope"
  96. neuron_module.tts_message = "I am french"
  97. expected_result = {
  98. 'neuron_name': "kalliope",
  99. 'generated_message': "I am french"
  100. }
  101. self.assertEqual(expected_result, neuron_module.serialize())