Skip to content

Commit

Permalink
remove test helper. add brand tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
broox committed Jan 19, 2023
1 parent 831f7d1 commit beafc78
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
9 changes: 1 addition & 8 deletions nuheat/thermostat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
13 changes: 13 additions & 0 deletions tests/test_nuheat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import responses

from mock import patch
from parameterized import parameterized
from urllib.parse import urlencode

from nuheat import NuHeat, NuHeatThermostat
Expand All @@ -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("[email protected]", "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("[email protected]", "secure-password", existing_session_id)
Expand Down
26 changes: 20 additions & 6 deletions tests/test_thermostat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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)
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
)
Expand Down

0 comments on commit beafc78

Please sign in to comment.