Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Commit

Permalink
Add test cases for tts cache __contains__
Browse files Browse the repository at this point in the history
  • Loading branch information
forslund committed Jun 10, 2021
1 parent 5d3393b commit 4799caa
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion test/unittests/tts/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pathlib import Path
from tempfile import mkdtemp
from unittest import TestCase
from unittest.mock import MagicMock, patch
from unittest.mock import Mock, MagicMock, patch

from mycroft.tts.cache import hash_sentence, TextToSpeechCache

Expand Down Expand Up @@ -121,3 +121,51 @@ def test_curate_cache(self, curate_mock):
# dict of hashes.
self.assertEqual(tts_cache.cached_sentences,
{'fozzie': (files['fozzie'], None)})


class MockFile(Mock):
def __init__(self, exists, *args, **kwargs):
super().__init__(args, kwargs)
self._exists = exists

def exists(self):
return self._exists


class TestCacheContains(TestCase):
"""Verify the `"X" in tts_cache` functionality."""
def setUp(self):
self.cache_dir = Path(mkdtemp())
self.tts_cache = TextToSpeechCache(
tts_config=dict(preloaded_cache=self.cache_dir),
tts_name="Test",
audio_file_type="wav"
)
files = {
'kermit': (MockFile(exists=True), MockFile(exists=True)),
'fozzie': (MockFile(exists=True), None),
'gobo': (MockFile(exists=False), None),
'piggy': (MockFile(exists=True), MockFile(exists=False))
}
for sentence_hash, (audio_file, phoneme_file) in files.items():
self.tts_cache.cached_sentences[sentence_hash] = (
audio_file, phoneme_file
)

def tearDown(self):
for file_path in self.cache_dir.iterdir():
file_path.unlink()
self.cache_dir.rmdir()

def test_hash_not_listed(self):
self.assertFalse('animal' in self.tts_cache)

def test_hash_exists_and_files_ok(self):
self.assertTrue('kermit' in self.tts_cache)

def test_only_audio_hash_exists_and_files_ok(self):
self.assertTrue('fozzie' in self.tts_cache)

def test_hash_exists_and_files_bad(self):
self.assertFalse('piggy' in self.tts_cache)
self.assertFalse('gobo' in self.tts_cache)

0 comments on commit 4799caa

Please sign in to comment.