rewrite reddit tests to pytest tests

pull/41002/head
Jason Rebelo 2020-10-01 21:04:53 +02:00
parent b36b06cd51
commit 7eae35f69a
1 changed files with 26 additions and 39 deletions

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,46 +149,35 @@ class MockSubreddit:
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
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",
}
state = self.hass.states.get("sensor.reddit_news")
assert int(state.state) == MOCK_RESULTS_LENGTH
assert state.attributes[CONF_SORT_BY] == "hot"
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[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)
assert not hass.states.get("sensor.reddit_worldnews")