test_tts_module.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import unittest
  2. import os
  3. from core.TTS.TTSModule import TTSModule
  4. from core.Models.Settings import Settings
  5. from core.FileManager import FileManager
  6. class TestTTSModule(unittest.TestCase):
  7. """
  8. Class to test TTSModule
  9. """
  10. def setUp(self):
  11. self.TTSMod = TTSModule(language='tests')
  12. pass
  13. def test_generate_md5_from_words(self):
  14. """
  15. Test generate md5 method
  16. """
  17. word = "kalliope"
  18. expected_result = "5c186d1e123be2667fb5fd54640e4fd0"
  19. self.assertEquals(TTSModule.generate_md5_from_words(words=word),
  20. expected_result,
  21. "Fail md5")
  22. def test_get_path_to_store_audio(self):
  23. """
  24. Test the path to store audio
  25. """
  26. self.TTSMod.words = "kalliope"
  27. settings = Settings(cache_path="/tmp/kalliope/tests")
  28. self.TTSMod.settings = settings
  29. expected_result = "/tmp/kalliope/tests/TTSModule/tests/default/5c186d1e123be2667fb5fd54640e4fd0.tts"
  30. self.assertEquals(self.TTSMod._get_path_to_store_audio(),
  31. expected_result,
  32. "fail test_get_path_to_store_audio, expected path not corresponding to result")
  33. def test_generate_and_play(self):
  34. """
  35. Test to generate and play sound
  36. """
  37. def play_audio():
  38. pass
  39. self.TTSMod.words = "kalliope"
  40. settings = Settings(cache_path="/tmp/kalliope/tests")
  41. self.TTSMod.settings = settings
  42. self.TTSMod.play_audio
  43. def test_is_file_already_in_cache(self):
  44. """
  45. Test if file is already stored in cache
  46. """
  47. base_cache_path = "/tmp/kalliope/tests/TTSModule/tests/default/"
  48. md5_word = "5c186d1e123be2667fb5fd54640e4fd0"
  49. file_path = "/tmp/kalliope/tests/TTSModule/tests/default/5c186d1e123be2667fb5fd54640e4fd0.tts"
  50. # Create a tmp file
  51. tmp_path = os.path.join(base_cache_path, md5_word+".tts")
  52. FileManager.write_in_file(tmp_path, "[kalliope-test] test_is_file_already_in_cache")
  53. # Test true
  54. self.assertTrue(TTSModule._is_file_already_in_cache(base_cache_path=base_cache_path, file_path=file_path),
  55. "Fail retrieving the cached file. The file does not exist but it should !")
  56. # Remove the tmp file
  57. FileManager.remove_file(tmp_path)
  58. # Test False
  59. self.assertFalse(TTSModule._is_file_already_in_cache(base_cache_path=base_cache_path, file_path=file_path),
  60. "Fail asserting that the file does not exist.")