From beafc78dea3ced4c6e2342b9fb4cb1f5cd16f9dc Mon Sep 17 00:00:00 2001 From: Derek Brooks Date: Wed, 18 Jan 2023 21:29:35 -0600 Subject: [PATCH] remove test helper. add brand tests. --- nuheat/thermostat.py | 9 +-------- setup.py | 1 + tests/test_nuheat.py | 13 +++++++++++++ tests/test_thermostat.py | 26 ++++++++++++++++++++------ 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/nuheat/thermostat.py b/nuheat/thermostat.py index 021913b..e21972d 100644 --- a/nuheat/thermostat.py +++ b/nuheat/thermostat.py @@ -40,16 +40,9 @@ def __repr__(self): self.target_celsius ) - @classmethod - def get_url(cls, api_url): - """ - A helper method to solve a circular dependency when testing - """ - return f"{api_url}/thermostat" - @property def _url(self): - return self.get_url(self._session._api_url) + return f"{self._session._api_url}/thermostat" @property def fahrenheit(self): diff --git a/setup.py b/setup.py index 0aed81d..84cff69 100644 --- a/setup.py +++ b/setup.py @@ -37,6 +37,7 @@ 'coveralls==3.3.1', 'coverage==6.5.0', 'mock==4.0.3', + 'parameterized==0.8.1', 'pytest==7.2.0', 'pytest-cov==4.0.0', 'responses==0.22.0', diff --git a/tests/test_nuheat.py b/tests/test_nuheat.py index 9912e6d..69f105a 100644 --- a/tests/test_nuheat.py +++ b/tests/test_nuheat.py @@ -2,6 +2,7 @@ import responses from mock import patch +from parameterized import parameterized from urllib.parse import urlencode from nuheat import NuHeat, NuHeatThermostat @@ -11,6 +12,18 @@ class TestNuHeat(NuTestCase): # pylint: disable=protected-access + @parameterized.expand([ + (None, "mynuheat.com"), + ("NUHEAT", "mynuheat.com"), + ("BAD-BRAND", "mynuheat.com"), + ("MAPEHEAT", "mymapeheat.com"), + ]) + def test_brands(self, brand, hostname): + api = NuHeat("test@example.com", "secure-password", session_id=None, brand=brand) + self.assertEqual(api._hostname, hostname) + self.assertEqual(api._api_url, f"https://{hostname}/api") + self.assertEqual(api._auth_url, f"https://{hostname}/api/authenticate/user") + def test_init_with_session(self): existing_session_id = "passed-session" api = NuHeat("test@example.com", "secure-password", existing_session_id) diff --git a/tests/test_thermostat.py b/tests/test_thermostat.py index 601c139..475f387 100644 --- a/tests/test_thermostat.py +++ b/tests/test_thermostat.py @@ -3,9 +3,10 @@ from datetime import datetime, timezone, timedelta from mock import patch +from parameterized import parameterized from urllib.parse import urlencode -from nuheat import NuHeat, NuHeatThermostat, config, util +from nuheat import NuHeat, NuHeatThermostat, config from . import NuTestCase, load_fixture @@ -21,6 +22,19 @@ def test_init(self, _): self.assertEqual(thermostat.serial_number, serial_number) self.assertEqual(thermostat._session, api) + @parameterized.expand([ + (None, "mynuheat.com"), + ("NUHEAT", "mynuheat.com"), + ("BAD-BRAND", "mynuheat.com"), + ("MAPEHEAT", "mymapeheat.com"), + ]) + @patch("nuheat.NuHeatThermostat.get_data") + def test_brand_urls(self, brand, hostname, _): + api = NuHeat(None, None, session_id=None, brand=brand) + serial_number = "serial-123" + thermostat = NuHeatThermostat(api, serial_number) + self.assertEqual(thermostat._url, f"https://{hostname}/api/thermostat") + @patch("nuheat.NuHeatThermostat.get_data") def test_repr_without_data(self, _): api = NuHeat(None, None) @@ -129,7 +143,7 @@ def test_get_data(self): responses.add( responses.GET, - NuHeatThermostat.get_url(api._api_url), + f"{api._api_url}/thermostat", status=200, body=json.dumps(response_data), content_type="application/json" @@ -180,7 +194,7 @@ def test_get_data_401(self): api = NuHeat(None, None, session_id=bad_session_id) responses.add( responses.GET, - NuHeatThermostat.get_url(api._api_url), + f"{api._api_url}/thermostat", status=200, body=json.dumps(response_data), content_type="application/json" @@ -401,7 +415,7 @@ def test_next_schedule_event(self): api = NuHeat(None, None, session_id="my-session") responses.add( responses.GET, - NuHeatThermostat.get_url(api._api_url), + f"{api._api_url}/thermostat", status=200, body=json.dumps(response_data), content_type="application/json" @@ -489,7 +503,7 @@ def test_set_target_temperature_temporary_hold_time(self, set_data): with patch("nuheat.thermostat.datetime", wraps=datetime) as mock_dt: responses.add( responses.GET, - NuHeatThermostat.get_url(api._api_url), + f"{api._api_url}/thermostat", status=200, body=json.dumps(response_data), content_type="application/json" @@ -515,7 +529,7 @@ def test_set_data(self, _): responses.add( responses.POST, - NuHeatThermostat.get_url(api._api_url), + f"{api._api_url}/thermostat", status=200, content_type="application/json" )