-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_document_admin.py
193 lines (162 loc) · 6.98 KB
/
test_document_admin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import os
import unittest
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase
from django.urls import reverse
from general.admin import DocumentForm, DocumentFormWithFulltext
from general.models import Document, Institution
class TestDocumentForm(TestCase):
def __init__(self, methodName: str = "runTest"):
super().__init__(methodName)
self.form = None
def setUp(self):
test_dir = os.getenv("TESTING_DIR", "/app/general/tests/files")
test_file = test_dir + "/Lorem.pdf"
with open(test_file, "rb") as f:
pdf_file = f.read()
self.file_mock = SimpleUploadedFile("test.pdf", pdf_file, content_type="application/pdf")
self.test_institution = Institution.objects.create(
name="Test Institution for Document tests"
)
def tearDown(self):
self.test_institution.delete()
def test_clean_without_url_and_file(self):
tests_form = {
"title": "Test",
"license": "MIT",
"document_type": "Glossary",
"mime_type": "pdf",
"institution": self.test_institution,
"url": "",
"uploaded_file": "",
"description": "Test description",
}
form = DocumentForm(tests_form)
self.assertFalse(form.is_valid())
self.assertEqual(form.errors["url"], ["Either URL or uploaded file must be provided."])
self.assertEqual(
form.errors["uploaded_file"], ["Either URL or uploaded file must be provided."]
)
def test_clean_without_file(self):
tests_form = {
"title": "Test",
"license": "(c)",
"document_type": "Glossary",
"mime_type": "pdf",
"institution": self.test_institution,
"url": "www.example.com",
"uploaded_file": "",
"document_data": "",
"description": "",
}
form = DocumentForm(tests_form)
self.assertTrue(form.is_valid())
#
def test_clean_without_url(self):
tests_form = {
"title": "Test",
"license": "CC0",
"document_type": "Glossary",
"mime_type": "pdf",
"institution": self.test_institution,
"url": "",
"uploaded_file": self.file_mock,
"document_data": "",
"description": "Test description",
}
form = DocumentForm(tests_form, files={"uploaded_file": self.file_mock})
self.assertTrue(form.is_valid())
def test_clean_with_large_file(self):
self.file_mock.size = 15728640
tests_form = {
"title": "Test",
"license": "MIT",
"document_type": "Glossary",
"mime_type": "pdf",
"institution": self.test_institution,
"url": "",
"uploaded_file": self.file_mock,
"description": "Test description",
}
form = DocumentForm(tests_form, files={"uploaded_file": self.file_mock})
self.assertFalse(form.is_valid())
self.assertIn("uploaded_file", form.errors)
self.assertEqual(form.errors["uploaded_file"], ["File size must not exceed 10MB."])
def test_edited_pdf_takes_priority_over_unedited_fulltext(self):
tests_form = {
"title": "Test",
"license": "CC0",
"document_type": "Glossary",
"mime_type": "pdf",
"institution": self.test_institution,
"url": "",
"uploaded_file": self.file_mock,
"document_data": "",
"description": "Test description",
}
# Test both kinds of forms - both can have edited PDFs and unedited fulltext
for FormType in [DocumentForm, DocumentFormWithFulltext]:
form = FormType(tests_form, files={"uploaded_file": self.file_mock})
self.assertTrue(form.is_valid())
self.assertNotEqual(form.cleaned_data["document_data"], "")
def test_edited_fulltext_takes_priority_over_edited_pdf(self):
custom_data = "testingstringthatisnotinsidelorem.pdf"
tests_form = {
"title": "Test",
"license": "CC0",
"document_type": "Glossary",
"mime_type": "pdf",
"institution": self.test_institution,
"url": "",
"uploaded_file": self.file_mock,
"document_data": custom_data,
"description": "Test description",
}
# We only test the DocumentFormWithFulltext because this is the only one that can have edited fulltext
form = DocumentFormWithFulltext(tests_form, files={"uploaded_file": self.file_mock})
self.assertTrue(form.is_valid())
self.assertEqual(form.cleaned_data["document_data"], custom_data)
def test_edited_fulltext_reflects_in_database_and_search(self):
title = "Test document with edited fulltext"
custom_data = "testingstringthatisnotinsidelorem.pdf"
original_form = {
"title": title,
"license": "CC0",
"document_type": "Glossary",
"mime_type": "pdf",
"institution": self.test_institution,
"url": "",
"uploaded_file": self.file_mock,
"document_data": "",
"description": "Test description",
}
# Upload the form with the PDF fulltext extracted
form = DocumentForm(original_form, files={"uploaded_file": self.file_mock})
self.assertTrue(form.is_valid())
doc = form.save()
orig_fulltext = doc.document_data
self.assertNotEqual(orig_fulltext, custom_data)
# Check we can search by PDF content
response = self.client.get(reverse("search"), {"search": orig_fulltext})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context["page_obj"][0]["heading"], title)
# Now, upload a copy with edited fulltext
edit_form = {**original_form, "document_data": custom_data, "id": doc.id}
form = DocumentFormWithFulltext(
edit_form, files={"uploaded_file": self.file_mock}, instance=doc
)
self.assertTrue(form.is_valid())
doc = form.save()
self.assertEqual(doc.document_data, custom_data)
# Check that: 1. we only have one document with this title, and 2. it has the correct fulltext
self.assertEqual(Document.objects.get(title=title).document_data, custom_data)
# Check we can search by the new content too
response = self.client.get(reverse("search"), {"search": custom_data})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context["page_obj"][0]["heading"], title)
# Check that we CANNOT search by the old content anymore
response = self.client.get(reverse("search"), {"search": orig_fulltext})
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.context["page_obj"]), 0)
if __name__ == "__main__":
unittest.main()