Skip to content

Conversation

@Nexarian
Copy link
Contributor

@Nexarian Nexarian commented Dec 6, 2025

Adding two more tests that validate simple use-cases for the /aggregates endpoint.

@codecov-commenter
Copy link

codecov-commenter commented Dec 6, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 27.24%. Comparing base (8f8c98e) to head (68bed80).
⚠️ Report is 2 commits behind head on main.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #241      +/-   ##
==========================================
- Coverage   27.26%   27.24%   -0.02%     
==========================================
  Files          35       35              
  Lines        5465     5468       +3     
  Branches      771      772       +1     
==========================================
  Hits         1490     1490              
- Misses       3898     3901       +3     
  Partials       77       77              
Flag Coverage Δ
py3.10 27.24% <ø> (-0.02%) ⬇️
py3.11 27.24% <ø> (-0.02%) ⬇️
py3.12 27.24% <ø> (-0.02%) ⬇️
py3.9 27.26% <ø> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Nexarian Nexarian force-pushed the add_aggregates_unit_tests branch 2 times, most recently from e6a9660 to f6d4438 Compare December 6, 2025 04:47
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds two unit tests for the /aggregates endpoint to validate basic functionality and negative solar power adjustment behavior.

  • Adds TestDoGetAggregatesEndpoints test class with two test cases
  • Tests basic /aggregates endpoint functionality and negative solar power handling
  • Imports HTTPStatus from the http module for clearer status code assertions

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 112 to 127
def test_aggregates_with_negative_solar_adjustment(self, proxystats_lock, mock_safe_call, mock_pw):
"""Test aggregates with negative solar power adjustment"""
self.handler.path = "/aggregates"
mock_pw.poll = Mock()
mock_safe_call.return_value = json.dumps({
"solar": {"instant_power": -500},
"load": {"instant_power": 2000}
})

self.handler.do_GET()

self.assertEqual(proxy.server.proxystats["gets"], 1)
proxystats_lock.__enter__.assert_called_once()
proxystats_lock.__exit__.assert_called_once()
result = self.get_written_json()
self.assertEqual(result["solar"]["instant_power"], 0)
Copy link

Copilot AI Dec 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test doesn't verify the load adjustment behavior. According to the server code (line 975 in server.py), when solar power is negative and neg_solar is False, the code attempts to shift energy from solar to load by subtracting solar["instant_power"] from load["instant_power"]. However, due to a bug in the server code (solar["instant_power"] is already set to 0 before the subtraction), the load value remains unchanged. This test should verify the load value to document the current behavior: self.assertEqual(result["load"]["instant_power"], 2000).

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, if this is truly a bug, we should fix it. I've done so, though I don't know if this is the intended behavior? What is "negative solar" supposed to mean?

Anyway, I've updated it to do what I think it's supposed to do and updated the unit test as well.

"""Test /aggregates endpoint"""
self.handler.path = "/aggregates"
mock_pw.poll = Mock()
mock_safe_call.return_value = {"solar": {"instant_power": 1000}}
Copy link

Copilot AI Dec 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mock return value is inconsistent with the second test. In the second test (line 116), mock_safe_call.return_value returns a JSON string, but here it returns a dictionary. While both are handled by the server code (lines 963-967 in server.py), the tests should be consistent. Consider using the same type for both tests, preferably a dictionary since that's what safe_endpoint_call typically returns when jsonformat=True (the default).

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair. Updated.

Comment on lines 92 to 106
def test_aggregates_endpoint(self, proxystats_lock, mock_safe_call, mock_pw):
"""Test /aggregates endpoint"""
self.handler.path = "/aggregates"
mock_pw.poll = Mock()
mock_safe_call.return_value = {"solar": {"instant_power": 1000}}

self.handler.do_GET()

mock_safe_call.assert_called_once_with(
"/aggregates", mock_pw.poll, "/api/meters/aggregates"
)
self.assertEqual(proxy.server.proxystats["gets"], 1)
proxystats_lock.__enter__.assert_called_once()
proxystats_lock.__exit__.assert_called_once()
self.handler.send_response.assert_called_with(HTTPStatus.OK)
Copy link

Copilot AI Dec 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test lacks assertions about the actual response content. While it verifies the mock was called correctly and stats were updated, it doesn't verify what data was written to the response. Consider adding an assertion like result = self.get_written_json() followed by self.assertEqual(result["solar"]["instant_power"], 1000) to verify the endpoint returns the expected data.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

@Nexarian Nexarian force-pushed the add_aggregates_unit_tests branch from f6d4438 to a60dd1f Compare December 9, 2025 02:18
@Nexarian
Copy link
Contributor Author

Nexarian commented Dec 9, 2025

@jasonacox Updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants