From 4d3802ff95d1781b2154c57237cc7b3e779366c5 Mon Sep 17 00:00:00 2001 From: Jason Rebelo Date: Tue, 6 Oct 2020 02:33:31 +0200 Subject: [PATCH] Rewrite reddit tests to pytest style tests (#41006) * :white_check_mark: rewrite reddit tests to pytest tests * :white_check_mark: add missing blocking --- tests/components/reddit/test_sensor.py | 66 +++++++++++--------------- 1 file changed, 27 insertions(+), 39 deletions(-) diff --git a/tests/components/reddit/test_sensor.py b/tests/components/reddit/test_sensor.py index faa3679f492dfa..c49abd4038a230 100644 --- a/tests/components/reddit/test_sensor.py +++ b/tests/components/reddit/test_sensor.py @@ -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 @@ 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,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")