diff --git a/earthsnake/identity.py b/earthsnake/identity.py index 1f68112..a1cbef3 100644 --- a/earthsnake/identity.py +++ b/earthsnake/identity.py @@ -58,6 +58,17 @@ class Identity: def __repr__(self) -> str: return f'' + 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( + 'Don’t know how to compare {self.__class__.__name__} and {other.__class__.__name__}' + ) + @classmethod def from_address(cls, address: str) -> 'Identity': """Load an identity from an author address""" diff --git a/tests/test_identity.py b/tests/test_identity.py index c9efe90..3e51953 100644 --- a/tests/test_identity.py +++ b/tests/test_identity.py @@ -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 + + +@pytest.mark.id_key_seed(TEST_SEED) +@pytest.mark.id_name('test') +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