test_neuron_module.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import os
  2. import unittest
  3. import mock
  4. from kalliope.core.NeuronModule import NeuronModule, TemplateFileNotFoundException
  5. from kalliope.core.Models.Neuron import Neuron
  6. from kalliope.core.Models.Synapse import Synapse
  7. from kalliope.core.Models.Brain import Brain
  8. from kalliope.core.Models.Order import Order
  9. class TestNeuronModule(unittest.TestCase):
  10. def setUp(self):
  11. self.expected_result = "hello, this is a replaced word"
  12. # this allow us to run the test from an IDE and from the root with python -m unittest Tests.TestNeuronModule
  13. if "/Tests" in os.getcwd():
  14. self.file_template = "templates/template_test.j2"
  15. else:
  16. self.file_template = "Tests/templates/template_test.j2"
  17. self.say_template = "hello, this is a {{ test }}"
  18. self.message = {
  19. "test": "replaced word"
  20. }
  21. self.neuron_module_test = NeuronModule()
  22. def tearDown(self):
  23. del self.neuron_module_test
  24. def test_get_audio_from_stt(self):
  25. """
  26. Test the OrderListener thread is started
  27. """
  28. with mock.patch("kalliope.core.OrderListener.start") as mock_orderListener_start:
  29. def callback():
  30. pass
  31. NeuronModule.get_audio_from_stt(callback=callback())
  32. mock_orderListener_start.assert_called_once_with()
  33. mock_orderListener_start.reset_mock()
  34. def test_update_cache_var(self):
  35. """
  36. Test Update the value of the cache in the provided arg list
  37. """
  38. # True -> False
  39. args_dict = {
  40. "cache": True
  41. }
  42. expected_dict = {
  43. "cache": False
  44. }
  45. self.assertEquals(NeuronModule._update_cache_var(False, args_dict=args_dict),
  46. expected_dict,
  47. "Fail to update the cache value from True to False")
  48. self.assertFalse(args_dict["cache"])
  49. # False -> True
  50. args_dict = {
  51. "cache": False
  52. }
  53. expected_dict = {
  54. "cache": True
  55. }
  56. self.assertEquals(NeuronModule._update_cache_var(True, args_dict=args_dict),
  57. expected_dict,
  58. "Fail to update the cache value from False to True")
  59. self.assertTrue(args_dict["cache"])
  60. def test_get_message_from_dict(self):
  61. self.neuron_module_test.say_template = self.say_template
  62. self.assertEqual(self.neuron_module_test._get_message_from_dict(self.message), self.expected_result)
  63. del self.neuron_module_test
  64. self.neuron_module_test = NeuronModule()
  65. # test with file_template
  66. self.neuron_module_test.file_template = self.file_template
  67. self.assertEqual(self.neuron_module_test._get_message_from_dict(self.message), self.expected_result)
  68. del self.neuron_module_test
  69. # test with no say_template and no file_template
  70. self.neuron_module_test = NeuronModule()
  71. self.assertEqual(self.neuron_module_test._get_message_from_dict(self.message), None)
  72. def test_get_say_template(self):
  73. # test with a string
  74. self.assertEqual(NeuronModule._get_say_template(self.say_template, self.message), self.expected_result)
  75. # test with a list
  76. say_template = list()
  77. say_template.append("hello, this is a {{ test }} one")
  78. say_template.append("hello, this is a {{ test }} two")
  79. expected_result = list()
  80. expected_result.append("hello, this is a replaced word one")
  81. expected_result.append("hello, this is a replaced word two")
  82. self.assertTrue(NeuronModule._get_say_template(say_template, self.message) in expected_result)
  83. def test_get_file_template(self):
  84. # test with a valid template
  85. self.assertEqual(NeuronModule._get_file_template(self.file_template, self.message), self.expected_result)
  86. # test raise with a non existing template
  87. file_template = "does_not_exist.j2"
  88. with self.assertRaises(TemplateFileNotFoundException):
  89. NeuronModule._get_file_template(file_template, self.message)
  90. def test_get_content_of_file(self):
  91. expected_result = "hello, this is a {{ test }}"
  92. self.assertEqual(NeuronModule._get_content_of_file(self.file_template), expected_result)
  93. def test_run_synapse_by_name_with_order(self):
  94. """
  95. Test to start a synapse with a specific given order
  96. Scenarii :
  97. - Neuron has been found and launched
  98. - Neuron has not been found
  99. """
  100. # Init
  101. neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
  102. neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
  103. neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
  104. neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})
  105. signal1 = Order(sentence="the sentence")
  106. signal2 = Order(sentence="the second sentence")
  107. signal3 = Order(sentence="part of the third sentence")
  108. synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
  109. synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2])
  110. synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3])
  111. all_synapse_list = [synapse1,
  112. synapse2,
  113. synapse3]
  114. br = Brain(synapses=all_synapse_list)
  115. order = "This is the order"
  116. synapse_name = "Synapse2"
  117. answer = "This is the {{ answer }}"
  118. with mock.patch("kalliope.core.OrderAnalyser.start") as mock_orderAnalyser_start:
  119. neuron_mod = NeuronModule()
  120. neuron_mod.brain = br
  121. # Success
  122. self.assertTrue(neuron_mod.run_synapse_by_name_with_order(order=order,
  123. synapse_name=synapse_name,
  124. order_template=answer),
  125. "fail to find the proper synapse")
  126. # mock_orderAnalyser_start.assert_called_once()
  127. mock_orderAnalyser_start.assert_called_once_with(synapses_to_run=[synapse2],
  128. external_order=answer)
  129. mock_orderAnalyser_start.reset_mock()
  130. # Fail
  131. synapse_name = "Synapse5"
  132. self.assertFalse(neuron_mod.run_synapse_by_name_with_order(order=order,
  133. synapse_name=synapse_name,
  134. order_template=answer),
  135. "fail to NOT find the synapse")
  136. mock_orderAnalyser_start.assert_not_called()
  137. mock_orderAnalyser_start.reset_mock()