Rewrite file unittest tests to pytest style test functions (#41616)

pull/41945/head
CurrentThread 2020-10-16 13:30:03 +02:00 committed by GitHub
parent 9c719beded
commit 250fbd857b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 119 additions and 141 deletions

View File

@ -1,42 +1,39 @@
"""The tests for the notify file platform.""" """The tests for the notify file platform."""
import os import os
import unittest
import pytest
import homeassistant.components.notify as notify import homeassistant.components.notify as notify
from homeassistant.components.notify import ATTR_TITLE_DEFAULT from homeassistant.components.notify import ATTR_TITLE_DEFAULT
from homeassistant.setup import setup_component from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from tests.async_mock import call, mock_open, patch from tests.async_mock import call, mock_open, patch
from tests.common import assert_setup_component, get_test_home_assistant from tests.common import assert_setup_component
class TestNotifyFile(unittest.TestCase): async def test_bad_config(hass):
"""Test the file notify."""
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 down everything that was started."""
self.hass.stop()
def test_bad_config(self):
"""Test set up the platform with bad/missing config.""" """Test set up the platform with bad/missing config."""
config = {notify.DOMAIN: {"name": "test", "platform": "file"}} config = {notify.DOMAIN: {"name": "test", "platform": "file"}}
with assert_setup_component(0) as handle_config: with assert_setup_component(0) as handle_config:
assert setup_component(self.hass, notify.DOMAIN, config) assert await async_setup_component(hass, notify.DOMAIN, config)
assert not handle_config[notify.DOMAIN] assert not handle_config[notify.DOMAIN]
def _test_notify_file(self, timestamp):
@pytest.mark.parametrize(
"timestamp",
[
False,
True,
],
)
async def test_notify_file(hass, timestamp):
"""Test the notify file output.""" """Test the notify file output."""
filename = "mock_file" filename = "mock_file"
message = "one, two, testing, testing" message = "one, two, testing, testing"
with assert_setup_component(1) as handle_config: with assert_setup_component(1) as handle_config:
assert setup_component( assert await async_setup_component(
self.hass, hass,
notify.DOMAIN, notify.DOMAIN,
{ {
"notify": { "notify": {
@ -50,11 +47,9 @@ class TestNotifyFile(unittest.TestCase):
assert handle_config[notify.DOMAIN] assert handle_config[notify.DOMAIN]
m_open = mock_open() m_open = mock_open()
with patch( with patch("homeassistant.components.file.notify.open", m_open, create=True), patch(
"homeassistant.components.file.notify.open", m_open, create=True "homeassistant.components.file.notify.os.stat"
), patch("homeassistant.components.file.notify.os.stat") as mock_st, patch( ) as mock_st, patch("homeassistant.util.dt.utcnow", return_value=dt_util.utcnow()):
"homeassistant.util.dt.utcnow", return_value=dt_util.utcnow()
):
mock_st.return_value.st_size = 0 mock_st.return_value.st_size = 0
title = ( title = (
@ -62,11 +57,11 @@ class TestNotifyFile(unittest.TestCase):
f"(Log started: {dt_util.utcnow().isoformat()})\n{'-' * 80}\n" f"(Log started: {dt_util.utcnow().isoformat()})\n{'-' * 80}\n"
) )
self.hass.services.call( await hass.services.async_call(
"notify", "test", {"message": message}, blocking=True "notify", "test", {"message": message}, blocking=True
) )
full_filename = os.path.join(self.hass.config.path(), filename) full_filename = os.path.join(hass.config.path(), filename)
assert m_open.call_count == 1 assert m_open.call_count == 1
assert m_open.call_args == call(full_filename, "a") assert m_open.call_args == call(full_filename, "a")
@ -81,11 +76,3 @@ class TestNotifyFile(unittest.TestCase):
call(title), call(title),
call(f"{dt_util.utcnow().isoformat()} {message}\n"), call(f"{dt_util.utcnow().isoformat()} {message}\n"),
] ]
def test_notify_file(self):
"""Test the notify file output without timestamp."""
self._test_notify_file(False)
def test_notify_file_timestamp(self):
"""Test the notify file output with timestamp."""
self._test_notify_file(True)

View File

@ -1,50 +1,41 @@
"""The tests for local file sensor platform.""" """The tests for local file sensor platform."""
import unittest import pytest
# Using third party package because of a bug reading binary data in Python 3.4
# https://bugs.python.org/issue23004
from mock_open import MockOpen
from homeassistant.const import STATE_UNKNOWN from homeassistant.const import STATE_UNKNOWN
from homeassistant.setup import setup_component from homeassistant.setup import async_setup_component
from tests.async_mock import Mock, patch from tests.async_mock import Mock, mock_open, patch
from tests.common import get_test_home_assistant, mock_registry from tests.common import mock_registry
class TestFileSensor(unittest.TestCase): @pytest.fixture
"""Test the File sensor.""" def entity_reg(hass):
"""Return an empty, loaded, registry."""
return mock_registry(hass)
def setup_method(self, method):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
# Patch out 'is_allowed_path' as the mock files aren't allowed
self.hass.config.is_allowed_path = Mock(return_value=True)
mock_registry(self.hass)
def teardown_method(self, method): @patch("os.path.isfile", Mock(return_value=True))
"""Stop everything that was started.""" @patch("os.access", Mock(return_value=True))
self.hass.stop() async def test_file_value(hass, entity_reg):
@patch("os.path.isfile", Mock(return_value=True))
@patch("os.access", Mock(return_value=True))
def test_file_value(self):
"""Test the File sensor.""" """Test the File sensor."""
config = { config = {
"sensor": {"platform": "file", "name": "file1", "file_path": "mock.file1"} "sensor": {"platform": "file", "name": "file1", "file_path": "mock.file1"}
} }
m_open = MockOpen(read_data="43\n45\n21") m_open = mock_open(read_data="43\n45\n21")
with patch("homeassistant.components.file.sensor.open", m_open, create=True): with patch(
assert setup_component(self.hass, "sensor", config) "homeassistant.components.file.sensor.open", m_open, create=True
self.hass.block_till_done() ), patch.object(hass.config, "is_allowed_path", return_value=True):
assert await async_setup_component(hass, "sensor", config)
await hass.async_block_till_done()
state = self.hass.states.get("sensor.file1") state = hass.states.get("sensor.file1")
assert state.state == "21" assert state.state == "21"
@patch("os.path.isfile", Mock(return_value=True))
@patch("os.access", Mock(return_value=True)) @patch("os.path.isfile", Mock(return_value=True))
def test_file_value_template(self): @patch("os.access", Mock(return_value=True))
async def test_file_value_template(hass, entity_reg):
"""Test the File sensor with JSON entries.""" """Test the File sensor with JSON entries."""
config = { config = {
"sensor": { "sensor": {
@ -55,31 +46,31 @@ class TestFileSensor(unittest.TestCase):
} }
} }
data = ( data = '{"temperature": 29, "humidity": 31}\n' '{"temperature": 26, "humidity": 36}'
'{"temperature": 29, "humidity": 31}\n'
'{"temperature": 26, "humidity": 36}'
)
m_open = MockOpen(read_data=data) m_open = mock_open(read_data=data)
with patch("homeassistant.components.file.sensor.open", m_open, create=True): with patch(
assert setup_component(self.hass, "sensor", config) "homeassistant.components.file.sensor.open", m_open, create=True
self.hass.block_till_done() ), patch.object(hass.config, "is_allowed_path", return_value=True):
assert await async_setup_component(hass, "sensor", config)
await hass.async_block_till_done()
state = self.hass.states.get("sensor.file2") state = hass.states.get("sensor.file2")
assert state.state == "26" assert state.state == "26"
@patch("os.path.isfile", Mock(return_value=True))
@patch("os.access", Mock(return_value=True)) @patch("os.path.isfile", Mock(return_value=True))
def test_file_empty(self): @patch("os.access", Mock(return_value=True))
async def test_file_empty(hass, entity_reg):
"""Test the File sensor with an empty file.""" """Test the File sensor with an empty file."""
config = { config = {"sensor": {"platform": "file", "name": "file3", "file_path": "mock.file"}}
"sensor": {"platform": "file", "name": "file3", "file_path": "mock.file"}
}
m_open = MockOpen(read_data="") m_open = mock_open(read_data="")
with patch("homeassistant.components.file.sensor.open", m_open, create=True): with patch(
assert setup_component(self.hass, "sensor", config) "homeassistant.components.file.sensor.open", m_open, create=True
self.hass.block_till_done() ), patch.object(hass.config, "is_allowed_path", return_value=True):
assert await async_setup_component(hass, "sensor", config)
await hass.async_block_till_done()
state = self.hass.states.get("sensor.file3") state = hass.states.get("sensor.file3")
assert state.state == STATE_UNKNOWN assert state.state == STATE_UNKNOWN