test_lifo_buffer.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. import os
  2. import unittest
  3. import mock
  4. from kalliope.core import LifoManager
  5. from kalliope.core.ConfigurationManager import BrainLoader
  6. from kalliope.core.Lifo.LIFOBuffer import Serialize, SynapseListAddedToLIFO
  7. from kalliope.core.Models import Singleton
  8. from kalliope.core.Models.MatchedSynapse import MatchedSynapse
  9. class TestLIFOBuffer(unittest.TestCase):
  10. def setUp(self):
  11. # be sure the brain haven't been instantiated before
  12. Singleton._instances = dict()
  13. if "/Tests" in os.getcwd():
  14. self.brain_to_test = os.getcwd() + os.sep + "brains/lifo_buffer_test_brain.yml"
  15. else:
  16. self.brain_to_test = os.getcwd() + os.sep + "Tests/brains/lifo_buffer_test_brain.yml"
  17. BrainLoader(file_path=self.brain_to_test)
  18. # create a new lifo buffer
  19. self.lifo_buffer = LifoManager.get_singleton_lifo()
  20. self.lifo_buffer.clean()
  21. def test_execute(self):
  22. """
  23. In this test the brain contains a neurotransmitter
  24. """
  25. # --------------------------------------
  26. # Test 1. The user answers correctly to all neurotransmitter
  27. # --------------------------------------
  28. # we suppose that the first synapse has matched the first synapse
  29. synapse = BrainLoader().brain.get_synapse_by_name("synapse1")
  30. order = "enter in synapse 1"
  31. matched_synapse = MatchedSynapse(matched_synapse=synapse,
  32. user_order=order,
  33. matched_order=order)
  34. list_matched_synapse = list()
  35. list_matched_synapse.append(matched_synapse)
  36. self.lifo_buffer.add_synapse_list_to_lifo(list_matched_synapse)
  37. self.lifo_buffer.api_response.user_order = order
  38. with mock.patch("kalliope.core.TTS.TTSModule.generate_and_play"):
  39. response = self.lifo_buffer.execute(is_api_call=True)
  40. expected_result = {
  41. 'status': 'waiting_for_answer',
  42. 'matched_synapses': [
  43. {
  44. 'matched_order': 'enter in synapse 1',
  45. 'neuron_module_list':
  46. [
  47. {
  48. 'neuron_name': 'Say',
  49. 'generated_message': 'question in synapse 1'
  50. }
  51. ],
  52. 'synapse_name': 'synapse1'
  53. }
  54. ],
  55. 'user_order': 'enter in synapse 1'
  56. }
  57. self.assertEqual(response, expected_result)
  58. # give an answer
  59. answer = "answer synapse1"
  60. response = self.lifo_buffer.execute(answer=answer,
  61. is_api_call=True)
  62. expected_result = {
  63. 'status': 'waiting_for_answer',
  64. 'matched_synapses': [
  65. {
  66. 'matched_order': 'enter in synapse 1',
  67. 'neuron_module_list': [
  68. {
  69. 'neuron_name': 'Say',
  70. 'generated_message': 'question in synapse 1'
  71. },
  72. {
  73. 'neuron_name': 'Neurotransmitter',
  74. 'generated_message': None
  75. }
  76. ],
  77. 'synapse_name': 'synapse1'
  78. },
  79. {
  80. 'matched_order': 'answer synapse1',
  81. 'neuron_module_list': [
  82. {
  83. 'neuron_name': 'Say',
  84. 'generated_message': 'enter synapse 2'
  85. }
  86. ],
  87. 'synapse_name': 'synapse2'
  88. }
  89. ],
  90. 'user_order': None
  91. }
  92. self.assertEqual(response, expected_result)
  93. # give the last answer
  94. answer = "synapse5"
  95. response = self.lifo_buffer.execute(answer=answer,
  96. is_api_call=True)
  97. expected_result = {
  98. 'status': 'complete',
  99. 'matched_synapses': [
  100. {
  101. 'matched_order': 'answer synapse1',
  102. 'neuron_module_list': [
  103. {
  104. 'neuron_name': 'Say',
  105. 'generated_message': 'enter synapse 2'
  106. },
  107. {
  108. 'neuron_name': 'Neurotransmitter',
  109. 'generated_message': None
  110. }
  111. ],
  112. 'synapse_name': 'synapse2'
  113. },
  114. {
  115. 'matched_order': 'synapse5',
  116. 'neuron_module_list': [
  117. {
  118. 'neuron_name': 'Say',
  119. 'generated_message': 'execution of synapse 5'
  120. }
  121. ],
  122. 'synapse_name': 'synapse5'
  123. },
  124. {
  125. 'matched_order': 'enter in synapse 1',
  126. 'neuron_module_list': [
  127. {
  128. 'neuron_name': 'Say',
  129. 'generated_message': 'question in synapse 1'
  130. },
  131. {
  132. 'neuron_name': 'Neurotransmitter',
  133. 'generated_message': None
  134. },
  135. {
  136. 'neuron_name': 'Say',
  137. 'generated_message': 'last neuron in synapse 1'
  138. }
  139. ],
  140. 'synapse_name': 'synapse1'
  141. }
  142. ],
  143. 'user_order': None
  144. }
  145. self.assertEqual(response, expected_result)
  146. # --------------------------------------
  147. # Test 2. The user doesn't answered correctly to the first neurotransmitter
  148. # --------------------------------------
  149. # we suppose that the first synapse has matched the first synapse
  150. synapse = BrainLoader().brain.get_synapse_by_name("synapse1")
  151. order = "enter in synapse 1"
  152. matched_synapse = MatchedSynapse(matched_synapse=synapse,
  153. user_order=order,
  154. matched_order=order)
  155. list_matched_synapse = list()
  156. list_matched_synapse.append(matched_synapse)
  157. self.lifo_buffer.add_synapse_list_to_lifo(list_matched_synapse)
  158. self.lifo_buffer.api_response.user_order = order
  159. with mock.patch("kalliope.core.TTS.TTSModule.generate_and_play"):
  160. # fist call to enter in the neurotransmitter
  161. self.lifo_buffer.execute(is_api_call=True)
  162. wrong_answer = "wrong answer"
  163. response = self.lifo_buffer.execute(answer=wrong_answer, is_api_call=True)
  164. expected_result = {
  165. 'status': 'complete',
  166. 'matched_synapses': [
  167. {
  168. 'matched_order': 'enter in synapse 1',
  169. 'neuron_module_list': [
  170. {
  171. 'neuron_name': 'Say',
  172. 'generated_message': 'question in synapse 1'
  173. },
  174. {
  175. 'neuron_name': 'Neurotransmitter',
  176. 'generated_message': None
  177. },
  178. {
  179. 'neuron_name': 'Say',
  180. 'generated_message': 'last neuron in synapse 1'
  181. }
  182. ],
  183. 'synapse_name': 'synapse1'
  184. },
  185. {
  186. 'matched_order': None,
  187. 'neuron_module_list': [
  188. {
  189. 'neuron_name': 'Say',
  190. 'generated_message': 'not understood'
  191. }
  192. ],
  193. 'synapse_name': 'synapse4'
  194. }
  195. ],
  196. 'user_order': None
  197. }
  198. self.assertEqual(response, expected_result)
  199. # --------------------------------------
  200. # Test 3. No synapse matched, we still execute the list
  201. # --------------------------------------
  202. list_matched_synapse = list()
  203. self.lifo_buffer.add_synapse_list_to_lifo(list_matched_synapse)
  204. self.lifo_buffer.api_response.user_order = "this is an order"
  205. with mock.patch("kalliope.core.TTS.TTSModule.generate_and_play"):
  206. # fist call to enter in the neurotransmitter
  207. response = self.lifo_buffer.execute(is_api_call=True)
  208. expected_result = {
  209. 'status': None,
  210. 'matched_synapses': [],
  211. 'user_order': 'this is an order'
  212. }
  213. self.assertEqual(response, expected_result)
  214. def test_add_synapse_list_to_lifo(self):
  215. """
  216. Testing to add a synapse to the lifo
  217. """
  218. synapse = BrainLoader().brain.get_synapse_by_name("synapse1")
  219. order = "enter in synapse 1"
  220. matched_synapse = MatchedSynapse(matched_synapse=synapse,
  221. user_order=order,
  222. matched_order=order)
  223. list_matched_synapse = list()
  224. list_matched_synapse.append(matched_synapse)
  225. self.lifo_buffer.add_synapse_list_to_lifo(list_matched_synapse)
  226. self.assertEqual(self.lifo_buffer.lifo_list, [list_matched_synapse])
  227. def test_clean(self):
  228. """
  229. Test the Cleaning of the matched synapses list
  230. """
  231. synapse = BrainLoader().brain.get_synapse_by_name("synapse1")
  232. order = "enter in synapse 1"
  233. matched_synapse = MatchedSynapse(matched_synapse=synapse,
  234. user_order=order,
  235. matched_order=order)
  236. list_matched_synapse = list()
  237. list_matched_synapse.append(matched_synapse)
  238. self.lifo_buffer.add_synapse_list_to_lifo(list_matched_synapse)
  239. self.lifo_buffer.clean()
  240. self.assertEqual(0, len(self.lifo_buffer.lifo_list))
  241. def test_return_serialized_api_response(self):
  242. """
  243. Test the serialization
  244. """
  245. self.lifo_buffer.clean()
  246. self.lifo_buffer.execute(is_api_call=True)
  247. expected_result = {'status': None, 'matched_synapses': [], 'user_order': None}
  248. response = self.lifo_buffer._return_serialized_api_response()
  249. self.assertEqual(expected_result, response)
  250. def test_process_synapse_list(self):
  251. """
  252. Testing the neuron list from a synapse
  253. """
  254. synapse = BrainLoader().brain.get_synapse_by_name("synapse1")
  255. order = "enter in synapse 1"
  256. matched_synapse = MatchedSynapse(matched_synapse=synapse,
  257. user_order=order,
  258. matched_order=order)
  259. list_matched_synapse = list()
  260. list_matched_synapse.append(matched_synapse)
  261. with mock.patch("kalliope.core.Lifo.LIFOBuffer._process_neuron_list"):
  262. self.lifo_buffer._process_synapse_list(list_matched_synapse)
  263. expected_response = {
  264. 'status': None,
  265. 'matched_synapses': [
  266. {
  267. 'matched_order': 'enter in synapse 1',
  268. 'neuron_module_list': [],
  269. 'synapse_name': 'synapse1'
  270. }
  271. ],
  272. 'user_order': None
  273. }
  274. self.assertEqual(expected_response, self.lifo_buffer.api_response.serialize())
  275. self.assertEqual(0, len(self.lifo_buffer.lifo_list))
  276. def test_process_neuron_list(self):
  277. # Test with a neuron that doesn't wait for an answer
  278. synapse = BrainLoader().brain.get_synapse_by_name("synapse5")
  279. order = "synapse5"
  280. matched_synapse = MatchedSynapse(matched_synapse=synapse,
  281. user_order=order,
  282. matched_order=order)
  283. with mock.patch("kalliope.core.TTS.TTSModule.generate_and_play"):
  284. self.lifo_buffer.set_api_call(True)
  285. self.lifo_buffer._process_neuron_list(matched_synapse=matched_synapse)
  286. self.assertEqual("complete", self.lifo_buffer.api_response.status)
  287. # test with neuron that wait for an answer
  288. LifoManager.clean_saved_lifo()
  289. synapse = BrainLoader().brain.get_synapse_by_name("synapse6")
  290. order = "synapse6"
  291. matched_synapse = MatchedSynapse(matched_synapse=synapse,
  292. user_order=order,
  293. matched_order=order)
  294. self.lifo_buffer.set_api_call(True)
  295. with mock.patch("kalliope.core.TTS.TTSModule.generate_and_play"):
  296. with self.assertRaises(Serialize):
  297. self.lifo_buffer._process_neuron_list(matched_synapse=matched_synapse)
  298. # test with a neuron that want to add a synapse list to the LIFO
  299. LifoManager.clean_saved_lifo()
  300. synapse = BrainLoader().brain.get_synapse_by_name("synapse6")
  301. order = "synapse6"
  302. matched_synapse = MatchedSynapse(matched_synapse=synapse,
  303. user_order=order,
  304. matched_order=order)
  305. self.lifo_buffer.set_api_call(True)
  306. self.lifo_buffer.set_answer("synapse 6 answer")
  307. with mock.patch("kalliope.core.TTS.TTSModule.generate_and_play"):
  308. self.assertRaises(SynapseListAddedToLIFO,
  309. self.lifo_buffer._process_neuron_list(matched_synapse=matched_synapse))
  310. if __name__ == '__main__':
  311. unittest.main()
  312. # suite = unittest.TestSuite()
  313. # suite.addTest(TestLIFOBuffer("test_execute"))
  314. # runner = unittest.TextTestRunner()
  315. # runner.run(suite)