Skip to content

Commit

Permalink
backup code
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-gray-tangent committed Jul 4, 2024
1 parent 597ce55 commit 0af1845
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/app/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pprint import pprint
# from pprint import pprint

from django.db.models import Count
from django.http import HttpResponse
Expand Down Expand Up @@ -230,7 +230,7 @@ def institutions(request):

def search(request):
# form = DocumentFileSearchForm(request.GET or None)
pprint(request.GET)
# pprint(request.GET)
f = DocumentFileFilter(request.GET, queryset=DocumentFile.objects.all())

context = {
Expand Down
7 changes: 5 additions & 2 deletions app/general/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Meta:
"languages",
]

def filter_search(self, queryset, name, value):
def filter_search(self, queryset, value):
if value:
queue = SearchQuery(value)
search_vector = SearchVector("title", "description", "document_data")
Expand All @@ -31,4 +31,7 @@ def filter_search(self, queryset, name, value):
.filter(search_vector_annotation=queue)
.order_by("-rank")
)
return queryset
return queryset

# else:
# return ''
83 changes: 83 additions & 0 deletions app/general/tests/test_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import unittest

from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.management import call_command
from django.test import RequestFactory

from general import filters
from general.models import DocumentFile, Institution, Language, Subject


class TestDocumentFileFilter(unittest.TestCase):
def setUp(self):
# call_command("flush", "--noinput")

pass

# self.subject = Subject.objects.create(name="Test Subject")
# self.language = Language.objects.create(name="Test Language", iso_code="TL")
# self.institution = Institution.objects.create(name="Test Institution")
#
# self.title = "Some document"
# self.url = "https://example.com"
# self.uploaded_file = SimpleUploadedFile(
# "example.pdf", b"file_content", content_type="application/pdf"
# )
# self.license = "MIT"
# self.mime_type = "pdf"
# self.document_type = "Glossary"
# self.institution = self.institution
#
# self.document = DocumentFile.objects.create(
# title=self.title,
# url=self.url,
# uploaded_file=self.uploaded_file,
# license=self.license,
# mime_type=self.mime_type,
# document_type=self.document_type,
# institution=self.institution,
# )
#
# # pprint(self.document)
# # self.factory = RequestFactory()
# self.doc_filter = filters.DocumentFileFilter()
#
# def test_filter_search_empty_query(self):
# value = ""
# result = self.doc_filter.filter_search(self.document, value)
#
# print(result)
# self.assertEqual(result, self.document)

# def test_filter_search_no_result(self):
# queryset = []
# name = ''
# value = 'some search term'
# result = self.doc_filter.filter_search(queryset, value)
# self.assertEqual(result, [])
#
# def test_filter_search_with_result(self):
# queryset = [{'title': 'Test Document', 'description': 'This is a test document', 'document_data': 'Test data'}]
# name = 'title'
# value = 'Test Document'
# result = self.doc_filter.filter_search(queryset, name, value)
# self.assertEqual(result, queryset)
#
# def test_filter_search_order_rank(self):
# queryset = [
# {'title': 'Test Document 1', 'description': 'This is a test document 1', 'document_data': 'Test data 1'},
# {'title': 'Test Document 2', 'description': 'This is a test document 2', 'document_data': 'Test data 2'},
# {'title': 'Test Document 3', 'description': 'This is a test document 3', 'document_data': 'Test data 3'}
# ]
# name = 'title'
# value = 'Test Document'
# result = self.doc_filter.filter_search(queryset, name, value)
# self.assertGreater(result[0]['rank'], result[1]['rank'])

# def tearDown(self):
# if self.document.uploaded_file:
# self.document.uploaded_file.delete()


# if __name__ == "__main__":
# unittest.main()

0 comments on commit 0af1845

Please sign in to comment.