Improve patching in marytts tests (#39458)

* improve patching in marytts tests

* Update test_tts.py

* Update test_tts.py

* Update test_tts.py

* Update test_tts.py

* Update test_tts.py

* Update test_tts.py
pull/39454/head
Chris Talkington 2020-08-30 13:58:25 -05:00 committed by GitHub
parent 56ddbd87b2
commit 65a9e18b27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 31 deletions

View File

@ -2,7 +2,6 @@
import asyncio
import os
import shutil
from urllib.parse import urlencode
from homeassistant.components.media_player.const import (
ATTR_MEDIA_CONTENT_ID,
@ -11,10 +10,9 @@ from homeassistant.components.media_player.const import (
)
import homeassistant.components.tts as tts
from homeassistant.config import async_process_ha_core_config
from homeassistant.const import HTTP_INTERNAL_SERVER_ERROR
from homeassistant.setup import setup_component
from tests.async_mock import Mock, patch
from tests.async_mock import patch
from tests.common import assert_setup_component, get_test_home_assistant, mock_service
@ -62,18 +60,15 @@ class TestTTSMaryTTSPlatform:
"""Test service call say."""
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
conn = Mock()
response = Mock()
conn.getresponse.return_value = response
response.status = 200
response.read.return_value = b"audio"
config = {tts.DOMAIN: {"platform": "marytts"}}
with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config)
with patch("http.client.HTTPConnection", return_value=conn):
with patch(
"homeassistant.components.marytts.tts.MaryTTS.speak",
return_value=b"audio",
) as mock_speak:
self.hass.services.call(
tts.DOMAIN,
"marytts_say",
@ -86,18 +81,14 @@ class TestTTSMaryTTSPlatform:
assert len(calls) == 1
assert calls[0].data[ATTR_MEDIA_CONTENT_ID].find(".wav") != -1
conn.request.assert_called_with("POST", "/process", urlencode(self.params))
mock_speak.assert_called_once()
mock_speak.assert_called_with("HomeAssistant", {})
def test_service_say_with_effect(self):
"""Test service call say with effects."""
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
conn = Mock()
response = Mock()
conn.getresponse.return_value = response
response.status = 200
response.read.return_value = b"audio"
config = {
tts.DOMAIN: {"platform": "marytts", "effect": {"Volume": "amount:2.0;"}}
}
@ -105,7 +96,10 @@ class TestTTSMaryTTSPlatform:
with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config)
with patch("http.client.HTTPConnection", return_value=conn):
with patch(
"homeassistant.components.marytts.tts.MaryTTS.speak",
return_value=b"audio",
) as mock_speak:
self.hass.services.call(
tts.DOMAIN,
"marytts_say",
@ -119,28 +113,22 @@ class TestTTSMaryTTSPlatform:
assert len(calls) == 1
assert calls[0].data[ATTR_MEDIA_CONTENT_ID].find(".wav") != -1
self.params.update(
{"effect_Volume_selected": "on", "effect_Volume_parameters": "amount:2.0;"}
)
conn.request.assert_called_with("POST", "/process", urlencode(self.params))
mock_speak.assert_called_once()
mock_speak.assert_called_with("HomeAssistant", {"Volume": "amount:2.0;"})
def test_service_say_http_error(self):
"""Test service call say."""
calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)
conn = Mock()
response = Mock()
conn.getresponse.return_value = response
response.status = HTTP_INTERNAL_SERVER_ERROR
response.reason = "test"
response.readline.return_value = "content"
config = {tts.DOMAIN: {"platform": "marytts"}}
with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config)
with patch("http.client.HTTPConnection", return_value=conn):
with patch(
"homeassistant.components.marytts.tts.MaryTTS.speak",
side_effect=Exception(),
) as mock_speak:
self.hass.services.call(
tts.DOMAIN,
"marytts_say",
@ -152,4 +140,5 @@ class TestTTSMaryTTSPlatform:
self.hass.block_till_done()
assert len(calls) == 0
conn.request.assert_called_with("POST", "/process", urlencode(self.params))
mock_speak.assert_called_once()