test_synapse_launcher.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import unittest
  2. import mock
  3. from kalliope.core import LIFOBuffer, LifoManager
  4. from kalliope.core.Models import Brain, Signal, Singleton
  5. from kalliope.core.Models.MatchedSynapse import MatchedSynapse
  6. from kalliope.core.Models.Settings import Settings
  7. from kalliope.core.SynapseLauncher import SynapseLauncher, SynapseNameNotFound
  8. from kalliope.core.Models import Neuron
  9. from kalliope.core.Models import Synapse
  10. class TestSynapseLauncher(unittest.TestCase):
  11. """
  12. Test the class SynapseLauncher
  13. """
  14. def setUp(self):
  15. # Init
  16. neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
  17. neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
  18. neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
  19. neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})
  20. signal1 = Signal(name="order", parameters="this is the sentence")
  21. signal2 = Signal(name="order", parameters="this is the second sentence")
  22. signal3 = Signal(name="order", parameters="that is part of the third sentence")
  23. self.synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
  24. self.synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2])
  25. self.synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3])
  26. self.all_synapse_list = [self.synapse1,
  27. self.synapse2,
  28. self.synapse3]
  29. self.brain_test = Brain(synapses=self.all_synapse_list)
  30. self.settings_test = Settings()
  31. # clean the LiFO
  32. Singleton._instances = dict()
  33. LifoManager.clean_saved_lifo()
  34. def test_start_synapse_by_name(self):
  35. # existing synapse in the brain
  36. with mock.patch("kalliope.core.Lifo.LIFOBuffer.execute"):
  37. should_be_created_matched_synapse = MatchedSynapse(matched_synapse=self.synapse1)
  38. SynapseLauncher.start_synapse_by_name("Synapse1", brain=self.brain_test)
  39. # we expect that the lifo has been loaded with the synapse to run
  40. expected_result = [[should_be_created_matched_synapse]]
  41. lifo_buffer = LifoManager.get_singleton_lifo()
  42. self.assertEqual(expected_result, lifo_buffer.lifo_list)
  43. # we expect that the lifo has been loaded with the synapse to run and overwritten parameters
  44. Singleton._instances = dict()
  45. LifoManager.clean_saved_lifo()
  46. lifo_buffer = LifoManager.get_singleton_lifo()
  47. overriding_param = {
  48. "val1": "val"
  49. }
  50. SynapseLauncher.start_synapse_by_name("Synapse1", brain=self.brain_test,
  51. overriding_parameter_dict=overriding_param)
  52. should_be_created_matched_synapse = MatchedSynapse(matched_synapse=self.synapse1,
  53. overriding_parameter=overriding_param)
  54. # we expect that the lifo has been loaded with the synapse to run
  55. expected_result = [[should_be_created_matched_synapse]]
  56. self.assertEqual(expected_result, lifo_buffer.lifo_list)
  57. # non existing synapse in the brain
  58. with self.assertRaises(SynapseNameNotFound):
  59. SynapseLauncher.start_synapse_by_name("not_existing", brain=self.brain_test)
  60. def test_start_synapse_by_list_name(self):
  61. # test to start a list of synapse
  62. with mock.patch("kalliope.core.Lifo.LIFOBuffer.execute"):
  63. created_matched_synapse1 = MatchedSynapse(matched_synapse=self.synapse1)
  64. created_matched_synapse2 = MatchedSynapse(matched_synapse=self.synapse2)
  65. expected_list_matched_synapse = [created_matched_synapse1, created_matched_synapse2]
  66. SynapseLauncher.start_synapse_by_list_name(["Synapse1", "Synapse2"], brain=self.brain_test)
  67. # we expect that the lifo has been loaded with the synapse to run
  68. expected_result = [expected_list_matched_synapse]
  69. lifo_buffer = LifoManager.get_singleton_lifo()
  70. self.maxDiff = None
  71. self.assertEqual(expected_result, lifo_buffer.lifo_list)
  72. # empty list should return none
  73. empty_list = list()
  74. self.assertIsNone(SynapseLauncher.start_synapse_by_list_name(empty_list))
  75. # test to start a synapse list with a new lifo
  76. # we create a Lifo that is the current singleton
  77. Singleton._instances = dict()
  78. LifoManager.clean_saved_lifo()
  79. lifo_buffer = LifoManager.get_singleton_lifo()
  80. created_matched_synapse1 = MatchedSynapse(matched_synapse=self.synapse1)
  81. lifo_buffer.lifo_list = [created_matched_synapse1]
  82. # the current status of the singleton lifo should not move even after the call of SynapseLauncher
  83. expected_result = [created_matched_synapse1]
  84. # create a new call
  85. with mock.patch("kalliope.core.Lifo.LIFOBuffer.execute"):
  86. SynapseLauncher.start_synapse_by_list_name(["Synapse2", "Synapse3"],
  87. brain=self.brain_test,
  88. new_lifo=True)
  89. # the current singleton should be the same
  90. self.assertEqual(expected_result, lifo_buffer.lifo_list)
  91. # test to start a synapse list with the singleton lifo
  92. Singleton._instances = dict()
  93. LifoManager.clean_saved_lifo()
  94. lifo_buffer = LifoManager.get_singleton_lifo()
  95. created_matched_synapse1 = MatchedSynapse(matched_synapse=self.synapse1)
  96. # place a synapse in the singleton
  97. lifo_buffer.lifo_list = [created_matched_synapse1]
  98. # the current status of the singleton lifo should contain synapse launched in the next call
  99. created_matched_synapse2 = MatchedSynapse(matched_synapse=self.synapse2)
  100. created_matched_synapse3 = MatchedSynapse(matched_synapse=self.synapse3)
  101. expected_result = [created_matched_synapse1, [created_matched_synapse2, created_matched_synapse3]]
  102. with mock.patch("kalliope.core.Lifo.LIFOBuffer.execute"):
  103. SynapseLauncher.start_synapse_by_list_name(["Synapse2", "Synapse3"],
  104. brain=self.brain_test)
  105. # the singleton should now contains the synapse that was already there and the 2 other synapses
  106. self.assertEqual(expected_result, lifo_buffer.lifo_list)
  107. def test_run_matching_synapse_from_order(self):
  108. # ------------------
  109. # test_match_synapse1
  110. # ------------------
  111. with mock.patch("kalliope.core.Lifo.LIFOBuffer.execute"):
  112. order_to_match = "this is the sentence"
  113. should_be_created_matched_synapse = MatchedSynapse(matched_synapse=self.synapse1,
  114. user_order=order_to_match,
  115. matched_order="this is the sentence")
  116. expected_result = [[should_be_created_matched_synapse]]
  117. SynapseLauncher.run_matching_synapse_from_order(order_to_match,
  118. brain=self.brain_test,
  119. settings=self.settings_test)
  120. lifo_buffer = LifoManager.get_singleton_lifo()
  121. self.assertEqual(expected_result, lifo_buffer.lifo_list)
  122. # -------------------------
  123. # test_match_synapse1_and_2
  124. # -------------------------
  125. # clean LIFO
  126. Singleton._instances = dict()
  127. LifoManager.clean_saved_lifo()
  128. with mock.patch("kalliope.core.Lifo.LIFOBuffer.execute"):
  129. order_to_match = "this is the second sentence"
  130. should_be_created_matched_synapse1 = MatchedSynapse(matched_synapse=self.synapse1,
  131. user_order=order_to_match,
  132. matched_order="this is the sentence")
  133. should_be_created_matched_synapse2 = MatchedSynapse(matched_synapse=self.synapse2,
  134. user_order=order_to_match,
  135. matched_order="this is the second sentence")
  136. expected_result = [[should_be_created_matched_synapse1, should_be_created_matched_synapse2]]
  137. SynapseLauncher.run_matching_synapse_from_order(order_to_match,
  138. brain=self.brain_test,
  139. settings=self.settings_test)
  140. lifo_buffer = LifoManager.get_singleton_lifo()
  141. self.assertEqual(expected_result, lifo_buffer.lifo_list)
  142. # -------------------------
  143. # test_call_hook_order_not_found
  144. # -------------------------
  145. # clean LIFO
  146. Singleton._instances = dict()
  147. LifoManager.clean_saved_lifo()
  148. with mock.patch("kalliope.core.HookManager.on_order_not_found") as mock_hook:
  149. order_to_match = "not existing sentence"
  150. SynapseLauncher.run_matching_synapse_from_order(order_to_match,
  151. brain=self.brain_test,
  152. settings=self.settings_test)
  153. mock_hook.assert_called_with()
  154. mock_hook.reset_mock()
  155. # -------------------------
  156. # test_call_hook_order_found
  157. # -------------------------
  158. # clean LIFO
  159. Singleton._instances = dict()
  160. with mock.patch("kalliope.core.Lifo.LIFOBuffer.execute"):
  161. with mock.patch("kalliope.core.HookManager.on_order_found") as mock_hook:
  162. order_to_match = "this is the second sentence"
  163. new_settings = Settings()
  164. SynapseLauncher.run_matching_synapse_from_order(order_to_match,
  165. brain=self.brain_test,
  166. settings=new_settings)
  167. mock_hook.assert_called_with()
  168. mock_hook.reset_mock()
  169. if __name__ == '__main__':
  170. unittest.main()
  171. # suite = unittest.TestSuite()
  172. # suite.addTest(TestSynapseLauncher("test_start_synapse_by_list_name"))
  173. # runner = unittest.TextTestRunner()
  174. # runner.run(suite)