Skip to content

Commit

Permalink
Merge pull request #736 from nationalarchives/doc-body-things-on-doc
Browse files Browse the repository at this point in the history
Erroneous doc.body attribute calls on body work with a warning
  • Loading branch information
dragon-dxw authored Oct 23, 2024
2 parents 1a73a87 + cd17d52 commit ebf5643
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog 1.0.0].

## v27.0.2 (unreleased)

- Allow things on doc.body to be called from doc with a warning

## v27.0.1 (2024-10-17)

- Fix `.content_as_html` on Document Factory
Expand Down
7 changes: 7 additions & 0 deletions src/caselawclient/models/documents/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,3 +495,10 @@ def can_reparse(self) -> bool:
Is it sensible to reparse this document?
"""
return self.docx_exists()

def __getattr__(self, name: str) -> Any:
warnings.warn(f"{name} no longer exists on Document, using Document.body instead", DeprecationWarning)
try:
return getattr(self.body, name)
except Exception:
raise AttributeError(f"Neither 'Document' nor 'DocumentBody' objects have an attribute '{name}'")
23 changes: 23 additions & 0 deletions tests/models/documents/test_documents.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import warnings
from unittest.mock import PropertyMock, patch

import pytest
Expand All @@ -8,6 +9,7 @@
NotSupportedOnVersion,
OnlySupportedOnVersion,
)
from caselawclient.factories import DocumentBodyFactory, DocumentFactory
from caselawclient.models.documents import (
DOCUMENT_STATUS_HOLD,
DOCUMENT_STATUS_IN_PROGRESS,
Expand Down Expand Up @@ -290,3 +292,24 @@ def test_returns_true_when_enriched_recently_is_true_and_validates_against_schem
mock_validates_against_schema.return_value = validates_against_schema

assert document.can_enrich is can_enrich

class TestMethodMissing:
def test_attribute_on_body(self, mock_api_client):
doc = DocumentFactory.build(uri="test/1234", body=DocumentBodyFactory.build(name="docname"))
with pytest.deprecated_call():
name = doc.name

assert name == "docname"

def test_real_attribute(self, mock_api_client):
doc = DocumentFactory.build(uri="test/1234", body=DocumentBodyFactory.build(name="docname"))
with warnings.catch_warnings():
identifier = doc.best_human_identifier
assert identifier is None

def test_absent_item(self, mock_api_client):
doc = DocumentFactory.build(uri="test/1234", body=DocumentBodyFactory.build(name="docname"))
with pytest.raises(
AttributeError, match="Neither 'Document' nor 'DocumentBody' objects have an attribute 'x'"
):
doc.x

0 comments on commit ebf5643

Please sign in to comment.