1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00
This commit is contained in:
Paulus Schoutsen
2019-07-31 12:25:30 -07:00
parent da05dfe708
commit 4de97abc3a
2676 changed files with 163166 additions and 140084 deletions

View File

@@ -4,55 +4,62 @@ import unittest
from unittest.mock import patch
from homeassistant.components.reddit.sensor import (
DOMAIN, ATTR_SUBREDDIT, ATTR_POSTS, CONF_SORT_BY,
ATTR_ID, ATTR_URL, ATTR_TITLE, ATTR_SCORE, ATTR_COMMENTS_NUMBER,
ATTR_CREATED, ATTR_BODY)
from homeassistant.const import (CONF_USERNAME, CONF_PASSWORD, CONF_MAXIMUM)
DOMAIN,
ATTR_SUBREDDIT,
ATTR_POSTS,
CONF_SORT_BY,
ATTR_ID,
ATTR_URL,
ATTR_TITLE,
ATTR_SCORE,
ATTR_COMMENTS_NUMBER,
ATTR_CREATED,
ATTR_BODY,
)
from homeassistant.const import CONF_USERNAME, CONF_PASSWORD, CONF_MAXIMUM
from homeassistant.setup import setup_component
from tests.common import (get_test_home_assistant,
MockDependency)
from tests.common import get_test_home_assistant, MockDependency
VALID_CONFIG = {
'sensor': {
'platform': DOMAIN,
'client_id': 'test_client_id',
'client_secret': 'test_client_secret',
CONF_USERNAME: 'test_username',
CONF_PASSWORD: 'test_password',
'subreddits': ['worldnews', 'news'],
"sensor": {
"platform": DOMAIN,
"client_id": "test_client_id",
"client_secret": "test_client_secret",
CONF_USERNAME: "test_username",
CONF_PASSWORD: "test_password",
"subreddits": ["worldnews", "news"],
}
}
VALID_LIMITED_CONFIG = {
'sensor': {
'platform': DOMAIN,
'client_id': 'test_client_id',
'client_secret': 'test_client_secret',
CONF_USERNAME: 'test_username',
CONF_PASSWORD: 'test_password',
'subreddits': ['worldnews', 'news'],
CONF_MAXIMUM: 1
"sensor": {
"platform": DOMAIN,
"client_id": "test_client_id",
"client_secret": "test_client_secret",
CONF_USERNAME: "test_username",
CONF_PASSWORD: "test_password",
"subreddits": ["worldnews", "news"],
CONF_MAXIMUM: 1,
}
}
INVALID_SORT_BY_CONFIG = {
'sensor': {
'platform': DOMAIN,
'client_id': 'test_client_id',
'client_secret': 'test_client_secret',
CONF_USERNAME: 'test_username',
CONF_PASSWORD: 'test_password',
'subreddits': ['worldnews', 'news'],
'sort_by': 'invalid_sort_by'
"sensor": {
"platform": DOMAIN,
"client_id": "test_client_id",
"client_secret": "test_client_secret",
CONF_USERNAME: "test_username",
CONF_PASSWORD: "test_password",
"subreddits": ["worldnews", "news"],
"sort_by": "invalid_sort_by",
}
}
class ObjectView():
class ObjectView:
"""Use dict properties as attributes."""
def __init__(self, d):
@@ -61,37 +68,46 @@ class ObjectView():
MOCK_RESULTS = {
'results': [
ObjectView({
'id': 0,
'url': 'http://example.com/1',
'title': 'example1',
'score': '1',
'num_comments': '1',
'created': '',
'selftext': 'example1 selftext'
}),
ObjectView({
'id': 1,
'url': 'http://example.com/2',
'title': 'example2',
'score': '2',
'num_comments': '2',
'created': '',
'selftext': 'example2 selftext'
})
"results": [
ObjectView(
{
"id": 0,
"url": "http://example.com/1",
"title": "example1",
"score": "1",
"num_comments": "1",
"created": "",
"selftext": "example1 selftext",
}
),
ObjectView(
{
"id": 1,
"url": "http://example.com/2",
"title": "example2",
"score": "2",
"num_comments": "2",
"created": "",
"selftext": "example2 selftext",
}
),
]
}
MOCK_RESULTS_LENGTH = len(MOCK_RESULTS['results'])
MOCK_RESULTS_LENGTH = len(MOCK_RESULTS["results"])
class MockPraw():
class MockPraw:
"""Mock class for tmdbsimple library."""
def __init__(self, client_id: str, client_secret:
str, username: str, password: str,
user_agent: str):
def __init__(
self,
client_id: str,
client_secret: str,
username: str,
password: str,
user_agent: str,
):
"""Add mock data for API return."""
self._data = MOCK_RESULTS
@@ -100,7 +116,7 @@ class MockPraw():
return MockSubreddit(subreddit, self._data)
class MockSubreddit():
class MockSubreddit:
"""Mock class for a subreddit instance."""
def __init__(self, subreddit: str, data):
@@ -127,7 +143,7 @@ class MockSubreddit():
def _return_data(self, limit):
"""Test method to return modified data."""
data = copy.deepcopy(self._data)
return data['results'][:limit]
return data["results"][:limit]
class TestRedditSetup(unittest.TestCase):
@@ -141,35 +157,35 @@ class TestRedditSetup(unittest.TestCase):
"""Stop everything that was started."""
self.hass.stop()
@MockDependency('praw')
@patch('praw.Reddit', new=MockPraw)
@MockDependency("praw")
@patch("praw.Reddit", new=MockPraw)
def test_setup_with_valid_config(self, mock_praw):
"""Test the platform setup with movie configuration."""
setup_component(self.hass, 'sensor', VALID_CONFIG)
setup_component(self.hass, "sensor", VALID_CONFIG)
state = self.hass.states.get('sensor.reddit_worldnews')
state = self.hass.states.get("sensor.reddit_worldnews")
assert int(state.state) == MOCK_RESULTS_LENGTH
state = self.hass.states.get('sensor.reddit_news')
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_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'
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"
@MockDependency('praw')
@patch('praw.Reddit', new=MockPraw)
@MockDependency("praw")
@patch("praw.Reddit", new=MockPraw)
def test_setup_with_invalid_config(self, mock_praw):
"""Test the platform setup with invalid movie configuration."""
setup_component(self.hass, 'sensor', INVALID_SORT_BY_CONFIG)
assert not self.hass.states.get('sensor.reddit_worldnews')
setup_component(self.hass, "sensor", INVALID_SORT_BY_CONFIG)
assert not self.hass.states.get("sensor.reddit_worldnews")