From 0d756ae14a0609a648057bb854ba5a408c6e3e78 Mon Sep 17 00:00:00 2001 From: Moul <moul@moul.re> Date: Wed, 10 Nov 2021 18:11:24 +0100 Subject: [PATCH] =?UTF-8?q?[enh]=20#187:=20Implement=20=5F=5Feq=5F=5F=20an?= =?UTF-8?q?d=20=5F=5Fhash=5F=5F=20Document=E2=80=99s=20methods?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use Document.__eq__() from Transaction.__eq__() --- duniterpy/documents/document.py | 21 +++++++++++++ duniterpy/documents/transaction.py | 3 +- tests/documents/test_document.py | 47 ++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 tests/documents/test_document.py diff --git a/duniterpy/documents/document.py b/duniterpy/documents/document.py index ea66d44e..72aeee75 100644 --- a/duniterpy/documents/document.py +++ b/duniterpy/documents/document.py @@ -64,6 +64,27 @@ class Document: self.currency = currency self.signature: Optional[str] = None + def __eq__(self, other: Any) -> bool: + """ + Check Document instances equality + """ + if not isinstance(other, Document): + return NotImplemented + return ( + self.version == other.version + and self.currency == other.currency + and self.signature == other.signature + ) + + def __hash__(self) -> int: + return hash( + ( + self.version, + self.currency, + self.signature, + ) + ) + @classmethod def parse_field(cls: Type[DocumentType], field_name: str, line: str) -> Any: """ diff --git a/duniterpy/documents/transaction.py b/duniterpy/documents/transaction.py index e61b9e34..d13de2b9 100644 --- a/duniterpy/documents/transaction.py +++ b/duniterpy/documents/transaction.py @@ -576,8 +576,7 @@ class Transaction(Document): if not isinstance(other, Transaction): return NotImplemented return ( - self.version == other.version - and self.currency == other.currency + super().__eq__(other) and self.signatures == other.signatures and self.block_id == other.block_id and self.locktime == other.locktime diff --git a/tests/documents/test_document.py b/tests/documents/test_document.py new file mode 100644 index 00000000..0043226f --- /dev/null +++ b/tests/documents/test_document.py @@ -0,0 +1,47 @@ +# Copyright 2014-2021 Vincent Texier <vit@free.fr> +# +# DuniterPy is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# DuniterPy is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +import pytest + +from duniterpy.constants import G1_CURRENCY_CODENAME, G1_TEST_CURRENCY_CODENAME +from duniterpy.documents.document import Document + +SIGNATURE = "sig" +DOC_REFERENCE = (10, G1_CURRENCY_CODENAME) + + +def test_document_equality(): + doc1 = Document(*DOC_REFERENCE) + doc1.signature = SIGNATURE + doc2 = Document(*DOC_REFERENCE) + doc2.signature = SIGNATURE + assert doc1 == doc2 + + +@pytest.mark.parametrize( + "doc_kwargs, signature", + [ + ((42, G1_CURRENCY_CODENAME), None), + ((10, G1_TEST_CURRENCY_CODENAME), None), + (DOC_REFERENCE, SIGNATURE), + ], +) +def test_document_inequality(doc_kwargs, signature): + doc1 = Document(*DOC_REFERENCE) + doc2 = Document(*doc_kwargs) + if signature: + doc1.signature = signature + doc2.signature = signature[::-1] + assert not doc1 == doc2 -- GitLab