From e8bc0cfd3841bba2f850c9d0b30dd8bbbf9aa338 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Fri, 6 May 2022 17:21:39 +0200 Subject: [PATCH] Add the content_length field to the Document base class --- earthsnake/document/__init__.py | 5 +++++ earthsnake/document/es4.py | 4 ++++ tests/test_document_es4.py | 8 ++++++++ 3 files changed, 17 insertions(+) diff --git a/earthsnake/document/__init__.py b/earthsnake/document/__init__.py index 5189859..a135646 100644 --- a/earthsnake/document/__init__.py +++ b/earthsnake/document/__init__.py @@ -19,6 +19,11 @@ class Document(ABC): # pragma: no cover pylint: disable=too-few-public-methods signature: Optional[str] + @property + @abstractmethod + def content_length(self) -> int: + """The length of the document""" + @abstractmethod def sign(self, identity: Optional[Identity] = None) -> None: """Sign the document as identity diff --git a/earthsnake/document/es4.py b/earthsnake/document/es4.py index c03d231..9b8fd29 100644 --- a/earthsnake/document/es4.py +++ b/earthsnake/document/es4.py @@ -221,6 +221,10 @@ class Es4Document(Document): # pylint: disable=too-many-instance-attributes self._content = value self._content_hash = self.hash_value(value) + @property + def content_length(self) -> int: + return len(self._content) + def generate_hash(self) -> str: """Calculate the hash of the document diff --git a/tests/test_document_es4.py b/tests/test_document_es4.py index 279ce97..e5e3dd4 100644 --- a/tests/test_document_es4.py +++ b/tests/test_document_es4.py @@ -272,3 +272,11 @@ def test_validate_unsigned_document(identity: Identity) -> None: document.validate_signature() assert str(ctx.value) == 'document has no signature assigned' + + +def test_content_length() -> None: + """Test the content_length property""" + + document = Es4Document.from_json(VALID_DOCUMENT) + + assert document.content_length == 4