From 2c3eaadbce9fac1a465c0e4c866f9df3ae4216b2 Mon Sep 17 00:00:00 2001 From: jonaswinkler Date: Thu, 17 Dec 2020 23:24:28 +0100 Subject: [PATCH] test cases --- src/documents/tests/test_api.py | 19 +++++++++++++++++++ src/documents/views.py | 8 ++++++++ 2 files changed, 27 insertions(+) diff --git a/src/documents/tests/test_api.py b/src/documents/tests/test_api.py index 49dddee87..ba1ab45ca 100644 --- a/src/documents/tests/test_api.py +++ b/src/documents/tests/test_api.py @@ -351,6 +351,25 @@ class TestDocumentApi(DirectoriesMixin, APITestCase): self.assertEqual(correction, None) + def test_search_more_like(self): + d1=Document.objects.create(title="invoice", content="the thing i bought at a shop and paid with bank account", checksum="A", pk=1) + d2=Document.objects.create(title="bank statement 1", content="things i paid for in august", pk=2, checksum="B") + d3=Document.objects.create(title="bank statement 3", content="things i paid for in september", pk=3, checksum="C") + with AsyncWriter(index.open_index()) as writer: + index.update_document(writer, d1) + index.update_document(writer, d2) + index.update_document(writer, d3) + + response = self.client.get(f"/api/search/?more_like={d2.id}") + + self.assertEqual(response.status_code, 200) + + results = response.data['results'] + + self.assertEqual(len(results), 2) + self.assertEqual(results[0]['id'], d3.id) + self.assertEqual(results[1]['id'], d1.id) + def test_statistics(self): doc1 = Document.objects.create(title="none1", checksum="A") diff --git a/src/documents/views.py b/src/documents/views.py index bd9a748e8..59fbfb213 100755 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -348,6 +348,14 @@ class SearchView(APIView): more_like_id = None more_like_content = None + if not query and not more_like_id: + return Response({ + 'count': 0, + 'page': 0, + 'page_count': 0, + 'corrected_query': None, + 'results': []}) + try: page = int(request.query_params.get('page', 1)) except (ValueError, TypeError):