test_neuron_launcher.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. # coding: utf-8
  2. import unittest
  3. import mock
  4. from kalliope.core.Models import Singleton
  5. from kalliope.core.Models.Resources import Resources
  6. from kalliope.core.NeuronLauncher import NeuronLauncher, NeuronParameterNotAvailable
  7. from kalliope.core.ConfigurationManager import SettingLoader
  8. from kalliope.core.Models.Neuron import Neuron
  9. class TestNeuronLauncher(unittest.TestCase):
  10. """
  11. Class to test Launchers Classes (TriggerLauncher, SynapseLauncher, NeuronLauncher) and methods
  12. """
  13. def setUp(self):
  14. # clean settings
  15. Singleton._instances = dict()
  16. def tearDown(self):
  17. Singleton._instances = dict()
  18. ####
  19. # Neurons Launcher
  20. def test_launch_neuron(self):
  21. """
  22. Test the Neuron Launcher trying to start a Neuron
  23. """
  24. neuron = Neuron(name='neurone1', parameters={'var1': 'val1'})
  25. sl = SettingLoader()
  26. resources = Resources(neuron_folder='/var/tmp/test/resources')
  27. sl.settings.resources = resources
  28. with mock.patch("kalliope.core.Utils.get_dynamic_class_instantiation") as mock_get_class_instantiation:
  29. NeuronLauncher.launch_neuron(neuron=neuron)
  30. mock_get_class_instantiation.assert_called_once_with(package_name="neurons",
  31. module_name=neuron.name,
  32. parameters=neuron.parameters,
  33. resources_dir=sl.settings.resources.neuron_folder)
  34. mock_get_class_instantiation.reset_mock()
  35. def test_start_neuron(self):
  36. """
  37. Testing params association and starting a Neuron
  38. """
  39. with mock.patch("kalliope.core.NeuronLauncher.launch_neuron") as mock_launch_neuron_method:
  40. # Assert to the neuron is launched with not parameter from order
  41. neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
  42. NeuronLauncher.start_neuron(neuron=neuron1)
  43. mock_launch_neuron_method.assert_called_with(neuron1)
  44. mock_launch_neuron_method.reset_mock()
  45. # Assert the params are well passed to the neuron
  46. neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2', 'var3': "{{ var3 }}"})
  47. params = {
  48. 'var3': 'value3'
  49. }
  50. NeuronLauncher.start_neuron(neuron=neuron2,
  51. parameters_dict=params)
  52. neuron2_params = Neuron(name='neurone2', parameters={'var2': 'val2', 'var3': 'value3'})
  53. mock_launch_neuron_method.assert_called_with(neuron2_params)
  54. mock_launch_neuron_method.reset_mock()
  55. # Assert the Neuron is not started when missing args
  56. neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3', 'var4': '{{val4}}'})
  57. params = {
  58. 'not_exist': 'test'
  59. }
  60. NeuronLauncher.start_neuron(neuron=neuron3,
  61. parameters_dict=params)
  62. mock_launch_neuron_method.assert_not_called()
  63. mock_launch_neuron_method.reset_mock()
  64. # Assert no neuron is launched when waiting for args and none are given
  65. neuron4 = Neuron(name='neurone4', parameters={'var5': 'val5', 'var6': '{{val6}}'})
  66. NeuronLauncher.start_neuron(neuron=neuron4)
  67. mock_launch_neuron_method.assert_not_called()
  68. mock_launch_neuron_method.reset_mock()
  69. def test_replace_brackets_by_loaded_parameter(self):
  70. # -------------------
  71. # test with string
  72. # -------------------
  73. # the target value to replace is present in the loaded parameter dict
  74. neuron_parameters = {
  75. "param1": "this is a value {{ replaced }}"
  76. }
  77. loaded_parameters = {
  78. "replaced": "replaced successfully"
  79. }
  80. expected_result = {
  81. "param1": "this is a value replaced successfully"
  82. }
  83. self.assertEqual(expected_result, NeuronLauncher._replace_brackets_by_loaded_parameter(neuron_parameters,
  84. loaded_parameters))
  85. # the target value with unicode to replace is present in the loaded parameter dict
  86. neuron_parameters = {
  87. "param1": "this is a value {{ replaced }}"
  88. }
  89. loaded_parameters = {
  90. "replaced": u"rêmpläcée successfülly"
  91. }
  92. expected_result = {
  93. "param1": "this is a value rêmpläcée successfülly"
  94. }
  95. self.assertEqual(expected_result, NeuronLauncher._replace_brackets_by_loaded_parameter(neuron_parameters,
  96. loaded_parameters))
  97. # the target value to replace is NOT present in the loaded parameter dict
  98. neuron_parameters = {
  99. "param1": "this is a value {{ replaced }}"
  100. }
  101. loaded_parameters = {
  102. "not_exist": "replaced successfully"
  103. }
  104. with self.assertRaises(NeuronParameterNotAvailable):
  105. NeuronLauncher._replace_brackets_by_loaded_parameter(neuron_parameters, loaded_parameters)
  106. # one parameter doesn't contains bracket, the other one do
  107. neuron_parameters = {
  108. "param1": "this is a value {{ replaced }}",
  109. "param2": "value"
  110. }
  111. loaded_parameters = {
  112. "replaced": "replaced successfully"
  113. }
  114. expected_result = {
  115. "param1": "this is a value replaced successfully",
  116. "param2": "value"
  117. }
  118. self.assertEqual(expected_result, NeuronLauncher._replace_brackets_by_loaded_parameter(neuron_parameters,
  119. loaded_parameters))
  120. # parameters are integer or boolean
  121. neuron_parameters = {
  122. "param1": 1,
  123. "param2": True
  124. }
  125. loaded_parameters = {
  126. "replaced": "replaced successfully"
  127. }
  128. expected_result = {
  129. "param1": 1,
  130. "param2": True
  131. }
  132. self.assertEqual(expected_result, NeuronLauncher._replace_brackets_by_loaded_parameter(neuron_parameters,
  133. loaded_parameters))
  134. # parameters are say_template or file template. Should not be altered by the loader
  135. neuron_parameters = {
  136. "say_template": "{{output}}",
  137. "file_template": "here is a file"
  138. }
  139. loaded_parameters = {
  140. "output": "should not be used"
  141. }
  142. expected_result = {
  143. "say_template": "{{output}}",
  144. "file_template": "here is a file"
  145. }
  146. self.assertEqual(expected_result, NeuronLauncher._replace_brackets_by_loaded_parameter(neuron_parameters,
  147. loaded_parameters))
  148. # replacing with variable
  149. sl = SettingLoader()
  150. sl.settings.variables = {
  151. "replaced": {
  152. "name": u'replaced successfully'
  153. }
  154. }
  155. neuron_parameters = {
  156. "param1": "this is a value {{ replaced['name'] }}"
  157. }
  158. loaded_parameters = {
  159. "name": "replaced successfully"
  160. }
  161. expected_result = {
  162. "param1": "this is a value replaced successfully"
  163. }
  164. self.assertEqual(expected_result, NeuronLauncher._replace_brackets_by_loaded_parameter(neuron_parameters,
  165. loaded_parameters))
  166. # the parameter is a reserved key. for example from_answer_link from the neurotransmitter
  167. list_reserved_keys = ["say_template", "file_template", "kalliope_memory", "from_answer_link"]
  168. for reserved_key in list_reserved_keys:
  169. neuron_parameters = {
  170. reserved_key: "this is a value with {{ 'brackets '}}"
  171. }
  172. loaded_parameters = dict()
  173. expected_result = {
  174. reserved_key: "this is a value with {{ 'brackets '}}"
  175. }
  176. self.assertEqual(expected_result, NeuronLauncher._replace_brackets_by_loaded_parameter(neuron_parameters,
  177. loaded_parameters))
  178. def test_parameters_are_available_in_loaded_parameters(self):
  179. # the parameter in bracket is available in the dict
  180. string_parameters = "this is a {{ parameter1 }}"
  181. loaded_parameters = {"parameter1": "value"}
  182. self.assertTrue(NeuronLauncher._neuron_parameters_are_available_in_loaded_parameters(string_parameters,
  183. loaded_parameters))
  184. # the parameter in bracket is NOT available in the dict
  185. string_parameters = "this is a {{ parameter1 }}"
  186. loaded_parameters = {"parameter2": "value"}
  187. self.assertFalse(NeuronLauncher._neuron_parameters_are_available_in_loaded_parameters(string_parameters,
  188. loaded_parameters))
  189. # the string_parameters doesn't contains bracket in bracket is available in the dict
  190. string_parameters = "this is a {{ parameter1 }}"
  191. loaded_parameters = {"parameter1": "value"}
  192. self.assertTrue(NeuronLauncher._neuron_parameters_are_available_in_loaded_parameters(string_parameters,
  193. loaded_parameters))
  194. # the string_parameters contains 2 parameters available in the dict
  195. string_parameters = "this is a {{ parameter1 }} and this is {{ parameter2 }}"
  196. loaded_parameters = {"parameter1": "value", "parameter2": "other value"}
  197. self.assertTrue(NeuronLauncher._neuron_parameters_are_available_in_loaded_parameters(string_parameters,
  198. loaded_parameters))
  199. # the string_parameters contains 2 parameters and one of them is not available in the dict
  200. string_parameters = "this is a {{ parameter1 }} and this is {{ parameter2 }}"
  201. loaded_parameters = {"parameter1": "value", "parameter3": "other value"}
  202. self.assertFalse(NeuronLauncher._neuron_parameters_are_available_in_loaded_parameters(string_parameters,
  203. loaded_parameters))
  204. if __name__ == '__main__':
  205. unittest.main()
  206. # suite = unittest.TestSuite()
  207. # suite.addTest(TestNeuronLauncher("test_replace_brackets_by_loaded_parameter"))
  208. # runner = unittest.TextTestRunner()
  209. # runner.run(suite)