Skip to content

Commit

Permalink
Enhance test coverage and error handling in test_helpers and test_rws…
Browse files Browse the repository at this point in the history
…ensors

- Added error handling tests for `pack_value` in `test_helpers.py` to validate behavior for invalid inputs.
- Updated `test_rwsensors.py` to include a factor for signed values in `NumberRWSensor`, improving test accuracy for negative value handling.
  • Loading branch information
maslyankov committed Jan 22, 2025
1 parent ad77e81 commit cc92fef
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/tests/sunsynk/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Test helpers."""

import struct

import pytest

from sunsynk.helpers import (
Expand Down Expand Up @@ -119,6 +121,12 @@ def test_pack_unpack() -> None:
assert unpack_value(pack_value(val, bits=16, signed=True), signed=True) == val
assert unpack_value(pack_value(val, bits=32, signed=True), signed=True) == val

# Test error cases
with pytest.raises(struct.error):
pack_value(-1, bits=16, signed=False)
with pytest.raises(ValueError):
pack_value(1, bits=8) # Invalid bit length


def test_patch_bitmask() -> None:
"""Test patch_bitmask function."""
Expand Down
1 change: 1 addition & 0 deletions src/tests/sunsynk/test_rwsensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def test_number_rw(state: InverterState) -> None:

# writing negative values (when allowed by min)
s.min = -10
s.factor = -1 # indicate signed values
assert s.value_to_reg(-1, state.get) == (65535,)

s = NumberRWSensor(1, "s2", factor=0.01)
Expand Down

0 comments on commit cc92fef

Please # to comment.