test_utils.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import unittest
  2. import os
  3. import mock
  4. from kalliope.core.Models.Neuron import Neuron
  5. from kalliope.core.Models.Order import Order
  6. from kalliope.core.Models.Synapse import Synapse
  7. from kalliope.neurons.say.say import Say
  8. from kalliope.core.Utils.Utils import Utils
  9. from kalliope.core.ConfigurationManager import SettingLoader
  10. from kalliope.core.ConfigurationManager import BrainLoader
  11. class TestUtils(unittest.TestCase):
  12. """
  13. Class to test Utils methods
  14. """
  15. def setUp(self):
  16. pass
  17. def test_get_current_file_parent_path(self):
  18. """
  19. Expect to get back the parent path file
  20. """
  21. path_to_test = "../kalliope/core/Utils"
  22. expected_result = os.path.normpath("../kalliope/core")
  23. self.assertEquals(Utils.get_current_file_parent_path(path_to_test),
  24. expected_result,
  25. "fail getting the parent parent path from the given path")
  26. def test_get_current_file_parent_parent_path(self):
  27. """
  28. Expect to get back the parent parent path file
  29. """
  30. path_to_test = "../kalliope/core/Utils"
  31. expected_result = os.path.normpath("../kalliope")
  32. self.assertEquals(Utils.get_current_file_parent_parent_path(path_to_test),
  33. expected_result,
  34. "fail getting the parent parent path from the given path")
  35. def test_get_real_file_path(self):
  36. """
  37. Expect to load the proper file following the order :
  38. - Provided absolute path
  39. - Current user path + file_name
  40. - /etc/kalliope + file_name
  41. - /path/to/kalliope/ +file_name
  42. """
  43. ###
  44. # Test the absolute path
  45. dir_path = "/tmp/kalliope/tests/"
  46. file_name = "test_real_file_path"
  47. absolute_path_to_test = os.path.join(dir_path,file_name)
  48. expected_result = absolute_path_to_test
  49. if not os.path.exists(dir_path):
  50. os.makedirs(dir_path)
  51. # touch the file
  52. open(absolute_path_to_test, 'a').close()
  53. self.assertEquals(Utils.get_real_file_path(absolute_path_to_test),
  54. expected_result,
  55. "Fail to match the given absolute path ")
  56. # Clean up
  57. if os.path.exists(absolute_path_to_test):
  58. os.remove(absolute_path_to_test)
  59. ###
  60. # test the Current path
  61. file_name = "test_real_file_path"
  62. expected_result = os.getcwd() + os.sep + file_name
  63. # touch the file
  64. open(file_name, 'a').close()
  65. self.assertEquals(Utils.get_real_file_path(file_name),
  66. expected_result,
  67. "Fail to match the Current path ")
  68. # Clean up
  69. if os.path.exists(file_name):
  70. os.remove(file_name)
  71. ###
  72. # test /etc/kalliope
  73. # /!\ need permissions
  74. # dir_path = "/etc/kalliope/"
  75. # file_name = "test_real_file_path"
  76. # path_to_test = os.path.join(dir_path,file_name)
  77. # expected_result = "/etc/kalliope" + os.sep + file_name
  78. # if not os.path.exists(dir_path):
  79. # os.makedirs(dir_path)
  80. #
  81. # # touch the file
  82. # open(path_to_test, 'a').close()
  83. #
  84. # self.assertEquals(Utils.get_real_file_path(file_name),
  85. # expected_result,
  86. # "Fail to match the /etc/kalliope path")
  87. # # Clean up
  88. # if os.path.exists(file_name):
  89. # os.remove(file_name)
  90. ###
  91. # /an/unknown/path/kalliope/
  92. dir_path = "../kalliope/"
  93. file_name = "test_real_file_path"
  94. path_to_test = os.path.join(dir_path, file_name)
  95. expected_result = os.path.normpath(os.getcwd() + os.sep + os.pardir + os.sep +"kalliope" + os.sep + file_name)
  96. if not os.path.exists(dir_path):
  97. os.makedirs(dir_path)
  98. # touch the file
  99. open(path_to_test, 'a').close()
  100. self.assertEquals(Utils.get_real_file_path(file_name),
  101. expected_result,
  102. "Fail to match the /an/unknown/path/kalliope path")
  103. # Clean up
  104. if os.path.exists(file_name):
  105. os.remove(file_name)
  106. def test_get_dynamic_class_instantiation(self):
  107. """
  108. Test that an instance as been instantiate properly.
  109. """
  110. sl = SettingLoader()
  111. sl.settings.resource_dir = '/var/tmp/test/resources'
  112. neuron = Neuron(name='Say', parameters={'message': 'test dynamic class instantiate'})
  113. self.assertTrue(isinstance(Utils.get_dynamic_class_instantiation(package_name="neurons",
  114. module_name=neuron.name.capitalize(),
  115. parameters=neuron.parameters,
  116. resources_dir='/var/tmp/test/resources'),
  117. Say),
  118. "Fail instantiate a class")
  119. def test_is_containing_bracket(self):
  120. # Success
  121. order_to_test = "This test contains {{ bracket }}"
  122. self.assertTrue(Utils.is_containing_bracket(order_to_test),
  123. "Fail returning True when order contains spaced brackets")
  124. order_to_test = "This test contains {{bracket }}"
  125. self.assertTrue(Utils.is_containing_bracket(order_to_test),
  126. "Fail returning True when order contains right spaced bracket")
  127. order_to_test = "This test contains {{ bracket}}"
  128. self.assertTrue(Utils.is_containing_bracket(order_to_test),
  129. "Fail returning True when order contains left spaced bracket")
  130. order_to_test = "This test contains {{bracket}}"
  131. self.assertTrue(Utils.is_containing_bracket(order_to_test),
  132. "Fail returning True when order contains no spaced bracket")
  133. # Failure
  134. order_to_test = "This test does not contain bracket"
  135. self.assertFalse(Utils.is_containing_bracket(order_to_test),
  136. "Fail returning False when order has no brackets")
  137. # Behaviour
  138. order_to_test = ""
  139. self.assertFalse(Utils.is_containing_bracket(order_to_test),
  140. "Fail returning False when no order")
  141. def test_get_next_value_list(self):
  142. # Success
  143. list_to_test = {1, 2, 3}
  144. self.assertEqual(Utils.get_next_value_list(list_to_test), 2,
  145. "Fail to match the expected next value from the list")
  146. # Failure
  147. list_to_test = {1}
  148. self.assertEqual(Utils.get_next_value_list(list_to_test), None,
  149. "Fail to ensure there is no next value from the list")
  150. # Behaviour
  151. list_to_test = {}
  152. self.assertEqual(Utils.get_next_value_list(list_to_test), None,
  153. "Fail to ensure the empty list return None value")
  154. def test_find_all_matching_brackets(self):
  155. """
  156. Test the Utils find all matching brackets
  157. """
  158. sentence = "This is the {{bracket}}"
  159. expected_result = ["{{bracket}}"]
  160. self.assertEqual(Utils.find_all_matching_brackets(sentence=sentence),
  161. expected_result,
  162. "Fail to match one bracket")
  163. sentence = "This is the {{bracket}} {{second}}"
  164. expected_result = ["{{bracket}}", "{{second}}"]
  165. self.assertEqual(Utils.find_all_matching_brackets(sentence=sentence),
  166. expected_result,
  167. "Fail to match two brackets")
  168. def test_remove_spaces_in_brackets(self):
  169. """
  170. Test the Utils remove_spaces_in_brackets
  171. """
  172. sentence = "This is the {{ bracket }}"
  173. expected_result = "This is the {{bracket}}"
  174. self.assertEqual(Utils.remove_spaces_in_brackets(sentence=sentence),
  175. expected_result,
  176. "Fail to remove spaces in one bracket")
  177. sentence = "This is the {{ bracket }} {{ second }}"
  178. expected_result = "This is the {{bracket}} {{second}}"
  179. self.assertEqual(Utils.remove_spaces_in_brackets(sentence=sentence),
  180. expected_result,
  181. "Fail to remove spaces in two brackets")