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,5 +1,4 @@
"""The tests for local file camera component."""
from tempfile import NamedTemporaryFile
import unittest
from unittest import mock
@ -26,45 +25,46 @@ class TestLocalCamera(unittest.TestCase):
def test_loading_file(self):
"""Test that it loads image from disk."""
test_string = 'hello'
self.hass.wsgi = mock.MagicMock()
with NamedTemporaryFile() as fptr:
fptr.write('hello'.encode('utf-8'))
fptr.flush()
with mock.patch('os.path.isfile', mock.Mock(return_value=True)), \
mock.patch('os.access', mock.Mock(return_value=True)):
assert setup_component(self.hass, 'camera', {
'camera': {
'name': 'config_test',
'platform': 'local_file',
'file_path': fptr.name,
'file_path': 'mock.file',
}})
image_view = self.hass.wsgi.mock_calls[0][1][0]
image_view = self.hass.wsgi.mock_calls[0][1][0]
m_open = mock.mock_open(read_data=test_string)
with mock.patch(
'homeassistant.components.camera.local_file.open',
m_open, create=True
):
builder = EnvironBuilder(method='GET')
Request = request_class() # pylint: disable=invalid-name
request = Request(builder.get_environ())
request.authenticated = True
resp = image_view.get(request, 'camera.config_test')
assert resp.status_code == 200, resp.response
assert resp.response[0].decode('utf-8') == 'hello'
assert resp.status_code == 200, resp.response
assert resp.response[0].decode('utf-8') == test_string
def test_file_not_readable(self):
"""Test local file will not setup when file is not readable."""
self.hass.wsgi = mock.MagicMock()
with NamedTemporaryFile() as fptr:
fptr.write('hello'.encode('utf-8'))
fptr.flush()
with mock.patch('os.path.isfile', mock.Mock(return_value=True)), \
mock.patch('os.access', return_value=False), \
assert_setup_component(0):
assert setup_component(self.hass, 'camera', {
'camera': {
'name': 'config_test',
'platform': 'local_file',
'file_path': 'mock.file',
}})
with mock.patch('os.access', return_value=False), \
assert_setup_component(0):
assert setup_component(self.hass, 'camera', {
'camera': {
'name': 'config_test',
'platform': 'local_file',
'file_path': fptr.name,
}})
assert [] == self.hass.states.all()
assert [] == self.hass.states.all()