test_utils.py 4.6 KB

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