test_order_analyser.py 11 KB

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