test_utils.py 8.9 KB

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