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 authored and GitHub committed 2018-10-24 12:10:05 +02:00
1 parent 4222f7562b
commit 08fe7c3ece
223 files changed
+6747 -7237

No files matched your search

@@ -299,27 +299,27 @@ class BaseMQTT(unittest.TestCase):
def assert_location_state(self, location):
"""Test the assertion of a location state."""
state = self.hass.states.get(DEVICE_TRACKER_STATE)
self.assertEqual(state.state, location)
assert state.state == location
def assert_location_latitude(self, latitude):
"""Test the assertion of a location latitude."""
state = self.hass.states.get(DEVICE_TRACKER_STATE)
self.assertEqual(state.attributes.get('latitude'), latitude)
assert state.attributes.get('latitude') == latitude
def assert_location_longitude(self, longitude):
"""Test the assertion of a location longitude."""
state = self.hass.states.get(DEVICE_TRACKER_STATE)
self.assertEqual(state.attributes.get('longitude'), longitude)
assert state.attributes.get('longitude') == longitude
def assert_location_accuracy(self, accuracy):
"""Test the assertion of a location accuracy."""
state = self.hass.states.get(DEVICE_TRACKER_STATE)
self.assertEqual(state.attributes.get('gps_accuracy'), accuracy)
assert state.attributes.get('gps_accuracy') == accuracy
def assert_location_source_type(self, source_type):
"""Test the assertion of source_type."""
state = self.hass.states.get(DEVICE_TRACKER_STATE)
self.assertEqual(state.attributes.get('source_type'), source_type)
assert state.attributes.get('source_type') == source_type
class TestDeviceTrackerOwnTracks(BaseMQTT):
@@ -382,19 +382,19 @@ class TestDeviceTrackerOwnTracks(BaseMQTT):
"""Test the assertion of a mobile beacon tracker state."""
dev_id = MOBILE_BEACON_FMT.format(beacon)
state = self.hass.states.get(dev_id)
self.assertEqual(state.state, location)
assert state.state == location
def assert_mobile_tracker_latitude(self, latitude, beacon=IBEACON_DEVICE):
"""Test the assertion of a mobile beacon tracker latitude."""
dev_id = MOBILE_BEACON_FMT.format(beacon)
state = self.hass.states.get(dev_id)
self.assertEqual(state.attributes.get('latitude'), latitude)
assert state.attributes.get('latitude') == latitude
def assert_mobile_tracker_accuracy(self, accuracy, beacon=IBEACON_DEVICE):
"""Test the assertion of a mobile beacon tracker accuracy."""
dev_id = MOBILE_BEACON_FMT.format(beacon)
state = self.hass.states.get(dev_id)
self.assertEqual(state.attributes.get('gps_accuracy'), accuracy)
assert state.attributes.get('gps_accuracy') == accuracy
def test_location_invalid_devid(self): # pylint: disable=invalid-name
"""Test the update of a location."""
@@ -460,7 +460,7 @@ class TestDeviceTrackerOwnTracks(BaseMQTT):
self.assert_location_state('outer')
# Left clean zone state
self.assertFalse(self.context.regions_entered[USER])
assert not self.context.regions_entered[USER]
self.send_message(LOCATION_TOPIC, LOCATION_MESSAGE)
@@ -480,7 +480,7 @@ class TestDeviceTrackerOwnTracks(BaseMQTT):
self.send_message(EVENT_TOPIC, message)
# Left clean zone state
self.assertFalse(self.context.regions_entered[USER])
assert not self.context.regions_entered[USER]
def test_event_gps_entry_inaccurate(self):
"""Test the event for inaccurate entry."""
@@ -511,7 +511,7 @@ class TestDeviceTrackerOwnTracks(BaseMQTT):
self.assert_location_state('inner')
# But does exit region correctly
self.assertFalse(self.context.regions_entered[USER])
assert not self.context.regions_entered[USER]
def test_event_gps_entry_exit_zero_accuracy(self):
"""Test entry/exit events with accuracy zero."""
@@ -530,7 +530,7 @@ class TestDeviceTrackerOwnTracks(BaseMQTT):
self.assert_location_state('inner')
# But does exit region correctly
self.assertFalse(self.context.regions_entered[USER])
assert not self.context.regions_entered[USER]
def test_event_gps_exit_outside_zone_sets_away(self):
"""Test the event for exit zone."""
@@ -730,7 +730,7 @@ class TestDeviceTrackerOwnTracks(BaseMQTT):
self.assert_location_state('inner')
# Left clean zone state
self.assertFalse(self.context.regions_entered[USER])
assert not self.context.regions_entered[USER]
# Now sending a location update moves me again.
self.send_message(LOCATION_TOPIC, LOCATION_MESSAGE)
@@ -749,7 +749,7 @@ class TestDeviceTrackerOwnTracks(BaseMQTT):
self.send_message(EVENT_TOPIC, message)
# Left clean zone state
self.assertFalse(self.context.regions_entered[USER])
assert not self.context.regions_entered[USER]
def test_event_region_entry_exit_right_order(self):
"""Test the event for ordering."""
@@ -959,8 +959,8 @@ class TestDeviceTrackerOwnTracks(BaseMQTT):
self.hass.block_till_done()
self.send_message(EVENT_TOPIC, MOBILE_BEACON_LEAVE_EVENT_MESSAGE)
self.assertEqual(len(self.context.mobile_beacons_active['greg_phone']),
0)
assert len(self.context.mobile_beacons_active['greg_phone']) == \
0
def test_mobile_multiple_enter_exit(self):
"""Test the multiple entering."""
@@ -968,8 +968,8 @@ class TestDeviceTrackerOwnTracks(BaseMQTT):
self.send_message(EVENT_TOPIC, MOBILE_BEACON_ENTER_EVENT_MESSAGE)
self.send_message(EVENT_TOPIC, MOBILE_BEACON_LEAVE_EVENT_MESSAGE)
self.assertEqual(len(self.context.mobile_beacons_active['greg_phone']),
0)
assert len(self.context.mobile_beacons_active['greg_phone']) == \
0
def test_complex_movement(self):
"""Test a complex sequence representative of real-world use."""
@@ -1168,9 +1168,9 @@ class TestDeviceTrackerOwnTracks(BaseMQTT):
self.send_message(WAYPOINTS_TOPIC, waypoints_message)
# Check if it made it into states
wayp = self.hass.states.get(WAYPOINT_ENTITY_NAMES[0])
self.assertTrue(wayp is not None)
assert wayp is not None
wayp = self.hass.states.get(WAYPOINT_ENTITY_NAMES[1])
self.assertTrue(wayp is not None)
assert wayp is not None
def test_waypoint_import_blacklist(self):
"""Test import of list of waypoints for blacklisted user."""
@@ -1178,9 +1178,9 @@ class TestDeviceTrackerOwnTracks(BaseMQTT):
self.send_message(WAYPOINTS_TOPIC_BLOCKED, waypoints_message)
# Check if it made it into states
wayp = self.hass.states.get(WAYPOINT_ENTITY_NAMES[2])
self.assertTrue(wayp is None)
assert wayp is None
wayp = self.hass.states.get(WAYPOINT_ENTITY_NAMES[3])
self.assertTrue(wayp is None)
assert wayp is None
def test_waypoint_import_no_whitelist(self):
"""Test import of list of waypoints with no whitelist set."""
@@ -1201,9 +1201,9 @@ class TestDeviceTrackerOwnTracks(BaseMQTT):
self.send_message(WAYPOINTS_TOPIC_BLOCKED, waypoints_message)
# Check if it made it into states
wayp = self.hass.states.get(WAYPOINT_ENTITY_NAMES[2])
self.assertTrue(wayp is not None)
assert wayp is not None
wayp = self.hass.states.get(WAYPOINT_ENTITY_NAMES[3])
self.assertTrue(wayp is not None)
assert wayp is not None
def test_waypoint_import_bad_json(self):
"""Test importing a bad JSON payload."""
@@ -1211,9 +1211,9 @@ class TestDeviceTrackerOwnTracks(BaseMQTT):
self.send_message(WAYPOINTS_TOPIC, waypoints_message, True)
# Check if it made it into states
wayp = self.hass.states.get(WAYPOINT_ENTITY_NAMES[2])
self.assertTrue(wayp is None)
assert wayp is None
wayp = self.hass.states.get(WAYPOINT_ENTITY_NAMES[3])
self.assertTrue(wayp is None)
assert wayp is None
def test_waypoint_import_existing(self):
"""Test importing a zone that exists."""
@@ -1225,14 +1225,14 @@ class TestDeviceTrackerOwnTracks(BaseMQTT):
waypoints_message = WAYPOINTS_UPDATED_MESSAGE.copy()
self.send_message(WAYPOINTS_TOPIC, waypoints_message)
new_wayp = self.hass.states.get(WAYPOINT_ENTITY_NAMES[0])
self.assertTrue(wayp == new_wayp)
assert wayp == new_wayp
def test_single_waypoint_import(self):
"""Test single waypoint message."""
waypoint_message = WAYPOINT_MESSAGE.copy()
self.send_message(WAYPOINT_TOPIC, waypoint_message)
wayp = self.hass.states.get(WAYPOINT_ENTITY_NAMES[0])
self.assertTrue(wayp is not None)
assert wayp is not None
def test_not_implemented_message(self):
"""Handle not implemented message type."""
@@ -1240,7 +1240,7 @@ class TestDeviceTrackerOwnTracks(BaseMQTT):
'owntracks.async_handle_not_impl_msg',
return_value=mock_coro(False))
patch_handler.start()
self.assertFalse(self.send_message(LWT_TOPIC, LWT_MESSAGE))
assert not self.send_message(LWT_TOPIC, LWT_MESSAGE)
patch_handler.stop()
def test_unsupported_message(self):
@@ -1249,7 +1249,7 @@ class TestDeviceTrackerOwnTracks(BaseMQTT):
'owntracks.async_handle_unsupported_msg',
return_value=mock_coro(False))
patch_handler.start()
self.assertFalse(self.send_message(BAD_TOPIC, BAD_MESSAGE))
assert not self.send_message(BAD_TOPIC, BAD_MESSAGE)
patch_handler.stop()
@@ -1465,7 +1465,7 @@ class TestDeviceTrackerOwnTrackConfigs(BaseMQTT):
'zone.inner', 'zoning', INNER_ZONE)
message = build_message({'desc': 'foo'}, REGION_GPS_ENTER_MESSAGE)
self.assertEqual(message['desc'], 'foo')
assert message['desc'] == 'foo'
self.send_message(EVENT_TOPIC, message)
self.assert_location_state('inner')