backup_test_order_analyser.py 28 KB

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