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:
1 parent
7d67017de7
commit
272539105f
12 files changed
+403
-335
No files matched your search
@@ -2,10 +2,9 @@
|
||||
# pylint: disable=protected-access,too-many-public-methods
|
||||
from contextlib import closing
|
||||
import json
|
||||
import tempfile
|
||||
import time
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import requests
|
||||
|
||||
@@ -244,15 +243,20 @@ class TestAPI(unittest.TestCase):
|
||||
|
||||
def test_api_get_error_log(self):
|
||||
"""Test the return of the error log."""
|
||||
test_content = 'Test String°'
|
||||
with tempfile.NamedTemporaryFile() as log:
|
||||
log.write(test_content.encode('utf-8'))
|
||||
log.flush()
|
||||
test_string = 'Test String°'.encode('UTF-8')
|
||||
|
||||
with patch.object(hass.config, 'path', return_value=log.name):
|
||||
req = requests.get(_url(const.URL_API_ERROR_LOG),
|
||||
headers=HA_HEADERS)
|
||||
self.assertEqual(test_content, req.text)
|
||||
# Can't use read_data with wsgiserver in Python 3.4.2. Due to a
|
||||
# bug in read_data, it can't handle byte types ('Type str doesn't
|
||||
# support the buffer API'), but wsgiserver requires byte types
|
||||
# ('WSGI Applications must yield bytes'). So just mock our own
|
||||
# read method.
|
||||
m_open = Mock(return_value=Mock(
|
||||
read=Mock(side_effect=[test_string]))
|
||||
)
|
||||
with patch('homeassistant.components.http.open', m_open, create=True):
|
||||
req = requests.get(_url(const.URL_API_ERROR_LOG),
|
||||
headers=HA_HEADERS)
|
||||
self.assertEqual(test_string, req.text.encode('UTF-8'))
|
||||
self.assertIsNone(req.headers.get('expires'))
|
||||
|
||||
def test_api_get_event_listeners(self):
|
||||
|
||||
Reference in new issue
Block a user