Skip to content

Commit c306b04

Browse files
MaStrCopilot
andauthored
fix: Replace example.com URL and mock network in test_evcc (#291)
test_evcc.py used 'http://test.example.com/api/prices' as the test URL. get_prices() calls refresh_data() which always hits the network on the first call (next_update_ts == 0), even when store_raw_data() was called beforehand. This caused the pipeline to make a real HTTP request to an external domain. - Replace test URL with 'https://demo.evcc.io/api/tariff/grid' - Mock get_raw_data_from_provider in test_get_prices_with_target_resolution_60 so the test is fully self-contained and never reaches out to the network Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 700f192 commit c306b04

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

tests/batcontrol/dynamictariff/test_evcc.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class TestEvcc(unittest.TestCase):
1414
def setUp(self):
1515
"""Set up test fixtures"""
1616
self.timezone = pytz.timezone('Europe/Berlin')
17-
self.url = 'http://test.example.com/api/prices'
17+
self.url = 'https://demo.evcc.io/api/tariff/grid'
1818

1919
def test_hourly_price_to_15min_intervals(self):
2020
"""Test that hourly prices are mapped to 15-min interval indices"""
@@ -365,16 +365,20 @@ def test_get_prices_with_target_resolution_60(self):
365365
}
366366
evcc.store_raw_data(raw_data)
367367

368-
with patch('batcontrol.dynamictariff.evcc.datetime') as mock_datetime:
369-
mock_now = self.timezone.localize(datetime.datetime(2024, 6, 20, 10, 0, 0))
370-
mock_datetime.datetime.now.return_value = mock_now
371-
mock_datetime.datetime.fromisoformat = datetime.datetime.fromisoformat
368+
# get_prices() triggers refresh_data() which calls get_raw_data_from_provider()
369+
# on the first invocation (next_update_ts == 0). Mock it to return the same
370+
# pre-loaded data so no real network request is made.
371+
with patch.object(evcc, 'get_raw_data_from_provider', return_value=raw_data):
372+
with patch('batcontrol.dynamictariff.evcc.datetime') as mock_datetime:
373+
mock_now = self.timezone.localize(datetime.datetime(2024, 6, 20, 10, 0, 0))
374+
mock_datetime.datetime.now.return_value = mock_now
375+
mock_datetime.datetime.fromisoformat = datetime.datetime.fromisoformat
372376

373-
with patch('batcontrol.dynamictariff.baseclass.datetime') as mock_base_datetime:
374-
mock_base_datetime.datetime.now.return_value = mock_now
375-
mock_base_datetime.timezone = datetime.timezone
377+
with patch('batcontrol.dynamictariff.baseclass.datetime') as mock_base_datetime:
378+
mock_base_datetime.datetime.now.return_value = mock_now
379+
mock_base_datetime.timezone = datetime.timezone
376380

377-
prices = evcc.get_prices()
381+
prices = evcc.get_prices()
378382

379383
# When target_resolution=60, baseclass averages 15-min prices to hourly
380384
# Average of 0.20, 0.22, 0.24, 0.26 = 0.23

0 commit comments

Comments
 (0)