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

Replacing tempfile with mock_open in tests (#3753)

- test_bootstrap.py
- test_config.py
- components/test_init.py
- components/test_panel_custom.py
- components/test_api.py
- components/notify/test_file.py
- components/notify/test_demo.py
- components/camera/test_local_file.py
- helpers/test_config_validation.py
- util/test_package.py
- util/test_yaml.py

No changes needed in:
- components/cover/test_command_line.py
- components/switch/test_command_line.py
- components/rollershutter/test_command_line.py
- components/test_shell_command.py
- components/notify/test_command_line.py

Misc changes in:
- components/mqtt/test_server.py

Also, removed some unused mock parameters in tests/components/mqtt/test_server.py.
This commit is contained in:
Rob Capellini
2016-10-17 23:16:36 -04:00
committed by Paulus Schoutsen
parent 7d67017de7
commit 272539105f
12 changed files with 403 additions and 335 deletions

View File

@@ -1,8 +1,7 @@
"""The tests for the notify file platform."""
import os
import unittest
import tempfile
from unittest.mock import patch
from unittest.mock import call, mock_open, patch
from homeassistant.bootstrap import setup_component
import homeassistant.components.notify as notify
@@ -34,13 +33,19 @@ class TestNotifyFile(unittest.TestCase):
},
}))
@patch('homeassistant.components.notify.file.os.stat')
@patch('homeassistant.util.dt.utcnow')
def test_notify_file(self, mock_utcnow):
def test_notify_file(self, mock_utcnow, mock_stat):
"""Test the notify file output."""
mock_utcnow.return_value = dt_util.as_utc(dt_util.now())
mock_stat.return_value.st_size = 0
with tempfile.TemporaryDirectory() as tempdirname:
filename = os.path.join(tempdirname, 'notify.txt')
m_open = mock_open()
with patch(
'homeassistant.components.notify.file.open',
m_open, create=True
):
filename = 'mock_file'
message = 'one, two, testing, testing'
self.assertTrue(setup_component(self.hass, notify.DOMAIN, {
'notify': {
@@ -58,5 +63,12 @@ class TestNotifyFile(unittest.TestCase):
self.hass.services.call('notify', 'test', {'message': message},
blocking=True)
result = open(filename).read()
self.assertEqual(result, "{}{}\n".format(title, message))
full_filename = os.path.join(self.hass.config.path(), filename)
self.assertEqual(m_open.call_count, 1)
self.assertEqual(m_open.call_args, call(full_filename, 'a'))
self.assertEqual(m_open.return_value.write.call_count, 2)
self.assertEqual(
m_open.return_value.write.call_args_list,
[call(title), call(message + '\n')]
)