Skip to content

Commit

Permalink
Add tests for reasoncode comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreF committed Jan 14, 2024
1 parent 2c73adb commit 1250e2b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/paho/mqtt/reasoncodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
*******************************************************************
"""

import functools

from .packettypes import PacketTypes


@functools.total_ordering
class ReasonCodes:
"""MQTT version 5.0 reason codes class.
Expand Down Expand Up @@ -173,11 +175,18 @@ def __eq__(self, other):
if isinstance(other, int):
return self.value == other
if isinstance(other, str):
return self.value == str(self)
return other == str(self)
if isinstance(other, ReasonCodes):
return self.value == other.value
return False

def __lt__(self, other):
if isinstance(other, int):
return self.value < other
if isinstance(other, ReasonCodes):
return self.value < other.value
return NotImplemented

def __str__(self):
return self.getName()

Expand Down
26 changes: 26 additions & 0 deletions tests/test_reasoncodes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from paho.mqtt.packettypes import PacketTypes
from paho.mqtt.reasoncodes import ReasonCodes


class TestReasonCode:
def test_equality(self):
rc_success = ReasonCodes(PacketTypes.CONNACK, "Success")
assert rc_success == 0
assert rc_success == "Success"
assert rc_success != "Protocol error"
assert rc_success == ReasonCodes(PacketTypes.CONNACK, "Success")

rc_protocol_error = ReasonCodes(PacketTypes.CONNACK, "Protocol error")
assert rc_protocol_error == 130
assert rc_protocol_error == "Protocol error"
assert rc_protocol_error != "Success"
assert rc_protocol_error == ReasonCodes(PacketTypes.CONNACK, "Protocol error")

def test_comparison(self):
rc_success = ReasonCodes(PacketTypes.CONNACK, "Success")
rc_protocol_error = ReasonCodes(PacketTypes.CONNACK, "Protocol error")

assert not rc_success > 0
assert rc_protocol_error > 0
assert not rc_success != 0
assert rc_protocol_error != 0

0 comments on commit 1250e2b

Please sign in to comment.