From d13baab0a6478877116df2b298b00c654dc27277 Mon Sep 17 00:00:00 2001 From: jonaswinkler <17569239+jonaswinkler@users.noreply.github.com> Date: Sun, 4 Apr 2021 01:19:07 +0200 Subject: [PATCH] more testing --- src/documents/index.py | 14 +++------- src/documents/tests/test_api.py | 48 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/src/documents/index.py b/src/documents/index.py index e17c82daa..6fdcff42f 100644 --- a/src/documents/index.py +++ b/src/documents/index.py @@ -82,11 +82,8 @@ def open_index(recreate=False): @contextmanager -def open_index_writer(ix=None, optimize=False): - if ix: - writer = AsyncWriter(ix) - else: - writer = AsyncWriter(open_index()) +def open_index_writer(optimize=False): + writer = AsyncWriter(open_index()) try: yield writer @@ -98,11 +95,8 @@ def open_index_writer(ix=None, optimize=False): @contextmanager -def open_index_searcher(ix=None): - if ix: - searcher = ix.searcher() - else: - searcher = open_index().searcher() +def open_index_searcher(): + searcher = open_index().searcher() try: yield searcher diff --git a/src/documents/tests/test_api.py b/src/documents/tests/test_api.py index 5c54a8d74..cfde28e2d 100644 --- a/src/documents/tests/test_api.py +++ b/src/documents/tests/test_api.py @@ -423,6 +423,54 @@ class TestDocumentApi(DirectoriesMixin, APITestCase): self.assertEqual(results[0]['id'], d3.id) self.assertEqual(results[1]['id'], d1.id) + def test_search_filtering(self): + t = Tag.objects.create(name="tag") + t2 = Tag.objects.create(name="tag2") + c = Correspondent.objects.create(name="correspondent") + dt = DocumentType.objects.create(name="type") + + d1 = Document.objects.create(checksum="1", correspondent=c, content="test") + d2 = Document.objects.create(checksum="2", document_type=dt, content="test") + d3 = Document.objects.create(checksum="3", content="test") + d3.tags.add(t) + d3.tags.add(t2) + d4 = Document.objects.create(checksum="4", created=datetime.datetime(2020, 7, 13), content="test") + d4.tags.add(t2) + d5 = Document.objects.create(checksum="5", added=datetime.datetime(2020, 7, 13), content="test") + d6 = Document.objects.create(checksum="6", content="test2") + + with AsyncWriter(index.open_index()) as writer: + for doc in Document.objects.all(): + index.update_document(writer, doc) + + def search_query(q): + r = self.client.get("/api/documents/?query=test" + q) + self.assertEqual(r.status_code, 200) + return [hit['id'] for hit in r.data['results']] + + self.assertCountEqual(search_query(""), [d1.id, d2.id, d3.id, d4.id, d5.id]) + self.assertCountEqual(search_query("&is_tagged=true"), [d3.id, d4.id]) + self.assertCountEqual(search_query("&is_tagged=false"), [d1.id, d2.id, d5.id]) + self.assertCountEqual(search_query("&correspondent__id=" + str(c.id)), [d1.id]) + self.assertCountEqual(search_query("&document_type__id=" + str(dt.id)), [d2.id]) + self.assertCountEqual(search_query("&correspondent__isnull"), [d2.id, d3.id, d4.id, d5.id]) + self.assertCountEqual(search_query("&document_type__isnull"), [d1.id, d3.id, d4.id, d5.id]) + self.assertCountEqual(search_query("&tags__id__all=" + str(t.id) + "," + str(t2.id)), [d3.id]) + self.assertCountEqual(search_query("&tags__id__all=" + str(t.id)), [d3.id]) + self.assertCountEqual(search_query("&tags__id__all=" + str(t2.id)), [d3.id, d4.id]) + + self.assertIn(d4.id, search_query("&created__date__lt=" + datetime.datetime(2020, 9, 2).strftime("%Y-%m-%d"))) + self.assertNotIn(d4.id, search_query("&created__date__gt=" + datetime.datetime(2020, 9, 2).strftime("%Y-%m-%d"))) + + self.assertNotIn(d4.id, search_query("&created__date__lt=" + datetime.datetime(2020, 1, 2).strftime("%Y-%m-%d"))) + self.assertIn(d4.id, search_query("&created__date__gt=" + datetime.datetime(2020, 1, 2).strftime("%Y-%m-%d"))) + + self.assertIn(d5.id, search_query("&added__date__lt=" + datetime.datetime(2020, 9, 2).strftime("%Y-%m-%d"))) + self.assertNotIn(d5.id, search_query("&added__date__gt=" + datetime.datetime(2020, 9, 2).strftime("%Y-%m-%d"))) + + self.assertNotIn(d5.id, search_query("&added__date__lt=" + datetime.datetime(2020, 1, 2).strftime("%Y-%m-%d"))) + self.assertIn(d5.id, search_query("&added__date__gt=" + datetime.datetime(2020, 1, 2).strftime("%Y-%m-%d"))) + def test_statistics(self): doc1 = Document.objects.create(title="none1", checksum="A")