From 18e965c3cdaa73d652dc51e815dfe015f070f455 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 3 Nov 2016 22:56:55 -0700 Subject: [PATCH] Fix flaky group notify test (#4212) --- homeassistant/components/notify/group.py | 3 +- tests/components/notify/test_group.py | 57 +++++++++--------------- 2 files changed, 23 insertions(+), 37 deletions(-) diff --git a/homeassistant/components/notify/group.py b/homeassistant/components/notify/group.py index 9a7d8b69681..d4c10ac884d 100644 --- a/homeassistant/components/notify/group.py +++ b/homeassistant/components/notify/group.py @@ -5,6 +5,7 @@ For more details about this platform, please refer to the documentation at https://home-assistant.io/components/notify.group/ """ import collections +from copy import deepcopy import logging import voluptuous as vol @@ -56,7 +57,7 @@ class GroupNotifyPlatform(BaseNotificationService): payload.update({key: val for key, val in kwargs.items() if val}) for entity in self.entities: - sending_payload = payload.copy() + sending_payload = deepcopy(payload.copy()) if entity.get(ATTR_DATA) is not None: update(sending_payload, entity.get(ATTR_DATA)) self.hass.services.call(DOMAIN, entity.get(ATTR_SERVICE), diff --git a/tests/components/notify/test_group.py b/tests/components/notify/test_group.py index 98c6ab6e5cf..f0871c78322 100644 --- a/tests/components/notify/test_group.py +++ b/tests/components/notify/test_group.py @@ -1,10 +1,10 @@ """The tests for the notify.group platform.""" import unittest +from unittest.mock import MagicMock, patch -from homeassistant.core import callback from homeassistant.bootstrap import setup_component import homeassistant.components.notify as notify -from homeassistant.components.notify import group +from homeassistant.components.notify import group, demo from tests.common import assert_setup_component, get_test_home_assistant @@ -16,7 +16,17 @@ class TestNotifyGroup(unittest.TestCase): """Setup things to be run when tests are started.""" self.hass = get_test_home_assistant() self.events = [] - with assert_setup_component(2): + self.service1 = MagicMock() + self.service2 = MagicMock() + + def mock_get_service(hass, config): + if config['name'] == 'demo1': + return self.service1 + else: + return self.service2 + + with assert_setup_component(2), \ + patch.object(demo, 'get_service', mock_get_service): setup_component(self.hass, notify.DOMAIN, { 'notify': [{ 'name': 'demo1', @@ -35,49 +45,24 @@ class TestNotifyGroup(unittest.TestCase): assert self.service is not None - @callback - def record_event(event): - """Record event to send notification.""" - self.events.append(event) - - self.hass.bus.listen("notify", record_event) - def tearDown(self): # pylint: disable=invalid-name """"Stop everything that was started.""" self.hass.stop() - def test_send_message_to_group(self): - """Test sending a message to a notify group.""" - self.service.send_message('Hello', title='Test notification') - self.hass.block_till_done() - self.assertTrue(len(self.events) == 2) - last_event = self.events[-1] - self.assertEqual(last_event.data[notify.ATTR_TITLE], - 'Test notification') - self.assertEqual(last_event.data[notify.ATTR_MESSAGE], 'Hello') - def test_send_message_with_data(self): """Test sending a message with to a notify group.""" - notify_data = {'hello': 'world'} self.service.send_message('Hello', title='Test notification', - data=notify_data) + data={'hello': 'world'}) self.hass.block_till_done() - last_event = self.events[-1] - self.assertEqual(last_event.data[notify.ATTR_TITLE], - 'Test notification') - self.assertEqual(last_event.data[notify.ATTR_MESSAGE], 'Hello') - self.assertEqual(last_event.data[notify.ATTR_DATA], notify_data) - def test_entity_data_passes_through(self): - """Test sending a message with data to merge to a notify group.""" - notify_data = {'hello': 'world'} - self.service.send_message('Hello', title='Test notification', - data=notify_data) - self.hass.block_till_done() - data = self.events[-1].data - assert { + assert self.service1.send_message.mock_calls[0][2] == { + 'message': 'Hello', + 'title': 'Test notification', + 'data': {'hello': 'world'} + } + assert self.service2.send_message.mock_calls[0][2] == { 'message': 'Hello', 'target': ['unnamed device'], 'title': 'Test notification', 'data': {'hello': 'world', 'test': 'message'} - } == data + }