|
1 |
| - |
2 | 1 | import json
|
3 | 2 | import unittest
|
4 | 3 | from contextlib import contextmanager
|
| 4 | +from functools import wraps |
5 | 5 | from io import BytesIO
|
6 | 6 | from unittest.mock import Mock, patch
|
7 | 7 |
|
| 8 | +import proxy |
8 | 9 | from proxy.server import Handler
|
9 | 10 |
|
| 11 | + |
10 | 12 | class UnittestHandler(Handler):
|
11 | 13 | """A testable version of Handler that doesn't auto-handle requests"""
|
12 | 14 |
|
@@ -34,6 +36,7 @@ def common_patches(func):
|
34 | 36 | 'uri': {}, 'start': 1000, 'clear': 0
|
35 | 37 | })
|
36 | 38 | @patch('proxy.server.proxystats_lock')
|
| 39 | + @wraps(func) |
37 | 40 | def wrapper(*args, **kwargs):
|
38 | 41 | return func(*args, **kwargs)
|
39 | 42 | return wrapper
|
@@ -78,6 +81,51 @@ def assert_json_response(self, expected_key, expected_value):
|
78 | 81 | self.assertIn(expected_key, result)
|
79 | 82 | self.assertEqual(result[expected_key], expected_value)
|
80 | 83 |
|
| 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 | + |
81 | 129 | class TestDoGetStatsEndpoints(BaseDoGetTest):
|
82 | 130 | """Test cases for stats-related endpoints"""
|
83 | 131 |
|
|
0 commit comments