Fix uncaught exceptions for discovery, unify_direct, spotify,… (#33735)

* used coroutinemock to avoid exception

* added spec to mock to remove exception

* added the current_user return value so it doesnt throw exception

* fix the mocks so properties do not need .return_value

* fixed missing mock values that were causing exceptions

* moved patch to asynctest so no need to define m_init

* fixed black error
This commit is contained in:
Ziv 2020-04-07 19:34:13 +03:00 committed by GitHub
parent 60bc517d01
commit bee742994e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 20 additions and 29 deletions

View File

@ -22,13 +22,18 @@ from tests.common import MockConfigEntry, mock_coro
def cast_mock(): def cast_mock():
"""Mock pychromecast.""" """Mock pychromecast."""
pycast_mock = MagicMock() pycast_mock = MagicMock()
dial_mock = MagicMock(name="XXX")
dial_mock.get_device_status.return_value.uuid = "fake_uuid"
dial_mock.get_device_status.return_value.manufacturer = "fake_manufacturer"
dial_mock.get_device_status.return_value.model_name = "fake_model_name"
dial_mock.get_device_status.return_value.friendly_name = "fake_friendly_name"
with patch( with patch(
"homeassistant.components.cast.media_player.pychromecast", pycast_mock "homeassistant.components.cast.media_player.pychromecast", pycast_mock
), patch( ), patch(
"homeassistant.components.cast.discovery.pychromecast", pycast_mock "homeassistant.components.cast.discovery.pychromecast", pycast_mock
), patch( ), patch(
"homeassistant.components.cast.helpers.dial", MagicMock() "homeassistant.components.cast.helpers.dial", dial_mock
), patch( ), patch(
"homeassistant.components.cast.media_player.MultizoneManager", MagicMock() "homeassistant.components.cast.media_player.MultizoneManager", MagicMock()
): ):
@ -127,7 +132,7 @@ async def test_start_discovery_called_once(hass):
"""Test pychromecast.start_discovery called exactly once.""" """Test pychromecast.start_discovery called exactly once."""
with patch( with patch(
"homeassistant.components.cast.discovery.pychromecast.start_discovery", "homeassistant.components.cast.discovery.pychromecast.start_discovery",
return_value=(None, None), return_value=(None, Mock()),
) as start_discovery: ) as start_discovery:
await async_setup_cast(hass) await async_setup_cast(hass)

View File

@ -1,6 +1,7 @@
"""The tests for the discovery component.""" """The tests for the discovery component."""
from unittest.mock import MagicMock, patch from unittest.mock import MagicMock
from asynctest import patch
import pytest import pytest
from homeassistant import config_entries from homeassistant import config_entries

View File

@ -157,8 +157,8 @@ async def test_scenes_unauthorized_loads_platforms(
request_info=request_info, history=None, status=403 request_info=request_info, history=None, status=403
) )
mock_token = Mock() mock_token = Mock()
mock_token.access_token.return_value = str(uuid4()) mock_token.access_token = str(uuid4())
mock_token.refresh_token.return_value = str(uuid4()) mock_token.refresh_token = str(uuid4())
smartthings_mock.generate_tokens.return_value = mock_token smartthings_mock.generate_tokens.return_value = mock_token
subscriptions = [ subscriptions = [
subscription_factory(capability) for capability in device.capabilities subscription_factory(capability) for capability in device.capabilities
@ -189,8 +189,8 @@ async def test_config_entry_loads_platforms(
smartthings_mock.devices.return_value = [device] smartthings_mock.devices.return_value = [device]
smartthings_mock.scenes.return_value = [scene] smartthings_mock.scenes.return_value = [scene]
mock_token = Mock() mock_token = Mock()
mock_token.access_token.return_value = str(uuid4()) mock_token.access_token = str(uuid4())
mock_token.refresh_token.return_value = str(uuid4()) mock_token.refresh_token = str(uuid4())
smartthings_mock.generate_tokens.return_value = mock_token smartthings_mock.generate_tokens.return_value = mock_token
subscriptions = [ subscriptions = [
subscription_factory(capability) for capability in device.capabilities subscription_factory(capability) for capability in device.capabilities
@ -223,8 +223,8 @@ async def test_config_entry_loads_unconnected_cloud(
smartthings_mock.devices.return_value = [device] smartthings_mock.devices.return_value = [device]
smartthings_mock.scenes.return_value = [scene] smartthings_mock.scenes.return_value = [scene]
mock_token = Mock() mock_token = Mock()
mock_token.access_token.return_value = str(uuid4()) mock_token.access_token = str(uuid4())
mock_token.refresh_token.return_value = str(uuid4()) mock_token.refresh_token = str(uuid4())
smartthings_mock.generate_tokens.return_value = mock_token smartthings_mock.generate_tokens.return_value = mock_token
subscriptions = [ subscriptions = [
subscription_factory(capability) for capability in device.capabilities subscription_factory(capability) for capability in device.capabilities

View File

@ -86,7 +86,8 @@ async def test_full_flow(hass, aiohttp_client, aioclient_mock):
}, },
) )
with patch("homeassistant.components.spotify.config_flow.Spotify"): with patch("homeassistant.components.spotify.config_flow.Spotify") as spotify_mock:
spotify_mock.return_value.current_user.return_value = {"id": "fake_id"}
result = await hass.config_entries.flow.async_configure(result["flow_id"]) result = await hass.config_entries.flow.async_configure(result["flow_id"])
assert result["data"]["auth_implementation"] == DOMAIN assert result["data"]["auth_implementation"] == DOMAIN

View File

@ -16,6 +16,7 @@ from homeassistant.components.unifi_direct.device_tracker import (
CONF_PORT, CONF_PORT,
DOMAIN, DOMAIN,
PLATFORM_SCHEMA, PLATFORM_SCHEMA,
UnifiDeviceScanner,
_response_to_json, _response_to_json,
get_scanner, get_scanner,
) )
@ -37,7 +38,7 @@ def setup_comp(hass):
os.remove(yaml_devices) os.remove(yaml_devices)
@patch(scanner_path, return_value=mock.MagicMock()) @patch(scanner_path, return_value=mock.MagicMock(spec=UnifiDeviceScanner))
async def test_get_scanner(unifi_mock, hass): async def test_get_scanner(unifi_mock, hass):
"""Test creating an Unifi direct scanner with a password.""" """Test creating an Unifi direct scanner with a password."""
conf_dict = { conf_dict = {

View File

@ -1,11 +1,6 @@
"""List of modules that have uncaught exceptions today. Will be shrunk over time.""" """List of modules that have uncaught exceptions today. Will be shrunk over time."""
IGNORE_UNCAUGHT_EXCEPTIONS = [ IGNORE_UNCAUGHT_EXCEPTIONS = [
("tests.components.cast.test_media_player", "test_start_discovery_called_once"),
("tests.components.cast.test_media_player", "test_entry_setup_single_config"),
("tests.components.cast.test_media_player", "test_entry_setup_list_config"),
("tests.components.cast.test_media_player", "test_entry_setup_platform_not_ready"),
("tests.components.demo.test_init", "test_setting_up_demo"), ("tests.components.demo.test_init", "test_setting_up_demo"),
("tests.components.discovery.test_init", "test_discover_config_flow"),
("tests.components.dyson.test_air_quality", "test_purecool_aiq_attributes"), ("tests.components.dyson.test_air_quality", "test_purecool_aiq_attributes"),
("tests.components.dyson.test_air_quality", "test_purecool_aiq_update_state"), ("tests.components.dyson.test_air_quality", "test_purecool_aiq_update_state"),
( (
@ -42,18 +37,6 @@ IGNORE_UNCAUGHT_EXCEPTIONS = [
("tests.components.qwikswitch.test_init", "test_binary_sensor_device"), ("tests.components.qwikswitch.test_init", "test_binary_sensor_device"),
("tests.components.qwikswitch.test_init", "test_sensor_device"), ("tests.components.qwikswitch.test_init", "test_sensor_device"),
("tests.components.rflink.test_init", "test_send_command_invalid_arguments"), ("tests.components.rflink.test_init", "test_send_command_invalid_arguments"),
("tests.components.unifi_direct.test_device_tracker", "test_get_scanner"),
] ]
IGNORE_UNCAUGHT_JSON_EXCEPTIONS = [ IGNORE_UNCAUGHT_JSON_EXCEPTIONS = []
("tests.components.spotify.test_config_flow", "test_full_flow"),
("tests.components.smartthings.test_init", "test_config_entry_loads_platforms"),
(
"tests.components.smartthings.test_init",
"test_scenes_unauthorized_loads_platforms",
),
(
"tests.components.smartthings.test_init",
"test_config_entry_loads_unconnected_cloud",
),
]