Rewrite remember_the_milk tests to pytest style tests (#41002)

*  rewrite remember_the_milk tests to pytest tests

*  rewrite reddit tests to pytest tests

* Revert " rewrite reddit tests to pytest tests"

This reverts commit 7eae35f69ad61b29ddbbf2054c0464a8d90d9b97.
Forgot to switch to a new branch 🙈
This commit is contained in:
Jason Rebelo 2020-10-01 21:55:04 +02:00 committed by GitHub
parent 4b225a87c5
commit cad2304968
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 70 deletions

View File

@ -0,0 +1,14 @@
"""Constants for remember_the_milk tests."""
import json
PROFILE = "myprofile"
TOKEN = "mytoken"
JSON_STRING = json.dumps(
{
"myprofile": {
"token": "mytoken",
"id_map": {"1234": {"list_id": "0", "timeseries_id": "1", "task_id": "2"}},
}
}
)

View File

@ -1,89 +1,66 @@
"""Tests for the Remember The Milk component."""
import json
import logging
import unittest
import homeassistant.components.remember_the_milk as rtm
from .const import JSON_STRING, PROFILE, TOKEN
from tests.async_mock import Mock, mock_open, patch
from tests.common import get_test_home_assistant
_LOGGER = logging.getLogger(__name__)
class TestConfiguration(unittest.TestCase):
"""Basic tests for the class RememberTheMilkConfiguration."""
def test_create_new(hass):
"""Test creating a new config file."""
with patch("builtins.open", mock_open()), patch(
"os.path.isfile", Mock(return_value=False)
), patch.object(rtm.RememberTheMilkConfiguration, "save_config"):
config = rtm.RememberTheMilkConfiguration(hass)
config.set_token(PROFILE, TOKEN)
assert config.get_token(PROFILE) == TOKEN
def setUp(self):
"""Set up test Home Assistant main loop."""
self.hass = get_test_home_assistant()
self.profile = "myprofile"
self.token = "mytoken"
self.json_string = json.dumps(
{
"myprofile": {
"token": "mytoken",
"id_map": {
"1234": {"list_id": "0", "timeseries_id": "1", "task_id": "2"}
},
}
}
)
self.addCleanup(self.tear_down_cleanup)
def tear_down_cleanup(self):
"""Exit Home Assistant."""
self.hass.stop()
def test_load_config(hass):
"""Test loading an existing token from the file."""
with patch("builtins.open", mock_open(read_data=JSON_STRING)), patch(
"os.path.isfile", Mock(return_value=True)
):
config = rtm.RememberTheMilkConfiguration(hass)
assert config.get_token(PROFILE) == TOKEN
def test_create_new(self):
"""Test creating a new config file."""
with patch("builtins.open", mock_open()), patch(
"os.path.isfile", Mock(return_value=False)
), patch.object(rtm.RememberTheMilkConfiguration, "save_config"):
config = rtm.RememberTheMilkConfiguration(self.hass)
config.set_token(self.profile, self.token)
assert config.get_token(self.profile) == self.token
def test_load_config(self):
"""Test loading an existing token from the file."""
with patch("builtins.open", mock_open(read_data=self.json_string)), patch(
"os.path.isfile", Mock(return_value=True)
):
config = rtm.RememberTheMilkConfiguration(self.hass)
assert config.get_token(self.profile) == self.token
def test_invalid_data(hass):
"""Test starts with invalid data and should not raise an exception."""
with patch("builtins.open", mock_open(read_data="random characters")), patch(
"os.path.isfile", Mock(return_value=True)
):
config = rtm.RememberTheMilkConfiguration(hass)
assert config is not None
def test_invalid_data(self):
"""Test starts with invalid data and should not raise an exception."""
with patch("builtins.open", mock_open(read_data="random characters")), patch(
"os.path.isfile", Mock(return_value=True)
):
config = rtm.RememberTheMilkConfiguration(self.hass)
assert config is not None
def test_id_map(self):
"""Test the hass to rtm task is mapping."""
hass_id = "hass-id-1234"
list_id = "mylist"
timeseries_id = "my_timeseries"
rtm_id = "rtm-id-4567"
with patch("builtins.open", mock_open()), patch(
"os.path.isfile", Mock(return_value=False)
), patch.object(rtm.RememberTheMilkConfiguration, "save_config"):
config = rtm.RememberTheMilkConfiguration(self.hass)
def test_id_map(hass):
"""Test the hass to rtm task is mapping."""
hass_id = "hass-id-1234"
list_id = "mylist"
timeseries_id = "my_timeseries"
rtm_id = "rtm-id-4567"
with patch("builtins.open", mock_open()), patch(
"os.path.isfile", Mock(return_value=False)
), patch.object(rtm.RememberTheMilkConfiguration, "save_config"):
config = rtm.RememberTheMilkConfiguration(hass)
assert config.get_rtm_id(self.profile, hass_id) is None
config.set_rtm_id(self.profile, hass_id, list_id, timeseries_id, rtm_id)
assert (list_id, timeseries_id, rtm_id) == config.get_rtm_id(
self.profile, hass_id
)
config.delete_rtm_id(self.profile, hass_id)
assert config.get_rtm_id(self.profile, hass_id) is None
assert config.get_rtm_id(PROFILE, hass_id) is None
config.set_rtm_id(PROFILE, hass_id, list_id, timeseries_id, rtm_id)
assert (list_id, timeseries_id, rtm_id) == config.get_rtm_id(PROFILE, hass_id)
config.delete_rtm_id(PROFILE, hass_id)
assert config.get_rtm_id(PROFILE, hass_id) is None
def test_load_key_map(self):
"""Test loading an existing key map from the file."""
with patch("builtins.open", mock_open(read_data=self.json_string)), patch(
"os.path.isfile", Mock(return_value=True)
):
config = rtm.RememberTheMilkConfiguration(self.hass)
assert ("0", "1", "2") == config.get_rtm_id(self.profile, "1234")
def test_load_key_map(hass):
"""Test loading an existing key map from the file."""
with patch("builtins.open", mock_open(read_data=JSON_STRING)), patch(
"os.path.isfile", Mock(return_value=True)
):
config = rtm.RememberTheMilkConfiguration(hass)
assert ("0", "1", "2") == config.get_rtm_id(PROFILE, "1234")