Skip to content

Commit

Permalink
Rewrite reddit tests to pytest style tests (#41006)
Browse files Browse the repository at this point in the history
* ✅ rewrite reddit tests to pytest tests

* ✅ add missing blocking
  • Loading branch information
igotinfected committed Oct 6, 2020
1 parent bcfa4ac commit 4d3802f
Showing 1 changed file with 27 additions and 39 deletions.
66 changes: 27 additions & 39 deletions tests/components/reddit/test_sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""The tests for the Reddit platform."""
import copy
import unittest

from homeassistant.components.reddit.sensor import (
ATTR_BODY,
Expand All @@ -22,10 +21,9 @@
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": {
Expand Down Expand Up @@ -151,46 +149,36 @@ def _return_data(self, limit):
return data["results"][:limit]


class TestRedditSetup(unittest.TestCase):
"""Test the Reddit platform."""
@patch("praw.Reddit", new=MockPraw)
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):
"""Initialize values for this testcase class."""
self.hass = get_test_home_assistant()
self.addCleanup(self.tear_down_cleanup)
state = hass.states.get("sensor.reddit_worldnews")
assert int(state.state) == MOCK_RESULTS_LENGTH

def tear_down_cleanup(self):
"""Stop everything that was started."""
self.hass.stop()
state = hass.states.get("sensor.reddit_news")
assert int(state.state) == MOCK_RESULTS_LENGTH

@patch("praw.Reddit", new=MockPraw)
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()
assert state.attributes[ATTR_SUBREDDIT] == "news"

state = self.hass.states.get("sensor.reddit_worldnews")
assert int(state.state) == MOCK_RESULTS_LENGTH

state = self.hass.states.get("sensor.reddit_news")
assert int(state.state) == MOCK_RESULTS_LENGTH

assert state.attributes[ATTR_SUBREDDIT] == "news"
assert state.attributes[ATTR_POSTS][0] == {
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",
}

assert state.attributes[ATTR_POSTS][0] == {
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",
}
assert state.attributes[CONF_SORT_BY] == "hot"

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")
@patch("praw.Reddit", new=MockPraw)
async def test_setup_with_invalid_config(hass):
"""Test the platform setup with invalid Reddit configuration."""
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")

0 comments on commit 4d3802f

Please sign in to comment.