<commit_before>import unittest
from unittest import mock

from mycroft.tts.mimic2_tts import Mimic2


@mock.patch('mycroft.tts.mimic2_tts.FuturesSession')
@mock.patch('mycroft.tts.tts.PlaybackThread')
class TestMimic2(unittest.TestCase):
    def test_get_tts(self, _, mock_session):
        mock_session_instance = mock.Mock(name='SessionMock')
        mock_session.return_value = mock_session_instance

        get_mock = mock.Mock(name='getMock')
        mock_session_instance.get.return_value = get_mock

        result_mock = mock.Mock(name='resultMock')
        get_mock.result.return_value = result_mock
        result_mock.json.return_value = {'audio_base64': '', 'visimes': ''}
        result_mock.status_code = 200
        m2 = Mimic2('en-US', {'url': 'https://just.testing.nu'})

        with mock.patch('mycroft.tts.mimic2_tts.open') as mock_open:
            wav_file, vis = m2.get_tts("Hello old friend", 'test.wav')
        self.assertTrue(mock_session_instance.get.called)

    def test_visemes(self, _, __):
        m2 = Mimic2('en-US', {'url': 'https://just.testing.nu'})
        phonemes = [('pau', 0.137), ('hh', 0.236), ('ax', 0.286), ('l', 0.387),
                    ('ow', 0.542), ('f', 0.642), ('r', 0.728), ('eh', 0.807),
                    ('n', 0.899), ('d', 1.033), ('pau', 1.187)]<commit_msg>Add test case for Server error<commit_after>import unittest
from unittest import mock

from mycroft.tts.mimic2_tts import Mimic2
from mycroft.tts.remote_tts import RemoteTTSException


@mock.patch('mycroft.tts.mimic2_tts.FuturesSession')
@mock.patch('mycroft.tts.tts.PlaybackThread')
class TestMimic2(unittest.TestCase):
    def test_get_tts(self, _, mock_session):
        mock_session_instance = mock.Mock(name='SessionMock')
        mock_session.return_value = mock_session_instance

        get_mock = mock.Mock(name='getMock')
        mock_session_instance.get.return_value = get_mock

        result_mock = mock.Mock(name='resultMock')
        get_mock.result.return_value = result_mock
        result_mock.json.return_value = {'audio_base64': '', 'visimes': ''}
        result_mock.status_code = 200
        m2 = Mimic2('en-US', {'url': 'https://just.testing.nu'})

        with mock.patch('mycroft.tts.mimic2_tts.open') as mock_open:
            wav_file, vis = m2.get_tts("Hello old friend", 'test.wav')
        self.assertTrue(mock_session_instance.get.called)

    def test_get_tts_backend_error(self, _, mock_session):
        mock_session_instance = mock.Mock(name='SessionMock')
        mock_session.return_value = mock_session_instance

        get_mock = mock.Mock(name='getMock')
        mock_session_instance.get.return_value = get_mock

        result_mock = mock.Mock(name='resultMock')
        get_mock.result.return_value = result_mock
        result_mock.json.return_value = ''
        result_mock.status_code = 500

        m2 = Mimic2('en-US', {'url': 'https://just.testing.nu'})
        with self.assertRaises(RemoteTTSException):
            with mock.patch('mycroft.tts.mimic2_tts.open') as mock_open:
                wav_file, vis = m2.get_tts("Hello old friend", 'test.wav')

    def test_visemes(self, _, __):
        m2 = Mimic2('en-US', {'url': 'https://just.testing.nu'})
        phonemes = [('pau', 0.137), ('hh', 0.236), ('ax', 0.286), ('l', 0.387),
                    ('ow', 0.542), ('f', 0.642), ('r', 0.728), ('eh', 0.807),
                    ('n', 0.899), ('d', 1.033), ('pau', 1.187)]
{'fraction_non_alphanumeric': 0.1559872426790374, 'fraction_numerical': 0.03624238909828936, 'mean_word_length': 3.151624548736462, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2349756', 'n_tokens_mistral': 1310, 'n_tokens_neox': 1205, 'n_words': 198}