test_neuron_module.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_params(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_params({'cache': 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_params({'cache': 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. # {} -> {'language': 'en-US'}
  58. args_dict = {}
  59. expected_dict = {
  60. "language": "en-US"
  61. }
  62. self.assertEquals(NeuronModule._update_params({'language': 'en-US'}, args_dict=args_dict),
  63. expected_dict,
  64. "Fail to create new parameter: language key and value")
  65. def test_get_message_from_dict(self):
  66. self.neuron_module_test.say_template = self.say_template
  67. self.assertEqual(self.neuron_module_test._get_message_from_dict(self.message), self.expected_result)
  68. del self.neuron_module_test
  69. self.neuron_module_test = NeuronModule()
  70. # test with file_template
  71. self.neuron_module_test.file_template = self.file_template
  72. self.assertEqual(self.neuron_module_test._get_message_from_dict(self.message), self.expected_result)
  73. del self.neuron_module_test
  74. # test with no say_template and no file_template
  75. self.neuron_module_test = NeuronModule()
  76. self.assertEqual(self.neuron_module_test._get_message_from_dict(self.message), None)
  77. def test_get_say_template(self):
  78. # test with a string
  79. self.assertEqual(NeuronModule._get_say_template(self.say_template, self.message), self.expected_result)
  80. # test with a list
  81. say_template = list()
  82. say_template.append("hello, this is a {{ test }} one")
  83. say_template.append("hello, this is a {{ test }} two")
  84. expected_result = list()
  85. expected_result.append("hello, this is a replaced word one")
  86. expected_result.append("hello, this is a replaced word two")
  87. self.assertTrue(NeuronModule._get_say_template(say_template, self.message) in expected_result)
  88. def test_get_file_template(self):
  89. # test with a valid template
  90. self.assertEqual(NeuronModule._get_file_template(self.file_template, self.message), self.expected_result)
  91. # test raise with a non existing template
  92. file_template = "does_not_exist.j2"
  93. with self.assertRaises(TemplateFileNotFoundException):
  94. NeuronModule._get_file_template(file_template, self.message)
  95. def test_get_content_of_file(self):
  96. expected_result = "hello, this is a {{ test }}"
  97. self.assertEqual(NeuronModule._get_content_of_file(self.file_template), expected_result)
  98. def test_serialize(self):
  99. """
  100. Test the serialisation of the neuron module
  101. """
  102. neuron_module = NeuronModule()
  103. neuron_module.neuron_name = "kalliope"
  104. neuron_module.tts_message = "I am french"
  105. expected_result = {
  106. 'neuron_name': "kalliope",
  107. 'generated_message': "I am french"
  108. }
  109. self.assertEqual(expected_result, neuron_module.serialize())