test_utils.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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")