Migrate local_file tests from coroutine to async/await (#30392)

This commit is contained in:
Franck Nijhof 2020-01-02 21:21:54 +01:00 committed by Andrew Sayre
parent 5edf72c9ea
commit 7b00e94184

View File

@ -1,5 +1,4 @@
"""The tests for local file camera component.""" """The tests for local file camera component."""
import asyncio
from unittest import mock from unittest import mock
from homeassistant.components.local_file.const import DOMAIN, SERVICE_UPDATE_FILE_PATH from homeassistant.components.local_file.const import DOMAIN, SERVICE_UPDATE_FILE_PATH
@ -8,15 +7,14 @@ from homeassistant.setup import async_setup_component
from tests.common import mock_registry from tests.common import mock_registry
@asyncio.coroutine async def test_loading_file(hass, hass_client):
def test_loading_file(hass, hass_client):
"""Test that it loads image from disk.""" """Test that it loads image from disk."""
mock_registry(hass) mock_registry(hass)
with mock.patch("os.path.isfile", mock.Mock(return_value=True)), mock.patch( with mock.patch("os.path.isfile", mock.Mock(return_value=True)), mock.patch(
"os.access", mock.Mock(return_value=True) "os.access", mock.Mock(return_value=True)
): ):
yield from async_setup_component( await async_setup_component(
hass, hass,
"camera", "camera",
{ {
@ -28,26 +26,25 @@ def test_loading_file(hass, hass_client):
}, },
) )
client = yield from hass_client() client = await hass_client()
m_open = mock.mock_open(read_data=b"hello") m_open = mock.mock_open(read_data=b"hello")
with mock.patch( with mock.patch(
"homeassistant.components.local_file.camera.open", m_open, create=True "homeassistant.components.local_file.camera.open", m_open, create=True
): ):
resp = yield from client.get("/api/camera_proxy/camera.config_test") resp = await client.get("/api/camera_proxy/camera.config_test")
assert resp.status == 200 assert resp.status == 200
body = yield from resp.text() body = await resp.text()
assert body == "hello" assert body == "hello"
@asyncio.coroutine async def test_file_not_readable(hass, caplog):
def test_file_not_readable(hass, caplog):
"""Test a warning is shown setup when file is not readable.""" """Test a warning is shown setup when file is not readable."""
with mock.patch("os.path.isfile", mock.Mock(return_value=True)), mock.patch( with mock.patch("os.path.isfile", mock.Mock(return_value=True)), mock.patch(
"os.access", mock.Mock(return_value=False) "os.access", mock.Mock(return_value=False)
): ):
yield from async_setup_component( await async_setup_component(
hass, hass,
"camera", "camera",
{ {
@ -64,8 +61,7 @@ def test_file_not_readable(hass, caplog):
assert "mock.file" in caplog.text assert "mock.file" in caplog.text
@asyncio.coroutine async def test_camera_content_type(hass, hass_client):
def test_camera_content_type(hass, hass_client):
"""Test local_file camera content_type.""" """Test local_file camera content_type."""
cam_config_jpg = { cam_config_jpg = {
"name": "test_jpg", "name": "test_jpg",
@ -88,43 +84,43 @@ def test_camera_content_type(hass, hass_client):
"file_path": "/path/to/image", "file_path": "/path/to/image",
} }
yield from async_setup_component( await async_setup_component(
hass, hass,
"camera", "camera",
{"camera": [cam_config_jpg, cam_config_png, cam_config_svg, cam_config_noext]}, {"camera": [cam_config_jpg, cam_config_png, cam_config_svg, cam_config_noext]},
) )
client = yield from hass_client() client = await hass_client()
image = "hello" image = "hello"
m_open = mock.mock_open(read_data=image.encode()) m_open = mock.mock_open(read_data=image.encode())
with mock.patch( with mock.patch(
"homeassistant.components.local_file.camera.open", m_open, create=True "homeassistant.components.local_file.camera.open", m_open, create=True
): ):
resp_1 = yield from client.get("/api/camera_proxy/camera.test_jpg") resp_1 = await client.get("/api/camera_proxy/camera.test_jpg")
resp_2 = yield from client.get("/api/camera_proxy/camera.test_png") resp_2 = await client.get("/api/camera_proxy/camera.test_png")
resp_3 = yield from client.get("/api/camera_proxy/camera.test_svg") resp_3 = await client.get("/api/camera_proxy/camera.test_svg")
resp_4 = yield from client.get("/api/camera_proxy/camera.test_no_ext") resp_4 = await client.get("/api/camera_proxy/camera.test_no_ext")
assert resp_1.status == 200 assert resp_1.status == 200
assert resp_1.content_type == "image/jpeg" assert resp_1.content_type == "image/jpeg"
body = yield from resp_1.text() body = await resp_1.text()
assert body == image assert body == image
assert resp_2.status == 200 assert resp_2.status == 200
assert resp_2.content_type == "image/png" assert resp_2.content_type == "image/png"
body = yield from resp_2.text() body = await resp_2.text()
assert body == image assert body == image
assert resp_3.status == 200 assert resp_3.status == 200
assert resp_3.content_type == "image/svg+xml" assert resp_3.content_type == "image/svg+xml"
body = yield from resp_3.text() body = await resp_3.text()
assert body == image assert body == image
# default mime type # default mime type
assert resp_4.status == 200 assert resp_4.status == 200
assert resp_4.content_type == "image/jpeg" assert resp_4.content_type == "image/jpeg"
body = yield from resp_4.text() body = await resp_4.text()
assert body == image assert body == image