Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions tests/test_sdk.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
import unittest.mock as mock
from unittest.mock import patch
from datetime import datetime, timedelta, date
from dateutil.parser import parse
from pytz import timezone, UTC
Expand Down Expand Up @@ -47,13 +48,27 @@ def raise_for_status(self):

class TestWattTimeBase(unittest.TestCase):
def setUp(self):
self.base = WattTimeBase()
"""Create both single-threaded and multi-threaded instances."""
self.base = WattTimeBase(multithreaded=False, rate_limit=2)
self.base_mt = WattTimeBase(multithreaded=True, rate_limit=2)

def test_login_with_real_api(self):
self.base._login()
assert self.base.token is not None
assert self.base.token_valid_until > datetime.now()

@patch("time.sleep", return_value=None)
def test_apply_rate_limit(self, mock_sleep):
"""Test _apply_rate_limit (single-threaded) triggers sleep when rate limit is exceeded."""
# Set up a scenario with more requests than allowed:
# Our rate_limit is 2, so with 3 prior requests all within the past second, we expect a wait.
self.base._last_request_times = [0, 0.2, 0.3]
# Use a current timestamp such that all three are within the last 1 second.
ts = 0.5
self.base._apply_rate_limit(ts)
# Expect sleep to be called with: wait_time = 1.0 - (ts - earliest_timestamp) = 1.0 - (0.5 - 0) = 0.5 seconds.
mock_sleep.assert_called_with(0.5)

def test_parse_dates_with_string(self):
start = "2022-01-01"
end = "2022-01-31"
Expand Down Expand Up @@ -120,7 +135,7 @@ def test_parse_dates_with_datetime(self):
self.assertIsInstance(parsed_end, datetime)
self.assertEqual(parsed_end.tzinfo, UTC)

@mock.patch("requests.post", side_effect=mocked_register)
@mock.patch("watttime.requests.Session.post", side_effect=mocked_register)
def test_mock_register(self, mock_post):
resp = self.base.register(email=os.getenv("WATTTIME_EMAIL"))
self.assertEqual(len(mock_post.call_args_list), 1)
Expand All @@ -129,6 +144,7 @@ def test_mock_register(self, mock_post):
class TestWattTimeHistorical(unittest.TestCase):
def setUp(self):
self.historical = WattTimeHistorical()
self.historical_mt = WattTimeHistorical(multithreaded=True)

def test_get_historical_jsons_3_months(self):
start = "2022-01-01 00:00Z"
Expand All @@ -139,6 +155,15 @@ def test_get_historical_jsons_3_months(self):
self.assertGreaterEqual(len(jsons), 1)
self.assertIsInstance(jsons[0], dict)

def test_get_historical_jsons_3_months_multithreaded(self):
start = "2022-01-01 00:00Z"
end = "2022-12-31 00:00Z"
jsons = self.historical_mt.get_historical_jsons(start, end, REGION)

self.assertIsInstance(jsons, list)
self.assertGreaterEqual(len(jsons), 1)
self.assertIsInstance(jsons[0], dict)

def test_get_historical_jsons_1_week(self):
start = "2022-01-01 00:00Z"
end = "2022-01-07 00:00Z"
Expand Down
1 change: 0 additions & 1 deletion watttime/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
from watttime.api import *
from watttime.tcy import TCYCalculator
from watttime.util import RateLimitedRequesterMixin
Loading
Loading