test_order_analyser.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import unittest
  2. from core.OrderAnalyser import OrderAnalyser
  3. from core.Models.Neuron import Neuron
  4. from core.Models.Synapse import Synapse
  5. from core.Models.Order import Order
  6. class TestOrderAnalyser(unittest.TestCase):
  7. """Test case for the OrderAnalyser Class"""
  8. def test_is_containing_bracket(self):
  9. # Success
  10. order_to_test = "This test contains {{ bracket }}"
  11. self.assertTrue(OrderAnalyser._is_containing_bracket(order_to_test),
  12. "Fail returning True when order contains spaced brackets")
  13. order_to_test = "This test contains {{bracket }}"
  14. self.assertTrue(OrderAnalyser._is_containing_bracket(order_to_test),
  15. "Fail returning True when order contains right spaced bracket")
  16. order_to_test = "This test contains {{ bracket}}"
  17. self.assertTrue(OrderAnalyser._is_containing_bracket(order_to_test),
  18. "Fail returning True when order contains left spaced bracket")
  19. order_to_test = "This test contains {{bracket}}"
  20. self.assertTrue(OrderAnalyser._is_containing_bracket(order_to_test),
  21. "Fail returning True when order contains no spaced bracket")
  22. # Failure
  23. order_to_test = "This test does not contain bracket"
  24. self.assertFalse(OrderAnalyser._is_containing_bracket(order_to_test),
  25. "Fail returning False when order has no brackets")
  26. # Behaviour
  27. order_to_test = ""
  28. self.assertFalse(OrderAnalyser._is_containing_bracket(order_to_test),
  29. "Fail returning False when no order")
  30. def test_get_next_value_list(self):
  31. # Success
  32. list_to_test = {1, 2, 3}
  33. self.assertEqual(OrderAnalyser._get_next_value_list(list_to_test), 2,
  34. "Fail to match the expected next value from the list")
  35. # Failure
  36. list_to_test = {1}
  37. self.assertEqual(OrderAnalyser._get_next_value_list(list_to_test), None,
  38. "Fail to ensure there is no next value from the list")
  39. # Behaviour
  40. list_to_test = {}
  41. self.assertEqual(OrderAnalyser._get_next_value_list(list_to_test), None,
  42. "Fail to ensure the empty list return None value")
  43. def test_spelt_order_match_brain_order_via_table(self):
  44. order_to_test = "this is the order"
  45. sentence_to_test = "this is the order"
  46. # Success
  47. self.assertTrue(OrderAnalyser._spelt_order_match_brain_order_via_table(order_to_test, sentence_to_test),
  48. "Fail matching order with the expected sentence")
  49. # Failure
  50. sentence_to_test = "unexpected sentence"
  51. self.assertFalse(OrderAnalyser._spelt_order_match_brain_order_via_table(order_to_test, sentence_to_test),
  52. "Fail to ensure the expected sentence is not matching the order")
  53. def test_get_split_order_without_bracket(self):
  54. # Success
  55. order_to_test = "this is the order"
  56. expected_result = ["this", "is", "the", "order"]
  57. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  58. "No brackets Fails to return the expected list")
  59. order_to_test = "this is the {{ order }}"
  60. expected_result = ["this", "is", "the"]
  61. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  62. "With spaced brackets Fails to return the expected list")
  63. order_to_test = "this is the {{order }}" # left bracket without space
  64. expected_result = ["this", "is", "the"]
  65. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  66. "Left brackets Fails to return the expected list")
  67. order_to_test = "this is the {{ order}}" # right bracket without space
  68. expected_result = ["this", "is", "the"]
  69. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  70. "Right brackets Fails to return the expected list")
  71. order_to_test = "this is the {{order}}" # bracket without space
  72. expected_result = ["this", "is", "the"]
  73. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  74. "No space brackets Fails to return the expected list")
  75. def test_associate_order_params_to_values(self):
  76. ##
  77. # Testing the brackets position behaviour
  78. ##
  79. # Success
  80. order_brain = "This is the {{ variable }}"
  81. order_user = "This is the value"
  82. expected_result = {'variable': 'value'}
  83. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  84. "Fail to match the order_brain {{ variable }} to the 'value'")
  85. # Success
  86. order_brain = "This is the {{variable }}"
  87. order_user = "This is the value"
  88. expected_result = {'variable': 'value'}
  89. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  90. "Fail to match the order_brain {{variable }} to the 'value'")
  91. # Success
  92. order_brain = "This is the {{ variable}}"
  93. order_user = "This is the value"
  94. expected_result = {'variable': 'value'}
  95. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  96. "Fail to match the order_brain {{ variable}} to the 'value'")
  97. # Success
  98. order_brain = "This is the {{variable}}"
  99. order_user = "This is the value"
  100. expected_result = {'variable': 'value'}
  101. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  102. "Fail to match the order_brain {{variable}} to the 'value'")
  103. # Fail
  104. order_brain = "This is the {variable}"
  105. order_user = "This is the value"
  106. expected_result = {'variable': 'value'}
  107. self.assertNotEquals(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  108. "Should not match the order_brain {variable} to the 'value'")
  109. # Fail
  110. order_brain = "This is the { variable}}"
  111. order_user = "This is the value"
  112. expected_result = {'variable': 'value'}
  113. self.assertNotEquals(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  114. "Should not match the order_brain { variable}} to the 'value'")
  115. ##
  116. # Testing the brackets position in the sentence
  117. ##
  118. # Success
  119. order_brain = "{{ variable }} This is the"
  120. order_user = "value This is the"
  121. expected_result = {'variable': 'value'}
  122. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  123. "Fail to match the order_brain {{ variable }} in first position "
  124. "ins the sentence to the 'value'")
  125. # Success
  126. order_brain = "This is {{ variable }} the"
  127. order_user = " This is value the"
  128. expected_result = {'variable': 'value'}
  129. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  130. "Fail to match the order_brain {{ variable }} in middle position ins "
  131. "the sentence to the 'value'")
  132. ##
  133. # Testing multi variables
  134. ##
  135. # Success
  136. order_brain = "This is {{ variable }} the {{ variable2 }}"
  137. order_user = "This is value the value2"
  138. expected_result = {'variable': 'value',
  139. 'variable2': 'value2'}
  140. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  141. "Fail to match the order_brain multi variable to the multi values")
  142. ##
  143. # Testing multi words in variable
  144. ##
  145. # Success
  146. order_brain = "This is the {{ variable }}"
  147. order_user = "This is the value with multiple words"
  148. expected_result = {'variable': 'value with multiple words'}
  149. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  150. "Fail to match the order_brain {{ variable }} to the 'value with multiple words'")
  151. # Success
  152. order_brain = "This is the {{ variable }} and {{ variable2 }}"
  153. order_user = "This is the value with multiple words and second value multiple"
  154. expected_result = {'variable': 'value with multiple words',
  155. 'variable2': 'second value multiple'}
  156. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  157. "Fail to match the order_brain multiple variables with multiple words as values'")
  158. def test_get_matching_synapse_list(self):
  159. # Init
  160. neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
  161. neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
  162. neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
  163. neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})
  164. signal1 = Order(sentence="this is the sentence")
  165. signal2 = Order(sentence="this is the second sentence")
  166. signal3 = Order(sentence="this is the third sentence")
  167. synapse1 = Synapse(name="Synapse1", neurons={neuron1, neuron2}, signals={signal1})
  168. synapse2 = Synapse(name="Synapse2", neurons={neuron3, neuron4}, signals={signal2})
  169. synapse3 = Synapse(name="Synapse3", neurons={neuron2, neuron4}, signals={signal3})
  170. order_to_match = "this is the sentence"
  171. all_synapse_list = [synapse1,
  172. synapse2,
  173. synapse3]
  174. expected_result = [synapse1]
  175. # Success
  176. self.assertEquals(OrderAnalyser._get_matching_synapse_list(all_synapses_list=all_synapse_list,
  177. order_to_match=order_to_match),
  178. expected_result,
  179. "Fail matching the expected synapse from the complete synapse list and the order")
  180. # TODO : to be continued
  181. def test_get_synapse_params(self):
  182. # Init
  183. neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
  184. neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
  185. signal1 = Order(sentence="this is the {{ sentence }}")
  186. synapse1 = Synapse(name="Synapse1", neurons={neuron1, neuron2}, signals={signal1})
  187. order_to_check = "this is the value"
  188. expected_result = {'sentence': 'value'}
  189. self.assertEquals(OrderAnalyser._get_synapse_params(synapse=synapse1, order_to_check=order_to_check),
  190. expected_result,
  191. "Fail to retrieve the params of the synapse from the order")
  192. # TODO : to be continued
  193. if __name__ == '__main__':
  194. unittest.main()