test_order_analyser.py 27 KB

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