Skip to content
Snippets Groups Projects
Commit 783237bd authored by Moul's avatar Moul
Browse files

[enh] #187: Implement __eq__ and __hash__ Membership’s methods

parent 1aca28b1
No related branches found
No related tags found
1 merge request!167#187: Implement __eq__ and __hash__ Documents methods
Pipeline #14052 waiting for manual action
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import re import re
from typing import Type, TypeVar from typing import Any, Type, TypeVar
from ..constants import ( from ..constants import (
BLOCK_ID_REGEX, BLOCK_ID_REGEX,
...@@ -105,6 +105,35 @@ class Membership(Document): ...@@ -105,6 +105,35 @@ class Membership(Document):
if signing_key is not None: if signing_key is not None:
self.sign(signing_key) self.sign(signing_key)
def __eq__(self, other: Any) -> bool:
"""
Check Membership instances equality
"""
if not isinstance(other, Membership):
return NotImplemented
return (
super().__eq__(other)
and self.issuer == other.issuer
and self.membership_block_id == other.membership_block_id
and self.uid == other.uid
and self.identity_block_id == other.identity_block_id
and self.membership_type == other.membership_type
)
def __hash__(self) -> int:
return hash(
(
self.issuer,
self.membership_block_id,
self.uid,
self.identity_block_id,
self.membership_type,
self.version,
self.currency,
self.signature,
)
)
@classmethod @classmethod
def from_inline( def from_inline(
cls: Type[MembershipType], cls: Type[MembershipType],
......
...@@ -15,6 +15,9 @@ ...@@ -15,6 +15,9 @@
import unittest import unittest
import pytest
from duniterpy.constants import G1_TEST_CURRENCY_CODENAME
from duniterpy.documents.membership import Membership from duniterpy.documents.membership import Membership
membership_inline = "HnFcSms8jzwngtVomTTnzudZx7SHUQY8sVE1y8yBmULk:\ membership_inline = "HnFcSms8jzwngtVomTTnzudZx7SHUQY8sVE1y8yBmULk:\
...@@ -104,3 +107,33 @@ class TestMembership(unittest.TestCase): ...@@ -104,3 +107,33 @@ class TestMembership(unittest.TestCase):
"dkaXIiCYUJtCg8Feh/BKvPYf4uFH9CJ/zY6J4MlA9BsjmcMe4YAblvNt/gJy31b1aGq3ue3h14mLMCu84rraDg==", "dkaXIiCYUJtCg8Feh/BKvPYf4uFH9CJ/zY6J4MlA9BsjmcMe4YAblvNt/gJy31b1aGq3ue3h14mLMCu84rraDg==",
) )
self.assertEqual(from_rendered_membership.membership_type, "IN") self.assertEqual(from_rendered_membership.membership_type, "IN")
ISSUER = "issuer"
MEMB_BLOCK_ID = "2-MEMB"
UID = "uid"
IDTY_BLOCK_ID = "0-IDTY"
MEMBERSHIP_KWARGS = [ISSUER, MEMB_BLOCK_ID, UID, IDTY_BLOCK_ID]
def test_membership_equality():
memb1 = Membership(*MEMBERSHIP_KWARGS)
memb2 = Membership(*MEMBERSHIP_KWARGS)
assert memb1 == memb2
@pytest.mark.parametrize(
"issuer, memb_block_id, uid, idty_block_id, currency",
[
(ISSUER, MEMB_BLOCK_ID, "test", IDTY_BLOCK_ID, None),
(ISSUER, MEMB_BLOCK_ID, UID, "1-TEST", None),
("test", MEMB_BLOCK_ID, UID, IDTY_BLOCK_ID, None),
MEMBERSHIP_KWARGS + [G1_TEST_CURRENCY_CODENAME],
],
)
def test_membership_inequality(issuer, memb_block_id, uid, idty_block_id, currency):
memb1 = Membership(*MEMBERSHIP_KWARGS)
memb2 = Membership(issuer, memb_block_id, uid, idty_block_id)
if currency:
memb2.currency = currency
assert not memb1 == memb2
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment