Pytest tests (#17750)

* Convert core tests

* Convert component tests to use pytest assert

* Lint 🤷‍♂️

* Fix test

* Fix 3 typos in docs
This commit is contained in:
Paulus Schoutsen 2018-10-24 12:10:05 +02:00 committed by GitHub
parent 4222f7562b
commit 08fe7c3ece
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
223 changed files with 6747 additions and 7237 deletions

View file

@ -15,6 +15,7 @@ from homeassistant.const import STATE_UNKNOWN
from homeassistant.helpers.config_validation import template
from tests.common import get_test_home_assistant, assert_setup_component
import pytest
class TestRestSensorSetup(unittest.TestCase):
@ -36,7 +37,7 @@ class TestRestSensorSetup(unittest.TestCase):
def test_setup_missing_schema(self):
"""Test setup with resource missing schema."""
with self.assertRaises(MissingSchema):
with pytest.raises(MissingSchema):
rest.setup_platform(self.hass, {
'platform': 'rest',
'resource': 'localhost',
@ -67,20 +68,20 @@ class TestRestSensorSetup(unittest.TestCase):
"""Test setup with minimum configuration."""
mock_req.get('http://localhost', status_code=200)
with assert_setup_component(1, 'sensor'):
self.assertTrue(setup_component(self.hass, 'sensor', {
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'rest',
'resource': 'http://localhost'
}
}))
self.assertEqual(2, mock_req.call_count)
})
assert 2 == mock_req.call_count
@requests_mock.Mocker()
def test_setup_get(self, mock_req):
"""Test setup with valid configuration."""
mock_req.get('http://localhost', status_code=200)
with assert_setup_component(1, 'sensor'):
self.assertTrue(setup_component(self.hass, 'sensor', {
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'rest',
'resource': 'http://localhost',
@ -94,15 +95,15 @@ class TestRestSensorSetup(unittest.TestCase):
'password': 'my password',
'headers': {'Accept': 'application/json'}
}
}))
self.assertEqual(2, mock_req.call_count)
})
assert 2 == mock_req.call_count
@requests_mock.Mocker()
def test_setup_post(self, mock_req):
"""Test setup with valid configuration."""
mock_req.post('http://localhost', status_code=200)
with assert_setup_component(1, 'sensor'):
self.assertTrue(setup_component(self.hass, 'sensor', {
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'rest',
'resource': 'http://localhost',
@ -117,8 +118,8 @@ class TestRestSensorSetup(unittest.TestCase):
'password': 'my password',
'headers': {'Accept': 'application/json'}
}
}))
self.assertEqual(2, mock_req.call_count)
})
assert 2 == mock_req.call_count
class TestRestSensor(unittest.TestCase):
@ -153,30 +154,28 @@ class TestRestSensor(unittest.TestCase):
def test_name(self):
"""Test the name."""
self.assertEqual(self.name, self.sensor.name)
assert self.name == self.sensor.name
def test_unit_of_measurement(self):
"""Test the unit of measurement."""
self.assertEqual(
self.unit_of_measurement, self.sensor.unit_of_measurement)
assert self.unit_of_measurement == self.sensor.unit_of_measurement
def test_force_update(self):
"""Test the unit of measurement."""
self.assertEqual(
self.force_update, self.sensor.force_update)
assert self.force_update == self.sensor.force_update
def test_state(self):
"""Test the initial state."""
self.sensor.update()
self.assertEqual(self.initial_state, self.sensor.state)
assert self.initial_state == self.sensor.state
def test_update_when_value_is_none(self):
"""Test state gets updated to unknown when sensor returns no data."""
self.rest.update = Mock(
'rest.RestData.update', side_effect=self.update_side_effect(None))
self.sensor.update()
self.assertEqual(STATE_UNKNOWN, self.sensor.state)
self.assertFalse(self.sensor.available)
assert STATE_UNKNOWN == self.sensor.state
assert not self.sensor.available
def test_update_when_value_changed(self):
"""Test state gets updated when sensor returns a new status."""
@ -184,8 +183,8 @@ class TestRestSensor(unittest.TestCase):
side_effect=self.update_side_effect(
'{ "key": "updated_state" }'))
self.sensor.update()
self.assertEqual('updated_state', self.sensor.state)
self.assertTrue(self.sensor.available)
assert 'updated_state' == self.sensor.state
assert self.sensor.available
def test_update_with_no_template(self):
"""Test update when there is no value template."""
@ -196,8 +195,8 @@ class TestRestSensor(unittest.TestCase):
self.unit_of_measurement, None, [],
self.force_update)
self.sensor.update()
self.assertEqual('plain_state', self.sensor.state)
self.assertTrue(self.sensor.available)
assert 'plain_state' == self.sensor.state
assert self.sensor.available
def test_update_with_json_attrs(self):
"""Test attributes get extracted from a JSON result."""
@ -208,8 +207,8 @@ class TestRestSensor(unittest.TestCase):
self.unit_of_measurement, None, ['key'],
self.force_update)
self.sensor.update()
self.assertEqual('some_json_value',
self.sensor.device_state_attributes['key'])
assert 'some_json_value' == \
self.sensor.device_state_attributes['key']
@patch('homeassistant.components.sensor.rest._LOGGER')
def test_update_with_json_attrs_no_data(self, mock_logger):
@ -220,8 +219,8 @@ class TestRestSensor(unittest.TestCase):
self.unit_of_measurement, None, ['key'],
self.force_update)
self.sensor.update()
self.assertEqual({}, self.sensor.device_state_attributes)
self.assertTrue(mock_logger.warning.called)
assert {} == self.sensor.device_state_attributes
assert mock_logger.warning.called
@patch('homeassistant.components.sensor.rest._LOGGER')
def test_update_with_json_attrs_not_dict(self, mock_logger):
@ -233,8 +232,8 @@ class TestRestSensor(unittest.TestCase):
self.unit_of_measurement, None, ['key'],
self.force_update)
self.sensor.update()
self.assertEqual({}, self.sensor.device_state_attributes)
self.assertTrue(mock_logger.warning.called)
assert {} == self.sensor.device_state_attributes
assert mock_logger.warning.called
@patch('homeassistant.components.sensor.rest._LOGGER')
def test_update_with_json_attrs_bad_JSON(self, mock_logger):
@ -246,9 +245,9 @@ class TestRestSensor(unittest.TestCase):
self.unit_of_measurement, None, ['key'],
self.force_update)
self.sensor.update()
self.assertEqual({}, self.sensor.device_state_attributes)
self.assertTrue(mock_logger.warning.called)
self.assertTrue(mock_logger.debug.called)
assert {} == self.sensor.device_state_attributes
assert mock_logger.warning.called
assert mock_logger.debug.called
def test_update_with_json_attrs_and_template(self):
"""Test attributes get extracted from a JSON result."""
@ -261,10 +260,10 @@ class TestRestSensor(unittest.TestCase):
self.force_update)
self.sensor.update()
self.assertEqual('json_state_updated_value', self.sensor.state)
self.assertEqual('json_state_updated_value',
self.sensor.device_state_attributes['key'],
self.force_update)
assert 'json_state_updated_value' == self.sensor.state
assert 'json_state_updated_value' == \
self.sensor.device_state_attributes['key'], \
self.force_update
class TestRestData(unittest.TestCase):
@ -283,10 +282,10 @@ class TestRestData(unittest.TestCase):
"""Test update."""
mock_req.get('http://localhost', text='test data')
self.rest.update()
self.assertEqual('test data', self.rest.data)
assert 'test data' == self.rest.data
@patch('requests.Session', side_effect=RequestException)
def test_update_request_exception(self, mock_req):
"""Test update when a request exception occurs."""
self.rest.update()
self.assertEqual(None, self.rest.data)
assert self.rest.data is None