diff --git a/duniterpy/documents/document.py b/duniterpy/documents/document.py index ea66d44ef140ba924fce93ca6168824dd2d83cc5..72aeee7550a49325db54253bbc754615e218c37f 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 e61b9e345eeb57a5cfe1d4f9785a1ed704efee8b..d13de2b9874ca521f43bb2a70fd09b7f37e898a5 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 0000000000000000000000000000000000000000..0043226f4bf16b05f56602c1f1a2acdec9b3a499 --- /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