Fix: Don't attempt to parse none objects during date searching

This commit is contained in:
Adam Bogdał 2023-12-14 16:39:49 +01:00 committed by GitHub
parent cd38c39908
commit 4510902677
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -3,6 +3,7 @@ import math
import os import os
from collections import Counter from collections import Counter
from contextlib import contextmanager from contextlib import contextmanager
from datetime import datetime
from typing import Optional from typing import Optional
from dateutil.parser import isoparse from dateutil.parser import isoparse
@ -371,7 +372,7 @@ class LocalDateParser(English):
if isinstance(d, timespan): if isinstance(d, timespan):
d.start = self.reverse_timezone_offset(d.start) d.start = self.reverse_timezone_offset(d.start)
d.end = self.reverse_timezone_offset(d.end) d.end = self.reverse_timezone_offset(d.end)
else: elif isinstance(d, datetime):
d = self.reverse_timezone_offset(d) d = self.reverse_timezone_offset(d)
return d return d

View File

@ -455,6 +455,31 @@ class TestDocumentSearchApi(DirectoriesMixin, APITestCase):
# Assert subset in results # Assert subset in results
self.assertDictEqual(result, {**result, **subset}) 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") @mock.patch("documents.index.autocomplete")
def test_search_autocomplete_limits(self, m): def test_search_autocomplete_limits(self, m):
""" """