test_order_analyser.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. import unittest
  2. import mock
  3. from kalliope.core.OrderAnalyser import OrderAnalyser
  4. from kalliope.core.Models.Neuron import Neuron
  5. from kalliope.core.Models.Synapse import Synapse
  6. from kalliope.core.Models.Brain import Brain
  7. from kalliope.core.Models.Settings import Settings
  8. from kalliope.core.Models.Order import Order
  9. class TestOrderAnalyser(unittest.TestCase):
  10. """Test case for the OrderAnalyser Class"""
  11. def setUp(self):
  12. pass
  13. def test_start(self):
  14. """
  15. Testing if the matches from the incoming messages and the signals/order sentences.
  16. Scenarii :
  17. - Order matchs a synapse and the synapse has been launched.
  18. - Order does not match but have a default synapse.
  19. - Order does not match and does not have default synapse.
  20. - Provide synapse without any external orders
  21. - Provide synapse with any external orders
  22. """
  23. # Init
  24. neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
  25. neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
  26. neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
  27. neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})
  28. signal1 = Order(sentence="this is the sentence")
  29. signal2 = Order(sentence="this is the second sentence")
  30. signal3 = Order(sentence="that is part of the third sentence")
  31. synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
  32. synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2])
  33. synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3])
  34. all_synapse_list = [synapse1,
  35. synapse2,
  36. synapse3]
  37. br = Brain(synapses=all_synapse_list)
  38. with mock.patch("kalliope.core.OrderAnalyser._start_neuron") as mock_start_neuron_method:
  39. # assert synapses have been launched
  40. order_to_match = "this is the sentence"
  41. oa = OrderAnalyser(order=order_to_match,
  42. brain=br)
  43. expected_result = [synapse1]
  44. self.assertEquals(oa.start(),
  45. expected_result,
  46. "Fail to run the expected Synapse matching the order")
  47. calls = [mock.call(neuron1, {}), mock.call(neuron2, {})]
  48. mock_start_neuron_method.assert_has_calls(calls=calls)
  49. mock_start_neuron_method.reset_mock()
  50. # No order matching Default Synapse to run
  51. order_to_match = "random sentence"
  52. oa = OrderAnalyser(order=order_to_match,
  53. brain=br)
  54. oa.settings = mock.MagicMock(default_synapse="Synapse3")
  55. expected_result = [synapse3]
  56. self.assertEquals(oa.start(),
  57. expected_result,
  58. "Fail to run the default Synapse because no other synapses match the order")
  59. # No order matching no Default Synapse
  60. order_to_match = "random sentence"
  61. oa = OrderAnalyser(order=order_to_match,
  62. brain=br)
  63. oa.settings = mock.MagicMock()
  64. expected_result = []
  65. self.assertEquals(oa.start(),
  66. expected_result,
  67. "Fail to no synapse because no synapse matchs and no default defined")
  68. # Provide synapse to run
  69. order_to_match = "this is the sentence"
  70. oa = OrderAnalyser(order=order_to_match,
  71. brain=br)
  72. expected_result = [synapse1]
  73. synapses_to_run = [synapse1]
  74. self.assertEquals(oa.start(synapses_to_run=synapses_to_run),
  75. expected_result,
  76. "Fail to run the provided synapse to run")
  77. calls = [mock.call(neuron1, {}), mock.call(neuron2, {})]
  78. mock_start_neuron_method.assert_has_calls(calls=calls)
  79. mock_start_neuron_method.reset_mock()
  80. # Provide synapse and external orders
  81. order_to_match = "this is an external sentence"
  82. oa = OrderAnalyser(order=order_to_match,
  83. brain=br)
  84. external_orders = "this is an external {{ order }}"
  85. synapses_to_run = [synapse2]
  86. expected_result = [synapse2]
  87. self.assertEquals(oa.start(synapses_to_run=synapses_to_run, external_order=external_orders),
  88. expected_result,
  89. "Fail to run a provided synapse with external order")
  90. calls = [mock.call(neuron3, {"order":u"sentence"}), mock.call(neuron4, {"order":u"sentence"})]
  91. mock_start_neuron_method.assert_has_calls(calls=calls)
  92. mock_start_neuron_method.reset_mock()
  93. def test_start_neuron(self):
  94. """
  95. Testing params association and starting a Neuron
  96. """
  97. neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})
  98. with mock.patch("kalliope.core.NeuronLauncher.NeuronLauncher.start_neuron") as mock_start_neuron_method:
  99. # Assert to the neuron is launched
  100. neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
  101. params = {
  102. 'param1':'parval1'
  103. }
  104. OrderAnalyser._start_neuron(neuron=neuron1,params=params)
  105. mock_start_neuron_method.assert_called_with(neuron1)
  106. mock_start_neuron_method.reset_mock()
  107. # Assert the params are well passed to the neuron
  108. neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2', 'args': ['arg1', 'arg2']})
  109. params = {
  110. 'arg1':'argval1',
  111. 'arg2':'argval2'
  112. }
  113. OrderAnalyser._start_neuron(neuron=neuron2, params=params)
  114. neuron2_params = Neuron(name='neurone2',
  115. parameters={'var2': 'val2',
  116. 'args': ['arg1', 'arg2'],
  117. 'arg1':'argval1',
  118. 'arg2':'argval2'}
  119. )
  120. mock_start_neuron_method.assert_called_with(neuron2_params)
  121. mock_start_neuron_method.reset_mock()
  122. # Assert the Neuron is not started when missing args
  123. neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3', 'args': ['arg3', 'arg4']})
  124. params = {
  125. 'arg1': 'argval1',
  126. 'arg2': 'argval2'
  127. }
  128. OrderAnalyser._start_neuron(neuron=neuron3, params=params)
  129. mock_start_neuron_method.assert_not_called()
  130. mock_start_neuron_method.reset_mock()
  131. # Assert no neuron is launched when waiting for args and none are given
  132. neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4', 'args': ['arg5', 'arg6']})
  133. params = {}
  134. OrderAnalyser._start_neuron(neuron=neuron4, params=params)
  135. mock_start_neuron_method.assert_not_called()
  136. mock_start_neuron_method.reset_mock()
  137. def test_is_containing_bracket(self):
  138. # Success
  139. order_to_test = "This test contains {{ bracket }}"
  140. self.assertTrue(OrderAnalyser._is_containing_bracket(order_to_test),
  141. "Fail returning True when order contains spaced brackets")
  142. order_to_test = "This test contains {{bracket }}"
  143. self.assertTrue(OrderAnalyser._is_containing_bracket(order_to_test),
  144. "Fail returning True when order contains right spaced bracket")
  145. order_to_test = "This test contains {{ bracket}}"
  146. self.assertTrue(OrderAnalyser._is_containing_bracket(order_to_test),
  147. "Fail returning True when order contains left spaced bracket")
  148. order_to_test = "This test contains {{bracket}}"
  149. self.assertTrue(OrderAnalyser._is_containing_bracket(order_to_test),
  150. "Fail returning True when order contains no spaced bracket")
  151. # Failure
  152. order_to_test = "This test does not contain bracket"
  153. self.assertFalse(OrderAnalyser._is_containing_bracket(order_to_test),
  154. "Fail returning False when order has no brackets")
  155. # Behaviour
  156. order_to_test = ""
  157. self.assertFalse(OrderAnalyser._is_containing_bracket(order_to_test),
  158. "Fail returning False when no order")
  159. def test_get_next_value_list(self):
  160. # Success
  161. list_to_test = {1, 2, 3}
  162. self.assertEqual(OrderAnalyser._get_next_value_list(list_to_test), 2,
  163. "Fail to match the expected next value from the list")
  164. # Failure
  165. list_to_test = {1}
  166. self.assertEqual(OrderAnalyser._get_next_value_list(list_to_test), None,
  167. "Fail to ensure there is no next value from the list")
  168. # Behaviour
  169. list_to_test = {}
  170. self.assertEqual(OrderAnalyser._get_next_value_list(list_to_test), None,
  171. "Fail to ensure the empty list return None value")
  172. def test_spelt_order_match_brain_order_via_table(self):
  173. order_to_test = "this is the order"
  174. sentence_to_test = "this is the order"
  175. # Success
  176. self.assertTrue(OrderAnalyser.spelt_order_match_brain_order_via_table(order_to_test, sentence_to_test),
  177. "Fail matching order with the expected sentence")
  178. # Failure
  179. sentence_to_test = "unexpected sentence"
  180. self.assertFalse(OrderAnalyser.spelt_order_match_brain_order_via_table(order_to_test, sentence_to_test),
  181. "Fail to ensure the expected sentence is not matching the order")
  182. def test_get_split_order_without_bracket(self):
  183. # Success
  184. order_to_test = "this is the order"
  185. expected_result = ["this", "is", "the", "order"]
  186. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  187. "No brackets Fails to return the expected list")
  188. order_to_test = "this is the {{ order }}"
  189. expected_result = ["this", "is", "the"]
  190. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  191. "With spaced brackets Fails to return the expected list")
  192. order_to_test = "this is the {{order }}" # left bracket without space
  193. expected_result = ["this", "is", "the"]
  194. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  195. "Left brackets Fails to return the expected list")
  196. order_to_test = "this is the {{ order}}" # right bracket without space
  197. expected_result = ["this", "is", "the"]
  198. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  199. "Right brackets Fails to return the expected list")
  200. order_to_test = "this is the {{order}}" # bracket without space
  201. expected_result = ["this", "is", "the"]
  202. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  203. "No space brackets Fails to return the expected list")
  204. def test_associate_order_params_to_values(self):
  205. ##
  206. # Testing the brackets position behaviour
  207. ##
  208. # Success
  209. order_brain = "This is the {{ variable }}"
  210. order_user = "This is the value"
  211. expected_result = {'variable': 'value'}
  212. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  213. "Fail to match the order_brain {{ variable }} to the 'value'")
  214. # Success
  215. order_brain = "This is the {{variable }}"
  216. order_user = "This is the value"
  217. expected_result = {'variable': 'value'}
  218. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  219. "Fail to match the order_brain {{variable }} to the 'value'")
  220. # Success
  221. order_brain = "This is the {{ variable}}"
  222. order_user = "This is the value"
  223. expected_result = {'variable': 'value'}
  224. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  225. "Fail to match the order_brain {{ variable}} to the 'value'")
  226. # Success
  227. order_brain = "This is the {{variable}}"
  228. order_user = "This is the value"
  229. expected_result = {'variable': 'value'}
  230. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  231. "Fail to match the order_brain {{variable}} to the 'value'")
  232. # Fail
  233. order_brain = "This is the {variable}"
  234. order_user = "This is the value"
  235. expected_result = {'variable': 'value'}
  236. self.assertNotEquals(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  237. "Should not match the order_brain {variable} to the 'value'")
  238. # Fail
  239. order_brain = "This is the { variable}}"
  240. order_user = "This is the value"
  241. expected_result = {'variable': 'value'}
  242. self.assertNotEquals(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  243. "Should not match the order_brain { variable}} to the 'value'")
  244. ##
  245. # Testing the brackets position in the sentence
  246. ##
  247. # Success
  248. order_brain = "{{ variable }} This is the"
  249. order_user = "value This is the"
  250. expected_result = {'variable': 'value'}
  251. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  252. "Fail to match the order_brain {{ variable }} in first position "
  253. "ins the sentence to the 'value'")
  254. # Success
  255. order_brain = "This is {{ variable }} the"
  256. order_user = " This is value the"
  257. expected_result = {'variable': 'value'}
  258. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  259. "Fail to match the order_brain {{ variable }} in middle position ins "
  260. "the sentence to the 'value'")
  261. ##
  262. # Testing multi variables
  263. ##
  264. # Success
  265. order_brain = "This is {{ variable }} the {{ variable2 }}"
  266. order_user = "This is value the value2"
  267. expected_result = {'variable': 'value',
  268. 'variable2': 'value2'}
  269. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  270. "Fail to match the order_brain multi variable to the multi values")
  271. ##
  272. # Testing multi words in variable
  273. ##
  274. # Success
  275. order_brain = "This is the {{ variable }}"
  276. order_user = "This is the value with multiple words"
  277. expected_result = {'variable': 'value with multiple words'}
  278. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  279. "Fail to match the order_brain {{ variable }} to the 'value with multiple words'")
  280. # Success
  281. order_brain = "This is the {{ variable }} and {{ variable2 }}"
  282. order_user = "This is the value with multiple words and second value multiple"
  283. expected_result = {'variable': 'value with multiple words',
  284. 'variable2': 'second value multiple'}
  285. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  286. "Fail to match the order_brain multiple variables with multiple words as values'")
  287. def test_get_matching_synapse_list(self):
  288. # Init
  289. neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
  290. neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
  291. neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
  292. neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})
  293. signal1 = Order(sentence="this is the sentence")
  294. signal2 = Order(sentence="this is the second sentence")
  295. signal3 = Order(sentence="that is part of the third sentence")
  296. synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
  297. synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2])
  298. synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3])
  299. order_to_match = "this is the sentence"
  300. all_synapse_list = [synapse1,
  301. synapse2,
  302. synapse3]
  303. # Success
  304. expected_result = synapse1
  305. oa_tuple_list = OrderAnalyser._get_matching_synapse_list(all_synapses_list=all_synapse_list,
  306. order_to_match=order_to_match)
  307. self.assertEquals(oa_tuple_list[0].synapse,
  308. expected_result,
  309. "Fail matching 'the expected synapse' from the complete synapse list and the order")
  310. # Multiple Matching synapses
  311. signal2 = Order(sentence="this is the sentence")
  312. synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2])
  313. order_to_match = "this is the sentence"
  314. all_synapse_list = [synapse1,
  315. synapse2,
  316. synapse3]
  317. expected_result = [synapse1,
  318. synapse2]
  319. oa_tuple_list = OrderAnalyser._get_matching_synapse_list(all_synapses_list=all_synapse_list,
  320. order_to_match=order_to_match)
  321. self.assertEquals(oa_tuple_list[0].synapse,
  322. expected_result[0],
  323. "Fail 'Multiple Matching synapses' from the complete synapse list and the order (first element)")
  324. self.assertEquals(oa_tuple_list[1].synapse,
  325. expected_result[1],
  326. "Fail 'Multiple Matching synapses' from the complete synapse list and the order (second element)")
  327. # matching no synapses
  328. order_to_match = "this is not the correct word"
  329. all_synapse_list = [synapse1,
  330. synapse2,
  331. synapse3]
  332. expected_result = []
  333. self.assertEquals(OrderAnalyser._get_matching_synapse_list(all_synapses_list=all_synapse_list,
  334. order_to_match=order_to_match),
  335. expected_result,
  336. "Fail matching 'no synapses' from the complete synapse list and the order")
  337. # matching synapse with all key worlds
  338. # /!\ Some words in the order are matching all words in synapses signals !
  339. order_to_match = "this is not the correct sentence"
  340. all_synapse_list = [synapse1,
  341. synapse2,
  342. synapse3]
  343. expected_result = [synapse1,
  344. synapse2]
  345. oa_tuple_list = OrderAnalyser._get_matching_synapse_list(all_synapses_list=all_synapse_list,
  346. order_to_match=order_to_match)
  347. self.assertEquals(oa_tuple_list[0].synapse,
  348. expected_result[0],
  349. "Fail matching 'synapse with all key worlds' from the complete synapse list and the order (first element)")
  350. self.assertEquals(oa_tuple_list[1].synapse,
  351. expected_result[1],
  352. "Fail matching 'synapse with all key worlds' from the complete synapse list and the order (second element)")
  353. def test_get_params_from_order(self):
  354. string_order = "this is the {{ sentence }}"
  355. order_to_check = "this is the value"
  356. expected_result = {'sentence': 'value'}
  357. self.assertEquals(OrderAnalyser._get_params_from_order(string_order=string_order, order_to_check=order_to_check),
  358. expected_result,
  359. "Fail to retrieve 'the params' of the string_order from the order")
  360. # Multiple match
  361. string_order = "this is the {{ sentence }}"
  362. order_to_check = "this is the value with multiple words"
  363. expected_result = {'sentence': 'value with multiple words'}
  364. self.assertEqual(OrderAnalyser._get_params_from_order(string_order=string_order, order_to_check=order_to_check),
  365. expected_result,
  366. "Fail to retrieve the 'multiple words params' of the string_order from the order")
  367. # Multiple params
  368. string_order = "this is the {{ sentence }} with multiple {{ params }}"
  369. order_to_check = "this is the value with multiple words"
  370. expected_result = {'sentence': 'value',
  371. 'params':'words'}
  372. self.assertEqual(OrderAnalyser._get_params_from_order(string_order=string_order, order_to_check=order_to_check),
  373. expected_result,
  374. "Fail to retrieve the 'multiple params' of the string_order from the order")
  375. # Multiple params with multiple words
  376. string_order = "this is the {{ sentence }} with multiple {{ params }}"
  377. order_to_check = "this is the multiple values with multiple values as words"
  378. expected_result = {'sentence': 'multiple values',
  379. 'params': 'values as words'}
  380. self.assertEqual(OrderAnalyser._get_params_from_order(string_order=string_order, order_to_check=order_to_check),
  381. expected_result,
  382. "Fail to retrieve the 'multiple params with multiple words' of the string_order from the order")
  383. # params at the begining of the sentence
  384. string_order = "{{ sentence }} this is the sentence"
  385. order_to_check = "hello world this is the multiple values with multiple values as words"
  386. expected_result = {'sentence': 'hello world'}
  387. self.assertEqual(OrderAnalyser._get_params_from_order(string_order=string_order, order_to_check=order_to_check),
  388. expected_result,
  389. "Fail to retrieve the 'params at the begining of the sentence' of the string_order from the order")
  390. # all of the sentence is a variable
  391. string_order = "{{ sentence }}"
  392. order_to_check = "this is the all sentence is a variable"
  393. expected_result = {'sentence': 'this is the all sentence is a variable'}
  394. self.assertEqual(OrderAnalyser._get_params_from_order(string_order=string_order, order_to_check=order_to_check),
  395. expected_result,
  396. "Fail to retrieve the 'all of the sentence is a variable' of the string_order from the order")
  397. def test_get_default_synapse_from_sysnapses_list(self):
  398. # Init
  399. neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
  400. neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
  401. neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
  402. neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})
  403. signal1 = Order(sentence="this is the sentence")
  404. signal2 = Order(sentence="this is the second sentence")
  405. signal3 = Order(sentence="that is part of the third sentence")
  406. synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
  407. synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2])
  408. synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3])
  409. default_synapse_name = "Synapse2"
  410. all_synapse_list = [synapse1,
  411. synapse2,
  412. synapse3]
  413. expected_result = synapse2
  414. # Assert equals
  415. self.assertEquals(OrderAnalyser._get_default_synapse_from_sysnapses_list(all_synapses_list=all_synapse_list,
  416. default_synapse_name=default_synapse_name),
  417. expected_result,
  418. "Fail to match the expected default Synapse")
  419. def test_find_synapse_to_run(self):
  420. """
  421. Test to find the good synapse to run
  422. Scenarii:
  423. - Find the synapse
  424. - No synpase found, no default synapse
  425. - No synapse found, run the default synapse
  426. """
  427. # Init
  428. neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
  429. neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
  430. neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
  431. neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})
  432. signal1 = Order(sentence="this is the sentence")
  433. signal2 = Order(sentence="this is the second sentence")
  434. signal3 = Order(sentence="that is part of the third sentence")
  435. synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
  436. synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2])
  437. synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3])
  438. all_synapse_list = [synapse1,
  439. synapse2,
  440. synapse3]
  441. br = Brain(synapses=all_synapse_list)
  442. st = Settings()
  443. # Find synapse
  444. order = "this is the sentence"
  445. expected_result = synapse1
  446. oa_tuple_list = OrderAnalyser._find_synapse_to_run(brain=br,settings=st, order=order)
  447. self.assertEquals(oa_tuple_list[0].synapse,
  448. expected_result,
  449. "Fail to run the proper synapse matching the order")
  450. expected_result = signal1.sentence
  451. self.assertEquals(oa_tuple_list[0].order,
  452. expected_result,
  453. "Fail to run the proper synapse matching the order")
  454. # No Default synapse
  455. order = "No default synapse"
  456. expected_result = []
  457. self.assertEquals(OrderAnalyser._find_synapse_to_run(brain=br,settings=st, order=order),
  458. expected_result,
  459. "Fail to run no synapse, when no default is defined")
  460. # Default synapse
  461. st = Settings(default_synapse="Synapse2")
  462. order = "default synapse"
  463. expected_result = synapse2
  464. oa_tuple_list = OrderAnalyser._find_synapse_to_run(brain=br, settings=st, order=order)
  465. self.assertEquals(oa_tuple_list[0].synapse,
  466. expected_result,
  467. "Fail to run the default synapse")
  468. if __name__ == '__main__':
  469. unittest.main()