test_neuron_module.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. def callback():
  26. pass
  27. NeuronModule.get_audio_from_stt(callback=callback())
  28. mock_orderListener_start.assert_called_once_with()
  29. mock_orderListener_start.reset_mock()
  30. def test_update_cache_var(self):
  31. """
  32. Test Update the value of the cache in the provided arg list
  33. """
  34. # True -> False
  35. args_dict = {
  36. "cache": True
  37. }
  38. expected_dict = {
  39. "cache": False
  40. }
  41. self.assertEquals(NeuronModule._update_cache_var(False, args_dict=args_dict),
  42. expected_dict,
  43. "Fail to update the cache value from True to False")
  44. self.assertFalse(args_dict["cache"])
  45. # False -> True
  46. args_dict = {
  47. "cache": False
  48. }
  49. expected_dict = {
  50. "cache": True
  51. }
  52. self.assertEquals(NeuronModule._update_cache_var(True, args_dict=args_dict),
  53. expected_dict,
  54. "Fail to update the cache value from False to True")
  55. self.assertTrue(args_dict["cache"])
  56. def test_get_message_from_dict(self):
  57. self.neuron_module_test.say_template = self.say_template
  58. self.assertEqual(self.neuron_module_test._get_message_from_dict(self.message), self.expected_result)
  59. del self.neuron_module_test
  60. self.neuron_module_test = NeuronModule()
  61. # test with file_template
  62. self.neuron_module_test.file_template = self.file_template
  63. self.assertEqual(self.neuron_module_test._get_message_from_dict(self.message), self.expected_result)
  64. del self.neuron_module_test
  65. # test with no say_template and no file_template
  66. self.neuron_module_test = NeuronModule()
  67. self.assertEqual(self.neuron_module_test._get_message_from_dict(self.message), None)
  68. def test_get_say_template(self):
  69. # test with a string
  70. self.assertEqual(NeuronModule._get_say_template(self.say_template, self.message), self.expected_result)
  71. # test with a list
  72. say_template = list()
  73. say_template.append("hello, this is a {{ test }} one")
  74. say_template.append("hello, this is a {{ test }} two")
  75. expected_result = list()
  76. expected_result.append("hello, this is a replaced word one")
  77. expected_result.append("hello, this is a replaced word two")
  78. self.assertTrue(NeuronModule._get_say_template(say_template, self.message) in expected_result)
  79. def test_get_file_template(self):
  80. # test with a valid template
  81. self.assertEqual(NeuronModule._get_file_template(self.file_template, self.message), self.expected_result)
  82. # test raise with a non existing template
  83. file_template = "does_not_exist.j2"
  84. with self.assertRaises(TemplateFileNotFoundException):
  85. NeuronModule._get_file_template(file_template, self.message)
  86. def test_get_content_of_file(self):
  87. expected_result = "hello, this is a {{ test }}"
  88. self.assertEqual(NeuronModule._get_content_of_file(self.file_template), expected_result)