test_synapse_launcher.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import unittest
  2. import mock
  3. from kalliope.core import LIFOBuffer
  4. from kalliope.core.Models import Brain, Signal
  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(default_synapse="Synapse3")
  31. # clean the LiFO
  32. LIFOBuffer.lifo_list = list()
  33. def test_start_synapse_by_name(self):
  34. # existing synapse in the brain
  35. with mock.patch("kalliope.core.LIFOBuffer.execute"):
  36. should_be_created_matched_synapse = MatchedSynapse(matched_synapse=self.synapse1)
  37. SynapseLauncher.start_synapse_by_name("Synapse1", brain=self.brain_test)
  38. # we expect that the lifo has been loaded with the synapse to run
  39. expected_result = [[should_be_created_matched_synapse]]
  40. self.assertEqual(expected_result, LIFOBuffer.lifo_list)
  41. # we expect that the lifo has been loaded with the synapse to run and overwritten parameters
  42. # clean the LiFO
  43. LIFOBuffer.lifo_list = list()
  44. overriding_param = {
  45. "val1": "val"
  46. }
  47. SynapseLauncher.start_synapse_by_name("Synapse1", brain=self.brain_test,
  48. overriding_parameter_dict=overriding_param)
  49. should_be_created_matched_synapse = MatchedSynapse(matched_synapse=self.synapse1,
  50. overriding_parameter=overriding_param)
  51. # we expect that the lifo has been loaded with the synapse to run
  52. expected_result = [[should_be_created_matched_synapse]]
  53. self.assertEqual(expected_result, LIFOBuffer.lifo_list)
  54. # non existing synapse in the brain
  55. with self.assertRaises(SynapseNameNotFound):
  56. SynapseLauncher.start_synapse_by_name("not_existing", brain=self.brain_test)
  57. def test_run_matching_synapse_from_order(self):
  58. # ------------------
  59. # test_match_synapse1
  60. # ------------------
  61. with mock.patch("kalliope.core.LIFOBuffer.execute"):
  62. order_to_match = "this is the sentence"
  63. should_be_created_matched_synapse = MatchedSynapse(matched_synapse=self.synapse1,
  64. user_order=order_to_match,
  65. matched_order="this is the sentence")
  66. expected_result = [[should_be_created_matched_synapse]]
  67. SynapseLauncher.run_matching_synapse_from_order(order_to_match,
  68. brain=self.brain_test,
  69. settings=self.settings_test)
  70. self.assertEqual(expected_result, LIFOBuffer.lifo_list)
  71. # -------------------------
  72. # test_match_synapse1_and_2
  73. # -------------------------
  74. # clean LIFO
  75. LIFOBuffer.lifo_list = list()
  76. with mock.patch("kalliope.core.LIFOBuffer.execute"):
  77. order_to_match = "this is the second sentence"
  78. should_be_created_matched_synapse1 = MatchedSynapse(matched_synapse=self.synapse1,
  79. user_order=order_to_match,
  80. matched_order="this is the sentence")
  81. should_be_created_matched_synapse2 = MatchedSynapse(matched_synapse=self.synapse2,
  82. user_order=order_to_match,
  83. matched_order="this is the second sentence")
  84. expected_result = [[should_be_created_matched_synapse1, should_be_created_matched_synapse2]]
  85. SynapseLauncher.run_matching_synapse_from_order(order_to_match,
  86. brain=self.brain_test,
  87. settings=self.settings_test)
  88. self.assertEqual(expected_result, LIFOBuffer.lifo_list)
  89. # -------------------------
  90. # test_match_default_synapse
  91. # -------------------------
  92. # clean LIFO
  93. LIFOBuffer.lifo_list = list()
  94. with mock.patch("kalliope.core.LIFOBuffer.execute"):
  95. order_to_match = "not existing sentence"
  96. should_be_created_matched_synapse = MatchedSynapse(matched_synapse=self.synapse3,
  97. user_order=order_to_match,
  98. matched_order=None)
  99. expected_result = [[should_be_created_matched_synapse]]
  100. SynapseLauncher.run_matching_synapse_from_order(order_to_match,
  101. brain=self.brain_test,
  102. settings=self.settings_test)
  103. self.assertEqual(expected_result, LIFOBuffer.lifo_list)
  104. # -------------------------
  105. # test_no_match_and_no_default_synapse
  106. # -------------------------
  107. # clean LIFO
  108. LIFOBuffer.lifo_list = list()
  109. with mock.patch("kalliope.core.LIFOBuffer.execute"):
  110. order_to_match = "not existing sentence"
  111. new_settings = Settings()
  112. expected_result = [[]]
  113. SynapseLauncher.run_matching_synapse_from_order(order_to_match,
  114. brain=self.brain_test,
  115. settings=new_settings)
  116. self.assertEqual(expected_result, LIFOBuffer.lifo_list)