Skip to content

Commit 23882ed

Browse files
committed
Adding more get tests
- Also update simulator
1 parent 5e50e0f commit 23882ed

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
strategy:
2121
fail-fast: false
2222
matrix:
23-
python-version: ["3.9", "3.10", "3.11", "3.12"]
23+
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
2424

2525
steps:
2626
- uses: "actions/checkout@v4"

proxy/tests/test_do_get.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
21
import json
32
import unittest
43
from contextlib import contextmanager
4+
from functools import wraps
55
from io import BytesIO
66
from unittest.mock import Mock, patch
77

8+
import proxy
89
from proxy.server import Handler
910

11+
1012
class UnittestHandler(Handler):
1113
"""A testable version of Handler that doesn't auto-handle requests"""
1214

@@ -34,6 +36,7 @@ def common_patches(func):
3436
'uri': {}, 'start': 1000, 'clear': 0
3537
})
3638
@patch('proxy.server.proxystats_lock')
39+
@wraps(func)
3740
def wrapper(*args, **kwargs):
3841
return func(*args, **kwargs)
3942
return wrapper
@@ -78,6 +81,51 @@ def assert_json_response(self, expected_key, expected_value):
7881
self.assertIn(expected_key, result)
7982
self.assertEqual(result[expected_key], expected_value)
8083

84+
85+
class TestDoGetAggregatesEndpoints(BaseDoGetTest):
86+
"""Test cases for aggregates-related endpoints"""
87+
88+
@common_patches
89+
@patch('proxy.server.safe_endpoint_call')
90+
@patch('proxy.server.neg_solar', False)
91+
@patch('proxy.server.pw')
92+
def test_aggregates_endpoint(self, mock_pw, mock_safe_call, proxystats_lock):
93+
"""Test /aggregates endpoint"""
94+
self.handler.path = "/aggregates"
95+
mock_pw.poll = Mock()
96+
mock_safe_call.return_value = {"solar": {"instant_power": 1000}}
97+
98+
self.handler.do_GET()
99+
100+
mock_safe_call.assert_called_once_with(
101+
"/aggregates", mock_pw.poll, "/api/meters/aggregates"
102+
)
103+
self.assertEqual(proxy.server.proxystats["gets"], 1)
104+
proxystats_lock.__enter__.assert_called_once()
105+
proxystats_lock.__exit__.assert_called_once()
106+
self.handler.send_response.assert_called_with(200)
107+
108+
@common_patches
109+
@patch('proxy.server.safe_endpoint_call')
110+
@patch('proxy.server.neg_solar', False)
111+
@patch('proxy.server.pw')
112+
def test_aggregates_with_negative_solar_adjustment(self, mock_pw, mock_safe_call, proxystats_lock):
113+
"""Test aggregates with negative solar power adjustment"""
114+
self.handler.path = "/aggregates"
115+
mock_pw.poll = Mock()
116+
mock_safe_call.return_value = json.dumps({
117+
"solar": {"instant_power": -500},
118+
"load": {"instant_power": 2000}
119+
})
120+
121+
self.handler.do_GET()
122+
123+
self.assertEqual(proxy.server.proxystats["gets"], 1)
124+
proxystats_lock.__enter__.assert_called_once()
125+
proxystats_lock.__exit__.assert_called_once()
126+
result = self.get_written_json()
127+
self.assertEqual(result["solar"]["instant_power"], 0)
128+
81129
class TestDoGetStatsEndpoints(BaseDoGetTest):
82130
"""Test cases for stats-related endpoints"""
83131

0 commit comments

Comments
 (0)