Skip to content

Commit

Permalink
Fix parameter relational methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
denpamusic committed Jul 31, 2024
1 parent 58e20e6 commit 3fdfd30
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
15 changes: 12 additions & 3 deletions pyplumio/helpers/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,18 @@ def __repr__(self) -> str:

def _call_relational_method(self, method_to_call: str, other: Any) -> Any:
"""Call a specified relational method."""
handler = getattr(self.values.value, method_to_call)
other = other.value if isinstance(other, ParameterValues) else other
return handler(_normalize_parameter_value(other))
if isinstance(other, Parameter):
other = other.values

if isinstance(other, ParameterValues):
handler = getattr(self.values, method_to_call)
return handler(other)

if isinstance(other, (int, float, bool)) or other in (STATE_OFF, STATE_ON):
handler = getattr(self.values.value, method_to_call)
return handler(_normalize_parameter_value(other))
else:
return NotImplemented

def __int__(self) -> int:
"""Return an integer representation of parameter's value."""
Expand Down
14 changes: 13 additions & 1 deletion tests/helpers/test_parameter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Contains tests for the parameter helper class."""

from copy import copy
from unittest.mock import Mock, patch

import pytest
Expand Down Expand Up @@ -171,21 +172,32 @@ def test_switch_update(switch: Switch) -> None:

def test_number_relational(number: Number):
"""Test number relational methods."""
new_number = copy(number)
assert number == new_number
assert (number - 1) == 0
new_values = ParameterValues(value=1, min_value=0, max_value=5)
number.update(new_values)
assert number == new_values
assert (number + 1) == 2
assert (number * 5) == 5
assert (number / 1) == 1
assert (number // 1) == 1
assert number.__eq__("cola") is NotImplemented


def test_switch_relational(switch: Switch):
"""Test switch relational methods."""
new_switch = copy(switch)
assert switch == new_switch
new_values = ParameterValues(value=1, min_value=0, max_value=1)
assert (switch + 1) == 1
switch.update(ParameterValues(value=1, min_value=0, max_value=0))
switch.update(new_values)
assert switch == new_values
assert (switch - 1) == 0
assert (switch * 0) == 0
assert (switch / 1) == 1
assert (switch // 1) == 1
assert switch.__eq__("cola") is NotImplemented


def test_number_compare(number: Number) -> None:
Expand Down

0 comments on commit 3fdfd30

Please sign in to comment.