test_synapse_launcher.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import unittest
  2. import mock
  3. from kalliope.core import LIFOBuffer
  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(default_synapse="Synapse3")
  31. # clean the LiFO
  32. Singleton._instances = dict()
  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. lifo_buffer = LIFOBuffer()
  41. self.assertEqual(expected_result, lifo_buffer.lifo_list)
  42. # we expect that the lifo has been loaded with the synapse to run and overwritten parameters
  43. Singleton._instances = dict()
  44. lifo_buffer = LIFOBuffer()
  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, lifo_buffer.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. lifo_buffer = LIFOBuffer()
  72. self.assertEqual(expected_result, lifo_buffer.lifo_list)
  73. # -------------------------
  74. # test_match_synapse1_and_2
  75. # -------------------------
  76. # clean LIFO
  77. Singleton._instances = dict()
  78. with mock.patch("kalliope.core.LIFOBuffer.execute"):
  79. order_to_match = "this is the second sentence"
  80. should_be_created_matched_synapse1 = MatchedSynapse(matched_synapse=self.synapse1,
  81. user_order=order_to_match,
  82. matched_order="this is the sentence")
  83. should_be_created_matched_synapse2 = MatchedSynapse(matched_synapse=self.synapse2,
  84. user_order=order_to_match,
  85. matched_order="this is the second sentence")
  86. expected_result = [[should_be_created_matched_synapse1, should_be_created_matched_synapse2]]
  87. SynapseLauncher.run_matching_synapse_from_order(order_to_match,
  88. brain=self.brain_test,
  89. settings=self.settings_test)
  90. lifo_buffer = LIFOBuffer()
  91. self.assertEqual(expected_result, lifo_buffer.lifo_list)
  92. # -------------------------
  93. # test_match_default_synapse
  94. # -------------------------
  95. # clean LIFO
  96. Singleton._instances = dict()
  97. with mock.patch("kalliope.core.LIFOBuffer.execute"):
  98. order_to_match = "not existing sentence"
  99. should_be_created_matched_synapse = MatchedSynapse(matched_synapse=self.synapse3,
  100. user_order=order_to_match,
  101. matched_order=None)
  102. expected_result = [[should_be_created_matched_synapse]]
  103. SynapseLauncher.run_matching_synapse_from_order(order_to_match,
  104. brain=self.brain_test,
  105. settings=self.settings_test)
  106. lifo_buffer = LIFOBuffer()
  107. self.assertEqual(expected_result, lifo_buffer.lifo_list)
  108. # -------------------------
  109. # test_no_match_and_no_default_synapse
  110. # -------------------------
  111. # clean LIFO
  112. Singleton._instances = dict()
  113. with mock.patch("kalliope.core.LIFOBuffer.execute"):
  114. order_to_match = "not existing sentence"
  115. new_settings = Settings()
  116. expected_result = [[]]
  117. SynapseLauncher.run_matching_synapse_from_order(order_to_match,
  118. brain=self.brain_test,
  119. settings=new_settings)
  120. lifo_buffer = LIFOBuffer()
  121. self.assertEqual(expected_result, lifo_buffer.lifo_list)
  122. if __name__ == '__main__':
  123. unittest.main()
  124. # suite = unittest.TestSuite()
  125. # suite.addTest(TestSynapseLauncher("test_run_matching_synapse_from_order"))
  126. # runner = unittest.TextTestRunner()
  127. # runner.run(suite)