Rewrite file unittest tests to pytest style test functions (#41616)
parent
9c719beded
commit
250fbd857b
|
@ -1,91 +1,78 @@
|
||||||
"""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."""
|
"""Test set up the platform with bad/missing config."""
|
||||||
|
config = {notify.DOMAIN: {"name": "test", "platform": "file"}}
|
||||||
|
with assert_setup_component(0) as handle_config:
|
||||||
|
assert await async_setup_component(hass, notify.DOMAIN, config)
|
||||||
|
assert not handle_config[notify.DOMAIN]
|
||||||
|
|
||||||
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):
|
@pytest.mark.parametrize(
|
||||||
"""Stop down everything that was started."""
|
"timestamp",
|
||||||
self.hass.stop()
|
[
|
||||||
|
False,
|
||||||
|
True,
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_notify_file(hass, timestamp):
|
||||||
|
"""Test the notify file output."""
|
||||||
|
filename = "mock_file"
|
||||||
|
message = "one, two, testing, testing"
|
||||||
|
with assert_setup_component(1) as handle_config:
|
||||||
|
assert await async_setup_component(
|
||||||
|
hass,
|
||||||
|
notify.DOMAIN,
|
||||||
|
{
|
||||||
|
"notify": {
|
||||||
|
"name": "test",
|
||||||
|
"platform": "file",
|
||||||
|
"filename": filename,
|
||||||
|
"timestamp": timestamp,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
assert handle_config[notify.DOMAIN]
|
||||||
|
|
||||||
def test_bad_config(self):
|
m_open = mock_open()
|
||||||
"""Test set up the platform with bad/missing config."""
|
with patch("homeassistant.components.file.notify.open", m_open, create=True), patch(
|
||||||
config = {notify.DOMAIN: {"name": "test", "platform": "file"}}
|
"homeassistant.components.file.notify.os.stat"
|
||||||
with assert_setup_component(0) as handle_config:
|
) as mock_st, patch("homeassistant.util.dt.utcnow", return_value=dt_util.utcnow()):
|
||||||
assert setup_component(self.hass, notify.DOMAIN, config)
|
|
||||||
assert not handle_config[notify.DOMAIN]
|
|
||||||
|
|
||||||
def _test_notify_file(self, timestamp):
|
mock_st.return_value.st_size = 0
|
||||||
"""Test the notify file output."""
|
title = (
|
||||||
filename = "mock_file"
|
f"{ATTR_TITLE_DEFAULT} notifications "
|
||||||
message = "one, two, testing, testing"
|
f"(Log started: {dt_util.utcnow().isoformat()})\n{'-' * 80}\n"
|
||||||
with assert_setup_component(1) as handle_config:
|
)
|
||||||
assert setup_component(
|
|
||||||
self.hass,
|
|
||||||
notify.DOMAIN,
|
|
||||||
{
|
|
||||||
"notify": {
|
|
||||||
"name": "test",
|
|
||||||
"platform": "file",
|
|
||||||
"filename": filename,
|
|
||||||
"timestamp": timestamp,
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
assert handle_config[notify.DOMAIN]
|
|
||||||
|
|
||||||
m_open = mock_open()
|
await hass.services.async_call(
|
||||||
with patch(
|
"notify", "test", {"message": message}, blocking=True
|
||||||
"homeassistant.components.file.notify.open", m_open, create=True
|
)
|
||||||
), patch("homeassistant.components.file.notify.os.stat") as mock_st, patch(
|
|
||||||
"homeassistant.util.dt.utcnow", return_value=dt_util.utcnow()
|
|
||||||
):
|
|
||||||
|
|
||||||
mock_st.return_value.st_size = 0
|
full_filename = os.path.join(hass.config.path(), filename)
|
||||||
title = (
|
assert m_open.call_count == 1
|
||||||
f"{ATTR_TITLE_DEFAULT} notifications "
|
assert m_open.call_args == call(full_filename, "a")
|
||||||
f"(Log started: {dt_util.utcnow().isoformat()})\n{'-' * 80}\n"
|
|
||||||
)
|
|
||||||
|
|
||||||
self.hass.services.call(
|
assert m_open.return_value.write.call_count == 2
|
||||||
"notify", "test", {"message": message}, blocking=True
|
if not timestamp:
|
||||||
)
|
assert m_open.return_value.write.call_args_list == [
|
||||||
|
call(title),
|
||||||
full_filename = os.path.join(self.hass.config.path(), filename)
|
call(f"{message}\n"),
|
||||||
assert m_open.call_count == 1
|
]
|
||||||
assert m_open.call_args == call(full_filename, "a")
|
else:
|
||||||
|
assert m_open.return_value.write.call_args_list == [
|
||||||
assert m_open.return_value.write.call_count == 2
|
call(title),
|
||||||
if not timestamp:
|
call(f"{dt_util.utcnow().isoformat()} {message}\n"),
|
||||||
assert m_open.return_value.write.call_args_list == [
|
]
|
||||||
call(title),
|
|
||||||
call(f"{message}\n"),
|
|
||||||
]
|
|
||||||
else:
|
|
||||||
assert m_open.return_value.write.call_args_list == [
|
|
||||||
call(title),
|
|
||||||
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)
|
|
||||||
|
|
|
@ -1,85 +1,76 @@
|
||||||
"""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
|
||||||
|
def entity_reg(hass):
|
||||||
|
"""Return an empty, loaded, registry."""
|
||||||
|
return mock_registry(hass)
|
||||||
|
|
||||||
|
|
||||||
|
@patch("os.path.isfile", Mock(return_value=True))
|
||||||
|
@patch("os.access", Mock(return_value=True))
|
||||||
|
async def test_file_value(hass, entity_reg):
|
||||||
"""Test the File sensor."""
|
"""Test the File sensor."""
|
||||||
|
config = {
|
||||||
|
"sensor": {"platform": "file", "name": "file1", "file_path": "mock.file1"}
|
||||||
|
}
|
||||||
|
|
||||||
def setup_method(self, method):
|
m_open = mock_open(read_data="43\n45\n21")
|
||||||
"""Set up things to be run when tests are started."""
|
with patch(
|
||||||
self.hass = get_test_home_assistant()
|
"homeassistant.components.file.sensor.open", m_open, create=True
|
||||||
# Patch out 'is_allowed_path' as the mock files aren't allowed
|
), patch.object(hass.config, "is_allowed_path", return_value=True):
|
||||||
self.hass.config.is_allowed_path = Mock(return_value=True)
|
assert await async_setup_component(hass, "sensor", config)
|
||||||
mock_registry(self.hass)
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
def teardown_method(self, method):
|
state = hass.states.get("sensor.file1")
|
||||||
"""Stop everything that was started."""
|
assert state.state == "21"
|
||||||
self.hass.stop()
|
|
||||||
|
|
||||||
@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(self):
|
@patch("os.access", Mock(return_value=True))
|
||||||
"""Test the File sensor."""
|
async def test_file_value_template(hass, entity_reg):
|
||||||
config = {
|
"""Test the File sensor with JSON entries."""
|
||||||
"sensor": {"platform": "file", "name": "file1", "file_path": "mock.file1"}
|
config = {
|
||||||
|
"sensor": {
|
||||||
|
"platform": "file",
|
||||||
|
"name": "file2",
|
||||||
|
"file_path": "mock.file2",
|
||||||
|
"value_template": "{{ value_json.temperature }}",
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
m_open = MockOpen(read_data="43\n45\n21")
|
data = '{"temperature": 29, "humidity": 31}\n' '{"temperature": 26, "humidity": 36}'
|
||||||
with patch("homeassistant.components.file.sensor.open", m_open, create=True):
|
|
||||||
assert setup_component(self.hass, "sensor", config)
|
|
||||||
self.hass.block_till_done()
|
|
||||||
|
|
||||||
state = self.hass.states.get("sensor.file1")
|
m_open = mock_open(read_data=data)
|
||||||
assert state.state == "21"
|
with patch(
|
||||||
|
"homeassistant.components.file.sensor.open", m_open, create=True
|
||||||
|
), patch.object(hass.config, "is_allowed_path", return_value=True):
|
||||||
|
assert await async_setup_component(hass, "sensor", config)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
@patch("os.path.isfile", Mock(return_value=True))
|
state = hass.states.get("sensor.file2")
|
||||||
@patch("os.access", Mock(return_value=True))
|
assert state.state == "26"
|
||||||
def test_file_value_template(self):
|
|
||||||
"""Test the File sensor with JSON entries."""
|
|
||||||
config = {
|
|
||||||
"sensor": {
|
|
||||||
"platform": "file",
|
|
||||||
"name": "file2",
|
|
||||||
"file_path": "mock.file2",
|
|
||||||
"value_template": "{{ value_json.temperature }}",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
data = (
|
|
||||||
'{"temperature": 29, "humidity": 31}\n'
|
|
||||||
'{"temperature": 26, "humidity": 36}'
|
|
||||||
)
|
|
||||||
|
|
||||||
m_open = MockOpen(read_data=data)
|
@patch("os.path.isfile", Mock(return_value=True))
|
||||||
with patch("homeassistant.components.file.sensor.open", m_open, create=True):
|
@patch("os.access", Mock(return_value=True))
|
||||||
assert setup_component(self.hass, "sensor", config)
|
async def test_file_empty(hass, entity_reg):
|
||||||
self.hass.block_till_done()
|
"""Test the File sensor with an empty file."""
|
||||||
|
config = {"sensor": {"platform": "file", "name": "file3", "file_path": "mock.file"}}
|
||||||
|
|
||||||
state = self.hass.states.get("sensor.file2")
|
m_open = mock_open(read_data="")
|
||||||
assert state.state == "26"
|
with patch(
|
||||||
|
"homeassistant.components.file.sensor.open", m_open, create=True
|
||||||
|
), patch.object(hass.config, "is_allowed_path", return_value=True):
|
||||||
|
assert await async_setup_component(hass, "sensor", config)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
@patch("os.path.isfile", Mock(return_value=True))
|
state = hass.states.get("sensor.file3")
|
||||||
@patch("os.access", Mock(return_value=True))
|
assert state.state == STATE_UNKNOWN
|
||||||
def test_file_empty(self):
|
|
||||||
"""Test the File sensor with an empty file."""
|
|
||||||
config = {
|
|
||||||
"sensor": {"platform": "file", "name": "file3", "file_path": "mock.file"}
|
|
||||||
}
|
|
||||||
|
|
||||||
m_open = MockOpen(read_data="")
|
|
||||||
with patch("homeassistant.components.file.sensor.open", m_open, create=True):
|
|
||||||
assert setup_component(self.hass, "sensor", config)
|
|
||||||
self.hass.block_till_done()
|
|
||||||
|
|
||||||
state = self.hass.states.get("sensor.file3")
|
|
||||||
assert state.state == STATE_UNKNOWN
|
|
||||||
|
|
Loading…
Reference in New Issue