1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Improve SmartThings test mocking (#25028)

* Migrate to asynctest

* Simplify mock access

* Use mocks
This commit is contained in:
Andrew Sayre
2019-07-08 22:39:55 -04:00
committed by GitHub
parent 2fbbcafaed
commit a31e49c857
5 changed files with 186 additions and 336 deletions

View File

@@ -1,7 +1,7 @@
"""Tests for the smartapp module."""
from unittest.mock import Mock, patch
from uuid import uuid4
from asynctest import CoroutineMock, Mock, patch
from pysmartthings import AppEntity, Capability
from homeassistant.components.smartthings import smartapp
@@ -9,8 +9,6 @@ from homeassistant.components.smartthings.const import (
CONF_INSTALLED_APP_ID, CONF_INSTALLED_APPS, CONF_LOCATION_ID,
CONF_REFRESH_TOKEN, DATA_MANAGER, DOMAIN)
from tests.common import mock_coro
async def test_update_app(hass, app):
"""Test update_app does not save if app is current."""
@@ -20,10 +18,8 @@ async def test_update_app(hass, app):
async def test_update_app_updated_needed(hass, app):
"""Test update_app updates when an app is needed."""
mock_app = Mock(spec=AppEntity)
mock_app = Mock(AppEntity)
mock_app.app_name = 'Test'
mock_app.refresh.return_value = mock_coro()
mock_app.save.return_value = mock_coro()
await smartapp.update_app(hass, mock_app)
@@ -64,7 +60,6 @@ async def test_smartapp_install_creates_flow(
"""Test installation creates flow."""
# Arrange
setattr(hass.config_entries, '_entries', [config_entry])
api = smartthings_mock.return_value
app = Mock()
app.app_id = config_entry.data['app_id']
request = Mock()
@@ -77,8 +72,7 @@ async def test_smartapp_install_creates_flow(
device_factory('', [Capability.switch, Capability.switch_level]),
device_factory('', [Capability.switch])
]
api.devices = Mock()
api.devices.return_value = mock_coro(return_value=devices)
smartthings_mock.devices.return_value = devices
# Act
await smartapp.smartapp_install(hass, request, None, app)
# Assert
@@ -131,8 +125,7 @@ async def test_smartapp_uninstall(hass, config_entry):
request = Mock()
request.installed_app_id = config_entry.data['installed_app_id']
with patch.object(hass.config_entries, 'async_remove',
return_value=mock_coro()) as remove:
with patch.object(hass.config_entries, 'async_remove') as remove:
await smartapp.smartapp_uninstall(hass, request, None, app)
assert remove.call_count == 1
@@ -140,12 +133,11 @@ async def test_smartapp_uninstall(hass, config_entry):
async def test_smartapp_webhook(hass):
"""Test the smartapp webhook calls the manager."""
manager = Mock()
manager.handle_request = Mock()
manager.handle_request.return_value = mock_coro(return_value={})
manager.handle_request = CoroutineMock(return_value={})
hass.data[DOMAIN][DATA_MANAGER] = manager
request = Mock()
request.headers = []
request.json.return_value = mock_coro(return_value={})
request.json = CoroutineMock(return_value={})
result = await smartapp.smartapp_webhook(hass, '', request)
assert result.body == b'{}'
@@ -154,15 +146,11 @@ async def test_smartapp_webhook(hass):
async def test_smartapp_sync_subscriptions(
hass, smartthings_mock, device_factory, subscription_factory):
"""Test synchronization adds and removes."""
api = smartthings_mock.return_value
api.delete_subscription.side_effect = lambda loc_id, sub_id: mock_coro()
api.create_subscription.side_effect = lambda sub: mock_coro()
subscriptions = [
smartthings_mock.subscriptions.return_value = [
subscription_factory(Capability.thermostat),
subscription_factory(Capability.switch),
subscription_factory(Capability.switch_level)
]
api.subscriptions.return_value = mock_coro(return_value=subscriptions)
devices = [
device_factory('', [Capability.battery, 'ping']),
device_factory('', [Capability.switch, Capability.switch_level]),
@@ -172,23 +160,19 @@ async def test_smartapp_sync_subscriptions(
await smartapp.smartapp_sync_subscriptions(
hass, str(uuid4()), str(uuid4()), str(uuid4()), devices)
assert api.subscriptions.call_count == 1
assert api.delete_subscription.call_count == 1
assert api.create_subscription.call_count == 1
assert smartthings_mock.subscriptions.call_count == 1
assert smartthings_mock.delete_subscription.call_count == 1
assert smartthings_mock.create_subscription.call_count == 1
async def test_smartapp_sync_subscriptions_up_to_date(
hass, smartthings_mock, device_factory, subscription_factory):
"""Test synchronization does nothing when current."""
api = smartthings_mock.return_value
api.delete_subscription.side_effect = lambda loc_id, sub_id: mock_coro()
api.create_subscription.side_effect = lambda sub: mock_coro()
subscriptions = [
smartthings_mock.subscriptions.return_value = [
subscription_factory(Capability.battery),
subscription_factory(Capability.switch),
subscription_factory(Capability.switch_level)
]
api.subscriptions.return_value = mock_coro(return_value=subscriptions)
devices = [
device_factory('', [Capability.battery, 'ping']),
device_factory('', [Capability.switch, Capability.switch_level]),
@@ -198,25 +182,21 @@ async def test_smartapp_sync_subscriptions_up_to_date(
await smartapp.smartapp_sync_subscriptions(
hass, str(uuid4()), str(uuid4()), str(uuid4()), devices)
assert api.subscriptions.call_count == 1
assert api.delete_subscription.call_count == 0
assert api.create_subscription.call_count == 0
assert smartthings_mock.subscriptions.call_count == 1
assert smartthings_mock.delete_subscription.call_count == 0
assert smartthings_mock.create_subscription.call_count == 0
async def test_smartapp_sync_subscriptions_handles_exceptions(
hass, smartthings_mock, device_factory, subscription_factory):
"""Test synchronization does nothing when current."""
api = smartthings_mock.return_value
api.delete_subscription.side_effect = \
lambda loc_id, sub_id: mock_coro(exception=Exception)
api.create_subscription.side_effect = \
lambda sub: mock_coro(exception=Exception)
subscriptions = [
smartthings_mock.delete_subscription.side_effect = Exception
smartthings_mock.create_subscription.side_effect = Exception
smartthings_mock.subscriptions.return_value = [
subscription_factory(Capability.battery),
subscription_factory(Capability.switch),
subscription_factory(Capability.switch_level)
]
api.subscriptions.return_value = mock_coro(return_value=subscriptions)
devices = [
device_factory('', [Capability.thermostat, 'ping']),
device_factory('', [Capability.switch, Capability.switch_level]),
@@ -226,6 +206,6 @@ async def test_smartapp_sync_subscriptions_handles_exceptions(
await smartapp.smartapp_sync_subscriptions(
hass, str(uuid4()), str(uuid4()), str(uuid4()), devices)
assert api.subscriptions.call_count == 1
assert api.delete_subscription.call_count == 1
assert api.create_subscription.call_count == 1
assert smartthings_mock.subscriptions.call_count == 1
assert smartthings_mock.delete_subscription.call_count == 1
assert smartthings_mock.create_subscription.call_count == 1