test_tts_module.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import unittest
  2. import mock
  3. import os
  4. from core.TTS.TTSModule import TTSModule
  5. from core.Models.Settings import Settings
  6. from core.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.assertEquals(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.assertEquals(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():
  39. pass
  40. with mock.patch.object(TTSModule, 'play_audio', new=new_play_audio):
  41. self.TTSMod.words = "kalliope"
  42. settings = Settings(cache_path="/tmp/kalliope/tests")
  43. self.TTSMod.settings = settings
  44. def test_is_file_already_in_cache(self):
  45. """
  46. Test if file is already stored in cache
  47. """
  48. base_cache_path = "/tmp/kalliope/tests/TTSModule/tests/default/"
  49. md5_word = "5c186d1e123be2667fb5fd54640e4fd0"
  50. file_path = "/tmp/kalliope/tests/TTSModule/tests/default/5c186d1e123be2667fb5fd54640e4fd0.tts"
  51. # Create a tmp file
  52. tmp_path = os.path.join(base_cache_path, md5_word+".tts")
  53. FileManager.write_in_file(tmp_path, "[kalliope-test] test_is_file_already_in_cache")
  54. # Test true
  55. self.assertTrue(TTSModule._is_file_already_in_cache(base_cache_path=base_cache_path, file_path=file_path),
  56. "Fail retrieving the cached file. The file does not exist but it should !")
  57. # Remove the tmp file
  58. FileManager.remove_file(tmp_path)
  59. # Test False
  60. self.assertFalse(TTSModule._is_file_already_in_cache(base_cache_path=base_cache_path, file_path=file_path),
  61. "Fail asserting that the file does not exist.")