test_order_analyser.py 23 KB

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