Rewrite reddit tests to pytest style tests (#41006)

*  rewrite reddit tests to pytest tests

*  add missing blocking
This commit is contained in:
Jason Rebelo 2020-10-06 02:33:31 +02:00 committed by GitHub
parent bcfa4ac959
commit 4d3802ff95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,5 @@
"""The tests for the Reddit platform."""
import copy
import unittest
from homeassistant.components.reddit.sensor import (
ATTR_BODY,
@ -22,10 +21,9 @@ from homeassistant.const import (
CONF_PASSWORD,
CONF_USERNAME,
)
from homeassistant.setup import setup_component
from homeassistant.setup import async_setup_component
from tests.async_mock import patch
from tests.common import get_test_home_assistant
VALID_CONFIG = {
"sensor": {
@ -151,28 +149,16 @@ class MockSubreddit:
return data["results"][:limit]
class TestRedditSetup(unittest.TestCase):
"""Test the Reddit platform."""
def setUp(self):
"""Initialize values for this testcase class."""
self.hass = get_test_home_assistant()
self.addCleanup(self.tear_down_cleanup)
def tear_down_cleanup(self):
"""Stop everything that was started."""
self.hass.stop()
@patch("praw.Reddit", new=MockPraw)
def test_setup_with_valid_config(self):
@patch("praw.Reddit", new=MockPraw)
async def test_setup_with_valid_config(hass):
"""Test the platform setup with Reddit configuration."""
setup_component(self.hass, "sensor", VALID_CONFIG)
self.hass.block_till_done()
assert await async_setup_component(hass, "sensor", VALID_CONFIG)
await hass.async_block_till_done()
state = self.hass.states.get("sensor.reddit_worldnews")
state = hass.states.get("sensor.reddit_worldnews")
assert int(state.state) == MOCK_RESULTS_LENGTH
state = self.hass.states.get("sensor.reddit_news")
state = hass.states.get("sensor.reddit_news")
assert int(state.state) == MOCK_RESULTS_LENGTH
assert state.attributes[ATTR_SUBREDDIT] == "news"
@ -189,8 +175,10 @@ class TestRedditSetup(unittest.TestCase):
assert state.attributes[CONF_SORT_BY] == "hot"
@patch("praw.Reddit", new=MockPraw)
def test_setup_with_invalid_config(self):
@patch("praw.Reddit", new=MockPraw)
async def test_setup_with_invalid_config(hass):
"""Test the platform setup with invalid Reddit configuration."""
setup_component(self.hass, "sensor", INVALID_SORT_BY_CONFIG)
assert not self.hass.states.get("sensor.reddit_worldnews")
assert await async_setup_component(hass, "sensor", INVALID_SORT_BY_CONFIG)
await hass.async_block_till_done()
assert not hass.states.get("sensor.reddit_worldnews")