test_dynamic_loading.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import inspect
  2. import os
  3. import unittest
  4. class TestDynamicLoading(unittest.TestCase):
  5. """
  6. Test case for dynamic loading of python class
  7. This is used to test we can successfully import:
  8. - STT engine
  9. - TTS engine
  10. - Trigger engine
  11. - All core neurons
  12. """
  13. def setUp(self):
  14. # get current script directory path. We are in /an/unknown/path/kalliope/core/tests
  15. cur_script_directory = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
  16. # get parent dir. Now we are in /an/unknown/path/kalliope
  17. root_dir = os.path.normpath(cur_script_directory + os.sep + os.pardir)
  18. # get the neuron dir
  19. self.neurons_dir = os.path.normpath(root_dir + os.sep + "kalliope/neurons")
  20. # get stt dir
  21. self.stt_dir = os.path.normpath(root_dir + os.sep + "kalliope/stt")
  22. # get tts dir
  23. self.tts_dir = os.path.normpath(root_dir + os.sep + "kalliope/tts")
  24. # get trigger dir
  25. self.trigger_dir = os.path.normpath(root_dir + os.sep + "kalliope/trigger")
  26. def test_packages_present(self):
  27. """
  28. Check that the neurons folder exist in the root of the project
  29. """
  30. self.assertTrue(os.path.isdir(self.neurons_dir))
  31. self.assertTrue(os.path.isdir(self.stt_dir))
  32. self.assertTrue(os.path.isdir(self.tts_dir))
  33. self.assertTrue(os.path.isdir(self.trigger_dir))
  34. def test_can_import_neurons(self):
  35. """
  36. Try to import each neurons that are present in the neurons package
  37. :return:
  38. """
  39. neurons = self.get_package_in_folder(self.neurons_dir)
  40. package_name = "neurons"
  41. for neuron_name in neurons:
  42. module_name = neuron_name.capitalize()
  43. self.dynamic_import(package_name, module_name)
  44. def test_can_import_stt(self):
  45. """
  46. Try to import each stt that are present in the stt package
  47. :return:
  48. """
  49. stts = self.get_package_in_folder(self.stt_dir)
  50. package_name = "stt"
  51. for stt_name in stts:
  52. module_name = stt_name.capitalize()
  53. self.dynamic_import(package_name, module_name)
  54. def test_can_import_tts(self):
  55. """
  56. Try to import each tts that are present in the tts package
  57. :return:
  58. """
  59. ttss = self.get_package_in_folder(self.tts_dir)
  60. package_name = "tts"
  61. for tts_name in ttss:
  62. module_name = tts_name.capitalize()
  63. self.dynamic_import(package_name, module_name)
  64. def test_can_import_trigger(self):
  65. """
  66. Try to import each trigger that are present in the trigger package
  67. :return:
  68. """
  69. triggers = self.get_package_in_folder(self.trigger_dir)
  70. package_name = "trigger"
  71. for trigger in triggers:
  72. module_name = trigger.capitalize()
  73. self.dynamic_import(package_name, module_name)
  74. @staticmethod
  75. def get_package_in_folder(folder):
  76. """
  77. receive a path in <folder>, return a list of package in that folder.
  78. The function test if elements in that path are directory and return a list of those directory
  79. :param folder: Path of a folder to return package
  80. :return: list of package name
  81. """
  82. # get the list of neurons in the neurons packages
  83. el_folder = os.listdir(folder)
  84. # we keep only package. Because we have _init_.py or other stuff in what listdir returned
  85. packages_in_folder = list()
  86. for el in el_folder:
  87. if os.path.isdir(folder + os.sep + el) and not '__pycache__' in el:
  88. packages_in_folder.append(el)
  89. return packages_in_folder
  90. def dynamic_import(self, package_name, module_name):
  91. """
  92. Dynamic import of a module by its name.
  93. package name can be:
  94. - triggers
  95. - neurons
  96. - stt
  97. - tts
  98. :param package_name: name of the mother package
  99. :param module_name: module name to load
  100. :return:
  101. """
  102. module_name_with_path = "kalliope." + package_name + "." + module_name.lower() + "." + module_name.lower()
  103. mod = __import__(module_name_with_path, fromlist=[module_name])
  104. try:
  105. getattr(mod, module_name)
  106. except AttributeError:
  107. self.fail("The module %s does not exist in package %s" % (module_name, package_name))
  108. if __name__ == '__main__':
  109. unittest.main()