test_order_analyser.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import unittest
  2. from core.OrderAnalyser import OrderAnalyser
  3. class TestOrderAnalyser(unittest.TestCase):
  4. """Test case for the OrderAnalyser Class"""
  5. def test_is_containing_bracket(self):
  6. # Success
  7. order_to_test = "This test contains {{ bracket }}"
  8. self.assertTrue(OrderAnalyser._is_containing_bracket(order_to_test),
  9. "Fail returning True when order contains spaced brackets")
  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 right spaced bracket")
  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 left 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 no spaced bracket")
  19. # Failure
  20. order_to_test = "This test does not contain bracket"
  21. self.assertFalse(OrderAnalyser._is_containing_bracket(order_to_test),
  22. "Fail returning False when order has no brackets")
  23. # Behaviour
  24. order_to_test = ""
  25. self.assertFalse(OrderAnalyser._is_containing_bracket(order_to_test),
  26. "Fail returning False when no order")
  27. def test_get_next_value_list(self):
  28. # Success
  29. list_to_test = {1, 2, 3}
  30. self.assertEqual(OrderAnalyser._get_next_value_list(list_to_test),2,
  31. "Fail to match the expected next value from the list")
  32. # Failure
  33. list_to_test = {1}
  34. self.assertEqual(OrderAnalyser._get_next_value_list(list_to_test), None,
  35. "Fail to ensure there is no next value from the list")
  36. # Behaviour
  37. list_to_test = {}
  38. self.assertEqual(OrderAnalyser._get_next_value_list(list_to_test), None,
  39. "Fail to ensure the empty list return None value")
  40. def test_spelt_order_match_brain_order_via_table(self):
  41. order_to_test = "this is the order"
  42. sentence_to_test = "this is the order"
  43. # Success
  44. self.assertTrue(OrderAnalyser._spelt_order_match_brain_order_via_table(order_to_test, sentence_to_test),
  45. "Fail matching order with the expected sentence")
  46. # Failure
  47. sentence_to_test = "unexpected sentence"
  48. self.assertFalse(OrderAnalyser._spelt_order_match_brain_order_via_table(order_to_test, sentence_to_test),
  49. "Fail to ensure the expected sentence is not matching the order")
  50. def test_get_split_order_without_bracket(self):
  51. # Success
  52. order_to_test = "this is the order"
  53. expected_result = ["this", "is", "the", "order"]
  54. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test),expected_result,
  55. "No brackets Fails to return the expected list")
  56. order_to_test = "this is the {{ order }}"
  57. expected_result = ["this", "is", "the"]
  58. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  59. "With spaced brackets Fails to return the expected list")
  60. order_to_test = "this is the {{order }}" # left bracket without space
  61. expected_result = ["this", "is", "the"]
  62. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  63. "Left brackets Fails to return the expected list")
  64. order_to_test = "this is the {{ order}}" # right bracket without space
  65. expected_result = ["this", "is", "the"]
  66. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  67. "Right brackets Fails to return the expected list")
  68. order_to_test = "this is the {{order}}" # bracket without space
  69. expected_result = ["this", "is", "the"]
  70. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  71. "No space brackets Fails to return the expected list")
  72. def test_associate_order_params_to_values(self):
  73. ##
  74. # Testing the brackets position behaviour
  75. ##
  76. # Success
  77. order_brain = "This is the {{ variable }}"
  78. order_user = "This is the value"
  79. expected_result = {'variable': 'value'}
  80. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user,order_brain), expected_result,
  81. "Fail to match the order_brain {{ variable }} to the 'value'")
  82. # Success
  83. order_brain = "This is the {{variable }}"
  84. order_user = "This is the value"
  85. expected_result = {'variable': 'value'}
  86. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  87. "Fail to match the order_brain {{variable }} to the 'value'")
  88. # Success
  89. order_brain = "This is the {{ variable}}"
  90. order_user = "This is the value"
  91. expected_result = {'variable': 'value'}
  92. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  93. "Fail to match the order_brain {{ variable}} to the 'value'")
  94. # Success
  95. order_brain = "This is the {{variable}}"
  96. order_user = "This is the value"
  97. expected_result = {'variable': 'value'}
  98. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  99. "Fail to match the order_brain {{variable}} to the 'value'")
  100. # Fail
  101. order_brain = "This is the {variable}"
  102. order_user = "This is the value"
  103. expected_result = {'variable': 'value'}
  104. self.assertNotEquals(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  105. "Should not match the order_brain {variable} to the 'value'")
  106. # Fail
  107. order_brain = "This is the { variable}}"
  108. order_user = "This is the value"
  109. expected_result = {'variable': 'value'}
  110. self.assertNotEquals(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  111. "Should not match the order_brain { variable}} to the 'value'")
  112. ##
  113. # Testing the brackets position in the sentence
  114. ##
  115. # Success
  116. order_brain = "{{ variable }} This is the"
  117. order_user = "value This is the"
  118. expected_result = {'variable': 'value'}
  119. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  120. "Fail to match the order_brain {{ variable }} in first position ins the sentence to the 'value'")
  121. # Success
  122. order_brain = "This is {{ variable }} the"
  123. order_user = " This is value the"
  124. expected_result = {'variable': 'value'}
  125. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  126. "Fail to match the order_brain {{ variable }} in middle position ins the sentence to the 'value'")
  127. ##
  128. # Testing multi variables
  129. ##
  130. # Success
  131. order_brain = "This is {{ variable }} the {{ variable2 }}"
  132. order_user = "This is value the value2"
  133. expected_result = {'variable': 'value',
  134. 'variable2': 'value2'}
  135. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user, order_brain), expected_result,
  136. "Fail to match the order_brain multi variable to the multi values")
  137. ##
  138. # Testing multi words in variable
  139. ##
  140. # Success
  141. order_brain = "This is the {{ variable }}"
  142. order_user = "This is the value with multiple words"
  143. expected_result = {'variable': 'value with multiple words'}
  144. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user,order_brain), expected_result,
  145. "Fail to match the order_brain {{ variable }} to the 'value with multiple words'")
  146. # Success
  147. order_brain = "This is the {{ variable }} and {{ variable2 }}"
  148. order_user = "This is the value with multiple words and second value multiple"
  149. expected_result = {'variable': 'value with multiple words',
  150. 'variable2': 'second value multiple'}
  151. self.assertEqual(OrderAnalyser._associate_order_params_to_values(order_user,order_brain), expected_result,
  152. "Fail to match the order_brain multiple variables with multiple words as values'")
  153. if __name__ == '__main__':
  154. unittest.main()