Add an __eq__ method to Identity

Gergely Polonkai 1 year ago
parent 476e6ad22d
commit 1d40bbebc5
No known key found for this signature in database
GPG Key ID: 2D2885533B869ED4

@ -58,6 +58,17 @@ class Identity:
def __repr__(self) -> str:
return f'<Identity {self}>'
def __eq__(self, other: object) -> bool:
if isinstance(other, str):
return str(self) == other
if isinstance(other, Identity):
return str(self) == str(other)
raise TypeError(
'Dont know how to compare {self.__class__.__name__} and {other.__class__.__name__}'
def from_address(cls, address: str) -> 'Identity':
"""Load an identity from an author address"""

@ -168,3 +168,34 @@ def test_valid_address() -> None:
"""Test if valid_address passes on valid addresses"""
assert Identity.valid_address('@test.bcz76z52y5dlpohtkmpuj3jsdcvfmebzpcgfmtmhu4u7hlexzreya')
def test_eq_str() -> None:
"""Test if an Identity is considered equal to its string representation"""
identity_str = '@test.bcz76z52y5dlpohtkmpuj3jsdcvfmebzpcgfmtmhu4u7hlexzreya'
identity = Identity.from_address(identity_str)
assert identity == identity_str
assert identity != '@test.cz76z52y5dlpohtkmpuj3jsdcvfmebzpcgfmtmhu4u7hlexzreyb'
def test_eq_identity() -> None:
"""Test if two different identities are considered equal if their verifying key is equal"""
identity_str = '@test.bcz76z52y5dlpohtkmpuj3jsdcvfmebzpcgfmtmhu4u7hlexzreya'
identity1 = Identity.from_address(identity_str)
identity2 = Identity.from_address(identity_str)
identity3 = Identity.generate('some')
assert identity1 == identity2
assert identity1 != identity3
def test_eq_other(identity: Identity) -> None:
"""Test if Identity cannot be compared to something like an int"""
with pytest.raises(TypeError):
assert identity == 1