From 45109026774b8d1d5c5a9ac14a9d850f8eb11069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Bogda=C5=82?= Date: Thu, 14 Dec 2023 16:39:49 +0100 Subject: [PATCH] Fix: Don't attempt to parse none objects during date searching --- src/documents/index.py | 3 ++- src/documents/tests/test_api_search.py | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/documents/index.py b/src/documents/index.py index 34b2a56c3..ebfe40e18 100644 --- a/src/documents/index.py +++ b/src/documents/index.py @@ -3,6 +3,7 @@ import math import os from collections import Counter from contextlib import contextmanager +from datetime import datetime from typing import Optional from dateutil.parser import isoparse @@ -371,7 +372,7 @@ class LocalDateParser(English): if isinstance(d, timespan): d.start = self.reverse_timezone_offset(d.start) d.end = self.reverse_timezone_offset(d.end) - else: + elif isinstance(d, datetime): d = self.reverse_timezone_offset(d) return d diff --git a/src/documents/tests/test_api_search.py b/src/documents/tests/test_api_search.py index 84461bb35..4cd1a367c 100644 --- a/src/documents/tests/test_api_search.py +++ b/src/documents/tests/test_api_search.py @@ -455,6 +455,31 @@ class TestDocumentSearchApi(DirectoriesMixin, APITestCase): # Assert subset in results self.assertDictEqual(result, {**result, **subset}) + def test_search_added_invalid_date(self): + """ + GIVEN: + - One document added right now + WHEN: + - Query with invalid added date + THEN: + - No documents returned + """ + d1 = Document.objects.create( + title="invoice", + content="the thing i bought at a shop and paid with bank account", + checksum="A", + pk=1, + ) + + with index.open_index_writer() as writer: + index.update_document(writer, d1) + + response = self.client.get("/api/documents/?query=added:invalid-date") + results = response.data["results"] + + # Expect 0 document returned + self.assertEqual(len(results), 0) + @mock.patch("documents.index.autocomplete") def test_search_autocomplete_limits(self, m): """