test_utils.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import unittest
  2. import os
  3. from kalliope.core.Utils.Utils import Utils
  4. class TestUtils(unittest.TestCase):
  5. """
  6. Class to test Utils methods
  7. """
  8. def setUp(self):
  9. pass
  10. def test_get_current_file_parent_path(self):
  11. """
  12. Expect to get back the parent path file
  13. """
  14. path_to_test = "../kalliope/core/Utils"
  15. expected_result = os.path.normpath("../kalliope/core")
  16. self.assertEquals(Utils.get_current_file_parent_path(path_to_test),
  17. expected_result,
  18. "fail getting the parent parent path from the given path")
  19. def test_get_current_file_parent_parent_path(self):
  20. """
  21. Expect to get back the parent parent path file
  22. """
  23. path_to_test = "../kalliope/core/Utils"
  24. expected_result = os.path.normpath("../kalliope")
  25. self.assertEquals(Utils.get_current_file_parent_parent_path(path_to_test),
  26. expected_result,
  27. "fail getting the parent parent path from the given path")
  28. def test_get_real_file_path(self):
  29. """
  30. Expect to load the proper file following the order :
  31. - Provided absolute path
  32. - Current user path + file_name
  33. - /etc/kalliope + file_name
  34. - /path/to/kalliope/ +file_name
  35. """
  36. ###
  37. # Test the absolute path
  38. dir_path = "/tmp/kalliope/tests/"
  39. file_name = "test_real_file_path"
  40. absolute_path_to_test = os.path.join(dir_path,file_name)
  41. expected_result = absolute_path_to_test
  42. if not os.path.exists(dir_path):
  43. os.makedirs(dir_path)
  44. # touch the file
  45. open(absolute_path_to_test, 'a').close()
  46. self.assertEquals(Utils.get_real_file_path(absolute_path_to_test),
  47. expected_result,
  48. "Fail to match the given absolute path ")
  49. # Clean up
  50. if os.path.exists(absolute_path_to_test):
  51. os.remove(absolute_path_to_test)
  52. ###
  53. # test the Current path
  54. file_name = "test_real_file_path"
  55. expected_result = os.getcwd() + os.sep + file_name
  56. # touch the file
  57. open(file_name, 'a').close()
  58. self.assertEquals(Utils.get_real_file_path(file_name),
  59. expected_result,
  60. "Fail to match the Current path ")
  61. # Clean up
  62. if os.path.exists(file_name):
  63. os.remove(file_name)
  64. ###
  65. # test /etc/kalliope
  66. # /!\ need permissions
  67. # dir_path = "/etc/kalliope/"
  68. # file_name = "test_real_file_path"
  69. # path_to_test = os.path.join(dir_path,file_name)
  70. # expected_result = "/etc/kalliope" + os.sep + file_name
  71. # if not os.path.exists(dir_path):
  72. # os.makedirs(dir_path)
  73. #
  74. # # touch the file
  75. # open(path_to_test, 'a').close()
  76. #
  77. # self.assertEquals(Utils.get_real_file_path(file_name),
  78. # expected_result,
  79. # "Fail to match the /etc/kalliope path")
  80. # # Clean up
  81. # if os.path.exists(file_name):
  82. # os.remove(file_name)
  83. ###
  84. # /an/unknown/path/kalliope/
  85. dir_path = "../kalliope/"
  86. file_name = "test_real_file_path"
  87. path_to_test = os.path.join(dir_path, file_name)
  88. expected_result = os.path.normpath(os.getcwd() + os.sep + os.pardir + os.sep +"kalliope" + os.sep + file_name)
  89. if not os.path.exists(dir_path):
  90. os.makedirs(dir_path)
  91. # touch the file
  92. open(path_to_test, 'a').close()
  93. pp = Utils.get_real_file_path(file_name)
  94. self.assertEquals(Utils.get_real_file_path(file_name),
  95. expected_result,
  96. "Fail to match the /an/unknown/path/kalliope path")
  97. # Clean up
  98. if os.path.exists(file_name):
  99. os.remove(file_name)