test_order_analyser.py 8.9 KB

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