test_synapse_launcher.py 6.1 KB

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