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

+66 -71
View File
@@ -36,10 +36,9 @@ class TestComponentsGroup(unittest.TestCase):
self.hass, 'person_and_light',
['light.Bowl', 'device_tracker.Paulus'])
self.assertEqual(
STATE_ON,
assert STATE_ON == \
self.hass.states.get(
group.ENTITY_ID_FORMAT.format('person_and_light')).state)
group.ENTITY_ID_FORMAT.format('person_and_light')).state
def test_setup_group_with_a_non_existing_state(self):
"""Try to set up a group with a non existing state."""
@@ -49,7 +48,7 @@ class TestComponentsGroup(unittest.TestCase):
self.hass, 'light_and_nothing',
['light.Bowl', 'non.existing'])
self.assertEqual(STATE_ON, grp.state)
assert STATE_ON == grp.state
def test_setup_group_with_non_groupable_states(self):
"""Test setup with groups which are not groupable."""
@@ -60,13 +59,13 @@ class TestComponentsGroup(unittest.TestCase):
self.hass, 'chromecasts',
['cast.living_room', 'cast.bedroom'])
self.assertEqual(STATE_UNKNOWN, grp.state)
assert STATE_UNKNOWN == grp.state
def test_setup_empty_group(self):
"""Try to set up an empty group."""
grp = group.Group.create_group(self.hass, 'nothing', [])
self.assertEqual(STATE_UNKNOWN, grp.state)
assert STATE_UNKNOWN == grp.state
def test_monitor_group(self):
"""Test if the group keeps track of states."""
@@ -76,11 +75,11 @@ class TestComponentsGroup(unittest.TestCase):
self.hass, 'init_group', ['light.Bowl', 'light.Ceiling'], False)
# Test if group setup in our init mode is ok
self.assertIn(test_group.entity_id, self.hass.states.entity_ids())
assert test_group.entity_id in self.hass.states.entity_ids()
group_state = self.hass.states.get(test_group.entity_id)
self.assertEqual(STATE_ON, group_state.state)
self.assertTrue(group_state.attributes.get(group.ATTR_AUTO))
assert STATE_ON == group_state.state
assert group_state.attributes.get(group.ATTR_AUTO)
def test_group_turns_off_if_all_off(self):
"""Test if turn off if the last device that was on turns off."""
@@ -92,7 +91,7 @@ class TestComponentsGroup(unittest.TestCase):
self.hass.block_till_done()
group_state = self.hass.states.get(test_group.entity_id)
self.assertEqual(STATE_OFF, group_state.state)
assert STATE_OFF == group_state.state
def test_group_turns_on_if_all_are_off_and_one_turns_on(self):
"""Test if turn on if all devices were turned off and one turns on."""
@@ -106,7 +105,7 @@ class TestComponentsGroup(unittest.TestCase):
self.hass.block_till_done()
group_state = self.hass.states.get(test_group.entity_id)
self.assertEqual(STATE_ON, group_state.state)
assert STATE_ON == group_state.state
def test_allgroup_stays_off_if_all_are_off_and_one_turns_on(self):
"""Group with all: true, stay off if one device turns on."""
@@ -121,7 +120,7 @@ class TestComponentsGroup(unittest.TestCase):
self.hass.block_till_done()
group_state = self.hass.states.get(test_group.entity_id)
self.assertEqual(STATE_OFF, group_state.state)
assert STATE_OFF == group_state.state
def test_allgroup_turn_on_if_last_turns_on(self):
"""Group with all: true, turn on if all devices are on."""
@@ -136,7 +135,7 @@ class TestComponentsGroup(unittest.TestCase):
self.hass.block_till_done()
group_state = self.hass.states.get(test_group.entity_id)
self.assertEqual(STATE_ON, group_state.state)
assert STATE_ON == group_state.state
def test_is_on(self):
"""Test is_on method."""
@@ -145,13 +144,13 @@ class TestComponentsGroup(unittest.TestCase):
test_group = group.Group.create_group(
self.hass, 'init_group', ['light.Bowl', 'light.Ceiling'], False)
self.assertTrue(group.is_on(self.hass, test_group.entity_id))
assert group.is_on(self.hass, test_group.entity_id)
self.hass.states.set('light.Bowl', STATE_OFF)
self.hass.block_till_done()
self.assertFalse(group.is_on(self.hass, test_group.entity_id))
assert not group.is_on(self.hass, test_group.entity_id)
# Try on non existing state
self.assertFalse(group.is_on(self.hass, 'non.existing'))
assert not group.is_on(self.hass, 'non.existing')
def test_expand_entity_ids(self):
"""Test expand_entity_ids method."""
@@ -160,9 +159,9 @@ class TestComponentsGroup(unittest.TestCase):
test_group = group.Group.create_group(
self.hass, 'init_group', ['light.Bowl', 'light.Ceiling'], False)
self.assertEqual(sorted(['light.ceiling', 'light.bowl']),
sorted(group.expand_entity_ids(
self.hass, [test_group.entity_id])))
assert sorted(['light.ceiling', 'light.bowl']) == \
sorted(group.expand_entity_ids(
self.hass, [test_group.entity_id]))
def test_expand_entity_ids_does_not_return_duplicates(self):
"""Test that expand_entity_ids does not return duplicates."""
@@ -171,15 +170,13 @@ class TestComponentsGroup(unittest.TestCase):
test_group = group.Group.create_group(
self.hass, 'init_group', ['light.Bowl', 'light.Ceiling'], False)
self.assertEqual(
['light.bowl', 'light.ceiling'],
assert ['light.bowl', 'light.ceiling'] == \
sorted(group.expand_entity_ids(
self.hass, [test_group.entity_id, 'light.Ceiling'])))
self.hass, [test_group.entity_id, 'light.Ceiling']))
self.assertEqual(
['light.bowl', 'light.ceiling'],
assert ['light.bowl', 'light.ceiling'] == \
sorted(group.expand_entity_ids(
self.hass, ['light.bowl', test_group.entity_id])))
self.hass, ['light.bowl', test_group.entity_id]))
def test_expand_entity_ids_recursive(self):
"""Test expand_entity_ids method with a group that contains itself."""
@@ -191,13 +188,13 @@ class TestComponentsGroup(unittest.TestCase):
['light.Bowl', 'light.Ceiling', 'group.init_group'],
False)
self.assertEqual(sorted(['light.ceiling', 'light.bowl']),
sorted(group.expand_entity_ids(
self.hass, [test_group.entity_id])))
assert sorted(['light.ceiling', 'light.bowl']) == \
sorted(group.expand_entity_ids(
self.hass, [test_group.entity_id]))
def test_expand_entity_ids_ignores_non_strings(self):
"""Test that non string elements in lists are ignored."""
self.assertEqual([], group.expand_entity_ids(self.hass, [5, True]))
assert [] == group.expand_entity_ids(self.hass, [5, True])
def test_get_entity_ids(self):
"""Test get_entity_ids method."""
@@ -206,9 +203,8 @@ class TestComponentsGroup(unittest.TestCase):
test_group = group.Group.create_group(
self.hass, 'init_group', ['light.Bowl', 'light.Ceiling'], False)
self.assertEqual(
['light.bowl', 'light.ceiling'],
sorted(group.get_entity_ids(self.hass, test_group.entity_id)))
assert ['light.bowl', 'light.ceiling'] == \
sorted(group.get_entity_ids(self.hass, test_group.entity_id))
def test_get_entity_ids_with_domain_filter(self):
"""Test if get_entity_ids works with a domain_filter."""
@@ -217,18 +213,17 @@ class TestComponentsGroup(unittest.TestCase):
mixed_group = group.Group.create_group(
self.hass, 'mixed_group', ['light.Bowl', 'switch.AC'], False)
self.assertEqual(
['switch.ac'],
assert ['switch.ac'] == \
group.get_entity_ids(
self.hass, mixed_group.entity_id, domain_filter="switch"))
self.hass, mixed_group.entity_id, domain_filter="switch")
def test_get_entity_ids_with_non_existing_group_name(self):
"""Test get_entity_ids with a non existing group."""
self.assertEqual([], group.get_entity_ids(self.hass, 'non_existing'))
assert [] == group.get_entity_ids(self.hass, 'non_existing')
def test_get_entity_ids_with_non_group_state(self):
"""Test get_entity_ids with a non group state."""
self.assertEqual([], group.get_entity_ids(self.hass, 'switch.AC'))
assert [] == group.get_entity_ids(self.hass, 'switch.AC')
def test_group_being_init_before_first_tracked_state_is_set_to_on(self):
"""Test if the groups turn on.
@@ -244,7 +239,7 @@ class TestComponentsGroup(unittest.TestCase):
self.hass.block_till_done()
group_state = self.hass.states.get(test_group.entity_id)
self.assertEqual(STATE_ON, group_state.state)
assert STATE_ON == group_state.state
def test_group_being_init_before_first_tracked_state_is_set_to_off(self):
"""Test if the group turns off.
@@ -260,7 +255,7 @@ class TestComponentsGroup(unittest.TestCase):
self.hass.block_till_done()
group_state = self.hass.states.get(test_group.entity_id)
self.assertEqual(STATE_OFF, group_state.state)
assert STATE_OFF == group_state.state
def test_setup(self):
"""Test setup method."""
@@ -283,36 +278,36 @@ class TestComponentsGroup(unittest.TestCase):
group_state = self.hass.states.get(
group.ENTITY_ID_FORMAT.format('second_group'))
self.assertEqual(STATE_ON, group_state.state)
self.assertEqual(set((test_group.entity_id, 'light.bowl')),
set(group_state.attributes['entity_id']))
self.assertIsNone(group_state.attributes.get(group.ATTR_AUTO))
self.assertEqual('mdi:work',
group_state.attributes.get(ATTR_ICON))
self.assertTrue(group_state.attributes.get(group.ATTR_VIEW))
self.assertEqual('hidden',
group_state.attributes.get(group.ATTR_CONTROL))
self.assertTrue(group_state.attributes.get(ATTR_HIDDEN))
self.assertEqual(1, group_state.attributes.get(group.ATTR_ORDER))
assert STATE_ON == group_state.state
assert set((test_group.entity_id, 'light.bowl')) == \
set(group_state.attributes['entity_id'])
assert group_state.attributes.get(group.ATTR_AUTO) is None
assert 'mdi:work' == \
group_state.attributes.get(ATTR_ICON)
assert group_state.attributes.get(group.ATTR_VIEW)
assert 'hidden' == \
group_state.attributes.get(group.ATTR_CONTROL)
assert group_state.attributes.get(ATTR_HIDDEN)
assert 1 == group_state.attributes.get(group.ATTR_ORDER)
group_state = self.hass.states.get(
group.ENTITY_ID_FORMAT.format('test_group'))
self.assertEqual(STATE_UNKNOWN, group_state.state)
self.assertEqual(set(('sensor.happy', 'hello.world')),
set(group_state.attributes['entity_id']))
self.assertIsNone(group_state.attributes.get(group.ATTR_AUTO))
self.assertIsNone(group_state.attributes.get(ATTR_ICON))
self.assertIsNone(group_state.attributes.get(group.ATTR_VIEW))
self.assertIsNone(group_state.attributes.get(group.ATTR_CONTROL))
self.assertIsNone(group_state.attributes.get(ATTR_HIDDEN))
self.assertEqual(2, group_state.attributes.get(group.ATTR_ORDER))
assert STATE_UNKNOWN == group_state.state
assert set(('sensor.happy', 'hello.world')) == \
set(group_state.attributes['entity_id'])
assert group_state.attributes.get(group.ATTR_AUTO) is None
assert group_state.attributes.get(ATTR_ICON) is None
assert group_state.attributes.get(group.ATTR_VIEW) is None
assert group_state.attributes.get(group.ATTR_CONTROL) is None
assert group_state.attributes.get(ATTR_HIDDEN) is None
assert 2 == group_state.attributes.get(group.ATTR_ORDER)
def test_groups_get_unique_names(self):
"""Two groups with same name should both have a unique entity id."""
grp1 = group.Group.create_group(self.hass, 'Je suis Charlie')
grp2 = group.Group.create_group(self.hass, 'Je suis Charlie')
self.assertNotEqual(grp1.entity_id, grp2.entity_id)
assert grp1.entity_id != grp2.entity_id
def test_expand_entity_ids_expands_nested_groups(self):
"""Test if entity ids epands to nested groups."""
@@ -323,10 +318,10 @@ class TestComponentsGroup(unittest.TestCase):
group.Group.create_group(
self.hass, 'group_of_groups', ['group.light', 'group.switch'])
self.assertEqual(
['light.test_1', 'light.test_2', 'switch.test_1', 'switch.test_2'],
assert ['light.test_1', 'light.test_2',
'switch.test_1', 'switch.test_2'] == \
sorted(group.expand_entity_ids(self.hass,
['group.group_of_groups'])))
['group.group_of_groups']))
def test_set_assumed_state_based_on_tracked(self):
"""Test assumed state."""
@@ -337,7 +332,7 @@ class TestComponentsGroup(unittest.TestCase):
['light.Bowl', 'light.Ceiling', 'sensor.no_exist'])
state = self.hass.states.get(test_group.entity_id)
self.assertFalse(state.attributes.get(ATTR_ASSUMED_STATE))
assert not state.attributes.get(ATTR_ASSUMED_STATE)
self.hass.states.set('light.Bowl', STATE_ON, {
ATTR_ASSUMED_STATE: True
@@ -345,13 +340,13 @@ class TestComponentsGroup(unittest.TestCase):
self.hass.block_till_done()
state = self.hass.states.get(test_group.entity_id)
self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE))
assert state.attributes.get(ATTR_ASSUMED_STATE)
self.hass.states.set('light.Bowl', STATE_ON)
self.hass.block_till_done()
state = self.hass.states.get(test_group.entity_id)
self.assertFalse(state.attributes.get(ATTR_ASSUMED_STATE))
assert not state.attributes.get(ATTR_ASSUMED_STATE)
def test_group_updated_after_device_tracker_zone_change(self):
"""Test group state when device tracker in group changes zone."""
@@ -363,9 +358,9 @@ class TestComponentsGroup(unittest.TestCase):
['device_tracker.Adam', 'device_tracker.Eve'])
self.hass.states.set('device_tracker.Adam', 'cool_state_not_home')
self.hass.block_till_done()
self.assertEqual(STATE_NOT_HOME,
self.hass.states.get(
group.ENTITY_ID_FORMAT.format('peeps')).state)
assert STATE_NOT_HOME == \
self.hass.states.get(
group.ENTITY_ID_FORMAT.format('peeps')).state
def test_reloading_groups(self):
"""Test reloading the group config."""
@@ -419,13 +414,13 @@ class TestComponentsGroup(unittest.TestCase):
common.set_visibility(self.hass, group_entity_id, False)
self.hass.block_till_done()
group_state = self.hass.states.get(group_entity_id)
self.assertTrue(group_state.attributes.get(ATTR_HIDDEN))
assert group_state.attributes.get(ATTR_HIDDEN)
# Show it again
common.set_visibility(self.hass, group_entity_id, True)
self.hass.block_till_done()
group_state = self.hass.states.get(group_entity_id)
self.assertIsNone(group_state.attributes.get(ATTR_HIDDEN))
assert group_state.attributes.get(ATTR_HIDDEN) is None
def test_modify_group(self):
"""Test modifying a group."""