Skip to content

Commit

Permalink
Improve robustness of SearchResult date parsing
Browse files Browse the repository at this point in the history
This is now tested, and will log a warning if a non-empty unparseable string is detected.
  • Loading branch information
jacksonj04 committed Apr 25, 2023
1 parent b6934ea commit 1209542
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
7 changes: 6 additions & 1 deletion judgments/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from os.path import dirname, join

from caselawclient.Client import api_client
Expand Down Expand Up @@ -29,7 +30,11 @@ def __init__(

try:
self.date = dateparser.parse(date)
except ParserError:
except ParserError as e:
if date != "":
logging.warning(
f'Unable to parse document date "{date}". Full error: {e}'
)
self.date = None
try:
self.court = courts.get_by_code(court)
Expand Down
34 changes: 34 additions & 0 deletions judgments/tests/test_search.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from datetime import datetime
from logging import WARNING
from typing import Any
from unittest.mock import patch

import pytest
from dateutil import parser as dateparser
from django.test import TestCase
from lxml import etree
Expand Down Expand Up @@ -159,3 +163,33 @@ def test_create_from_node_with_missing_elements(self, fake_client):
self.assertEqual(None, search_result.neutral_citation)
self.assertEqual(None, search_result.court)
self.assertEqual(None, search_result.content_hash)


class TestSearchResultInit:
@pytest.mark.parametrize(
"date_string, expected",
[
["", None],
["2023-05-09", datetime(2023, 5, 9, 0, 0)],
["2023-04-05T06:54:00", datetime(2023, 4, 5, 6, 54)],
["ffffff", None],
],
)
def test_searchresult_date_parsing(self, date_string: str, expected: Any):
search_result = fake_search_result(date=date_string)

assert search_result.date == expected

def test_unparseable_non_empty_string_logs_warning(self, caplog):
caplog.set_level(WARNING)

fake_search_result(date="ffffff")

assert "Unable to parse document date" in caplog.text

def test_unparseable_empty_string_doesnt_log_warning(self, caplog):
caplog.set_level(WARNING)

fake_search_result(date="")

assert "Unable to parse document date" not in caplog.text

0 comments on commit 1209542

Please sign in to comment.