Skip to content

Rewrite imap_email_content tests to use pytest#41200

Merged
balloob merged 1 commit intohome-assistant:devfrom
Edward-Knight:imap_email_content-unittest-pytest-rewrite
Oct 6, 2020
Merged

Rewrite imap_email_content tests to use pytest#41200
balloob merged 1 commit intohome-assistant:devfrom
Edward-Knight:imap_email_content-unittest-pytest-rewrite

Conversation

@Edward-Knight
Copy link
Copy Markdown
Contributor

@Edward-Knight Edward-Knight commented Oct 4, 2020

Proposed change

Refactor tests for the imap_email_content component from unittest to pytest.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the [development checklist][dev-checklist]
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

  • Documentation added/updated for [www.home-assistant.io][docs-repository]

If the code communicates with devices, web services, or third-party tools:

  • The [manifest file][manifest-docs] has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following [Integration Quality Scale][quality-scale]:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

To help with the load of incoming pull requests:

@homeassistant
Copy link
Copy Markdown
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!

@Edward-Knight
Copy link
Copy Markdown
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

@Edward-Knight Edward-Knight marked this pull request as ready for review October 4, 2020 11:35
@balloob
Copy link
Copy Markdown
Member

balloob commented Oct 6, 2020

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)

Copy link
Copy Markdown
Member

@balloob balloob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! 🎉 🌮 🕺

@balloob balloob merged commit 8bcd6c1 into home-assistant:dev Oct 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rewrite imap_email_content unittest tests to pytest style test functions

4 participants