Rewrite imap_email_content tests to use pytest#41200
Merged
balloob merged 1 commit intohome-assistant:devfrom Oct 6, 2020
Merged
Rewrite imap_email_content tests to use pytest#41200balloob merged 1 commit intohome-assistant:devfrom
balloob merged 1 commit intohome-assistant:devfrom
Conversation
Contributor
|
Hi @Edward-Knight, It seems you haven't yet signed a CLA. Please do so here. Once you do that we will be able to review and accept this pull request. Thanks! |
Contributor
Author
|
Here is a smaller diff that ignores whitespace changes: diff --git a/tests/components/imap_email_content/test_sensor.py b/tests/components/imap_email_content/test_sensor.py
index 3a0c006d15..241dc4dc50 100644
--- a/tests/components/imap_email_content/test_sensor.py
+++ b/tests/components/imap_email_content/test_sensor.py
@@ -4,14 +4,11 @@ import datetime
import email
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
-import unittest
from homeassistant.components.imap_email_content import sensor as imap_email_content
-from homeassistant.helpers.event import track_state_change
+from homeassistant.helpers.event import async_track_state_change
from homeassistant.helpers.template import Template
-from tests.common import get_test_home_assistant
-
class FakeEMailReader:
"""A test class for sending test emails."""
@@ -31,15 +28,7 @@ class FakeEMailReader:
return self._messages.popleft()
-class EmailContentSensor(unittest.TestCase):
- """Test the IMAP email content sensor."""
-
- def setUp(self):
- """Set up things to be run when tests are started."""
- self.hass = get_test_home_assistant()
- self.addCleanup(self.hass.stop)
-
- def test_allowed_sender(self):
+async def test_allowed_sender(hass):
"""Test emails from allowed sender."""
test_message = email.message.Message()
test_message["From"] = "sender@test.com"
@@ -48,7 +37,7 @@ class EmailContentSensor(unittest.TestCase):
test_message.set_payload("Test Message")
sensor = imap_email_content.EmailContentSensor(
- self.hass,
+ hass,
FakeEMailReader(deque([test_message])),
"test_emails_sensor",
["sender@test.com"],
@@ -56,8 +45,8 @@ class EmailContentSensor(unittest.TestCase):
)
sensor.entity_id = "sensor.emailtest"
- sensor.schedule_update_ha_state(True)
- self.hass.block_till_done()
+ sensor.async_schedule_update_ha_state(True)
+ await hass.async_block_till_done()
assert "Test" == sensor.state
assert "Test Message" == sensor.device_state_attributes["body"]
assert "sender@test.com" == sensor.device_state_attributes["from"]
@@ -67,7 +56,8 @@ class EmailContentSensor(unittest.TestCase):
== sensor.device_state_attributes["date"]
)
- def test_multi_part_with_text(self):
+
+async def test_multi_part_with_text(hass):
"""Test multi part emails."""
msg = MIMEMultipart("alternative")
msg["Subject"] = "Link"
@@ -83,7 +73,7 @@ class EmailContentSensor(unittest.TestCase):
msg.attach(htmlPart)
sensor = imap_email_content.EmailContentSensor(
- self.hass,
+ hass,
FakeEMailReader(deque([msg])),
"test_emails_sensor",
["sender@test.com"],
@@ -91,12 +81,13 @@ class EmailContentSensor(unittest.TestCase):
)
sensor.entity_id = "sensor.emailtest"
- sensor.schedule_update_ha_state(True)
- self.hass.block_till_done()
+ sensor.async_schedule_update_ha_state(True)
+ await hass.async_block_till_done()
assert "Link" == sensor.state
assert "Test Message" == sensor.device_state_attributes["body"]
- def test_multi_part_only_html(self):
+
+async def test_multi_part_only_html(hass):
"""Test multi part emails with only HTML."""
msg = MIMEMultipart("alternative")
msg["Subject"] = "Link"
@@ -109,7 +100,7 @@ class EmailContentSensor(unittest.TestCase):
msg.attach(htmlPart)
sensor = imap_email_content.EmailContentSensor(
- self.hass,
+ hass,
FakeEMailReader(deque([msg])),
"test_emails_sensor",
["sender@test.com"],
@@ -117,15 +108,16 @@ class EmailContentSensor(unittest.TestCase):
)
sensor.entity_id = "sensor.emailtest"
- sensor.schedule_update_ha_state(True)
- self.hass.block_till_done()
+ sensor.async_schedule_update_ha_state(True)
+ await hass.async_block_till_done()
assert "Link" == sensor.state
assert (
"<html><head></head><body>Test Message</body></html>"
== sensor.device_state_attributes["body"]
)
- def test_multi_part_only_other_text(self):
+
+async def test_multi_part_only_other_text(hass):
"""Test multi part emails with only other text."""
msg = MIMEMultipart("alternative")
msg["Subject"] = "Link"
@@ -138,7 +130,7 @@ class EmailContentSensor(unittest.TestCase):
msg.attach(htmlPart)
sensor = imap_email_content.EmailContentSensor(
- self.hass,
+ hass,
FakeEMailReader(deque([msg])),
"test_emails_sensor",
["sender@test.com"],
@@ -146,12 +138,13 @@ class EmailContentSensor(unittest.TestCase):
)
sensor.entity_id = "sensor.emailtest"
- sensor.schedule_update_ha_state(True)
- self.hass.block_till_done()
+ sensor.async_schedule_update_ha_state(True)
+ await hass.async_block_till_done()
assert "Link" == sensor.state
assert "Test Message" == sensor.device_state_attributes["body"]
- def test_multiple_emails(self):
+
+async def test_multiple_emails(hass):
"""Test multiple emails."""
states = []
@@ -170,10 +163,10 @@ class EmailContentSensor(unittest.TestCase):
def state_changed_listener(entity_id, from_s, to_s):
states.append(to_s)
- track_state_change(self.hass, ["sensor.emailtest"], state_changed_listener)
+ async_track_state_change(hass, ["sensor.emailtest"], state_changed_listener)
sensor = imap_email_content.EmailContentSensor(
- self.hass,
+ hass,
FakeEMailReader(deque([test_message1, test_message2])),
"test_emails_sensor",
["sender@test.com"],
@@ -182,17 +175,18 @@ class EmailContentSensor(unittest.TestCase):
sensor.entity_id = "sensor.emailtest"
- sensor.schedule_update_ha_state(True)
- self.hass.block_till_done()
- sensor.schedule_update_ha_state(True)
- self.hass.block_till_done()
+ sensor.async_schedule_update_ha_state(True)
+ await hass.async_block_till_done()
+ sensor.async_schedule_update_ha_state(True)
+ await hass.async_block_till_done()
assert "Test" == states[0].state
assert "Test 2" == states[1].state
assert "Test Message 2" == sensor.device_state_attributes["body"]
- def test_sender_not_allowed(self):
+
+async def test_sender_not_allowed(hass):
"""Test not whitelisted emails."""
test_message = email.message.Message()
test_message["From"] = "sender@test.com"
@@ -201,7 +195,7 @@ class EmailContentSensor(unittest.TestCase):
test_message.set_payload("Test Message")
sensor = imap_email_content.EmailContentSensor(
- self.hass,
+ hass,
FakeEMailReader(deque([test_message])),
"test_emails_sensor",
["other@test.com"],
@@ -209,11 +203,12 @@ class EmailContentSensor(unittest.TestCase):
)
sensor.entity_id = "sensor.emailtest"
- sensor.schedule_update_ha_state(True)
- self.hass.block_till_done()
+ sensor.async_schedule_update_ha_state(True)
+ await hass.async_block_till_done()
assert sensor.state is None
- def test_template(self):
+
+async def test_template(hass):
"""Test value template."""
test_message = email.message.Message()
test_message["From"] = "sender@test.com"
@@ -222,16 +217,14 @@ class EmailContentSensor(unittest.TestCase):
test_message.set_payload("Test Message")
sensor = imap_email_content.EmailContentSensor(
- self.hass,
+ hass,
FakeEMailReader(deque([test_message])),
"test_emails_sensor",
["sender@test.com"],
- Template(
- "{{ subject }} from {{ from }} with message {{ body }}", self.hass
- ),
+ Template("{{ subject }} from {{ from }} with message {{ body }}", hass),
)
sensor.entity_id = "sensor.emailtest"
- sensor.schedule_update_ha_state(True)
- self.hass.block_till_done()
+ sensor.async_schedule_update_ha_state(True)
+ await hass.async_block_till_done()
assert "Test from sender@test.com with message Test Message" == sensor.state |
sycx2
approved these changes
Oct 6, 2020
Member
|
GitHub has the no-whitespace diff feature built-in https://github.com/home-assistant/core/pull/41200/files?w=1 (access via settings icon on "files changed" tab or by adding w=1 to url) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed change
Refactor tests for the imap_email_content component from unittest to pytest.
Type of change
Additional information
Checklist
black --fast homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all..coveragerc.The integration reached or maintains the following [Integration Quality Scale][quality-scale]:
To help with the load of incoming pull requests: