test_synapse_launcher.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import unittest
  2. import mock
  3. from kalliope.core import LIFOBuffer
  4. from kalliope.core.Models import Brain
  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 Order
  10. from kalliope.core.Models import Synapse
  11. class TestSynapseLauncher(unittest.TestCase):
  12. """
  13. Test the class SynapseLauncher
  14. """
  15. def setUp(self):
  16. # Init
  17. neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
  18. neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
  19. neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
  20. neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})
  21. signal1 = Order(sentence="this is the sentence")
  22. signal2 = Order(sentence="this is the second sentence")
  23. signal3 = Order(sentence="that is part of the third sentence")
  24. self.synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
  25. self.synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2])
  26. self.synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3])
  27. self.all_synapse_list = [self.synapse1,
  28. self.synapse2,
  29. self.synapse3]
  30. self.brain_test = Brain(synapses=self.all_synapse_list)
  31. self.settings_test = Settings(default_synapse="Synapse3")
  32. # clean the LiFO
  33. LIFOBuffer.lifo_list = list()
  34. def test_start_synapse_by_name(self):
  35. # existing synapse in the brain
  36. with mock.patch("kalliope.core.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. self.assertEqual(expected_result, LIFOBuffer.lifo_list)
  42. # we expect that the lifo has been loaded with the synapse to run and overwritten parameters
  43. # clean the LiFO
  44. LIFOBuffer.lifo_list = list()
  45. overriding_param = {
  46. "val1": "val"
  47. }
  48. SynapseLauncher.start_synapse_by_name("Synapse1", brain=self.brain_test,
  49. overriding_parameter_dict=overriding_param)
  50. should_be_created_matched_synapse = MatchedSynapse(matched_synapse=self.synapse1,
  51. overriding_parameter=overriding_param)
  52. # we expect that the lifo has been loaded with the synapse to run
  53. expected_result = [[should_be_created_matched_synapse]]
  54. self.assertEqual(expected_result, LIFOBuffer.lifo_list)
  55. # non existing synapse in the brain
  56. with self.assertRaises(SynapseNameNotFound):
  57. SynapseLauncher.start_synapse_by_name("not_existing", brain=self.brain_test)
  58. def test_run_matching_synapse_from_order(self):
  59. # ------------------
  60. # test_match_synapse1
  61. # ------------------
  62. with mock.patch("kalliope.core.LIFOBuffer.execute"):
  63. order_to_match = "this is the sentence"
  64. should_be_created_matched_synapse = MatchedSynapse(matched_synapse=self.synapse1,
  65. user_order=order_to_match,
  66. matched_order="this is the sentence")
  67. expected_result = [[should_be_created_matched_synapse]]
  68. SynapseLauncher.run_matching_synapse_from_order(order_to_match,
  69. brain=self.brain_test,
  70. settings=self.settings_test)
  71. self.assertEqual(expected_result, LIFOBuffer.lifo_list)
  72. # -------------------------
  73. # test_match_synapse1_and_2
  74. # -------------------------
  75. # clean LIFO
  76. LIFOBuffer.lifo_list = list()
  77. with mock.patch("kalliope.core.LIFOBuffer.execute"):
  78. order_to_match = "this is the second sentence"
  79. should_be_created_matched_synapse1 = MatchedSynapse(matched_synapse=self.synapse1,
  80. user_order=order_to_match,
  81. matched_order="this is the sentence")
  82. should_be_created_matched_synapse2 = MatchedSynapse(matched_synapse=self.synapse2,
  83. user_order=order_to_match,
  84. matched_order="this is the second sentence")
  85. expected_result = [[should_be_created_matched_synapse1, should_be_created_matched_synapse2]]
  86. SynapseLauncher.run_matching_synapse_from_order(order_to_match,
  87. brain=self.brain_test,
  88. settings=self.settings_test)
  89. self.assertEqual(expected_result, LIFOBuffer.lifo_list)
  90. # -------------------------
  91. # test_match_default_synapse
  92. # -------------------------
  93. # clean LIFO
  94. LIFOBuffer.lifo_list = list()
  95. with mock.patch("kalliope.core.LIFOBuffer.execute"):
  96. order_to_match = "not existing sentence"
  97. should_be_created_matched_synapse = MatchedSynapse(matched_synapse=self.synapse3,
  98. user_order=order_to_match,
  99. matched_order=None)
  100. expected_result = [[should_be_created_matched_synapse]]
  101. SynapseLauncher.run_matching_synapse_from_order(order_to_match,
  102. brain=self.brain_test,
  103. settings=self.settings_test)
  104. self.assertEqual(expected_result, LIFOBuffer.lifo_list)
  105. # -------------------------
  106. # test_no_match_and_no_default_synapse
  107. # -------------------------
  108. # clean LIFO
  109. LIFOBuffer.lifo_list = list()
  110. with mock.patch("kalliope.core.LIFOBuffer.execute"):
  111. order_to_match = "not existing sentence"
  112. new_settings = Settings()
  113. expected_result = [[]]
  114. SynapseLauncher.run_matching_synapse_from_order(order_to_match,
  115. brain=self.brain_test,
  116. settings=new_settings)
  117. self.assertEqual(expected_result, LIFOBuffer.lifo_list)