test_tts_module.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import os
  2. import unittest
  3. import mock
  4. from kalliope.core.Models.Settings import Settings
  5. from kalliope.core.TTS.TTSModule import TTSModule, TtsGenerateAudioFunctionNotFound
  6. from kalliope.core.Utils.FileManager import FileManager
  7. class TestTTSModule(unittest.TestCase):
  8. """
  9. Class to test TTSModule
  10. """
  11. def setUp(self):
  12. self.TTSMod = TTSModule(language='tests')
  13. pass
  14. def test_generate_md5_from_words(self):
  15. """
  16. Test generate md5 method
  17. """
  18. word = "kalliope"
  19. expected_result = "5c186d1e123be2667fb5fd54640e4fd0"
  20. self.assertEqual(TTSModule.generate_md5_from_words(words=word),
  21. expected_result,
  22. "Fail md5")
  23. def test_get_path_to_store_audio(self):
  24. """
  25. Test the path to store audio
  26. """
  27. self.TTSMod.words = "kalliope"
  28. settings = Settings(cache_path="/tmp/kalliope/tests")
  29. self.TTSMod.settings = settings
  30. expected_result = "/tmp/kalliope/tests/TTSModule/tests/default/5c186d1e123be2667fb5fd54640e4fd0.tts"
  31. self.assertEqual(self.TTSMod._get_path_to_store_audio(),
  32. expected_result,
  33. "fail test_get_path_to_store_audio, expected path not corresponding to result")
  34. def test_generate_and_play(self):
  35. """
  36. Test to generate and play sound
  37. """
  38. def new_play_audio(TTSModule):
  39. pass
  40. words = "kalliope"
  41. with mock.patch.object(TTSModule, 'play_audio', new=new_play_audio):
  42. settings = Settings(cache_path="/tmp/kalliope/tests")
  43. self.TTSMod.settings = settings
  44. # test missing callback
  45. with self.assertRaises(TtsGenerateAudioFunctionNotFound):
  46. self.TTSMod.generate_and_play(words=words)
  47. # Assert Callback is called
  48. # no Cache
  49. self.TTSMod.cache = False
  50. generate_audio_function_from_child = mock.Mock()
  51. self.TTSMod.generate_and_play(words=words,
  52. generate_audio_function_from_child=generate_audio_function_from_child)
  53. generate_audio_function_from_child.assert_called()
  54. # with cache True but not existing on system
  55. self.TTSMod.cache = True
  56. generate_audio_function_from_child = mock.Mock()
  57. self.TTSMod.generate_and_play(words=words,
  58. generate_audio_function_from_child=generate_audio_function_from_child)
  59. generate_audio_function_from_child.assert_called()
  60. # with cache True and existing on system
  61. # create tmp file
  62. tmp_base_path = "/tmp/kalliope/tests/TTSModule/tests/default/"
  63. file_path = os.path.join(tmp_base_path, "5c186d1e123be2667fb5fd54640e4fd0.tts")
  64. if os.path.isfile(file_path):
  65. # Remove the file
  66. FileManager.remove_file(file_path)
  67. if not os.path.exists(tmp_base_path):
  68. os.makedirs(tmp_base_path)
  69. FileManager.write_in_file(file_path, "[kalliope-test] test_generate_and_play")
  70. self.TTSMod.cache = True
  71. generate_audio_function_from_child = mock.Mock()
  72. self.TTSMod.generate_and_play(words=words,
  73. generate_audio_function_from_child=generate_audio_function_from_child)
  74. generate_audio_function_from_child.assert_not_called()
  75. # Remove the tmp file
  76. FileManager.remove_file(file_path)
  77. def test_is_file_already_in_cache(self):
  78. """
  79. Test if file is already stored in cache
  80. """
  81. base_cache_path = "/tmp/kalliope/tests/TTSModule/tests/default/"
  82. md5_word = "5c186d1e123be2667fb5fd54640e4fd0"
  83. file_path = os.path.join(base_cache_path, "5c186d1e123be2667fb5fd54640e4fd0.tts")
  84. if os.path.isfile(file_path):
  85. # Remove the file
  86. FileManager.remove_file(file_path)
  87. # Create a tmp file
  88. if not os.path.exists(base_cache_path):
  89. os.makedirs(base_cache_path)
  90. tmp_path = os.path.join(base_cache_path, md5_word+".tts")
  91. FileManager.write_in_file(tmp_path, "[kalliope-test] test_is_file_already_in_cache")
  92. # Test true
  93. self.assertTrue(TTSModule._is_file_already_in_cache(base_cache_path=base_cache_path, file_path=file_path),
  94. "Fail retrieving the cached file. The file does not exist but it should !")
  95. # Remove the tmp file
  96. FileManager.remove_file(tmp_path)
  97. # Test False
  98. self.assertFalse(TTSModule._is_file_already_in_cache(base_cache_path=base_cache_path, file_path=file_path),
  99. "Fail asserting that the file does not exist.")
  100. if __name__ == '__main__':
  101. unittest.main()