Skip to content

Commit

Permalink
Entity ID TLV eq custom method and AbstractTlvBase __repr__
Browse files Browse the repository at this point in the history
  • Loading branch information
robamu committed Sep 9, 2024
1 parent 9b9f629 commit 8bd305f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

# [unreleased]

# [v0.24.2] 2024-09-09

## Fixed

- Custom `EntityIdTlv` `__eq__` implementation which only compares the numerical value
of the entity ID TLVs

## Added

- `AbstractTlvBase` `__repr__` implementation

# [v0.24.1] 2024-04-23

## Reverted
Expand Down
7 changes: 6 additions & 1 deletion spacepackets/cfdp/tlv/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ def tlv_type(self) -> TlvType:
def value(self) -> bytes:
pass

def __eq__(self, other: AbstractTlvBase):
def __repr__(self) -> str:
return f"Tlv(tlv_type={self.tlv_type!r}, value=0x[{self.value.hex(sep=',')}])"

def __eq__(self, other: object):
if not isinstance(other, AbstractTlvBase):
return False
return self.tlv_type == other.tlv_type and self.value == other.value

def check_type(self, tlv_type: TlvType):
Expand Down
1 change: 1 addition & 0 deletions spacepackets/cfdp/tlv/tlv.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ def _set_fields(cls, instance: FileStoreResponseTlv, data: bytes):
class EntityIdTlv(AbstractTlvBase):
"""This helper class has a :py:meth:`__eq__` implementation which only compares the numerical value
of the entity IDs"""

TLV_TYPE = TlvType.ENTITY_ID

def __init__(self, entity_id: bytes):
Expand Down
11 changes: 11 additions & 0 deletions tests/cfdp/tlvslvs/test_entity_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,14 @@ def test_invalid_type(self):
entity_id_tlv_tlv.tlv_type = TlvType.FILESTORE_REQUEST
with self.assertRaises(TlvTypeMissmatch):
EntityIdTlv.from_tlv(cfdp_tlv=entity_id_tlv_tlv)

def test_custom_eq(self):
self.entity_id_tlv = EntityIdTlv(entity_id=bytes([0x00, 0x01]))
self.entity_id_tlv_same = EntityIdTlv(entity_id=bytes([0x01]))
self.assertEqual(self.entity_id_tlv, self.entity_id_tlv_same)

def test_repr(self):
repr_str = repr(self.entity_id_tlv)
self.assertEqual(
repr_str, f"Tlv(tlv_type={TlvType.ENTITY_ID!r}, value=0x[00,01,02,03])"
)

0 comments on commit 8bd305f

Please sign in to comment.