Rewrite datadog tests to pytest (#42059)
This commit is contained in:
parent
6f4be7882b
commit
5a533a8c5d
1 changed files with 66 additions and 77 deletions
|
@ -1,5 +1,4 @@
|
|||
"""The tests for the Datadog component."""
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
import homeassistant.components.datadog as datadog
|
||||
|
@ -10,59 +9,47 @@ from homeassistant.const import (
|
|||
STATE_ON,
|
||||
)
|
||||
import homeassistant.core as ha
|
||||
from homeassistant.setup import setup_component
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import assert_setup_component, get_test_home_assistant
|
||||
from tests.async_mock import MagicMock, patch
|
||||
from tests.common import assert_setup_component
|
||||
|
||||
|
||||
class TestDatadog(unittest.TestCase):
|
||||
"""Test the Datadog component."""
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
self.addCleanup(self.tear_down_cleanup)
|
||||
|
||||
def tear_down_cleanup(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def test_invalid_config(self):
|
||||
"""Test invalid configuration."""
|
||||
with assert_setup_component(0):
|
||||
assert not setup_component(
|
||||
self.hass, datadog.DOMAIN, {datadog.DOMAIN: {"host1": "host1"}}
|
||||
)
|
||||
|
||||
@mock.patch("homeassistant.components.datadog.statsd")
|
||||
@mock.patch("homeassistant.components.datadog.initialize")
|
||||
def test_datadog_setup_full(self, mock_connection, mock_client):
|
||||
"""Test setup with all data."""
|
||||
self.hass.bus.listen = mock.MagicMock()
|
||||
|
||||
assert setup_component(
|
||||
self.hass,
|
||||
datadog.DOMAIN,
|
||||
{datadog.DOMAIN: {"host": "host", "port": 123, "rate": 1, "prefix": "foo"}},
|
||||
async def test_invalid_config(hass):
|
||||
"""Test invalid configuration."""
|
||||
with assert_setup_component(0):
|
||||
assert not await async_setup_component(
|
||||
hass, datadog.DOMAIN, {datadog.DOMAIN: {"host1": "host1"}}
|
||||
)
|
||||
|
||||
assert mock_connection.call_count == 1
|
||||
assert mock_connection.call_args == mock.call(
|
||||
statsd_host="host", statsd_port=123
|
||||
)
|
||||
|
||||
assert self.hass.bus.listen.called
|
||||
assert EVENT_LOGBOOK_ENTRY == self.hass.bus.listen.call_args_list[0][0][0]
|
||||
assert EVENT_STATE_CHANGED == self.hass.bus.listen.call_args_list[1][0][0]
|
||||
async def test_datadog_setup_full(hass):
|
||||
"""Test setup with all data."""
|
||||
config = {datadog.DOMAIN: {"host": "host", "port": 123, "rate": 1, "prefix": "foo"}}
|
||||
hass.bus.listen = MagicMock()
|
||||
|
||||
@mock.patch("homeassistant.components.datadog.statsd")
|
||||
@mock.patch("homeassistant.components.datadog.initialize")
|
||||
def test_datadog_setup_defaults(self, mock_connection, mock_client):
|
||||
"""Test setup with defaults."""
|
||||
self.hass.bus.listen = mock.MagicMock()
|
||||
with patch("homeassistant.components.datadog.initialize") as mock_init, patch(
|
||||
"homeassistant.components.datadog.statsd"
|
||||
):
|
||||
assert await async_setup_component(hass, datadog.DOMAIN, config)
|
||||
|
||||
assert setup_component(
|
||||
self.hass,
|
||||
assert mock_init.call_count == 1
|
||||
assert mock_init.call_args == mock.call(statsd_host="host", statsd_port=123)
|
||||
|
||||
assert hass.bus.listen.called
|
||||
assert EVENT_LOGBOOK_ENTRY == hass.bus.listen.call_args_list[0][0][0]
|
||||
assert EVENT_STATE_CHANGED == hass.bus.listen.call_args_list[1][0][0]
|
||||
|
||||
|
||||
async def test_datadog_setup_defaults(hass):
|
||||
"""Test setup with defaults."""
|
||||
hass.bus.listen = mock.MagicMock()
|
||||
|
||||
with patch("homeassistant.components.datadog.initialize") as mock_init, patch(
|
||||
"homeassistant.components.datadog.statsd"
|
||||
):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
datadog.DOMAIN,
|
||||
{
|
||||
datadog.DOMAIN: {
|
||||
|
@ -73,26 +60,26 @@ class TestDatadog(unittest.TestCase):
|
|||
},
|
||||
)
|
||||
|
||||
assert mock_connection.call_count == 1
|
||||
assert mock_connection.call_args == mock.call(
|
||||
statsd_host="host", statsd_port=8125
|
||||
)
|
||||
assert self.hass.bus.listen.called
|
||||
assert mock_init.call_count == 1
|
||||
assert mock_init.call_args == mock.call(statsd_host="host", statsd_port=8125)
|
||||
assert hass.bus.listen.called
|
||||
|
||||
@mock.patch("homeassistant.components.datadog.statsd")
|
||||
@mock.patch("homeassistant.components.datadog.initialize")
|
||||
def test_logbook_entry(self, mock_connection, mock_client):
|
||||
"""Test event listener."""
|
||||
self.hass.bus.listen = mock.MagicMock()
|
||||
|
||||
assert setup_component(
|
||||
self.hass,
|
||||
async def test_logbook_entry(hass):
|
||||
"""Test event listener."""
|
||||
hass.bus.listen = mock.MagicMock()
|
||||
|
||||
with patch("homeassistant.components.datadog.initialize"), patch(
|
||||
"homeassistant.components.datadog.statsd"
|
||||
) as mock_statsd:
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
datadog.DOMAIN,
|
||||
{datadog.DOMAIN: {"host": "host", "rate": datadog.DEFAULT_RATE}},
|
||||
)
|
||||
|
||||
assert self.hass.bus.listen.called
|
||||
handler_method = self.hass.bus.listen.call_args_list[0][0][1]
|
||||
assert hass.bus.listen.called
|
||||
handler_method = hass.bus.listen.call_args_list[0][0][1]
|
||||
|
||||
event = {
|
||||
"domain": "automation",
|
||||
|
@ -102,23 +89,25 @@ class TestDatadog(unittest.TestCase):
|
|||
}
|
||||
handler_method(mock.MagicMock(data=event))
|
||||
|
||||
assert mock_client.event.call_count == 1
|
||||
assert mock_client.event.call_args == mock.call(
|
||||
assert mock_statsd.event.call_count == 1
|
||||
assert mock_statsd.event.call_args == mock.call(
|
||||
title="Home Assistant",
|
||||
text="%%% \n **{}** {} \n %%%".format(event["name"], event["message"]),
|
||||
tags=["entity:sensor.foo.bar", "domain:automation"],
|
||||
)
|
||||
|
||||
mock_client.event.reset_mock()
|
||||
mock_statsd.event.reset_mock()
|
||||
|
||||
@mock.patch("homeassistant.components.datadog.statsd")
|
||||
@mock.patch("homeassistant.components.datadog.initialize")
|
||||
def test_state_changed(self, mock_connection, mock_client):
|
||||
"""Test event listener."""
|
||||
self.hass.bus.listen = mock.MagicMock()
|
||||
|
||||
assert setup_component(
|
||||
self.hass,
|
||||
async def test_state_changed(hass):
|
||||
"""Test event listener."""
|
||||
hass.bus.listen = mock.MagicMock()
|
||||
|
||||
with patch("homeassistant.components.datadog.initialize"), patch(
|
||||
"homeassistant.components.datadog.statsd"
|
||||
) as mock_statsd:
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
datadog.DOMAIN,
|
||||
{
|
||||
datadog.DOMAIN: {
|
||||
|
@ -129,8 +118,8 @@ class TestDatadog(unittest.TestCase):
|
|||
},
|
||||
)
|
||||
|
||||
assert self.hass.bus.listen.called
|
||||
handler_method = self.hass.bus.listen.call_args_list[1][0][1]
|
||||
assert hass.bus.listen.called
|
||||
handler_method = hass.bus.listen.call_args_list[1][0][1]
|
||||
|
||||
valid = {"1": 1, "1.0": 1.0, STATE_ON: 1, STATE_OFF: 0}
|
||||
|
||||
|
@ -145,11 +134,11 @@ class TestDatadog(unittest.TestCase):
|
|||
)
|
||||
handler_method(mock.MagicMock(data={"new_state": state}))
|
||||
|
||||
assert mock_client.gauge.call_count == 5
|
||||
assert mock_statsd.gauge.call_count == 5
|
||||
|
||||
for attribute, value in attributes.items():
|
||||
value = int(value) if isinstance(value, bool) else value
|
||||
mock_client.gauge.assert_has_calls(
|
||||
mock_statsd.gauge.assert_has_calls(
|
||||
[
|
||||
mock.call(
|
||||
f"ha.sensor.{attribute}",
|
||||
|
@ -160,17 +149,17 @@ class TestDatadog(unittest.TestCase):
|
|||
]
|
||||
)
|
||||
|
||||
assert mock_client.gauge.call_args == mock.call(
|
||||
assert mock_statsd.gauge.call_args == mock.call(
|
||||
"ha.sensor",
|
||||
out,
|
||||
sample_rate=1,
|
||||
tags=[f"entity:{state.entity_id}"],
|
||||
)
|
||||
|
||||
mock_client.gauge.reset_mock()
|
||||
mock_statsd.gauge.reset_mock()
|
||||
|
||||
for invalid in ("foo", "", object):
|
||||
handler_method(
|
||||
mock.MagicMock(data={"new_state": ha.State("domain.test", invalid, {})})
|
||||
)
|
||||
assert not mock_client.gauge.called
|
||||
assert not mock_statsd.gauge.called
|
||||
|
|
Loading…
Add table
Reference in a new issue