test_order_analyser.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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. # Upper/lower cases
  183. sentence_to_test = "THIS is THE order"
  184. self.assertTrue(OrderAnalyser.spelt_order_match_brain_order_via_table(order_to_test, sentence_to_test),
  185. "Fail matching Upper/lower cases")
  186. def test_format_sentences_to_analyse(self):
  187. # First capital in sentence
  188. order_to_test = "this is the order"
  189. sentence_to_test = "This is the order"
  190. expected_result = "this is the order", "this is the order"
  191. self.assertEqual(OrderAnalyser._format_sentences_to_analyse(order_to_analyse=order_to_test,
  192. user_said=sentence_to_test),
  193. expected_result,
  194. "Fails formatting the sentences with first capital in sentence")
  195. # random uppercase in sentence
  196. order_to_test = "this is the order"
  197. sentence_to_test = "This IS the ordeR"
  198. expected_result = "this is the order", "this is the order"
  199. self.assertEqual(OrderAnalyser._format_sentences_to_analyse(order_to_analyse=order_to_test,
  200. user_said=sentence_to_test),
  201. expected_result,
  202. "Fails formatting the sentences with random in sentence")
  203. # random uppercase in order
  204. order_to_test = "thiS is THE orDer"
  205. sentence_to_test = "this is the order"
  206. expected_result = "this is the order", "this is the order"
  207. self.assertEqual(OrderAnalyser._format_sentences_to_analyse(order_to_analyse=order_to_test,
  208. user_said=sentence_to_test),
  209. expected_result,
  210. "Fails formatting the sentences with random in order")
  211. # random uppercase in both order and sentence
  212. order_to_test = "thiS is THE orDer"
  213. sentence_to_test = "THIS is the Order"
  214. expected_result = "this is the order", "this is the order"
  215. self.assertEqual(OrderAnalyser._format_sentences_to_analyse(order_to_analyse=order_to_test,
  216. user_said=sentence_to_test),
  217. expected_result,
  218. "Fails formatting the sentences with random in both order and sentence")
  219. def test_get_split_order_without_bracket(self):
  220. # Success
  221. order_to_test = "this is the order"
  222. expected_result = ["this", "is", "the", "order"]
  223. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  224. "No brackets Fails to return the expected list")
  225. order_to_test = "this is the {{ order }}"
  226. expected_result = ["this", "is", "the"]
  227. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  228. "With spaced brackets Fails to return the expected list")
  229. order_to_test = "this is the {{order }}" # left bracket without space
  230. expected_result = ["this", "is", "the"]
  231. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  232. "Left brackets Fails to return the expected list")
  233. order_to_test = "this is the {{ order}}" # right bracket without space
  234. expected_result = ["this", "is", "the"]
  235. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  236. "Right brackets Fails to return the expected list")
  237. order_to_test = "this is the {{order}}" # bracket without space
  238. expected_result = ["this", "is", "the"]
  239. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  240. "No space brackets Fails to return the expected list")
  241. def test_associate_order_params_to_values(self):
  242. ##
  243. # Testing the brackets position behaviour
  244. ##
  245. # Success
  246. order_brain = "This is the {{ variable }}"
  247. order_user = "This is the value"
  248. expected_result = {'variable': 'value'}
  249. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  250. "Fail to match the order_brain {{ variable }} to the 'value'")
  251. # Success
  252. order_brain = "This is the {{variable }}"
  253. order_user = "This is the value"
  254. expected_result = {'variable': 'value'}
  255. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  256. "Fail to match the order_brain {{variable }} to the 'value'")
  257. # Success
  258. order_brain = "This is the {{ variable}}"
  259. order_user = "This is the value"
  260. expected_result = {'variable': 'value'}
  261. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  262. "Fail to match the order_brain {{ variable}} to the 'value'")
  263. # Success
  264. order_brain = "This is the {{variable}}"
  265. order_user = "This is the value"
  266. expected_result = {'variable': 'value'}
  267. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  268. "Fail to match the order_brain {{variable}} to the 'value'")
  269. # Fail
  270. order_brain = "This is the {variable}"
  271. order_user = "This is the value"
  272. expected_result = {'variable': 'value'}
  273. self.assertNotEquals(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  274. "Should not match the order_brain {variable} to the 'value'")
  275. # Fail
  276. order_brain = "This is the { variable}}"
  277. order_user = "This is the value"
  278. expected_result = {'variable': 'value'}
  279. self.assertNotEquals(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  280. "Should not match the order_brain { variable}} to the 'value'")
  281. ##
  282. # Testing the brackets position in the sentence
  283. ##
  284. # Success
  285. order_brain = "{{ variable }} This is the"
  286. order_user = "value This is the"
  287. expected_result = {'variable': 'value'}
  288. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  289. "Fail to match the order_brain {{ variable }} in first position "
  290. "ins the sentence to the 'value'")
  291. # Success
  292. order_brain = "This is {{ variable }} the"
  293. order_user = " This is value the"
  294. expected_result = {'variable': 'value'}
  295. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  296. "Fail to match the order_brain {{ variable }} in middle position ins "
  297. "the sentence to the 'value'")
  298. ##
  299. # Testing multi variables
  300. ##
  301. # Success
  302. order_brain = "This is {{ variable }} the {{ variable2 }}"
  303. order_user = "This is value the value2"
  304. expected_result = {'variable': 'value',
  305. 'variable2': 'value2'}
  306. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  307. "Fail to match the order_brain multi variable to the multi values")
  308. ##
  309. # Testing multi words in variable
  310. ##
  311. # Success
  312. order_brain = "This is the {{ variable }}"
  313. order_user = "This is the value with multiple words"
  314. expected_result = {'variable': 'value with multiple words'}
  315. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  316. "Fail to match the order_brain {{ variable }} to the 'value with multiple words'")
  317. # Success
  318. order_brain = "This is the {{ variable }} and {{ variable2 }}"
  319. order_user = "This is the value with multiple words and second value multiple"
  320. expected_result = {'variable': 'value with multiple words',
  321. 'variable2': 'second value multiple'}
  322. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  323. "Fail to match the order_brain multiple variables with multiple words as values'")
  324. def test_get_matching_synapse_list(self):
  325. # Init
  326. neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
  327. neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
  328. neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
  329. neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})
  330. signal1 = Order(sentence="this is the sentence")
  331. signal2 = Order(sentence="this is the second sentence")
  332. signal3 = Order(sentence="that is part of the third sentence")
  333. synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
  334. synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2])
  335. synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3])
  336. order_to_match = "this is the sentence"
  337. all_synapse_list = [synapse1,
  338. synapse2,
  339. synapse3]
  340. # Success
  341. expected_result = synapse1
  342. oa_tuple_list = OrderAnalyser._get_matching_synapse_list(all_synapses_list=all_synapse_list,
  343. order_to_match=order_to_match)
  344. self.assertEquals(oa_tuple_list[0].synapse,
  345. expected_result,
  346. "Fail matching 'the expected synapse' from the complete synapse list and the order")
  347. # Multiple Matching synapses
  348. signal2 = Order(sentence="this is the sentence")
  349. synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2])
  350. order_to_match = "this is the sentence"
  351. all_synapse_list = [synapse1,
  352. synapse2,
  353. synapse3]
  354. expected_result = [synapse1,
  355. synapse2]
  356. oa_tuple_list = OrderAnalyser._get_matching_synapse_list(all_synapses_list=all_synapse_list,
  357. order_to_match=order_to_match)
  358. self.assertEquals(oa_tuple_list[0].synapse,
  359. expected_result[0],
  360. "Fail 'Multiple Matching synapses' from the complete synapse list and the order (first element)")
  361. self.assertEquals(oa_tuple_list[1].synapse,
  362. expected_result[1],
  363. "Fail 'Multiple Matching synapses' from the complete synapse list and the order (second element)")
  364. # matching no synapses
  365. order_to_match = "this is not the correct word"
  366. all_synapse_list = [synapse1,
  367. synapse2,
  368. synapse3]
  369. expected_result = []
  370. self.assertEquals(OrderAnalyser._get_matching_synapse_list(all_synapses_list=all_synapse_list,
  371. order_to_match=order_to_match),
  372. expected_result,
  373. "Fail matching 'no synapses' from the complete synapse list and the order")
  374. # matching synapse with all key worlds
  375. # /!\ Some words in the order are matching all words in synapses signals !
  376. order_to_match = "this is not the correct sentence"
  377. all_synapse_list = [synapse1,
  378. synapse2,
  379. synapse3]
  380. expected_result = [synapse1,
  381. synapse2]
  382. oa_tuple_list = OrderAnalyser._get_matching_synapse_list(all_synapses_list=all_synapse_list,
  383. order_to_match=order_to_match)
  384. self.assertEquals(oa_tuple_list[0].synapse,
  385. expected_result[0],
  386. "Fail matching 'synapse with all key worlds' from the complete synapse list and the order (first element)")
  387. self.assertEquals(oa_tuple_list[1].synapse,
  388. expected_result[1],
  389. "Fail matching 'synapse with all key worlds' from the complete synapse list and the order (second element)")
  390. def test_get_params_from_order(self):
  391. string_order = "this is the {{ sentence }}"
  392. order_to_check = "this is the value"
  393. expected_result = {'sentence': 'value'}
  394. self.assertEquals(OrderAnalyser._get_params_from_order(string_order=string_order, order_to_check=order_to_check),
  395. expected_result,
  396. "Fail to retrieve 'the params' of the string_order from the order")
  397. # Multiple match
  398. string_order = "this is the {{ sentence }}"
  399. order_to_check = "this is the value with multiple words"
  400. expected_result = {'sentence': 'value with multiple words'}
  401. self.assertEqual(OrderAnalyser._get_params_from_order(string_order=string_order, order_to_check=order_to_check),
  402. expected_result,
  403. "Fail to retrieve the 'multiple words params' of the string_order from the order")
  404. # Multiple params
  405. string_order = "this is the {{ sentence }} with multiple {{ params }}"
  406. order_to_check = "this is the value with multiple words"
  407. expected_result = {'sentence': 'value',
  408. 'params':'words'}
  409. self.assertEqual(OrderAnalyser._get_params_from_order(string_order=string_order, order_to_check=order_to_check),
  410. expected_result,
  411. "Fail to retrieve the 'multiple params' of the string_order from the order")
  412. # Multiple params with multiple words
  413. string_order = "this is the {{ sentence }} with multiple {{ params }}"
  414. order_to_check = "this is the multiple values with multiple values as words"
  415. expected_result = {'sentence': 'multiple values',
  416. 'params': 'values as words'}
  417. self.assertEqual(OrderAnalyser._get_params_from_order(string_order=string_order, order_to_check=order_to_check),
  418. expected_result,
  419. "Fail to retrieve the 'multiple params with multiple words' of the string_order from the order")
  420. # params at the begining of the sentence
  421. string_order = "{{ sentence }} this is the sentence"
  422. order_to_check = "hello world this is the multiple values with multiple values as words"
  423. expected_result = {'sentence': 'hello world'}
  424. self.assertEqual(OrderAnalyser._get_params_from_order(string_order=string_order, order_to_check=order_to_check),
  425. expected_result,
  426. "Fail to retrieve the 'params at the begining of the sentence' of the string_order from the order")
  427. # all of the sentence is a variable
  428. string_order = "{{ sentence }}"
  429. order_to_check = "this is the all sentence is a variable"
  430. expected_result = {'sentence': 'this is the all sentence is a variable'}
  431. self.assertEqual(OrderAnalyser._get_params_from_order(string_order=string_order, order_to_check=order_to_check),
  432. expected_result,
  433. "Fail to retrieve the 'all of the sentence is a variable' of the string_order from the order")
  434. def test_get_default_synapse_from_sysnapses_list(self):
  435. # Init
  436. neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
  437. neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
  438. neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
  439. neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})
  440. signal1 = Order(sentence="this is the sentence")
  441. signal2 = Order(sentence="this is the second sentence")
  442. signal3 = Order(sentence="that is part of the third sentence")
  443. synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
  444. synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2])
  445. synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3])
  446. default_synapse_name = "Synapse2"
  447. all_synapse_list = [synapse1,
  448. synapse2,
  449. synapse3]
  450. expected_result = synapse2
  451. # Assert equals
  452. self.assertEquals(OrderAnalyser._get_default_synapse_from_sysnapses_list(all_synapses_list=all_synapse_list,
  453. default_synapse_name=default_synapse_name),
  454. expected_result,
  455. "Fail to match the expected default Synapse")
  456. def test_find_synapse_to_run(self):
  457. """
  458. Test to find the good synapse to run
  459. Scenarii:
  460. - Find the synapse
  461. - No synpase found, no default synapse
  462. - No synapse found, run the default synapse
  463. """
  464. # Init
  465. neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
  466. neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
  467. neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
  468. neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})
  469. signal1 = Order(sentence="this is the sentence")
  470. signal2 = Order(sentence="this is the second sentence")
  471. signal3 = Order(sentence="that is part of the third sentence")
  472. synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
  473. synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2])
  474. synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3])
  475. all_synapse_list = [synapse1,
  476. synapse2,
  477. synapse3]
  478. br = Brain(synapses=all_synapse_list)
  479. st = Settings()
  480. # Find synapse
  481. order = "this is the sentence"
  482. expected_result = synapse1
  483. oa_tuple_list = OrderAnalyser._find_synapse_to_run(brain=br,settings=st, order=order)
  484. self.assertEquals(oa_tuple_list[0].synapse,
  485. expected_result,
  486. "Fail to run the proper synapse matching the order")
  487. expected_result = signal1.sentence
  488. self.assertEquals(oa_tuple_list[0].order,
  489. expected_result,
  490. "Fail to run the proper synapse matching the order")
  491. # No Default synapse
  492. order = "No default synapse"
  493. expected_result = []
  494. self.assertEquals(OrderAnalyser._find_synapse_to_run(brain=br,settings=st, order=order),
  495. expected_result,
  496. "Fail to run no synapse, when no default is defined")
  497. # Default synapse
  498. st = Settings(default_synapse="Synapse2")
  499. order = "default synapse"
  500. expected_result = synapse2
  501. oa_tuple_list = OrderAnalyser._find_synapse_to_run(brain=br, settings=st, order=order)
  502. self.assertEquals(oa_tuple_list[0].synapse,
  503. expected_result,
  504. "Fail to run the default synapse")
  505. if __name__ == '__main__':
  506. unittest.main()