Skip to content

Commit

Permalink
Parse empty dates as None, warn on invalid non-empty dates
Browse files Browse the repository at this point in the history
  • Loading branch information
dragon-dxw committed Sep 18, 2023
1 parent 908d12d commit 35df2e8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/caselawclient/models/documents.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import warnings
from functools import cached_property
from typing import TYPE_CHECKING, Any, Dict, NewType, Optional

Expand Down Expand Up @@ -26,6 +27,11 @@
uri_for_s3,
)


class UnparsableDate(Warning):
pass


DOCUMENT_STATUS_HOLD = "On hold"
""" This document has been placed on hold to actively prevent publication. """

Expand Down Expand Up @@ -171,10 +177,19 @@ def document_date_as_string(self) -> str:
)

@cached_property
def document_date_as_date(self) -> datetime.date:
return datetime.datetime.strptime(
self.document_date_as_string, "%Y-%m-%d"
).date()
def document_date_as_date(self) -> Optional[datetime.date]:
if not self.document_date_as_string:
return None
try:
return datetime.datetime.strptime(
self.document_date_as_string, "%Y-%m-%d"
).date()
except ValueError:
warnings.warn(
f"Unparsable date encountered: {self.document_date_as_string}",
UnparsableDate,
)
return None

def get_manifestation_datetimes(self, name: str) -> list[datetime.datetime]:
iso_datetimes = self._get_xpath_match_strings(
Expand Down
49 changes: 49 additions & 0 deletions tests/models/test_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
CannotPublishUnpublishableDocument,
Document,
DocumentNotSafeForDeletion,
UnparsableDate,
)


Expand Down Expand Up @@ -490,6 +491,54 @@ def test_date_as_string(self, opening_tag, closing_tag, mock_api_client):
"test/1234", show_unpublished=True
)

@pytest.mark.parametrize(
"opening_tag, closing_tag",
[
("judgment", "judgment"),
('doc name="pressSummary"', "doc"),
],
)
def test_bad_date_as_string(self, opening_tag, closing_tag, mock_api_client):
mock_api_client.get_judgment_xml_bytestring.return_value = f"""
<akomaNtoso xmlns:uk="https://caselaw.nationalarchives.gov.uk/akn"
xmlns="http://docs.oasis-open.org/legaldocml/ns/akn/3.0">
<{opening_tag}>
<meta>
<identification>
<FRBRWork>
<FRBRdate date="kitten"/>
</FRBRWork>
</identification>
</meta>
</{closing_tag}>
</akomaNtoso>
""".encode(
"utf-8"
)

document = Document("test/1234", mock_api_client)

assert document.document_date_as_string == "kitten"
with pytest.warns(UnparsableDate):
assert document.document_date_as_date is None
mock_api_client.get_judgment_xml_bytestring.assert_called_once_with(
"test/1234", show_unpublished=True
)

def test_absent_date_as_string(self, mock_api_client):
mock_api_client.get_judgment_xml_bytestring.return_value = """
<akomaNtoso xmlns:uk="https://caselaw.nationalarchives.gov.uk/akn"
xmlns="http://docs.oasis-open.org/legaldocml/ns/akn/3.0">
</akomaNtoso>
""".encode(
"utf-8"
)

document = Document("test/1234", mock_api_client)

assert document.document_date_as_string == ""
assert document.document_date_as_date is None

def test_dates(self, mock_api_client):
mock_api_client.get_judgment_xml_bytestring.return_value = """
<akomaNtoso xmlns:uk="https://caselaw.nationalarchives.gov.uk/akn"
Expand Down

0 comments on commit 35df2e8

Please sign in to comment.