test_utils.py 9.5 KB

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