Fix static/class async mocks on Python 3.8.0 and .1 (#40147)

* forked_daapd

* shelly

* simplipy
This commit is contained in:
Ville Skyttä 2020-09-17 08:20:00 +03:00 committed by GitHub
parent f6584c1866
commit 271ffac4a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 27 deletions

View file

@ -16,7 +16,7 @@ from homeassistant.config_entries import (
)
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT
from tests.async_mock import patch
from tests.async_mock import AsyncMock, patch
from tests.common import MockConfigEntry
SAMPLE_CONFIG = {
@ -69,7 +69,8 @@ async def test_show_form(hass):
async def test_config_flow(hass, config_entry):
"""Test that the user step works."""
with patch(
"homeassistant.components.forked_daapd.config_flow.ForkedDaapdAPI.test_connection"
"homeassistant.components.forked_daapd.config_flow.ForkedDaapdAPI.test_connection",
new=AsyncMock(),
) as mock_test_connection, patch(
"homeassistant.components.forked_daapd.media_player.ForkedDaapdAPI.get_request",
autospec=True,
@ -119,7 +120,8 @@ async def test_zeroconf_updates_title(hass, config_entry):
async def test_config_flow_no_websocket(hass, config_entry):
"""Test config flow setup without websocket enabled on server."""
with patch(
"homeassistant.components.forked_daapd.config_flow.ForkedDaapdAPI.test_connection"
"homeassistant.components.forked_daapd.config_flow.ForkedDaapdAPI.test_connection",
new=AsyncMock(),
) as mock_test_connection:
# test invalid config data
mock_test_connection.return_value = ["websocket_not_enabled"]

View file

@ -25,9 +25,11 @@ async def test_form(hass):
return_value={"mac": "test-mac", "type": "SHSW-1", "auth": False},
), patch(
"aioshelly.Device.create",
return_value=Mock(
shutdown=AsyncMock(),
settings={"name": "Test name", "device": {"mac": "test-mac"}},
new=AsyncMock(
return_value=Mock(
shutdown=AsyncMock(),
settings={"name": "Test name", "device": {"mac": "test-mac"}},
)
),
), patch(
"homeassistant.components.shelly.async_setup", return_value=True
@ -72,9 +74,11 @@ async def test_form_auth(hass):
with patch(
"aioshelly.Device.create",
return_value=Mock(
shutdown=AsyncMock(),
settings={"name": "Test name", "device": {"mac": "test-mac"}},
new=AsyncMock(
return_value=Mock(
shutdown=AsyncMock(),
settings={"name": "Test name", "device": {"mac": "test-mac"}},
)
),
), patch(
"homeassistant.components.shelly.async_setup", return_value=True
@ -136,7 +140,7 @@ async def test_form_errors_test_connection(hass, error):
"aioshelly.get_info", return_value={"mac": "test-mac", "auth": False}
), patch(
"aioshelly.Device.create",
side_effect=exc,
new=AsyncMock(side_effect=exc),
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
@ -199,7 +203,7 @@ async def test_form_auth_errors_test_connection(hass, error):
with patch(
"aioshelly.Device.create",
side_effect=exc,
new=AsyncMock(side_effect=exc),
):
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
@ -227,9 +231,11 @@ async def test_zeroconf(hass):
with patch(
"aioshelly.Device.create",
return_value=Mock(
shutdown=AsyncMock(),
settings={"name": "Test name", "device": {"mac": "test-mac"}},
new=AsyncMock(
return_value=Mock(
shutdown=AsyncMock(),
settings={"name": "Test name", "device": {"mac": "test-mac"}},
)
),
), patch(
"homeassistant.components.shelly.async_setup", return_value=True
@ -274,7 +280,7 @@ async def test_zeroconf_confirm_error(hass, error):
with patch(
"aioshelly.Device.create",
side_effect=exc,
new=AsyncMock(side_effect=exc),
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
@ -349,9 +355,11 @@ async def test_zeroconf_require_auth(hass):
with patch(
"aioshelly.Device.create",
return_value=Mock(
shutdown=AsyncMock(),
settings={"name": "Test name", "device": {"mac": "test-mac"}},
new=AsyncMock(
return_value=Mock(
shutdown=AsyncMock(),
settings={"name": "Test name", "device": {"mac": "test-mac"}},
)
),
), patch(
"homeassistant.components.shelly.async_setup", return_value=True

View file

@ -10,7 +10,7 @@ from homeassistant.components.simplisafe import DOMAIN
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER
from homeassistant.const import CONF_CODE, CONF_PASSWORD, CONF_TOKEN, CONF_USERNAME
from tests.async_mock import MagicMock, PropertyMock, patch
from tests.async_mock import AsyncMock, MagicMock, PropertyMock, patch
from tests.common import MockConfigEntry
@ -49,7 +49,7 @@ async def test_invalid_credentials(hass):
with patch(
"simplipy.API.login_via_credentials",
side_effect=InvalidCredentialsError,
new=AsyncMock(side_effect=InvalidCredentialsError),
):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=conf
@ -105,7 +105,9 @@ async def test_step_import(hass):
with patch(
"homeassistant.components.simplisafe.async_setup_entry", return_value=True
), patch("simplipy.API.login_via_credentials", return_value=mock_api()):
), patch(
"simplipy.API.login_via_credentials", new=AsyncMock(return_value=mock_api())
):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=conf
)
@ -140,7 +142,9 @@ async def test_step_reauth(hass):
with patch(
"homeassistant.components.simplisafe.async_setup_entry", return_value=True
), patch("simplipy.API.login_via_credentials", return_value=mock_api()):
), patch(
"simplipy.API.login_via_credentials", new=AsyncMock(return_value=mock_api())
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_PASSWORD: "password"}
)
@ -160,7 +164,9 @@ async def test_step_user(hass):
with patch(
"homeassistant.components.simplisafe.async_setup_entry", return_value=True
), patch("simplipy.API.login_via_credentials", return_value=mock_api()):
), patch(
"simplipy.API.login_via_credentials", new=AsyncMock(return_value=mock_api())
):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=conf
)
@ -183,7 +189,8 @@ async def test_step_user_mfa(hass):
}
with patch(
"simplipy.API.login_via_credentials", side_effect=PendingAuthorizationError
"simplipy.API.login_via_credentials",
new=AsyncMock(side_effect=PendingAuthorizationError),
):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=conf
@ -191,7 +198,8 @@ async def test_step_user_mfa(hass):
assert result["step_id"] == "mfa"
with patch(
"simplipy.API.login_via_credentials", side_effect=PendingAuthorizationError
"simplipy.API.login_via_credentials",
new=AsyncMock(side_effect=PendingAuthorizationError),
):
# Simulate the user pressing the MFA submit button without having clicked
# the link in the MFA email:
@ -202,7 +210,9 @@ async def test_step_user_mfa(hass):
with patch(
"homeassistant.components.simplisafe.async_setup_entry", return_value=True
), patch("simplipy.API.login_via_credentials", return_value=mock_api()):
), patch(
"simplipy.API.login_via_credentials", new=AsyncMock(return_value=mock_api())
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
@ -222,7 +232,7 @@ async def test_unknown_error(hass):
with patch(
"simplipy.API.login_via_credentials",
side_effect=SimplipyError,
new=AsyncMock(side_effect=SimplipyError),
):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=conf