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.""" """The tests for the Reddit platform."""
import copy import copy
import unittest
from homeassistant.components.reddit.sensor import ( from homeassistant.components.reddit.sensor import (
ATTR_BODY, ATTR_BODY,
@ -22,10 +21,9 @@ from homeassistant.const import (
CONF_PASSWORD, CONF_PASSWORD,
CONF_USERNAME, CONF_USERNAME,
) )
from homeassistant.setup import setup_component from homeassistant.setup import async_setup_component
from tests.async_mock import patch from tests.async_mock import patch
from tests.common import get_test_home_assistant
VALID_CONFIG = { VALID_CONFIG = {
"sensor": { "sensor": {
@ -151,46 +149,36 @@ class MockSubreddit:
return data["results"][:limit] return data["results"][:limit]
class TestRedditSetup(unittest.TestCase): @patch("praw.Reddit", new=MockPraw)
"""Test the Reddit platform.""" async def test_setup_with_valid_config(hass):
"""Test the platform setup with Reddit configuration."""
assert await async_setup_component(hass, "sensor", VALID_CONFIG)
await hass.async_block_till_done()
def setUp(self): state = hass.states.get("sensor.reddit_worldnews")
"""Initialize values for this testcase class.""" assert int(state.state) == MOCK_RESULTS_LENGTH
self.hass = get_test_home_assistant()
self.addCleanup(self.tear_down_cleanup)
def tear_down_cleanup(self): state = hass.states.get("sensor.reddit_news")
"""Stop everything that was started.""" assert int(state.state) == MOCK_RESULTS_LENGTH
self.hass.stop()
@patch("praw.Reddit", new=MockPraw) assert state.attributes[ATTR_SUBREDDIT] == "news"
def test_setup_with_valid_config(self):
"""Test the platform setup with Reddit configuration."""
setup_component(self.hass, "sensor", VALID_CONFIG)
self.hass.block_till_done()
state = self.hass.states.get("sensor.reddit_worldnews") assert state.attributes[ATTR_POSTS][0] == {
assert int(state.state) == MOCK_RESULTS_LENGTH ATTR_ID: 0,
ATTR_URL: "http://example.com/1",
ATTR_TITLE: "example1",
ATTR_SCORE: "1",
ATTR_COMMENTS_NUMBER: "1",
ATTR_CREATED: "",
ATTR_BODY: "example1 selftext",
}
state = self.hass.states.get("sensor.reddit_news") assert state.attributes[CONF_SORT_BY] == "hot"
assert int(state.state) == MOCK_RESULTS_LENGTH
assert state.attributes[ATTR_SUBREDDIT] == "news"
assert state.attributes[ATTR_POSTS][0] == { @patch("praw.Reddit", new=MockPraw)
ATTR_ID: 0, async def test_setup_with_invalid_config(hass):
ATTR_URL: "http://example.com/1", """Test the platform setup with invalid Reddit configuration."""
ATTR_TITLE: "example1", assert await async_setup_component(hass, "sensor", INVALID_SORT_BY_CONFIG)
ATTR_SCORE: "1", await hass.async_block_till_done()
ATTR_COMMENTS_NUMBER: "1", assert not hass.states.get("sensor.reddit_worldnews")
ATTR_CREATED: "",
ATTR_BODY: "example1 selftext",
}
assert state.attributes[CONF_SORT_BY] == "hot"
@patch("praw.Reddit", new=MockPraw)
def test_setup_with_invalid_config(self):
"""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")