mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-07-28 18:24:38 -05:00
Runs the pre-commit hooks over all the Python files
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
from factory import Faker
|
||||
from factory.django import DjangoModelFactory
|
||||
|
||||
from ..models import Document, Correspondent
|
||||
from ..models import Correspondent
|
||||
from ..models import Document
|
||||
|
||||
|
||||
class CorrespondentFactory(DjangoModelFactory):
|
||||
|
@@ -3,7 +3,6 @@ from unittest import mock
|
||||
from django.contrib.admin.sites import AdminSite
|
||||
from django.test import TestCase
|
||||
from django.utils import timezone
|
||||
|
||||
from documents import index
|
||||
from documents.admin import DocumentAdmin
|
||||
from documents.models import Document
|
||||
@@ -42,7 +41,8 @@ class TestDocumentAdmin(DirectoriesMixin, TestCase):
|
||||
docs = []
|
||||
for i in range(42):
|
||||
doc = Document.objects.create(
|
||||
title="Many documents with the same title", checksum=f"{i:02}"
|
||||
title="Many documents with the same title",
|
||||
checksum=f"{i:02}",
|
||||
)
|
||||
docs.append(doc)
|
||||
index.add_or_update_document(doc)
|
||||
@@ -61,6 +61,7 @@ class TestDocumentAdmin(DirectoriesMixin, TestCase):
|
||||
|
||||
def test_created(self):
|
||||
doc = Document.objects.create(
|
||||
title="test", created=timezone.make_aware(timezone.datetime(2020, 4, 12))
|
||||
title="test",
|
||||
created=timezone.make_aware(timezone.datetime(2020, 4, 12)),
|
||||
)
|
||||
self.assertEqual(self.doc_admin.created_(doc), "2020-04-12")
|
||||
|
@@ -10,22 +10,20 @@ from unittest import mock
|
||||
import pytest
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import User
|
||||
from django.utils import timezone
|
||||
from django.test import override_settings
|
||||
from django.utils import timezone
|
||||
from documents import bulk_edit
|
||||
from documents import index
|
||||
from documents.models import Correspondent
|
||||
from documents.models import Document
|
||||
from documents.models import DocumentType
|
||||
from documents.models import MatchingModel
|
||||
from documents.models import SavedView
|
||||
from documents.models import Tag
|
||||
from documents.tests.utils import DirectoriesMixin
|
||||
from rest_framework.test import APITestCase
|
||||
from whoosh.writing import AsyncWriter
|
||||
|
||||
from documents import index, bulk_edit
|
||||
from documents.models import (
|
||||
Document,
|
||||
Correspondent,
|
||||
DocumentType,
|
||||
Tag,
|
||||
SavedView,
|
||||
MatchingModel,
|
||||
)
|
||||
from documents.tests.utils import DirectoriesMixin
|
||||
|
||||
|
||||
class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
def setUp(self):
|
||||
@@ -72,7 +70,9 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
returned_doc["title"] = "the new title"
|
||||
|
||||
response = self.client.put(
|
||||
"/api/documents/{}/".format(doc.pk), returned_doc, format="json"
|
||||
"/api/documents/{}/".format(doc.pk),
|
||||
returned_doc,
|
||||
format="json",
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
@@ -127,7 +127,8 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
self.assertEqual(len(results[0]), 2)
|
||||
|
||||
response = self.client.get(
|
||||
"/api/documents/?fields=id,conteasdnt", format="json"
|
||||
"/api/documents/?fields=id,conteasdnt",
|
||||
format="json",
|
||||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
results = response.data["results"]
|
||||
@@ -162,7 +163,8 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
)
|
||||
|
||||
with open(
|
||||
os.path.join(self.dirs.thumbnail_dir, "{:07d}.png".format(doc.pk)), "wb"
|
||||
os.path.join(self.dirs.thumbnail_dir, "{:07d}.png".format(doc.pk)),
|
||||
"wb",
|
||||
) as f:
|
||||
f.write(content_thumbnail)
|
||||
|
||||
@@ -206,7 +208,7 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
self.assertEqual(response.content, content_archive)
|
||||
|
||||
response = self.client.get(
|
||||
"/api/documents/{}/download/?original=true".format(doc.pk)
|
||||
"/api/documents/{}/download/?original=true".format(doc.pk),
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
@@ -218,7 +220,7 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
self.assertEqual(response.content, content_archive)
|
||||
|
||||
response = self.client.get(
|
||||
"/api/documents/{}/preview/?original=true".format(doc.pk)
|
||||
"/api/documents/{}/preview/?original=true".format(doc.pk),
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
@@ -227,7 +229,9 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
def test_document_actions_not_existing_file(self):
|
||||
|
||||
doc = Document.objects.create(
|
||||
title="none", filename=os.path.basename("asd"), mime_type="application/pdf"
|
||||
title="none",
|
||||
filename=os.path.basename("asd"),
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
|
||||
response = self.client.get("/api/documents/{}/download/".format(doc.pk))
|
||||
@@ -242,13 +246,19 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
def test_document_filters(self):
|
||||
|
||||
doc1 = Document.objects.create(
|
||||
title="none1", checksum="A", mime_type="application/pdf"
|
||||
title="none1",
|
||||
checksum="A",
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
doc2 = Document.objects.create(
|
||||
title="none2", checksum="B", mime_type="application/pdf"
|
||||
title="none2",
|
||||
checksum="B",
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
doc3 = Document.objects.create(
|
||||
title="none3", checksum="C", mime_type="application/pdf"
|
||||
title="none3",
|
||||
checksum="C",
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
|
||||
tag_inbox = Tag.objects.create(name="t1", is_inbox_tag=True)
|
||||
@@ -273,7 +283,7 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
self.assertCountEqual([results[0]["id"], results[1]["id"]], [doc2.id, doc3.id])
|
||||
|
||||
response = self.client.get(
|
||||
"/api/documents/?tags__id__in={},{}".format(tag_inbox.id, tag_3.id)
|
||||
"/api/documents/?tags__id__in={},{}".format(tag_inbox.id, tag_3.id),
|
||||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
results = response.data["results"]
|
||||
@@ -281,7 +291,7 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
self.assertCountEqual([results[0]["id"], results[1]["id"]], [doc1.id, doc3.id])
|
||||
|
||||
response = self.client.get(
|
||||
"/api/documents/?tags__id__in={},{}".format(tag_2.id, tag_3.id)
|
||||
"/api/documents/?tags__id__in={},{}".format(tag_2.id, tag_3.id),
|
||||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
results = response.data["results"]
|
||||
@@ -289,7 +299,7 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
self.assertCountEqual([results[0]["id"], results[1]["id"]], [doc2.id, doc3.id])
|
||||
|
||||
response = self.client.get(
|
||||
"/api/documents/?tags__id__all={},{}".format(tag_2.id, tag_3.id)
|
||||
"/api/documents/?tags__id__all={},{}".format(tag_2.id, tag_3.id),
|
||||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
results = response.data["results"]
|
||||
@@ -297,14 +307,14 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
self.assertEqual(results[0]["id"], doc3.id)
|
||||
|
||||
response = self.client.get(
|
||||
"/api/documents/?tags__id__all={},{}".format(tag_inbox.id, tag_3.id)
|
||||
"/api/documents/?tags__id__all={},{}".format(tag_inbox.id, tag_3.id),
|
||||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
results = response.data["results"]
|
||||
self.assertEqual(len(results), 0)
|
||||
|
||||
response = self.client.get(
|
||||
"/api/documents/?tags__id__all={}a{}".format(tag_inbox.id, tag_3.id)
|
||||
"/api/documents/?tags__id__all={}a{}".format(tag_inbox.id, tag_3.id),
|
||||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
results = response.data["results"]
|
||||
@@ -317,7 +327,7 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
self.assertCountEqual([results[0]["id"], results[1]["id"]], [doc1.id, doc2.id])
|
||||
|
||||
response = self.client.get(
|
||||
"/api/documents/?tags__id__none={},{}".format(tag_3.id, tag_2.id)
|
||||
"/api/documents/?tags__id__none={},{}".format(tag_3.id, tag_2.id),
|
||||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
results = response.data["results"]
|
||||
@@ -325,7 +335,7 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
self.assertEqual(results[0]["id"], doc1.id)
|
||||
|
||||
response = self.client.get(
|
||||
"/api/documents/?tags__id__none={},{}".format(tag_2.id, tag_inbox.id)
|
||||
"/api/documents/?tags__id__none={},{}".format(tag_2.id, tag_inbox.id),
|
||||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
results = response.data["results"]
|
||||
@@ -443,7 +453,7 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
|
||||
for i in range(1, 6):
|
||||
response = self.client.get(
|
||||
f"/api/documents/?query=content&page={i}&page_size=10"
|
||||
f"/api/documents/?query=content&page={i}&page_size=10",
|
||||
)
|
||||
results = response.data["results"]
|
||||
self.assertEqual(response.data["count"], 55)
|
||||
@@ -595,31 +605,35 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
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]
|
||||
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]
|
||||
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]
|
||||
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]
|
||||
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")
|
||||
+ 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")
|
||||
+ datetime.datetime(2020, 9, 2).strftime("%Y-%m-%d"),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -627,40 +641,44 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
d4.id,
|
||||
search_query(
|
||||
"&created__date__lt="
|
||||
+ datetime.datetime(2020, 1, 2).strftime("%Y-%m-%d")
|
||||
+ 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")
|
||||
+ 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")
|
||||
"&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")
|
||||
"&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")
|
||||
"&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")
|
||||
"&added__date__gt="
|
||||
+ datetime.datetime(2020, 1, 2).strftime("%Y-%m-%d"),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -700,18 +718,22 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
return [hit["id"] for hit in r.data["results"]]
|
||||
|
||||
self.assertListEqual(
|
||||
search_query("&ordering=archive_serial_number"), [d3.id, d1.id, d2.id]
|
||||
search_query("&ordering=archive_serial_number"),
|
||||
[d3.id, d1.id, d2.id],
|
||||
)
|
||||
self.assertListEqual(
|
||||
search_query("&ordering=-archive_serial_number"), [d2.id, d1.id, d3.id]
|
||||
search_query("&ordering=-archive_serial_number"),
|
||||
[d2.id, d1.id, d3.id],
|
||||
)
|
||||
self.assertListEqual(search_query("&ordering=title"), [d3.id, d2.id, d1.id])
|
||||
self.assertListEqual(search_query("&ordering=-title"), [d1.id, d2.id, d3.id])
|
||||
self.assertListEqual(
|
||||
search_query("&ordering=correspondent__name"), [d1.id, d3.id, d2.id]
|
||||
search_query("&ordering=correspondent__name"),
|
||||
[d1.id, d3.id, d2.id],
|
||||
)
|
||||
self.assertListEqual(
|
||||
search_query("&ordering=-correspondent__name"), [d2.id, d3.id, d1.id]
|
||||
search_query("&ordering=-correspondent__name"),
|
||||
[d2.id, d3.id, d1.id],
|
||||
)
|
||||
|
||||
def test_statistics(self):
|
||||
@@ -740,10 +762,12 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
def test_upload(self, m):
|
||||
|
||||
with open(
|
||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb"
|
||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
||||
"rb",
|
||||
) as f:
|
||||
response = self.client.post(
|
||||
"/api/documents/post_document/", {"document": f}
|
||||
"/api/documents/post_document/",
|
||||
{"document": f},
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
@@ -761,7 +785,8 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
def test_upload_empty_metadata(self, m):
|
||||
|
||||
with open(
|
||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb"
|
||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
||||
"rb",
|
||||
) as f:
|
||||
response = self.client.post(
|
||||
"/api/documents/post_document/",
|
||||
@@ -783,10 +808,12 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
def test_upload_invalid_form(self, m):
|
||||
|
||||
with open(
|
||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb"
|
||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
||||
"rb",
|
||||
) as f:
|
||||
response = self.client.post(
|
||||
"/api/documents/post_document/", {"documenst": f}
|
||||
"/api/documents/post_document/",
|
||||
{"documenst": f},
|
||||
)
|
||||
self.assertEqual(response.status_code, 400)
|
||||
m.assert_not_called()
|
||||
@@ -795,10 +822,12 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
def test_upload_invalid_file(self, m):
|
||||
|
||||
with open(
|
||||
os.path.join(os.path.dirname(__file__), "samples", "simple.zip"), "rb"
|
||||
os.path.join(os.path.dirname(__file__), "samples", "simple.zip"),
|
||||
"rb",
|
||||
) as f:
|
||||
response = self.client.post(
|
||||
"/api/documents/post_document/", {"document": f}
|
||||
"/api/documents/post_document/",
|
||||
{"document": f},
|
||||
)
|
||||
self.assertEqual(response.status_code, 400)
|
||||
m.assert_not_called()
|
||||
@@ -806,7 +835,8 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
@mock.patch("documents.views.async_task")
|
||||
def test_upload_with_title(self, async_task):
|
||||
with open(
|
||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb"
|
||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
||||
"rb",
|
||||
) as f:
|
||||
response = self.client.post(
|
||||
"/api/documents/post_document/",
|
||||
@@ -824,10 +854,12 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
def test_upload_with_correspondent(self, async_task):
|
||||
c = Correspondent.objects.create(name="test-corres")
|
||||
with open(
|
||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb"
|
||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
||||
"rb",
|
||||
) as f:
|
||||
response = self.client.post(
|
||||
"/api/documents/post_document/", {"document": f, "correspondent": c.id}
|
||||
"/api/documents/post_document/",
|
||||
{"document": f, "correspondent": c.id},
|
||||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
@@ -840,10 +872,12 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
@mock.patch("documents.views.async_task")
|
||||
def test_upload_with_invalid_correspondent(self, async_task):
|
||||
with open(
|
||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb"
|
||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
||||
"rb",
|
||||
) as f:
|
||||
response = self.client.post(
|
||||
"/api/documents/post_document/", {"document": f, "correspondent": 3456}
|
||||
"/api/documents/post_document/",
|
||||
{"document": f, "correspondent": 3456},
|
||||
)
|
||||
self.assertEqual(response.status_code, 400)
|
||||
|
||||
@@ -853,10 +887,12 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
def test_upload_with_document_type(self, async_task):
|
||||
dt = DocumentType.objects.create(name="invoice")
|
||||
with open(
|
||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb"
|
||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
||||
"rb",
|
||||
) as f:
|
||||
response = self.client.post(
|
||||
"/api/documents/post_document/", {"document": f, "document_type": dt.id}
|
||||
"/api/documents/post_document/",
|
||||
{"document": f, "document_type": dt.id},
|
||||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
@@ -869,10 +905,12 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
@mock.patch("documents.views.async_task")
|
||||
def test_upload_with_invalid_document_type(self, async_task):
|
||||
with open(
|
||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb"
|
||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
||||
"rb",
|
||||
) as f:
|
||||
response = self.client.post(
|
||||
"/api/documents/post_document/", {"document": f, "document_type": 34578}
|
||||
"/api/documents/post_document/",
|
||||
{"document": f, "document_type": 34578},
|
||||
)
|
||||
self.assertEqual(response.status_code, 400)
|
||||
|
||||
@@ -883,10 +921,12 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
t1 = Tag.objects.create(name="tag1")
|
||||
t2 = Tag.objects.create(name="tag2")
|
||||
with open(
|
||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb"
|
||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
||||
"rb",
|
||||
) as f:
|
||||
response = self.client.post(
|
||||
"/api/documents/post_document/", {"document": f, "tags": [t2.id, t1.id]}
|
||||
"/api/documents/post_document/",
|
||||
{"document": f, "tags": [t2.id, t1.id]},
|
||||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
@@ -901,7 +941,8 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
t1 = Tag.objects.create(name="tag1")
|
||||
t2 = Tag.objects.create(name="tag2")
|
||||
with open(
|
||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), "rb"
|
||||
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
|
||||
"rb",
|
||||
) as f:
|
||||
response = self.client.post(
|
||||
"/api/documents/post_document/",
|
||||
@@ -952,7 +993,9 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
|
||||
def test_get_metadata_no_archive(self):
|
||||
doc = Document.objects.create(
|
||||
title="test", filename="file.pdf", mime_type="application/pdf"
|
||||
title="test",
|
||||
filename="file.pdf",
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
|
||||
shutil.copy(
|
||||
@@ -999,7 +1042,8 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(
|
||||
response.data, {"correspondents": [], "tags": [], "document_types": []}
|
||||
response.data,
|
||||
{"correspondents": [], "tags": [], "document_types": []},
|
||||
)
|
||||
|
||||
def test_get_suggestions_invalid_doc(self):
|
||||
@@ -1010,10 +1054,15 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
@mock.patch("documents.views.match_tags")
|
||||
@mock.patch("documents.views.match_document_types")
|
||||
def test_get_suggestions(
|
||||
self, match_document_types, match_tags, match_correspondents
|
||||
self,
|
||||
match_document_types,
|
||||
match_tags,
|
||||
match_correspondents,
|
||||
):
|
||||
doc = Document.objects.create(
|
||||
title="test", mime_type="application/pdf", content="this is an invoice!"
|
||||
title="test",
|
||||
mime_type="application/pdf",
|
||||
content="this is an invoice!",
|
||||
)
|
||||
match_tags.return_value = [Tag(id=56), Tag(id=123)]
|
||||
match_document_types.return_value = [DocumentType(id=23)]
|
||||
@@ -1094,7 +1143,9 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
self.assertEqual(v1.user, self.user)
|
||||
|
||||
response = self.client.patch(
|
||||
f"/api/saved_views/{v1.id}/", {"show_in_sidebar": False}, format="json"
|
||||
f"/api/saved_views/{v1.id}/",
|
||||
{"show_in_sidebar": False},
|
||||
format="json",
|
||||
)
|
||||
|
||||
v1 = SavedView.objects.get(id=v1.id)
|
||||
@@ -1183,7 +1234,9 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
def test_regex_no_algorithm(self):
|
||||
for endpoint in ["correspondents", "tags", "document_types"]:
|
||||
response = self.client.post(
|
||||
f"/api/{endpoint}/", {"name": "test", "match": "[0-9]"}, format="json"
|
||||
f"/api/{endpoint}/",
|
||||
{"name": "test", "match": "[0-9]"},
|
||||
format="json",
|
||||
)
|
||||
self.assertEqual(response.status_code, 201, endpoint)
|
||||
|
||||
@@ -1200,7 +1253,9 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
|
||||
def test_tag_color(self):
|
||||
response = self.client.post(
|
||||
"/api/tags/", {"name": "tag", "colour": 3}, format="json"
|
||||
"/api/tags/",
|
||||
{"name": "tag", "colour": 3},
|
||||
format="json",
|
||||
)
|
||||
self.assertEqual(response.status_code, 201)
|
||||
self.assertEqual(Tag.objects.get(id=response.data["id"]).color, "#b2df8a")
|
||||
@@ -1213,14 +1268,17 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
||||
|
||||
def test_tag_color_invalid(self):
|
||||
response = self.client.post(
|
||||
"/api/tags/", {"name": "tag", "colour": 34}, format="json"
|
||||
"/api/tags/",
|
||||
{"name": "tag", "colour": 34},
|
||||
format="json",
|
||||
)
|
||||
self.assertEqual(response.status_code, 400)
|
||||
|
||||
def test_tag_color_custom(self):
|
||||
tag = Tag.objects.create(name="test", color="#abcdef")
|
||||
self.assertEqual(
|
||||
self.client.get(f"/api/tags/{tag.id}/", format="json").data["colour"], 1
|
||||
self.client.get(f"/api/tags/{tag.id}/", format="json").data["colour"],
|
||||
1,
|
||||
)
|
||||
|
||||
|
||||
@@ -1236,32 +1294,42 @@ class TestDocumentApiV2(DirectoriesMixin, APITestCase):
|
||||
def test_tag_validate_color(self):
|
||||
self.assertEqual(
|
||||
self.client.post(
|
||||
"/api/tags/", {"name": "test", "color": "#12fFaA"}, format="json"
|
||||
"/api/tags/",
|
||||
{"name": "test", "color": "#12fFaA"},
|
||||
format="json",
|
||||
).status_code,
|
||||
201,
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
self.client.post(
|
||||
"/api/tags/", {"name": "test1", "color": "abcdef"}, format="json"
|
||||
"/api/tags/",
|
||||
{"name": "test1", "color": "abcdef"},
|
||||
format="json",
|
||||
).status_code,
|
||||
400,
|
||||
)
|
||||
self.assertEqual(
|
||||
self.client.post(
|
||||
"/api/tags/", {"name": "test2", "color": "#abcdfg"}, format="json"
|
||||
"/api/tags/",
|
||||
{"name": "test2", "color": "#abcdfg"},
|
||||
format="json",
|
||||
).status_code,
|
||||
400,
|
||||
)
|
||||
self.assertEqual(
|
||||
self.client.post(
|
||||
"/api/tags/", {"name": "test3", "color": "#asd"}, format="json"
|
||||
"/api/tags/",
|
||||
{"name": "test3", "color": "#asd"},
|
||||
format="json",
|
||||
).status_code,
|
||||
400,
|
||||
)
|
||||
self.assertEqual(
|
||||
self.client.post(
|
||||
"/api/tags/", {"name": "test4", "color": "#12121212"}, format="json"
|
||||
"/api/tags/",
|
||||
{"name": "test4", "color": "#12121212"},
|
||||
format="json",
|
||||
).status_code,
|
||||
400,
|
||||
)
|
||||
@@ -1313,10 +1381,16 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||
self.t2 = Tag.objects.create(name="t2")
|
||||
self.doc1 = Document.objects.create(checksum="A", title="A")
|
||||
self.doc2 = Document.objects.create(
|
||||
checksum="B", title="B", correspondent=self.c1, document_type=self.dt1
|
||||
checksum="B",
|
||||
title="B",
|
||||
correspondent=self.c1,
|
||||
document_type=self.dt1,
|
||||
)
|
||||
self.doc3 = Document.objects.create(
|
||||
checksum="C", title="C", correspondent=self.c2, document_type=self.dt2
|
||||
checksum="C",
|
||||
title="C",
|
||||
correspondent=self.c2,
|
||||
document_type=self.dt2,
|
||||
)
|
||||
self.doc4 = Document.objects.create(checksum="D", title="D")
|
||||
self.doc5 = Document.objects.create(checksum="E", title="E")
|
||||
@@ -1327,7 +1401,8 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||
def test_set_correspondent(self):
|
||||
self.assertEqual(Document.objects.filter(correspondent=self.c2).count(), 1)
|
||||
bulk_edit.set_correspondent(
|
||||
[self.doc1.id, self.doc2.id, self.doc3.id], self.c2.id
|
||||
[self.doc1.id, self.doc2.id, self.doc3.id],
|
||||
self.c2.id,
|
||||
)
|
||||
self.assertEqual(Document.objects.filter(correspondent=self.c2).count(), 3)
|
||||
self.async_task.assert_called_once()
|
||||
@@ -1345,7 +1420,8 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||
def test_set_document_type(self):
|
||||
self.assertEqual(Document.objects.filter(document_type=self.dt2).count(), 1)
|
||||
bulk_edit.set_document_type(
|
||||
[self.doc1.id, self.doc2.id, self.doc3.id], self.dt2.id
|
||||
[self.doc1.id, self.doc2.id, self.doc3.id],
|
||||
self.dt2.id,
|
||||
)
|
||||
self.assertEqual(Document.objects.filter(document_type=self.dt2).count(), 3)
|
||||
self.async_task.assert_called_once()
|
||||
@@ -1363,7 +1439,8 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||
def test_add_tag(self):
|
||||
self.assertEqual(Document.objects.filter(tags__id=self.t1.id).count(), 2)
|
||||
bulk_edit.add_tag(
|
||||
[self.doc1.id, self.doc2.id, self.doc3.id, self.doc4.id], self.t1.id
|
||||
[self.doc1.id, self.doc2.id, self.doc3.id, self.doc4.id],
|
||||
self.t1.id,
|
||||
)
|
||||
self.assertEqual(Document.objects.filter(tags__id=self.t1.id).count(), 4)
|
||||
self.async_task.assert_called_once()
|
||||
@@ -1415,7 +1492,7 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||
"documents": [self.doc1.id],
|
||||
"method": "set_correspondent",
|
||||
"parameters": {"correspondent": self.c1.id},
|
||||
}
|
||||
},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
@@ -1435,7 +1512,7 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||
"documents": [self.doc1.id],
|
||||
"method": "set_correspondent",
|
||||
"parameters": {"correspondent": None},
|
||||
}
|
||||
},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
@@ -1455,7 +1532,7 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||
"documents": [self.doc1.id],
|
||||
"method": "set_document_type",
|
||||
"parameters": {"document_type": self.dt1.id},
|
||||
}
|
||||
},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
@@ -1475,7 +1552,7 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||
"documents": [self.doc1.id],
|
||||
"method": "set_document_type",
|
||||
"parameters": {"document_type": None},
|
||||
}
|
||||
},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
@@ -1495,7 +1572,7 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||
"documents": [self.doc1.id],
|
||||
"method": "add_tag",
|
||||
"parameters": {"tag": self.t1.id},
|
||||
}
|
||||
},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
@@ -1515,7 +1592,7 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||
"documents": [self.doc1.id],
|
||||
"method": "remove_tag",
|
||||
"parameters": {"tag": self.t1.id},
|
||||
}
|
||||
},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
@@ -1538,7 +1615,7 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||
"add_tags": [self.t1.id],
|
||||
"remove_tags": [self.t2.id],
|
||||
},
|
||||
}
|
||||
},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
@@ -1555,7 +1632,7 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||
response = self.client.post(
|
||||
"/api/documents/bulk_edit/",
|
||||
json.dumps(
|
||||
{"documents": [self.doc1.id], "method": "delete", "parameters": {}}
|
||||
{"documents": [self.doc1.id], "method": "delete", "parameters": {}},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
@@ -1580,7 +1657,11 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||
response = self.client.post(
|
||||
"/api/documents/bulk_edit/",
|
||||
json.dumps(
|
||||
{"documents": [self.doc2.id], "method": "exterminate", "parameters": {}}
|
||||
{
|
||||
"documents": [self.doc2.id],
|
||||
"method": "exterminate",
|
||||
"parameters": {},
|
||||
},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
@@ -1596,7 +1677,7 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||
"documents": [self.doc2.id],
|
||||
"method": "set_correspondent",
|
||||
"parameters": {"correspondent": 345657},
|
||||
}
|
||||
},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
@@ -1613,7 +1694,7 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||
"documents": [self.doc2.id],
|
||||
"method": "set_correspondent",
|
||||
"parameters": {},
|
||||
}
|
||||
},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
@@ -1628,7 +1709,7 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||
"documents": [self.doc2.id],
|
||||
"method": "set_document_type",
|
||||
"parameters": {"document_type": 345657},
|
||||
}
|
||||
},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
@@ -1645,7 +1726,7 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||
"documents": [self.doc2.id],
|
||||
"method": "set_document_type",
|
||||
"parameters": {},
|
||||
}
|
||||
},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
@@ -1660,7 +1741,7 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||
"documents": [self.doc2.id],
|
||||
"method": "add_tag",
|
||||
"parameters": {"tag": 345657},
|
||||
}
|
||||
},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
@@ -1672,7 +1753,7 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||
response = self.client.post(
|
||||
"/api/documents/bulk_edit/",
|
||||
json.dumps(
|
||||
{"documents": [self.doc2.id], "method": "add_tag", "parameters": {}}
|
||||
{"documents": [self.doc2.id], "method": "add_tag", "parameters": {}},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
@@ -1687,7 +1768,7 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||
"documents": [self.doc2.id],
|
||||
"method": "remove_tag",
|
||||
"parameters": {"tag": 345657},
|
||||
}
|
||||
},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
@@ -1699,7 +1780,7 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||
response = self.client.post(
|
||||
"/api/documents/bulk_edit/",
|
||||
json.dumps(
|
||||
{"documents": [self.doc2.id], "method": "remove_tag", "parameters": {}}
|
||||
{"documents": [self.doc2.id], "method": "remove_tag", "parameters": {}},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
@@ -1717,7 +1798,7 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||
"add_tags": [self.t2.id, 1657],
|
||||
"remove_tags": [1123123],
|
||||
},
|
||||
}
|
||||
},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
@@ -1731,7 +1812,7 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||
"documents": [self.doc2.id],
|
||||
"method": "modify_tags",
|
||||
"parameters": {"remove_tags": [1123123]},
|
||||
}
|
||||
},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
@@ -1744,7 +1825,7 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||
"documents": [self.doc2.id],
|
||||
"method": "modify_tags",
|
||||
"parameters": {"add_tags": [self.t2.id, 1657]},
|
||||
}
|
||||
},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
@@ -1774,7 +1855,7 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||
response = self.client.post(
|
||||
"/api/documents/selection_data/",
|
||||
json.dumps(
|
||||
{"documents": [self.doc1.id, self.doc2.id, self.doc4.id, self.doc5.id]}
|
||||
{"documents": [self.doc1.id, self.doc2.id, self.doc4.id, self.doc5.id]},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
@@ -1856,7 +1937,7 @@ class TestBulkDownload(DirectoriesMixin, APITestCase):
|
||||
response = self.client.post(
|
||||
"/api/documents/bulk_download/",
|
||||
json.dumps(
|
||||
{"documents": [self.doc2.id, self.doc3.id], "content": "originals"}
|
||||
{"documents": [self.doc2.id, self.doc3.id], "content": "originals"},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
@@ -1914,17 +1995,20 @@ class TestBulkDownload(DirectoriesMixin, APITestCase):
|
||||
|
||||
with self.doc2.source_file as f:
|
||||
self.assertEqual(
|
||||
f.read(), zipf.read("originals/2021-01-01 document A.pdf")
|
||||
f.read(),
|
||||
zipf.read("originals/2021-01-01 document A.pdf"),
|
||||
)
|
||||
|
||||
with self.doc3.archive_file as f:
|
||||
self.assertEqual(
|
||||
f.read(), zipf.read("archive/2020-03-21 document B.pdf")
|
||||
f.read(),
|
||||
zipf.read("archive/2020-03-21 document B.pdf"),
|
||||
)
|
||||
|
||||
with self.doc3.source_file as f:
|
||||
self.assertEqual(
|
||||
f.read(), zipf.read("originals/2020-03-21 document B.jpg")
|
||||
f.read(),
|
||||
zipf.read("originals/2020-03-21 document B.jpg"),
|
||||
)
|
||||
|
||||
def test_filename_clashes(self):
|
||||
@@ -1953,7 +2037,7 @@ class TestBulkDownload(DirectoriesMixin, APITestCase):
|
||||
response = self.client.post(
|
||||
"/api/documents/bulk_download/",
|
||||
json.dumps(
|
||||
{"documents": [self.doc2.id, self.doc2b.id], "compression": "lzma"}
|
||||
{"documents": [self.doc2.id, self.doc2b.id], "compression": "lzma"},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
@@ -1968,13 +2052,16 @@ class TestApiAuth(APITestCase):
|
||||
|
||||
self.assertEqual(self.client.get(f"/api/documents/{d.id}/").status_code, 401)
|
||||
self.assertEqual(
|
||||
self.client.get(f"/api/documents/{d.id}/download/").status_code, 401
|
||||
self.client.get(f"/api/documents/{d.id}/download/").status_code,
|
||||
401,
|
||||
)
|
||||
self.assertEqual(
|
||||
self.client.get(f"/api/documents/{d.id}/preview/").status_code, 401
|
||||
self.client.get(f"/api/documents/{d.id}/preview/").status_code,
|
||||
401,
|
||||
)
|
||||
self.assertEqual(
|
||||
self.client.get(f"/api/documents/{d.id}/thumb/").status_code, 401
|
||||
self.client.get(f"/api/documents/{d.id}/thumb/").status_code,
|
||||
401,
|
||||
)
|
||||
|
||||
self.assertEqual(self.client.get("/api/tags/").status_code, 401)
|
||||
@@ -1987,10 +2074,12 @@ class TestApiAuth(APITestCase):
|
||||
self.assertEqual(self.client.get("/api/search/autocomplete/").status_code, 401)
|
||||
self.assertEqual(self.client.get("/api/documents/bulk_edit/").status_code, 401)
|
||||
self.assertEqual(
|
||||
self.client.get("/api/documents/bulk_download/").status_code, 401
|
||||
self.client.get("/api/documents/bulk_download/").status_code,
|
||||
401,
|
||||
)
|
||||
self.assertEqual(
|
||||
self.client.get("/api/documents/selection_data/").status_code, 401
|
||||
self.client.get("/api/documents/selection_data/").status_code,
|
||||
401,
|
||||
)
|
||||
|
||||
def test_api_version_no_auth(self):
|
||||
|
@@ -4,10 +4,11 @@ from unittest import mock
|
||||
from django.core.checks import Error
|
||||
from django.test import TestCase
|
||||
|
||||
from .factories import DocumentFactory
|
||||
from .. import document_consumer_declaration
|
||||
from ..checks import changed_password_check, parser_check
|
||||
from ..checks import changed_password_check
|
||||
from ..checks import parser_check
|
||||
from ..models import Document
|
||||
from ..signals import document_consumer_declaration
|
||||
from .factories import DocumentFactory
|
||||
|
||||
|
||||
class ChecksTestCase(TestCase):
|
||||
@@ -30,7 +31,7 @@ class ChecksTestCase(TestCase):
|
||||
[
|
||||
Error(
|
||||
"No parsers found. This is a bug. The consumer won't be "
|
||||
"able to consume any documents without parsers."
|
||||
)
|
||||
"able to consume any documents without parsers.",
|
||||
),
|
||||
],
|
||||
)
|
||||
|
@@ -5,14 +5,15 @@ from unittest import mock
|
||||
|
||||
import pytest
|
||||
from django.conf import settings
|
||||
from django.test import TestCase, override_settings
|
||||
|
||||
from documents.classifier import (
|
||||
DocumentClassifier,
|
||||
IncompatibleClassifierVersionError,
|
||||
load_classifier,
|
||||
)
|
||||
from documents.models import Correspondent, Document, Tag, DocumentType
|
||||
from django.test import override_settings
|
||||
from django.test import TestCase
|
||||
from documents.classifier import DocumentClassifier
|
||||
from documents.classifier import IncompatibleClassifierVersionError
|
||||
from documents.classifier import load_classifier
|
||||
from documents.models import Correspondent
|
||||
from documents.models import Document
|
||||
from documents.models import DocumentType
|
||||
from documents.models import Tag
|
||||
from documents.tests.utils import DirectoriesMixin
|
||||
|
||||
|
||||
@@ -23,26 +24,37 @@ class TestClassifier(DirectoriesMixin, TestCase):
|
||||
|
||||
def generate_test_data(self):
|
||||
self.c1 = Correspondent.objects.create(
|
||||
name="c1", matching_algorithm=Correspondent.MATCH_AUTO
|
||||
name="c1",
|
||||
matching_algorithm=Correspondent.MATCH_AUTO,
|
||||
)
|
||||
self.c2 = Correspondent.objects.create(name="c2")
|
||||
self.c3 = Correspondent.objects.create(
|
||||
name="c3", matching_algorithm=Correspondent.MATCH_AUTO
|
||||
name="c3",
|
||||
matching_algorithm=Correspondent.MATCH_AUTO,
|
||||
)
|
||||
self.t1 = Tag.objects.create(
|
||||
name="t1", matching_algorithm=Tag.MATCH_AUTO, pk=12
|
||||
name="t1",
|
||||
matching_algorithm=Tag.MATCH_AUTO,
|
||||
pk=12,
|
||||
)
|
||||
self.t2 = Tag.objects.create(
|
||||
name="t2", matching_algorithm=Tag.MATCH_ANY, pk=34, is_inbox_tag=True
|
||||
name="t2",
|
||||
matching_algorithm=Tag.MATCH_ANY,
|
||||
pk=34,
|
||||
is_inbox_tag=True,
|
||||
)
|
||||
self.t3 = Tag.objects.create(
|
||||
name="t3", matching_algorithm=Tag.MATCH_AUTO, pk=45
|
||||
name="t3",
|
||||
matching_algorithm=Tag.MATCH_AUTO,
|
||||
pk=45,
|
||||
)
|
||||
self.dt = DocumentType.objects.create(
|
||||
name="dt", matching_algorithm=DocumentType.MATCH_AUTO
|
||||
name="dt",
|
||||
matching_algorithm=DocumentType.MATCH_AUTO,
|
||||
)
|
||||
self.dt2 = DocumentType.objects.create(
|
||||
name="dt2", matching_algorithm=DocumentType.MATCH_AUTO
|
||||
name="dt2",
|
||||
matching_algorithm=DocumentType.MATCH_AUTO,
|
||||
)
|
||||
|
||||
self.doc1 = Document.objects.create(
|
||||
@@ -59,7 +71,9 @@ class TestClassifier(DirectoriesMixin, TestCase):
|
||||
checksum="B",
|
||||
)
|
||||
self.doc_inbox = Document.objects.create(
|
||||
title="doc235", content="aa", checksum="C"
|
||||
title="doc235",
|
||||
content="aa",
|
||||
checksum="C",
|
||||
)
|
||||
|
||||
self.doc1.tags.add(self.t1)
|
||||
@@ -90,27 +104,33 @@ class TestClassifier(DirectoriesMixin, TestCase):
|
||||
self.generate_test_data()
|
||||
self.classifier.train()
|
||||
self.assertListEqual(
|
||||
list(self.classifier.correspondent_classifier.classes_), [-1, self.c1.pk]
|
||||
list(self.classifier.correspondent_classifier.classes_),
|
||||
[-1, self.c1.pk],
|
||||
)
|
||||
self.assertListEqual(
|
||||
list(self.classifier.tags_binarizer.classes_), [self.t1.pk, self.t3.pk]
|
||||
list(self.classifier.tags_binarizer.classes_),
|
||||
[self.t1.pk, self.t3.pk],
|
||||
)
|
||||
|
||||
def testPredict(self):
|
||||
self.generate_test_data()
|
||||
self.classifier.train()
|
||||
self.assertEqual(
|
||||
self.classifier.predict_correspondent(self.doc1.content), self.c1.pk
|
||||
self.classifier.predict_correspondent(self.doc1.content),
|
||||
self.c1.pk,
|
||||
)
|
||||
self.assertEqual(self.classifier.predict_correspondent(self.doc2.content), None)
|
||||
self.assertListEqual(
|
||||
self.classifier.predict_tags(self.doc1.content), [self.t1.pk]
|
||||
self.classifier.predict_tags(self.doc1.content),
|
||||
[self.t1.pk],
|
||||
)
|
||||
self.assertListEqual(
|
||||
self.classifier.predict_tags(self.doc2.content), [self.t1.pk, self.t3.pk]
|
||||
self.classifier.predict_tags(self.doc2.content),
|
||||
[self.t1.pk, self.t3.pk],
|
||||
)
|
||||
self.assertEqual(
|
||||
self.classifier.predict_document_type(self.doc1.content), self.dt.pk
|
||||
self.classifier.predict_document_type(self.doc1.content),
|
||||
self.dt.pk,
|
||||
)
|
||||
self.assertEqual(self.classifier.predict_document_type(self.doc2.content), None)
|
||||
|
||||
@@ -133,7 +153,8 @@ class TestClassifier(DirectoriesMixin, TestCase):
|
||||
|
||||
current_ver = DocumentClassifier.FORMAT_VERSION
|
||||
with mock.patch(
|
||||
"documents.classifier.DocumentClassifier.FORMAT_VERSION", current_ver + 1
|
||||
"documents.classifier.DocumentClassifier.FORMAT_VERSION",
|
||||
current_ver + 1,
|
||||
):
|
||||
# assure that we won't load old classifiers.
|
||||
self.assertRaises(IncompatibleClassifierVersionError, classifier2.load)
|
||||
@@ -157,7 +178,7 @@ class TestClassifier(DirectoriesMixin, TestCase):
|
||||
self.assertFalse(new_classifier.train())
|
||||
|
||||
@override_settings(
|
||||
MODEL_FILE=os.path.join(os.path.dirname(__file__), "data", "model.pickle")
|
||||
MODEL_FILE=os.path.join(os.path.dirname(__file__), "data", "model.pickle"),
|
||||
)
|
||||
def test_load_and_classify(self):
|
||||
self.generate_test_data()
|
||||
@@ -169,7 +190,8 @@ class TestClassifier(DirectoriesMixin, TestCase):
|
||||
|
||||
def test_one_correspondent_predict(self):
|
||||
c1 = Correspondent.objects.create(
|
||||
name="c1", matching_algorithm=Correspondent.MATCH_AUTO
|
||||
name="c1",
|
||||
matching_algorithm=Correspondent.MATCH_AUTO,
|
||||
)
|
||||
doc1 = Document.objects.create(
|
||||
title="doc1",
|
||||
@@ -183,7 +205,8 @@ class TestClassifier(DirectoriesMixin, TestCase):
|
||||
|
||||
def test_one_correspondent_predict_manydocs(self):
|
||||
c1 = Correspondent.objects.create(
|
||||
name="c1", matching_algorithm=Correspondent.MATCH_AUTO
|
||||
name="c1",
|
||||
matching_algorithm=Correspondent.MATCH_AUTO,
|
||||
)
|
||||
doc1 = Document.objects.create(
|
||||
title="doc1",
|
||||
@@ -192,7 +215,9 @@ class TestClassifier(DirectoriesMixin, TestCase):
|
||||
checksum="A",
|
||||
)
|
||||
doc2 = Document.objects.create(
|
||||
title="doc2", content="this is a document from noone", checksum="B"
|
||||
title="doc2",
|
||||
content="this is a document from noone",
|
||||
checksum="B",
|
||||
)
|
||||
|
||||
self.classifier.train()
|
||||
@@ -201,7 +226,8 @@ class TestClassifier(DirectoriesMixin, TestCase):
|
||||
|
||||
def test_one_type_predict(self):
|
||||
dt = DocumentType.objects.create(
|
||||
name="dt", matching_algorithm=DocumentType.MATCH_AUTO
|
||||
name="dt",
|
||||
matching_algorithm=DocumentType.MATCH_AUTO,
|
||||
)
|
||||
|
||||
doc1 = Document.objects.create(
|
||||
@@ -216,7 +242,8 @@ class TestClassifier(DirectoriesMixin, TestCase):
|
||||
|
||||
def test_one_type_predict_manydocs(self):
|
||||
dt = DocumentType.objects.create(
|
||||
name="dt", matching_algorithm=DocumentType.MATCH_AUTO
|
||||
name="dt",
|
||||
matching_algorithm=DocumentType.MATCH_AUTO,
|
||||
)
|
||||
|
||||
doc1 = Document.objects.create(
|
||||
@@ -227,7 +254,9 @@ class TestClassifier(DirectoriesMixin, TestCase):
|
||||
)
|
||||
|
||||
doc2 = Document.objects.create(
|
||||
title="doc1", content="this is a document from c2", checksum="B"
|
||||
title="doc1",
|
||||
content="this is a document from c2",
|
||||
checksum="B",
|
||||
)
|
||||
|
||||
self.classifier.train()
|
||||
@@ -238,7 +267,9 @@ class TestClassifier(DirectoriesMixin, TestCase):
|
||||
t1 = Tag.objects.create(name="t1", matching_algorithm=Tag.MATCH_AUTO, pk=12)
|
||||
|
||||
doc1 = Document.objects.create(
|
||||
title="doc1", content="this is a document from c1", checksum="A"
|
||||
title="doc1",
|
||||
content="this is a document from c1",
|
||||
checksum="A",
|
||||
)
|
||||
|
||||
doc1.tags.add(t1)
|
||||
@@ -249,7 +280,9 @@ class TestClassifier(DirectoriesMixin, TestCase):
|
||||
t1 = Tag.objects.create(name="t1", matching_algorithm=Tag.MATCH_AUTO, pk=12)
|
||||
|
||||
doc1 = Document.objects.create(
|
||||
title="doc1", content="this is a document from c1", checksum="A"
|
||||
title="doc1",
|
||||
content="this is a document from c1",
|
||||
checksum="A",
|
||||
)
|
||||
|
||||
self.classifier.train()
|
||||
@@ -260,7 +293,9 @@ class TestClassifier(DirectoriesMixin, TestCase):
|
||||
t2 = Tag.objects.create(name="t2", matching_algorithm=Tag.MATCH_AUTO, pk=121)
|
||||
|
||||
doc4 = Document.objects.create(
|
||||
title="doc1", content="this is a document from c4", checksum="D"
|
||||
title="doc1",
|
||||
content="this is a document from c4",
|
||||
checksum="D",
|
||||
)
|
||||
|
||||
doc4.tags.add(t1)
|
||||
@@ -273,16 +308,24 @@ class TestClassifier(DirectoriesMixin, TestCase):
|
||||
t2 = Tag.objects.create(name="t2", matching_algorithm=Tag.MATCH_AUTO, pk=121)
|
||||
|
||||
doc1 = Document.objects.create(
|
||||
title="doc1", content="this is a document from c1", checksum="A"
|
||||
title="doc1",
|
||||
content="this is a document from c1",
|
||||
checksum="A",
|
||||
)
|
||||
doc2 = Document.objects.create(
|
||||
title="doc1", content="this is a document from c2", checksum="B"
|
||||
title="doc1",
|
||||
content="this is a document from c2",
|
||||
checksum="B",
|
||||
)
|
||||
doc3 = Document.objects.create(
|
||||
title="doc1", content="this is a document from c3", checksum="C"
|
||||
title="doc1",
|
||||
content="this is a document from c3",
|
||||
checksum="C",
|
||||
)
|
||||
doc4 = Document.objects.create(
|
||||
title="doc1", content="this is a document from c4", checksum="D"
|
||||
title="doc1",
|
||||
content="this is a document from c4",
|
||||
checksum="D",
|
||||
)
|
||||
|
||||
doc1.tags.add(t1)
|
||||
@@ -300,10 +343,14 @@ class TestClassifier(DirectoriesMixin, TestCase):
|
||||
t1 = Tag.objects.create(name="t1", matching_algorithm=Tag.MATCH_AUTO, pk=12)
|
||||
|
||||
doc1 = Document.objects.create(
|
||||
title="doc1", content="this is a document from c1", checksum="A"
|
||||
title="doc1",
|
||||
content="this is a document from c1",
|
||||
checksum="A",
|
||||
)
|
||||
doc2 = Document.objects.create(
|
||||
title="doc2", content="this is a document from c2", checksum="B"
|
||||
title="doc2",
|
||||
content="this is a document from c2",
|
||||
checksum="B",
|
||||
)
|
||||
|
||||
doc1.tags.add(t1)
|
||||
@@ -316,10 +363,14 @@ class TestClassifier(DirectoriesMixin, TestCase):
|
||||
t1 = Tag.objects.create(name="t1", matching_algorithm=Tag.MATCH_AUTO, pk=12)
|
||||
|
||||
doc1 = Document.objects.create(
|
||||
title="doc1", content="this is a document from c1", checksum="A"
|
||||
title="doc1",
|
||||
content="this is a document from c1",
|
||||
checksum="A",
|
||||
)
|
||||
doc2 = Document.objects.create(
|
||||
title="doc2", content="this is a document from c2", checksum="B"
|
||||
title="doc2",
|
||||
content="this is a document from c2",
|
||||
checksum="B",
|
||||
)
|
||||
|
||||
doc1.tags.add(t1)
|
||||
@@ -338,13 +389,15 @@ class TestClassifier(DirectoriesMixin, TestCase):
|
||||
load.assert_called_once()
|
||||
|
||||
@override_settings(
|
||||
CACHES={"default": {"BACKEND": "django.core.cache.backends.locmem.LocMemCache"}}
|
||||
CACHES={
|
||||
"default": {"BACKEND": "django.core.cache.backends.locmem.LocMemCache"},
|
||||
},
|
||||
)
|
||||
@override_settings(
|
||||
MODEL_FILE=os.path.join(os.path.dirname(__file__), "data", "model.pickle")
|
||||
MODEL_FILE=os.path.join(os.path.dirname(__file__), "data", "model.pickle"),
|
||||
)
|
||||
@pytest.mark.skip(
|
||||
reason="Disabled caching due to high memory usage - need to investigate."
|
||||
reason="Disabled caching due to high memory usage - need to investigate.",
|
||||
)
|
||||
def test_load_classifier_cached(self):
|
||||
classifier = load_classifier()
|
||||
|
@@ -6,13 +6,20 @@ from unittest import mock
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from django.conf import settings
|
||||
from django.test import TestCase, override_settings
|
||||
from django.test import override_settings
|
||||
from django.test import TestCase
|
||||
|
||||
from .utils import DirectoriesMixin
|
||||
from ..consumer import Consumer, ConsumerError
|
||||
from ..models import FileInfo, Tag, Correspondent, DocumentType, Document
|
||||
from ..parsers import DocumentParser, ParseError
|
||||
from ..consumer import Consumer
|
||||
from ..consumer import ConsumerError
|
||||
from ..models import Correspondent
|
||||
from ..models import Document
|
||||
from ..models import DocumentType
|
||||
from ..models import FileInfo
|
||||
from ..models import Tag
|
||||
from ..parsers import DocumentParser
|
||||
from ..parsers import ParseError
|
||||
from ..tasks import sanity_check
|
||||
from .utils import DirectoriesMixin
|
||||
|
||||
|
||||
class TestAttributes(TestCase):
|
||||
@@ -33,12 +40,18 @@ class TestAttributes(TestCase):
|
||||
|
||||
def test_guess_attributes_from_name_when_title_starts_with_dash(self):
|
||||
self._test_guess_attributes_from_name(
|
||||
"- weird but should not break.pdf", None, "- weird but should not break", ()
|
||||
"- weird but should not break.pdf",
|
||||
None,
|
||||
"- weird but should not break",
|
||||
(),
|
||||
)
|
||||
|
||||
def test_guess_attributes_from_name_when_title_ends_with_dash(self):
|
||||
self._test_guess_attributes_from_name(
|
||||
"weird but should not break -.pdf", None, "weird but should not break -", ()
|
||||
"weird but should not break -.pdf",
|
||||
None,
|
||||
"weird but should not break -",
|
||||
(),
|
||||
)
|
||||
|
||||
|
||||
@@ -53,7 +66,12 @@ class TestFieldPermutations(TestCase):
|
||||
valid_tags = ["tag", "tig,tag", "tag1,tag2,tag-3"]
|
||||
|
||||
def _test_guessed_attributes(
|
||||
self, filename, created=None, correspondent=None, title=None, tags=None
|
||||
self,
|
||||
filename,
|
||||
created=None,
|
||||
correspondent=None,
|
||||
title=None,
|
||||
tags=None,
|
||||
):
|
||||
|
||||
info = FileInfo.from_filename(filename)
|
||||
@@ -131,7 +149,7 @@ class TestFieldPermutations(TestCase):
|
||||
FILENAME_PARSE_TRANSFORMS=[
|
||||
(all_patt, "all.gif"),
|
||||
(all_patt, "anotherall.gif"),
|
||||
]
|
||||
],
|
||||
):
|
||||
info = FileInfo.from_filename(filename)
|
||||
self.assertEqual(info.title, "all")
|
||||
@@ -141,7 +159,7 @@ class TestFieldPermutations(TestCase):
|
||||
FILENAME_PARSE_TRANSFORMS=[
|
||||
(none_patt, "none.gif"),
|
||||
(all_patt, "anotherall.gif"),
|
||||
]
|
||||
],
|
||||
):
|
||||
info = FileInfo.from_filename(filename)
|
||||
self.assertEqual(info.title, "anotherall")
|
||||
@@ -238,7 +256,9 @@ class TestConsumer(DirectoriesMixin, TestCase):
|
||||
|
||||
def make_dummy_parser(self, logging_group, progress_callback=None):
|
||||
return DummyParser(
|
||||
logging_group, self.dirs.scratch_dir, self.get_test_archive_file()
|
||||
logging_group,
|
||||
self.dirs.scratch_dir,
|
||||
self.get_test_archive_file(),
|
||||
)
|
||||
|
||||
def make_faulty_parser(self, logging_group, progress_callback=None):
|
||||
@@ -257,7 +277,7 @@ class TestConsumer(DirectoriesMixin, TestCase):
|
||||
"mime_types": {"application/pdf": ".pdf"},
|
||||
"weight": 0,
|
||||
},
|
||||
)
|
||||
),
|
||||
]
|
||||
self.addCleanup(patcher.stop)
|
||||
|
||||
@@ -282,7 +302,11 @@ class TestConsumer(DirectoriesMixin, TestCase):
|
||||
|
||||
def get_test_archive_file(self):
|
||||
src = os.path.join(
|
||||
os.path.dirname(__file__), "samples", "documents", "archive", "0000001.pdf"
|
||||
os.path.dirname(__file__),
|
||||
"samples",
|
||||
"documents",
|
||||
"archive",
|
||||
"0000001.pdf",
|
||||
)
|
||||
dst = os.path.join(self.dirs.scratch_dir, "sample_archive.pdf")
|
||||
shutil.copy(src, dst)
|
||||
@@ -296,7 +320,8 @@ class TestConsumer(DirectoriesMixin, TestCase):
|
||||
|
||||
self.assertEqual(document.content, "The Text")
|
||||
self.assertEqual(
|
||||
document.title, os.path.splitext(os.path.basename(filename))[0]
|
||||
document.title,
|
||||
os.path.splitext(os.path.basename(filename))[0],
|
||||
)
|
||||
self.assertIsNone(document.correspondent)
|
||||
self.assertIsNone(document.document_type)
|
||||
@@ -339,7 +364,8 @@ class TestConsumer(DirectoriesMixin, TestCase):
|
||||
override_filename = "Statement for November.pdf"
|
||||
|
||||
document = self.consumer.try_consume_file(
|
||||
filename, override_filename=override_filename
|
||||
filename,
|
||||
override_filename=override_filename,
|
||||
)
|
||||
|
||||
self.assertEqual(document.title, "Statement for November")
|
||||
@@ -348,7 +374,8 @@ class TestConsumer(DirectoriesMixin, TestCase):
|
||||
|
||||
def testOverrideTitle(self):
|
||||
document = self.consumer.try_consume_file(
|
||||
self.get_test_file(), override_title="Override Title"
|
||||
self.get_test_file(),
|
||||
override_title="Override Title",
|
||||
)
|
||||
self.assertEqual(document.title, "Override Title")
|
||||
self._assert_first_last_send_progress()
|
||||
@@ -357,7 +384,8 @@ class TestConsumer(DirectoriesMixin, TestCase):
|
||||
c = Correspondent.objects.create(name="test")
|
||||
|
||||
document = self.consumer.try_consume_file(
|
||||
self.get_test_file(), override_correspondent_id=c.pk
|
||||
self.get_test_file(),
|
||||
override_correspondent_id=c.pk,
|
||||
)
|
||||
self.assertEqual(document.correspondent.id, c.id)
|
||||
self._assert_first_last_send_progress()
|
||||
@@ -366,7 +394,8 @@ class TestConsumer(DirectoriesMixin, TestCase):
|
||||
dt = DocumentType.objects.create(name="test")
|
||||
|
||||
document = self.consumer.try_consume_file(
|
||||
self.get_test_file(), override_document_type_id=dt.pk
|
||||
self.get_test_file(),
|
||||
override_document_type_id=dt.pk,
|
||||
)
|
||||
self.assertEqual(document.document_type.id, dt.id)
|
||||
self._assert_first_last_send_progress()
|
||||
@@ -376,7 +405,8 @@ class TestConsumer(DirectoriesMixin, TestCase):
|
||||
t2 = Tag.objects.create(name="t2")
|
||||
t3 = Tag.objects.create(name="t3")
|
||||
document = self.consumer.try_consume_file(
|
||||
self.get_test_file(), override_tag_ids=[t1.id, t3.id]
|
||||
self.get_test_file(),
|
||||
override_tag_ids=[t1.id, t3.id],
|
||||
)
|
||||
|
||||
self.assertIn(t1, document.tags.all())
|
||||
@@ -446,7 +476,7 @@ class TestConsumer(DirectoriesMixin, TestCase):
|
||||
"mime_types": {"application/pdf": ".pdf"},
|
||||
"weight": 0,
|
||||
},
|
||||
)
|
||||
),
|
||||
]
|
||||
|
||||
self.assertRaisesMessage(
|
||||
@@ -595,16 +625,16 @@ class TestConsumer(DirectoriesMixin, TestCase):
|
||||
"mime_types": {"application/pdf": ".pdf", "image/png": ".png"},
|
||||
"weight": 0,
|
||||
},
|
||||
)
|
||||
),
|
||||
]
|
||||
doc1 = self.consumer.try_consume_file(
|
||||
os.path.join(settings.CONSUMPTION_DIR, "simple.png")
|
||||
os.path.join(settings.CONSUMPTION_DIR, "simple.png"),
|
||||
)
|
||||
doc2 = self.consumer.try_consume_file(
|
||||
os.path.join(settings.CONSUMPTION_DIR, "simple.pdf")
|
||||
os.path.join(settings.CONSUMPTION_DIR, "simple.pdf"),
|
||||
)
|
||||
doc3 = self.consumer.try_consume_file(
|
||||
os.path.join(settings.CONSUMPTION_DIR, "simple.png.pdf")
|
||||
os.path.join(settings.CONSUMPTION_DIR, "simple.png.pdf"),
|
||||
)
|
||||
|
||||
self.assertEqual(doc1.filename, "simple.png")
|
||||
@@ -691,7 +721,9 @@ class PostConsumeTestCase(TestCase):
|
||||
with override_settings(POST_CONSUME_SCRIPT=script.name):
|
||||
c = Correspondent.objects.create(name="my_bank")
|
||||
doc = Document.objects.create(
|
||||
title="Test", mime_type="application/pdf", correspondent=c
|
||||
title="Test",
|
||||
mime_type="application/pdf",
|
||||
correspondent=c,
|
||||
)
|
||||
tag1 = Tag.objects.create(name="a")
|
||||
tag2 = Tag.objects.create(name="b")
|
||||
|
@@ -5,15 +5,16 @@ from uuid import uuid4
|
||||
|
||||
from dateutil import tz
|
||||
from django.conf import settings
|
||||
from django.test import TestCase, override_settings
|
||||
|
||||
from django.test import override_settings
|
||||
from django.test import TestCase
|
||||
from documents.parsers import parse_date
|
||||
|
||||
|
||||
class TestDate(TestCase):
|
||||
|
||||
SAMPLE_FILES = os.path.join(
|
||||
os.path.dirname(__file__), "../../paperless_tesseract/tests/samples"
|
||||
os.path.dirname(__file__),
|
||||
"../../paperless_tesseract/tests/samples",
|
||||
)
|
||||
SCRATCH = "/tmp/paperless-tests-{}".format(str(uuid4())[:8])
|
||||
|
||||
@@ -111,11 +112,11 @@ class TestDate(TestCase):
|
||||
@override_settings(FILENAME_DATE_ORDER="YMD")
|
||||
def test_filename_date_parse_invalid(self, *args):
|
||||
self.assertIsNone(
|
||||
parse_date("/tmp/20 408000l 2475 - test.pdf", "No date in here")
|
||||
parse_date("/tmp/20 408000l 2475 - test.pdf", "No date in here"),
|
||||
)
|
||||
|
||||
@override_settings(
|
||||
IGNORE_DATES=(datetime.date(2019, 11, 3), datetime.date(2020, 1, 17))
|
||||
IGNORE_DATES=(datetime.date(2019, 11, 3), datetime.date(2020, 1, 17)),
|
||||
)
|
||||
def test_ignored_dates(self, *args):
|
||||
text = "lorem ipsum 110319, 20200117 and lorem 13.02.2018 lorem " "ipsum"
|
||||
|
@@ -3,10 +3,12 @@ import tempfile
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
from django.test import TestCase, override_settings
|
||||
from django.test import override_settings
|
||||
from django.test import TestCase
|
||||
from django.utils import timezone
|
||||
|
||||
from ..models import Document, Correspondent
|
||||
from ..models import Correspondent
|
||||
from ..models import Document
|
||||
|
||||
|
||||
class TestDocument(TestCase):
|
||||
|
@@ -9,17 +9,19 @@ from unittest import mock
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import DatabaseError
|
||||
from django.test import TestCase, override_settings
|
||||
from django.test import override_settings
|
||||
from django.test import TestCase
|
||||
from django.utils import timezone
|
||||
|
||||
from ..file_handling import create_source_path_directory
|
||||
from ..file_handling import delete_empty_directories
|
||||
from ..file_handling import generate_filename
|
||||
from ..file_handling import generate_unique_filename
|
||||
from ..models import Correspondent
|
||||
from ..models import Document
|
||||
from ..models import DocumentType
|
||||
from ..models import Tag
|
||||
from .utils import DirectoriesMixin
|
||||
from ..file_handling import (
|
||||
generate_filename,
|
||||
create_source_path_directory,
|
||||
delete_empty_directories,
|
||||
generate_unique_filename,
|
||||
)
|
||||
from ..models import Document, Correspondent, Tag, DocumentType
|
||||
|
||||
|
||||
class TestFileHandling(DirectoriesMixin, TestCase):
|
||||
@@ -34,7 +36,8 @@ class TestFileHandling(DirectoriesMixin, TestCase):
|
||||
|
||||
document.storage_type = Document.STORAGE_TYPE_GPG
|
||||
self.assertEqual(
|
||||
generate_filename(document), "{:07d}.pdf.gpg".format(document.pk)
|
||||
generate_filename(document),
|
||||
"{:07d}.pdf.gpg".format(document.pk),
|
||||
)
|
||||
|
||||
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}")
|
||||
@@ -75,7 +78,8 @@ class TestFileHandling(DirectoriesMixin, TestCase):
|
||||
self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/test"), True)
|
||||
self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/none"), False)
|
||||
self.assertEqual(
|
||||
os.path.isfile(settings.ORIGINALS_DIR + "/test/test.pdf.gpg"), True
|
||||
os.path.isfile(settings.ORIGINALS_DIR + "/test/test.pdf.gpg"),
|
||||
True,
|
||||
)
|
||||
|
||||
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}")
|
||||
@@ -93,7 +97,8 @@ class TestFileHandling(DirectoriesMixin, TestCase):
|
||||
|
||||
# Test source_path
|
||||
self.assertEqual(
|
||||
document.source_path, settings.ORIGINALS_DIR + "/none/none.pdf"
|
||||
document.source_path,
|
||||
settings.ORIGINALS_DIR + "/none/none.pdf",
|
||||
)
|
||||
|
||||
# Make the folder read- and execute-only (no writing and no renaming)
|
||||
@@ -105,7 +110,8 @@ class TestFileHandling(DirectoriesMixin, TestCase):
|
||||
|
||||
# Check proper handling of files
|
||||
self.assertEqual(
|
||||
os.path.isfile(settings.ORIGINALS_DIR + "/none/none.pdf"), True
|
||||
os.path.isfile(settings.ORIGINALS_DIR + "/none/none.pdf"),
|
||||
True,
|
||||
)
|
||||
self.assertEqual(document.filename, "none/none.pdf")
|
||||
|
||||
@@ -145,7 +151,8 @@ class TestFileHandling(DirectoriesMixin, TestCase):
|
||||
# Check proper handling of files
|
||||
self.assertTrue(os.path.isfile(document.source_path))
|
||||
self.assertEqual(
|
||||
os.path.isfile(settings.ORIGINALS_DIR + "/none/none.pdf"), True
|
||||
os.path.isfile(settings.ORIGINALS_DIR + "/none/none.pdf"),
|
||||
True,
|
||||
)
|
||||
self.assertEqual(document.filename, "none/none.pdf")
|
||||
|
||||
@@ -167,7 +174,8 @@ class TestFileHandling(DirectoriesMixin, TestCase):
|
||||
pk = document.pk
|
||||
document.delete()
|
||||
self.assertEqual(
|
||||
os.path.isfile(settings.ORIGINALS_DIR + "/none/none.pdf"), False
|
||||
os.path.isfile(settings.ORIGINALS_DIR + "/none/none.pdf"),
|
||||
False,
|
||||
)
|
||||
self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/none"), False)
|
||||
|
||||
@@ -192,7 +200,8 @@ class TestFileHandling(DirectoriesMixin, TestCase):
|
||||
self.assertEqual(os.path.isfile(settings.TRASH_DIR + "/none/none.pdf"), False)
|
||||
document.delete()
|
||||
self.assertEqual(
|
||||
os.path.isfile(settings.ORIGINALS_DIR + "/none/none.pdf"), False
|
||||
os.path.isfile(settings.ORIGINALS_DIR + "/none/none.pdf"),
|
||||
False,
|
||||
)
|
||||
self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/none"), False)
|
||||
self.assertEqual(os.path.isfile(settings.TRASH_DIR + "/none.pdf"), True)
|
||||
@@ -363,7 +372,9 @@ class TestFileHandling(DirectoriesMixin, TestCase):
|
||||
self.assertEqual(generate_filename(doc), "doc1 tag1,tag2.pdf")
|
||||
|
||||
doc = Document.objects.create(
|
||||
title="doc2", checksum="B", mime_type="application/pdf"
|
||||
title="doc2",
|
||||
checksum="B",
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
|
||||
self.assertEqual(generate_filename(doc), "doc2.pdf")
|
||||
@@ -380,12 +391,14 @@ class TestFileHandling(DirectoriesMixin, TestCase):
|
||||
)
|
||||
|
||||
@override_settings(
|
||||
PAPERLESS_FILENAME_FORMAT="{created_year}-{created_month}-{created_day}"
|
||||
PAPERLESS_FILENAME_FORMAT="{created_year}-{created_month}-{created_day}",
|
||||
)
|
||||
def test_created_year_month_day(self):
|
||||
d1 = timezone.make_aware(datetime.datetime(2020, 3, 6, 1, 1, 1))
|
||||
doc1 = Document.objects.create(
|
||||
title="doc1", mime_type="application/pdf", created=d1
|
||||
title="doc1",
|
||||
mime_type="application/pdf",
|
||||
created=d1,
|
||||
)
|
||||
|
||||
self.assertEqual(generate_filename(doc1), "2020-03-06.pdf")
|
||||
@@ -395,12 +408,14 @@ class TestFileHandling(DirectoriesMixin, TestCase):
|
||||
self.assertEqual(generate_filename(doc1), "2020-11-16.pdf")
|
||||
|
||||
@override_settings(
|
||||
PAPERLESS_FILENAME_FORMAT="{added_year}-{added_month}-{added_day}"
|
||||
PAPERLESS_FILENAME_FORMAT="{added_year}-{added_month}-{added_day}",
|
||||
)
|
||||
def test_added_year_month_day(self):
|
||||
d1 = timezone.make_aware(datetime.datetime(232, 1, 9, 1, 1, 1))
|
||||
doc1 = Document.objects.create(
|
||||
title="doc1", mime_type="application/pdf", added=d1
|
||||
title="doc1",
|
||||
mime_type="application/pdf",
|
||||
added=d1,
|
||||
)
|
||||
|
||||
self.assertEqual(generate_filename(doc1), "232-01-09.pdf")
|
||||
@@ -410,7 +425,7 @@ class TestFileHandling(DirectoriesMixin, TestCase):
|
||||
self.assertEqual(generate_filename(doc1), "2020-11-16.pdf")
|
||||
|
||||
@override_settings(
|
||||
PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}/{correspondent}"
|
||||
PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}/{correspondent}",
|
||||
)
|
||||
def test_nested_directory_cleanup(self):
|
||||
document = Document()
|
||||
@@ -431,7 +446,8 @@ class TestFileHandling(DirectoriesMixin, TestCase):
|
||||
document.delete()
|
||||
|
||||
self.assertEqual(
|
||||
os.path.isfile(settings.ORIGINALS_DIR + "/none/none/none.pdf"), False
|
||||
os.path.isfile(settings.ORIGINALS_DIR + "/none/none/none.pdf"),
|
||||
False,
|
||||
)
|
||||
self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/none/none"), False)
|
||||
self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/none"), False)
|
||||
@@ -456,7 +472,8 @@ class TestFileHandling(DirectoriesMixin, TestCase):
|
||||
os.makedirs(os.path.join(tmp, "notempty", "empty"))
|
||||
|
||||
delete_empty_directories(
|
||||
os.path.join(tmp, "notempty", "empty"), root=settings.ORIGINALS_DIR
|
||||
os.path.join(tmp, "notempty", "empty"),
|
||||
root=settings.ORIGINALS_DIR,
|
||||
)
|
||||
self.assertEqual(os.path.isdir(os.path.join(tmp, "notempty")), True)
|
||||
self.assertEqual(os.path.isfile(os.path.join(tmp, "notempty", "file")), True)
|
||||
@@ -483,10 +500,16 @@ class TestFileHandling(DirectoriesMixin, TestCase):
|
||||
@override_settings(PAPERLESS_FILENAME_FORMAT="{title}")
|
||||
def test_duplicates(self):
|
||||
document = Document.objects.create(
|
||||
mime_type="application/pdf", title="qwe", checksum="A", pk=1
|
||||
mime_type="application/pdf",
|
||||
title="qwe",
|
||||
checksum="A",
|
||||
pk=1,
|
||||
)
|
||||
document2 = Document.objects.create(
|
||||
mime_type="application/pdf", title="qwe", checksum="B", pk=2
|
||||
mime_type="application/pdf",
|
||||
title="qwe",
|
||||
checksum="B",
|
||||
pk=2,
|
||||
)
|
||||
Path(document.source_path).touch()
|
||||
Path(document2.source_path).touch()
|
||||
@@ -584,10 +607,12 @@ class TestFileHandlingWithArchive(DirectoriesMixin, TestCase):
|
||||
self.assertTrue(os.path.isfile(doc.source_path))
|
||||
self.assertTrue(os.path.isfile(doc.archive_path))
|
||||
self.assertEqual(
|
||||
doc.source_path, os.path.join(settings.ORIGINALS_DIR, "none", "my_doc.pdf")
|
||||
doc.source_path,
|
||||
os.path.join(settings.ORIGINALS_DIR, "none", "my_doc.pdf"),
|
||||
)
|
||||
self.assertEqual(
|
||||
doc.archive_path, os.path.join(settings.ARCHIVE_DIR, "none", "my_doc.pdf")
|
||||
doc.archive_path,
|
||||
os.path.join(settings.ARCHIVE_DIR, "none", "my_doc.pdf"),
|
||||
)
|
||||
|
||||
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{title}")
|
||||
@@ -851,7 +876,10 @@ class TestFilenameGeneration(TestCase):
|
||||
def test_invalid_characters(self):
|
||||
|
||||
doc = Document.objects.create(
|
||||
title="This. is the title.", mime_type="application/pdf", pk=1, checksum="1"
|
||||
title="This. is the title.",
|
||||
mime_type="application/pdf",
|
||||
pk=1,
|
||||
checksum="1",
|
||||
)
|
||||
self.assertEqual(generate_filename(doc), "This. is the title.pdf")
|
||||
|
||||
@@ -877,7 +905,9 @@ class TestFilenameGeneration(TestCase):
|
||||
|
||||
def run():
|
||||
doc = Document.objects.create(
|
||||
checksum=str(uuid.uuid4()), title=str(uuid.uuid4()), content="wow"
|
||||
checksum=str(uuid.uuid4()),
|
||||
title=str(uuid.uuid4()),
|
||||
content="wow",
|
||||
)
|
||||
doc.filename = generate_unique_filename(doc)
|
||||
Path(doc.thumbnail_path).touch()
|
||||
|
@@ -1,7 +1,7 @@
|
||||
from django.core.management.base import CommandError
|
||||
from django.test import TestCase
|
||||
|
||||
from documents.settings import EXPORTER_FILE_NAME
|
||||
|
||||
from ..management.commands.document_importer import Command
|
||||
|
||||
|
||||
@@ -12,7 +12,9 @@ class TestImporter(TestCase):
|
||||
def test_check_manifest_exists(self):
|
||||
cmd = Command()
|
||||
self.assertRaises(
|
||||
CommandError, cmd._check_manifest_exists, "/tmp/manifest.json"
|
||||
CommandError,
|
||||
cmd._check_manifest_exists,
|
||||
"/tmp/manifest.json",
|
||||
)
|
||||
|
||||
def test_check_manifest(self):
|
||||
@@ -26,11 +28,11 @@ class TestImporter(TestCase):
|
||||
self.assertTrue("The manifest file contains a record" in str(cm.exception))
|
||||
|
||||
cmd.manifest = [
|
||||
{"model": "documents.document", EXPORTER_FILE_NAME: "noexist.pdf"}
|
||||
{"model": "documents.document", EXPORTER_FILE_NAME: "noexist.pdf"},
|
||||
]
|
||||
# self.assertRaises(CommandError, cmd._check_manifest)
|
||||
with self.assertRaises(CommandError) as cm:
|
||||
cmd._check_manifest()
|
||||
self.assertTrue(
|
||||
'The manifest file refers to "noexist.pdf"' in str(cm.exception)
|
||||
'The manifest file refers to "noexist.pdf"' in str(cm.exception),
|
||||
)
|
||||
|
@@ -1,5 +1,4 @@
|
||||
from django.test import TestCase
|
||||
|
||||
from documents import index
|
||||
from documents.models import Document
|
||||
from documents.tests.utils import DirectoriesMixin
|
||||
@@ -9,7 +8,9 @@ class TestAutoComplete(DirectoriesMixin, TestCase):
|
||||
def test_auto_complete(self):
|
||||
|
||||
doc1 = Document.objects.create(
|
||||
title="doc1", checksum="A", content="test test2 test3"
|
||||
title="doc1",
|
||||
checksum="A",
|
||||
content="test test2 test3",
|
||||
)
|
||||
doc2 = Document.objects.create(title="doc2", checksum="B", content="test test2")
|
||||
doc3 = Document.objects.create(title="doc3", checksum="C", content="test2")
|
||||
@@ -21,10 +22,12 @@ class TestAutoComplete(DirectoriesMixin, TestCase):
|
||||
ix = index.open_index()
|
||||
|
||||
self.assertListEqual(
|
||||
index.autocomplete(ix, "tes"), [b"test3", b"test", b"test2"]
|
||||
index.autocomplete(ix, "tes"),
|
||||
[b"test3", b"test", b"test2"],
|
||||
)
|
||||
self.assertListEqual(
|
||||
index.autocomplete(ix, "tes", limit=3), [b"test3", b"test", b"test2"]
|
||||
index.autocomplete(ix, "tes", limit=3),
|
||||
[b"test3", b"test", b"test2"],
|
||||
)
|
||||
self.assertListEqual(index.autocomplete(ix, "tes", limit=1), [b"test3"])
|
||||
self.assertListEqual(index.autocomplete(ix, "tes", limit=0), [])
|
||||
|
@@ -1,16 +1,14 @@
|
||||
import hashlib
|
||||
import tempfile
|
||||
import filecmp
|
||||
import hashlib
|
||||
import os
|
||||
import shutil
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
from django.test import TestCase, override_settings
|
||||
|
||||
|
||||
from django.core.management import call_command
|
||||
|
||||
from django.test import override_settings
|
||||
from django.test import TestCase
|
||||
from documents.file_handling import generate_filename
|
||||
from documents.management.commands.document_archiver import handle_document
|
||||
from documents.models import Document
|
||||
@@ -34,7 +32,8 @@ class TestArchiver(DirectoriesMixin, TestCase):
|
||||
|
||||
doc = self.make_models()
|
||||
shutil.copy(
|
||||
sample_file, os.path.join(self.dirs.originals_dir, f"{doc.id:07}.pdf")
|
||||
sample_file,
|
||||
os.path.join(self.dirs.originals_dir, f"{doc.id:07}.pdf"),
|
||||
)
|
||||
|
||||
call_command("document_archiver")
|
||||
@@ -43,7 +42,8 @@ class TestArchiver(DirectoriesMixin, TestCase):
|
||||
|
||||
doc = self.make_models()
|
||||
shutil.copy(
|
||||
sample_file, os.path.join(self.dirs.originals_dir, f"{doc.id:07}.pdf")
|
||||
sample_file,
|
||||
os.path.join(self.dirs.originals_dir, f"{doc.id:07}.pdf"),
|
||||
)
|
||||
|
||||
handle_document(doc.pk)
|
||||
@@ -90,7 +90,8 @@ class TestArchiver(DirectoriesMixin, TestCase):
|
||||
)
|
||||
shutil.copy(sample_file, os.path.join(self.dirs.originals_dir, f"document.pdf"))
|
||||
shutil.copy(
|
||||
sample_file, os.path.join(self.dirs.originals_dir, f"document_01.pdf")
|
||||
sample_file,
|
||||
os.path.join(self.dirs.originals_dir, f"document_01.pdf"),
|
||||
)
|
||||
|
||||
handle_document(doc2.pk)
|
||||
@@ -120,7 +121,9 @@ class TestDecryptDocuments(TestCase):
|
||||
os.makedirs(thumb_dir, exist_ok=True)
|
||||
|
||||
override_settings(
|
||||
ORIGINALS_DIR=originals_dir, THUMBNAIL_DIR=thumb_dir, PASSPHRASE="test"
|
||||
ORIGINALS_DIR=originals_dir,
|
||||
THUMBNAIL_DIR=thumb_dir,
|
||||
PASSPHRASE="test",
|
||||
).enable()
|
||||
|
||||
doc = Document.objects.create(
|
||||
@@ -206,7 +209,7 @@ class TestRenamer(DirectoriesMixin, TestCase):
|
||||
|
||||
class TestCreateClassifier(TestCase):
|
||||
@mock.patch(
|
||||
"documents.management.commands.document_create_classifier.train_classifier"
|
||||
"documents.management.commands.document_create_classifier.train_classifier",
|
||||
)
|
||||
def test_create_classifier(self, m):
|
||||
call_command("document_create_classifier")
|
||||
@@ -224,7 +227,10 @@ class TestSanityChecker(DirectoriesMixin, TestCase):
|
||||
|
||||
def test_errors(self):
|
||||
doc = Document.objects.create(
|
||||
title="test", content="test", filename="test.pdf", checksum="abc"
|
||||
title="test",
|
||||
content="test",
|
||||
filename="test.pdf",
|
||||
checksum="abc",
|
||||
)
|
||||
Path(doc.source_path).touch()
|
||||
Path(doc.thumbnail_path).touch()
|
||||
|
@@ -6,12 +6,13 @@ from time import sleep
|
||||
from unittest import mock
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.management import call_command, CommandError
|
||||
from django.test import override_settings, TransactionTestCase
|
||||
|
||||
from documents.models import Tag
|
||||
from django.core.management import call_command
|
||||
from django.core.management import CommandError
|
||||
from django.test import override_settings
|
||||
from django.test import TransactionTestCase
|
||||
from documents.consumer import ConsumerError
|
||||
from documents.management.commands import document_consumer
|
||||
from documents.models import Tag
|
||||
from documents.tests.utils import DirectoriesMixin
|
||||
|
||||
|
||||
@@ -41,7 +42,7 @@ class ConsumerMixin:
|
||||
super(ConsumerMixin, self).setUp()
|
||||
self.t = None
|
||||
patcher = mock.patch(
|
||||
"documents.management.commands.document_consumer.async_task"
|
||||
"documents.management.commands.document_consumer.async_task",
|
||||
)
|
||||
self.task_mock = patcher.start()
|
||||
self.addCleanup(patcher.stop)
|
||||
@@ -208,13 +209,16 @@ class TestConsumer(DirectoriesMixin, ConsumerMixin, TransactionTestCase):
|
||||
self.t_start()
|
||||
|
||||
shutil.copy(
|
||||
self.sample_file, os.path.join(self.dirs.consumption_dir, ".DS_STORE")
|
||||
self.sample_file,
|
||||
os.path.join(self.dirs.consumption_dir, ".DS_STORE"),
|
||||
)
|
||||
shutil.copy(
|
||||
self.sample_file, os.path.join(self.dirs.consumption_dir, "my_file.pdf")
|
||||
self.sample_file,
|
||||
os.path.join(self.dirs.consumption_dir, "my_file.pdf"),
|
||||
)
|
||||
shutil.copy(
|
||||
self.sample_file, os.path.join(self.dirs.consumption_dir, "._my_file.pdf")
|
||||
self.sample_file,
|
||||
os.path.join(self.dirs.consumption_dir, "._my_file.pdf"),
|
||||
)
|
||||
shutil.copy(
|
||||
self.sample_file,
|
||||
@@ -258,7 +262,9 @@ class TestConsumer(DirectoriesMixin, ConsumerMixin, TransactionTestCase):
|
||||
|
||||
|
||||
@override_settings(
|
||||
CONSUMER_POLLING=1, CONSUMER_POLLING_DELAY=3, CONSUMER_POLLING_RETRY_COUNT=20
|
||||
CONSUMER_POLLING=1,
|
||||
CONSUMER_POLLING_DELAY=3,
|
||||
CONSUMER_POLLING_RETRY_COUNT=20,
|
||||
)
|
||||
class TestConsumerPolling(TestConsumer):
|
||||
# just do all the tests with polling
|
||||
@@ -319,7 +325,9 @@ class TestConsumerTags(DirectoriesMixin, ConsumerMixin, TransactionTestCase):
|
||||
self.assertCountEqual(kwargs["override_tag_ids"], tag_ids)
|
||||
|
||||
@override_settings(
|
||||
CONSUMER_POLLING=1, CONSUMER_POLLING_DELAY=1, CONSUMER_POLLING_RETRY_COUNT=20
|
||||
CONSUMER_POLLING=1,
|
||||
CONSUMER_POLLING_DELAY=1,
|
||||
CONSUMER_POLLING_RETRY_COUNT=20,
|
||||
)
|
||||
def test_consume_file_with_path_tags_polling(self):
|
||||
self.test_consume_file_with_path_tags()
|
||||
|
@@ -7,13 +7,17 @@ from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
from django.core.management import call_command
|
||||
from django.test import TestCase, override_settings
|
||||
|
||||
from django.test import override_settings
|
||||
from django.test import TestCase
|
||||
from documents.management.commands import document_exporter
|
||||
from documents.models import Document, Tag, DocumentType, Correspondent
|
||||
from documents.models import Correspondent
|
||||
from documents.models import Document
|
||||
from documents.models import DocumentType
|
||||
from documents.models import Tag
|
||||
from documents.sanity_checker import check_sanity
|
||||
from documents.settings import EXPORTER_FILE_NAME
|
||||
from documents.tests.utils import DirectoriesMixin, paperless_environment
|
||||
from documents.tests.utils import DirectoriesMixin
|
||||
from documents.tests.utils import paperless_environment
|
||||
|
||||
|
||||
class TestExportImport(DirectoriesMixin, TestCase):
|
||||
@@ -66,8 +70,9 @@ class TestExportImport(DirectoriesMixin, TestCase):
|
||||
def _get_document_from_manifest(self, manifest, id):
|
||||
f = list(
|
||||
filter(
|
||||
lambda d: d["model"] == "documents.document" and d["pk"] == id, manifest
|
||||
)
|
||||
lambda d: d["model"] == "documents.document" and d["pk"] == id,
|
||||
manifest,
|
||||
),
|
||||
)
|
||||
if len(f) == 1:
|
||||
return f[0]
|
||||
@@ -76,7 +81,10 @@ class TestExportImport(DirectoriesMixin, TestCase):
|
||||
|
||||
@override_settings(PASSPHRASE="test")
|
||||
def _do_export(
|
||||
self, use_filename_format=False, compare_checksums=False, delete=False
|
||||
self,
|
||||
use_filename_format=False,
|
||||
compare_checksums=False,
|
||||
delete=False,
|
||||
):
|
||||
args = ["document_exporter", self.target]
|
||||
if use_filename_format:
|
||||
@@ -104,7 +112,8 @@ class TestExportImport(DirectoriesMixin, TestCase):
|
||||
|
||||
self.assertEqual(len(manifest), 8)
|
||||
self.assertEqual(
|
||||
len(list(filter(lambda e: e["model"] == "documents.document", manifest))), 4
|
||||
len(list(filter(lambda e: e["model"] == "documents.document", manifest))),
|
||||
4,
|
||||
)
|
||||
|
||||
self.assertTrue(os.path.exists(os.path.join(self.target, "manifest.json")))
|
||||
@@ -129,7 +138,8 @@ class TestExportImport(DirectoriesMixin, TestCase):
|
||||
for element in manifest:
|
||||
if element["model"] == "documents.document":
|
||||
fname = os.path.join(
|
||||
self.target, element[document_exporter.EXPORTER_FILE_NAME]
|
||||
self.target,
|
||||
element[document_exporter.EXPORTER_FILE_NAME],
|
||||
)
|
||||
self.assertTrue(os.path.exists(fname))
|
||||
self.assertTrue(
|
||||
@@ -137,8 +147,8 @@ class TestExportImport(DirectoriesMixin, TestCase):
|
||||
os.path.join(
|
||||
self.target,
|
||||
element[document_exporter.EXPORTER_THUMBNAIL_NAME],
|
||||
)
|
||||
)
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
with open(fname, "rb") as f:
|
||||
@@ -146,12 +156,14 @@ class TestExportImport(DirectoriesMixin, TestCase):
|
||||
self.assertEqual(checksum, element["fields"]["checksum"])
|
||||
|
||||
self.assertEqual(
|
||||
element["fields"]["storage_type"], Document.STORAGE_TYPE_UNENCRYPTED
|
||||
element["fields"]["storage_type"],
|
||||
Document.STORAGE_TYPE_UNENCRYPTED,
|
||||
)
|
||||
|
||||
if document_exporter.EXPORTER_ARCHIVE_NAME in element:
|
||||
fname = os.path.join(
|
||||
self.target, element[document_exporter.EXPORTER_ARCHIVE_NAME]
|
||||
self.target,
|
||||
element[document_exporter.EXPORTER_ARCHIVE_NAME],
|
||||
)
|
||||
self.assertTrue(os.path.exists(fname))
|
||||
|
||||
@@ -188,7 +200,7 @@ class TestExportImport(DirectoriesMixin, TestCase):
|
||||
)
|
||||
|
||||
with override_settings(
|
||||
PAPERLESS_FILENAME_FORMAT="{created_year}/{correspondent}/{title}"
|
||||
PAPERLESS_FILENAME_FORMAT="{created_year}/{correspondent}/{title}",
|
||||
):
|
||||
self.test_exporter(use_filename_format=True)
|
||||
|
||||
@@ -205,7 +217,7 @@ class TestExportImport(DirectoriesMixin, TestCase):
|
||||
st_mtime_1 = os.stat(os.path.join(self.target, "manifest.json")).st_mtime
|
||||
|
||||
with mock.patch(
|
||||
"documents.management.commands.document_exporter.shutil.copy2"
|
||||
"documents.management.commands.document_exporter.shutil.copy2",
|
||||
) as m:
|
||||
self._do_export()
|
||||
m.assert_not_called()
|
||||
@@ -216,7 +228,7 @@ class TestExportImport(DirectoriesMixin, TestCase):
|
||||
Path(self.d1.source_path).touch()
|
||||
|
||||
with mock.patch(
|
||||
"documents.management.commands.document_exporter.shutil.copy2"
|
||||
"documents.management.commands.document_exporter.shutil.copy2",
|
||||
) as m:
|
||||
self._do_export()
|
||||
self.assertEqual(m.call_count, 1)
|
||||
@@ -239,7 +251,7 @@ class TestExportImport(DirectoriesMixin, TestCase):
|
||||
self.assertTrue(os.path.exists(os.path.join(self.target, "manifest.json")))
|
||||
|
||||
with mock.patch(
|
||||
"documents.management.commands.document_exporter.shutil.copy2"
|
||||
"documents.management.commands.document_exporter.shutil.copy2",
|
||||
) as m:
|
||||
self._do_export()
|
||||
m.assert_not_called()
|
||||
@@ -250,7 +262,7 @@ class TestExportImport(DirectoriesMixin, TestCase):
|
||||
self.d2.save()
|
||||
|
||||
with mock.patch(
|
||||
"documents.management.commands.document_exporter.shutil.copy2"
|
||||
"documents.management.commands.document_exporter.shutil.copy2",
|
||||
) as m:
|
||||
self._do_export(compare_checksums=True)
|
||||
self.assertEqual(m.call_count, 1)
|
||||
@@ -270,26 +282,29 @@ class TestExportImport(DirectoriesMixin, TestCase):
|
||||
doc_from_manifest = self._get_document_from_manifest(manifest, self.d3.id)
|
||||
self.assertTrue(
|
||||
os.path.isfile(
|
||||
os.path.join(self.target, doc_from_manifest[EXPORTER_FILE_NAME])
|
||||
)
|
||||
os.path.join(self.target, doc_from_manifest[EXPORTER_FILE_NAME]),
|
||||
),
|
||||
)
|
||||
self.d3.delete()
|
||||
|
||||
manifest = self._do_export()
|
||||
self.assertRaises(
|
||||
ValueError, self._get_document_from_manifest, manifest, self.d3.id
|
||||
ValueError,
|
||||
self._get_document_from_manifest,
|
||||
manifest,
|
||||
self.d3.id,
|
||||
)
|
||||
self.assertTrue(
|
||||
os.path.isfile(
|
||||
os.path.join(self.target, doc_from_manifest[EXPORTER_FILE_NAME])
|
||||
)
|
||||
os.path.join(self.target, doc_from_manifest[EXPORTER_FILE_NAME]),
|
||||
),
|
||||
)
|
||||
|
||||
manifest = self._do_export(delete=True)
|
||||
self.assertFalse(
|
||||
os.path.isfile(
|
||||
os.path.join(self.target, doc_from_manifest[EXPORTER_FILE_NAME])
|
||||
)
|
||||
os.path.join(self.target, doc_from_manifest[EXPORTER_FILE_NAME]),
|
||||
),
|
||||
)
|
||||
|
||||
self.assertTrue(len(manifest), 6)
|
||||
@@ -316,7 +331,7 @@ class TestExportImport(DirectoriesMixin, TestCase):
|
||||
self.assertTrue(os.path.exists(os.path.join(self.target, "manifest.json")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(self.target, "wow2", "none.pdf")))
|
||||
self.assertTrue(
|
||||
os.path.isfile(os.path.join(self.target, "wow2", "none_01.pdf"))
|
||||
os.path.isfile(os.path.join(self.target, "wow2", "none_01.pdf")),
|
||||
)
|
||||
|
||||
def test_export_missing_files(self):
|
||||
|
@@ -1,35 +1,50 @@
|
||||
from django.core.management import call_command
|
||||
from django.test import TestCase
|
||||
|
||||
from documents.models import Document, Tag, Correspondent, DocumentType
|
||||
from documents.models import Correspondent
|
||||
from documents.models import Document
|
||||
from documents.models import DocumentType
|
||||
from documents.models import Tag
|
||||
from documents.tests.utils import DirectoriesMixin
|
||||
|
||||
|
||||
class TestRetagger(DirectoriesMixin, TestCase):
|
||||
def make_models(self):
|
||||
self.d1 = Document.objects.create(
|
||||
checksum="A", title="A", content="first document"
|
||||
checksum="A",
|
||||
title="A",
|
||||
content="first document",
|
||||
)
|
||||
self.d2 = Document.objects.create(
|
||||
checksum="B", title="B", content="second document"
|
||||
checksum="B",
|
||||
title="B",
|
||||
content="second document",
|
||||
)
|
||||
self.d3 = Document.objects.create(
|
||||
checksum="C", title="C", content="unrelated document"
|
||||
checksum="C",
|
||||
title="C",
|
||||
content="unrelated document",
|
||||
)
|
||||
self.d4 = Document.objects.create(
|
||||
checksum="D", title="D", content="auto document"
|
||||
checksum="D",
|
||||
title="D",
|
||||
content="auto document",
|
||||
)
|
||||
|
||||
self.tag_first = Tag.objects.create(
|
||||
name="tag1", match="first", matching_algorithm=Tag.MATCH_ANY
|
||||
name="tag1",
|
||||
match="first",
|
||||
matching_algorithm=Tag.MATCH_ANY,
|
||||
)
|
||||
self.tag_second = Tag.objects.create(
|
||||
name="tag2", match="second", matching_algorithm=Tag.MATCH_ANY
|
||||
name="tag2",
|
||||
match="second",
|
||||
matching_algorithm=Tag.MATCH_ANY,
|
||||
)
|
||||
self.tag_inbox = Tag.objects.create(name="test", is_inbox_tag=True)
|
||||
self.tag_no_match = Tag.objects.create(name="test2")
|
||||
self.tag_auto = Tag.objects.create(
|
||||
name="tagauto", matching_algorithm=Tag.MATCH_AUTO
|
||||
name="tagauto",
|
||||
matching_algorithm=Tag.MATCH_AUTO,
|
||||
)
|
||||
|
||||
self.d3.tags.add(self.tag_inbox)
|
||||
@@ -37,17 +52,25 @@ class TestRetagger(DirectoriesMixin, TestCase):
|
||||
self.d4.tags.add(self.tag_auto)
|
||||
|
||||
self.correspondent_first = Correspondent.objects.create(
|
||||
name="c1", match="first", matching_algorithm=Correspondent.MATCH_ANY
|
||||
name="c1",
|
||||
match="first",
|
||||
matching_algorithm=Correspondent.MATCH_ANY,
|
||||
)
|
||||
self.correspondent_second = Correspondent.objects.create(
|
||||
name="c2", match="second", matching_algorithm=Correspondent.MATCH_ANY
|
||||
name="c2",
|
||||
match="second",
|
||||
matching_algorithm=Correspondent.MATCH_ANY,
|
||||
)
|
||||
|
||||
self.doctype_first = DocumentType.objects.create(
|
||||
name="dt1", match="first", matching_algorithm=DocumentType.MATCH_ANY
|
||||
name="dt1",
|
||||
match="first",
|
||||
matching_algorithm=DocumentType.MATCH_ANY,
|
||||
)
|
||||
self.doctype_second = DocumentType.objects.create(
|
||||
name="dt2", match="second", matching_algorithm=DocumentType.MATCH_ANY
|
||||
name="dt2",
|
||||
match="second",
|
||||
matching_algorithm=DocumentType.MATCH_ANY,
|
||||
)
|
||||
|
||||
def get_updated_docs(self):
|
||||
@@ -98,10 +121,12 @@ class TestRetagger(DirectoriesMixin, TestCase):
|
||||
self.assertIsNotNone(Tag.objects.get(id=self.tag_second.id))
|
||||
|
||||
self.assertCountEqual(
|
||||
[tag.id for tag in d_first.tags.all()], [self.tag_first.id]
|
||||
[tag.id for tag in d_first.tags.all()],
|
||||
[self.tag_first.id],
|
||||
)
|
||||
self.assertCountEqual(
|
||||
[tag.id for tag in d_second.tags.all()], [self.tag_second.id]
|
||||
[tag.id for tag in d_second.tags.all()],
|
||||
[self.tag_second.id],
|
||||
)
|
||||
self.assertCountEqual(
|
||||
[tag.id for tag in d_unrelated.tags.all()],
|
||||
@@ -133,7 +158,10 @@ class TestRetagger(DirectoriesMixin, TestCase):
|
||||
|
||||
def test_add_tags_suggest_url(self):
|
||||
call_command(
|
||||
"document_retagger", "--tags", "--suggest", "--base-url=http://localhost"
|
||||
"document_retagger",
|
||||
"--tags",
|
||||
"--suggest",
|
||||
"--base-url=http://localhost",
|
||||
)
|
||||
d_first, d_second, d_unrelated, d_auto = self.get_updated_docs()
|
||||
|
||||
|
@@ -5,9 +5,11 @@ from unittest import mock
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.management import call_command
|
||||
from django.test import TestCase
|
||||
|
||||
from documents.management.commands.document_thumbnails import _process_document
|
||||
from documents.models import Document, Tag, Correspondent, DocumentType
|
||||
from documents.models import Correspondent
|
||||
from documents.models import Document
|
||||
from documents.models import DocumentType
|
||||
from documents.models import Tag
|
||||
from documents.tests.utils import DirectoriesMixin
|
||||
|
||||
|
||||
|
@@ -4,9 +4,11 @@ from unittest import mock
|
||||
|
||||
from django.core.management import call_command
|
||||
from django.test import TestCase
|
||||
|
||||
from documents.management.commands.document_thumbnails import _process_document
|
||||
from documents.models import Document, Tag, Correspondent, DocumentType
|
||||
from documents.models import Correspondent
|
||||
from documents.models import Document
|
||||
from documents.models import DocumentType
|
||||
from documents.models import Tag
|
||||
from documents.tests.utils import DirectoriesMixin
|
||||
|
||||
|
||||
|
@@ -4,10 +4,14 @@ from random import randint
|
||||
|
||||
from django.contrib.admin.models import LogEntry
|
||||
from django.contrib.auth.models import User
|
||||
from django.test import TestCase, override_settings
|
||||
from django.test import override_settings
|
||||
from django.test import TestCase
|
||||
|
||||
from .. import matching
|
||||
from ..models import Correspondent, Document, Tag, DocumentType
|
||||
from ..models import Correspondent
|
||||
from ..models import Document
|
||||
from ..models import DocumentType
|
||||
from ..models import Tag
|
||||
from ..signals import document_consumption_finished
|
||||
|
||||
|
||||
@@ -209,7 +213,8 @@ class TestDocumentConsumptionFinishedSignal(TestCase):
|
||||
TestCase.setUp(self)
|
||||
User.objects.create_user(username="test_consumer", password="12345")
|
||||
self.doc_contains = Document.objects.create(
|
||||
content="I contain the keyword.", mime_type="application/pdf"
|
||||
content="I contain the keyword.",
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
|
||||
self.index_dir = tempfile.mkdtemp()
|
||||
@@ -221,43 +226,56 @@ class TestDocumentConsumptionFinishedSignal(TestCase):
|
||||
|
||||
def test_tag_applied_any(self):
|
||||
t1 = Tag.objects.create(
|
||||
name="test", match="keyword", matching_algorithm=Tag.MATCH_ANY
|
||||
name="test",
|
||||
match="keyword",
|
||||
matching_algorithm=Tag.MATCH_ANY,
|
||||
)
|
||||
document_consumption_finished.send(
|
||||
sender=self.__class__, document=self.doc_contains
|
||||
sender=self.__class__,
|
||||
document=self.doc_contains,
|
||||
)
|
||||
self.assertTrue(list(self.doc_contains.tags.all()) == [t1])
|
||||
|
||||
def test_tag_not_applied(self):
|
||||
Tag.objects.create(
|
||||
name="test", match="no-match", matching_algorithm=Tag.MATCH_ANY
|
||||
name="test",
|
||||
match="no-match",
|
||||
matching_algorithm=Tag.MATCH_ANY,
|
||||
)
|
||||
document_consumption_finished.send(
|
||||
sender=self.__class__, document=self.doc_contains
|
||||
sender=self.__class__,
|
||||
document=self.doc_contains,
|
||||
)
|
||||
self.assertTrue(list(self.doc_contains.tags.all()) == [])
|
||||
|
||||
def test_correspondent_applied(self):
|
||||
correspondent = Correspondent.objects.create(
|
||||
name="test", match="keyword", matching_algorithm=Correspondent.MATCH_ANY
|
||||
name="test",
|
||||
match="keyword",
|
||||
matching_algorithm=Correspondent.MATCH_ANY,
|
||||
)
|
||||
document_consumption_finished.send(
|
||||
sender=self.__class__, document=self.doc_contains
|
||||
sender=self.__class__,
|
||||
document=self.doc_contains,
|
||||
)
|
||||
self.assertTrue(self.doc_contains.correspondent == correspondent)
|
||||
|
||||
def test_correspondent_not_applied(self):
|
||||
Tag.objects.create(
|
||||
name="test", match="no-match", matching_algorithm=Correspondent.MATCH_ANY
|
||||
name="test",
|
||||
match="no-match",
|
||||
matching_algorithm=Correspondent.MATCH_ANY,
|
||||
)
|
||||
document_consumption_finished.send(
|
||||
sender=self.__class__, document=self.doc_contains
|
||||
sender=self.__class__,
|
||||
document=self.doc_contains,
|
||||
)
|
||||
self.assertEqual(self.doc_contains.correspondent, None)
|
||||
|
||||
def test_logentry_created(self):
|
||||
document_consumption_finished.send(
|
||||
sender=self.__class__, document=self.doc_contains
|
||||
sender=self.__class__,
|
||||
document=self.doc_contains,
|
||||
)
|
||||
|
||||
self.assertEqual(LogEntry.objects.count(), 1)
|
||||
|
@@ -6,9 +6,9 @@ from unittest import mock
|
||||
|
||||
from django.conf import settings
|
||||
from django.test import override_settings
|
||||
|
||||
from documents.parsers import ParseError
|
||||
from documents.tests.utils import DirectoriesMixin, TestMigrations
|
||||
from documents.tests.utils import DirectoriesMixin
|
||||
from documents.tests.utils import TestMigrations
|
||||
|
||||
|
||||
STORAGE_TYPE_GPG = "gpg"
|
||||
@@ -93,10 +93,18 @@ def make_test_document(
|
||||
simple_jpg = os.path.join(os.path.dirname(__file__), "samples", "simple.jpg")
|
||||
simple_pdf = os.path.join(os.path.dirname(__file__), "samples", "simple.pdf")
|
||||
simple_pdf2 = os.path.join(
|
||||
os.path.dirname(__file__), "samples", "documents", "originals", "0000002.pdf"
|
||||
os.path.dirname(__file__),
|
||||
"samples",
|
||||
"documents",
|
||||
"originals",
|
||||
"0000002.pdf",
|
||||
)
|
||||
simple_pdf3 = os.path.join(
|
||||
os.path.dirname(__file__), "samples", "documents", "originals", "0000003.pdf"
|
||||
os.path.dirname(__file__),
|
||||
"samples",
|
||||
"documents",
|
||||
"originals",
|
||||
"0000003.pdf",
|
||||
)
|
||||
simple_txt = os.path.join(os.path.dirname(__file__), "samples", "simple.txt")
|
||||
simple_png = os.path.join(os.path.dirname(__file__), "samples", "simple-noalpha.png")
|
||||
@@ -121,19 +129,43 @@ class TestMigrateArchiveFiles(DirectoriesMixin, TestMigrations):
|
||||
simple_pdf,
|
||||
)
|
||||
self.no_text = make_test_document(
|
||||
Document, "no-text", "image/png", simple_png2, "no-text.png", simple_pdf
|
||||
Document,
|
||||
"no-text",
|
||||
"image/png",
|
||||
simple_png2,
|
||||
"no-text.png",
|
||||
simple_pdf,
|
||||
)
|
||||
self.doc_no_archive = make_test_document(
|
||||
Document, "no_archive", "text/plain", simple_txt, "no_archive.txt"
|
||||
Document,
|
||||
"no_archive",
|
||||
"text/plain",
|
||||
simple_txt,
|
||||
"no_archive.txt",
|
||||
)
|
||||
self.clash1 = make_test_document(
|
||||
Document, "clash", "application/pdf", simple_pdf, "clash.pdf", simple_pdf
|
||||
Document,
|
||||
"clash",
|
||||
"application/pdf",
|
||||
simple_pdf,
|
||||
"clash.pdf",
|
||||
simple_pdf,
|
||||
)
|
||||
self.clash2 = make_test_document(
|
||||
Document, "clash", "image/jpeg", simple_jpg, "clash.jpg", simple_pdf
|
||||
Document,
|
||||
"clash",
|
||||
"image/jpeg",
|
||||
simple_jpg,
|
||||
"clash.jpg",
|
||||
simple_pdf,
|
||||
)
|
||||
self.clash3 = make_test_document(
|
||||
Document, "clash", "image/png", simple_png, "clash.png", simple_pdf
|
||||
Document,
|
||||
"clash",
|
||||
"image/png",
|
||||
simple_png,
|
||||
"clash.png",
|
||||
simple_pdf,
|
||||
)
|
||||
self.clash4 = make_test_document(
|
||||
Document,
|
||||
@@ -147,7 +179,8 @@ class TestMigrateArchiveFiles(DirectoriesMixin, TestMigrations):
|
||||
self.assertEqual(archive_path_old(self.clash1), archive_path_old(self.clash2))
|
||||
self.assertEqual(archive_path_old(self.clash1), archive_path_old(self.clash3))
|
||||
self.assertNotEqual(
|
||||
archive_path_old(self.clash1), archive_path_old(self.clash4)
|
||||
archive_path_old(self.clash1),
|
||||
archive_path_old(self.clash4),
|
||||
)
|
||||
|
||||
def testArchiveFilesMigrated(self):
|
||||
@@ -171,19 +204,23 @@ class TestMigrateArchiveFiles(DirectoriesMixin, TestMigrations):
|
||||
self.assertEqual(archive_checksum, doc.archive_checksum)
|
||||
|
||||
self.assertEqual(
|
||||
Document.objects.filter(archive_checksum__isnull=False).count(), 6
|
||||
Document.objects.filter(archive_checksum__isnull=False).count(),
|
||||
6,
|
||||
)
|
||||
|
||||
def test_filenames(self):
|
||||
Document = self.apps.get_model("documents", "Document")
|
||||
self.assertEqual(
|
||||
Document.objects.get(id=self.unrelated.id).archive_filename, "unrelated.pdf"
|
||||
Document.objects.get(id=self.unrelated.id).archive_filename,
|
||||
"unrelated.pdf",
|
||||
)
|
||||
self.assertEqual(
|
||||
Document.objects.get(id=self.no_text.id).archive_filename, "no-text.pdf"
|
||||
Document.objects.get(id=self.no_text.id).archive_filename,
|
||||
"no-text.pdf",
|
||||
)
|
||||
self.assertEqual(
|
||||
Document.objects.get(id=self.doc_no_archive.id).archive_filename, None
|
||||
Document.objects.get(id=self.doc_no_archive.id).archive_filename,
|
||||
None,
|
||||
)
|
||||
self.assertEqual(
|
||||
Document.objects.get(id=self.clash1.id).archive_filename,
|
||||
@@ -198,7 +235,8 @@ class TestMigrateArchiveFiles(DirectoriesMixin, TestMigrations):
|
||||
f"{self.clash3.id:07}.pdf",
|
||||
)
|
||||
self.assertEqual(
|
||||
Document.objects.get(id=self.clash4.id).archive_filename, "clash.png.pdf"
|
||||
Document.objects.get(id=self.clash4.id).archive_filename,
|
||||
"clash.png.pdf",
|
||||
)
|
||||
|
||||
|
||||
@@ -207,16 +245,20 @@ class TestMigrateArchiveFilesWithFilenameFormat(TestMigrateArchiveFiles):
|
||||
def test_filenames(self):
|
||||
Document = self.apps.get_model("documents", "Document")
|
||||
self.assertEqual(
|
||||
Document.objects.get(id=self.unrelated.id).archive_filename, "unrelated.pdf"
|
||||
Document.objects.get(id=self.unrelated.id).archive_filename,
|
||||
"unrelated.pdf",
|
||||
)
|
||||
self.assertEqual(
|
||||
Document.objects.get(id=self.no_text.id).archive_filename, "no-text.pdf"
|
||||
Document.objects.get(id=self.no_text.id).archive_filename,
|
||||
"no-text.pdf",
|
||||
)
|
||||
self.assertEqual(
|
||||
Document.objects.get(id=self.doc_no_archive.id).archive_filename, None
|
||||
Document.objects.get(id=self.doc_no_archive.id).archive_filename,
|
||||
None,
|
||||
)
|
||||
self.assertEqual(
|
||||
Document.objects.get(id=self.clash1.id).archive_filename, "none/clash.pdf"
|
||||
Document.objects.get(id=self.clash1.id).archive_filename,
|
||||
"none/clash.pdf",
|
||||
)
|
||||
self.assertEqual(
|
||||
Document.objects.get(id=self.clash2.id).archive_filename,
|
||||
@@ -227,7 +269,8 @@ class TestMigrateArchiveFilesWithFilenameFormat(TestMigrateArchiveFiles):
|
||||
"none/clash_02.pdf",
|
||||
)
|
||||
self.assertEqual(
|
||||
Document.objects.get(id=self.clash4.id).archive_filename, "clash.png.pdf"
|
||||
Document.objects.get(id=self.clash4.id).archive_filename,
|
||||
"clash.png.pdf",
|
||||
)
|
||||
|
||||
|
||||
@@ -248,12 +291,19 @@ class TestMigrateArchiveFilesErrors(DirectoriesMixin, TestMigrations):
|
||||
Document = self.apps.get_model("documents", "Document")
|
||||
|
||||
doc = make_test_document(
|
||||
Document, "clash", "application/pdf", simple_pdf, "clash.pdf", simple_pdf
|
||||
Document,
|
||||
"clash",
|
||||
"application/pdf",
|
||||
simple_pdf,
|
||||
"clash.pdf",
|
||||
simple_pdf,
|
||||
)
|
||||
os.unlink(archive_path_old(doc))
|
||||
|
||||
self.assertRaisesMessage(
|
||||
ValueError, "does not exist at: ", self.performMigration
|
||||
ValueError,
|
||||
"does not exist at: ",
|
||||
self.performMigration,
|
||||
)
|
||||
|
||||
def test_parser_missing(self):
|
||||
@@ -277,7 +327,9 @@ class TestMigrateArchiveFilesErrors(DirectoriesMixin, TestMigrations):
|
||||
)
|
||||
|
||||
self.assertRaisesMessage(
|
||||
ValueError, "no parsers are available", self.performMigration
|
||||
ValueError,
|
||||
"no parsers are available",
|
||||
self.performMigration,
|
||||
)
|
||||
|
||||
@mock.patch("documents.migrations.1012_fix_archive_files.parse_wrapper")
|
||||
@@ -286,7 +338,12 @@ class TestMigrateArchiveFilesErrors(DirectoriesMixin, TestMigrations):
|
||||
Document = self.apps.get_model("documents", "Document")
|
||||
|
||||
doc1 = make_test_document(
|
||||
Document, "document", "image/png", simple_png, "document.png", simple_pdf
|
||||
Document,
|
||||
"document",
|
||||
"image/png",
|
||||
simple_png,
|
||||
"document.png",
|
||||
simple_pdf,
|
||||
)
|
||||
doc2 = make_test_document(
|
||||
Document,
|
||||
@@ -311,8 +368,8 @@ class TestMigrateArchiveFilesErrors(DirectoriesMixin, TestMigrations):
|
||||
filter(
|
||||
lambda log: "Parse error, will try again in 5 seconds" in log,
|
||||
capture.output,
|
||||
)
|
||||
)
|
||||
),
|
||||
),
|
||||
),
|
||||
4,
|
||||
)
|
||||
@@ -324,8 +381,8 @@ class TestMigrateArchiveFilesErrors(DirectoriesMixin, TestMigrations):
|
||||
lambda log: "Unable to regenerate archive document for ID:"
|
||||
in log,
|
||||
capture.output,
|
||||
)
|
||||
)
|
||||
),
|
||||
),
|
||||
),
|
||||
2,
|
||||
)
|
||||
@@ -347,7 +404,12 @@ class TestMigrateArchiveFilesErrors(DirectoriesMixin, TestMigrations):
|
||||
Document = self.apps.get_model("documents", "Document")
|
||||
|
||||
doc1 = make_test_document(
|
||||
Document, "document", "image/png", simple_png, "document.png", simple_pdf
|
||||
Document,
|
||||
"document",
|
||||
"image/png",
|
||||
simple_png,
|
||||
"document.png",
|
||||
simple_pdf,
|
||||
)
|
||||
doc2 = make_test_document(
|
||||
Document,
|
||||
@@ -368,8 +430,8 @@ class TestMigrateArchiveFilesErrors(DirectoriesMixin, TestMigrations):
|
||||
lambda log: "Parser did not return an archive document for document"
|
||||
in log,
|
||||
capture.output,
|
||||
)
|
||||
)
|
||||
),
|
||||
),
|
||||
),
|
||||
2,
|
||||
)
|
||||
@@ -405,7 +467,11 @@ class TestMigrateArchiveFilesBackwards(DirectoriesMixin, TestMigrations):
|
||||
"unrelated.pdf",
|
||||
)
|
||||
doc_no_archive = make_test_document(
|
||||
Document, "no_archive", "text/plain", simple_txt, "no_archive.txt"
|
||||
Document,
|
||||
"no_archive",
|
||||
"text/plain",
|
||||
simple_txt,
|
||||
"no_archive.txt",
|
||||
)
|
||||
clashB = make_test_document(
|
||||
Document,
|
||||
@@ -434,13 +500,14 @@ class TestMigrateArchiveFilesBackwards(DirectoriesMixin, TestMigrations):
|
||||
self.assertEqual(archive_checksum, doc.archive_checksum)
|
||||
|
||||
self.assertEqual(
|
||||
Document.objects.filter(archive_checksum__isnull=False).count(), 2
|
||||
Document.objects.filter(archive_checksum__isnull=False).count(),
|
||||
2,
|
||||
)
|
||||
|
||||
|
||||
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{title}")
|
||||
class TestMigrateArchiveFilesBackwardsWithFilenameFormat(
|
||||
TestMigrateArchiveFilesBackwards
|
||||
TestMigrateArchiveFilesBackwards,
|
||||
):
|
||||
pass
|
||||
|
||||
@@ -505,5 +572,7 @@ class TestMigrateArchiveFilesBackwardsErrors(DirectoriesMixin, TestMigrations):
|
||||
)
|
||||
|
||||
self.assertRaisesMessage(
|
||||
ValueError, "file already exists.", self.performMigration
|
||||
ValueError,
|
||||
"file already exists.",
|
||||
self.performMigration,
|
||||
)
|
||||
|
@@ -3,9 +3,9 @@ import shutil
|
||||
|
||||
from django.conf import settings
|
||||
from django.test import override_settings
|
||||
|
||||
from documents.parsers import get_default_file_extension
|
||||
from documents.tests.utils import DirectoriesMixin, TestMigrations
|
||||
from documents.tests.utils import DirectoriesMixin
|
||||
from documents.tests.utils import TestMigrations
|
||||
|
||||
STORAGE_TYPE_UNENCRYPTED = "unencrypted"
|
||||
STORAGE_TYPE_GPG = "gpg"
|
||||
@@ -46,7 +46,9 @@ class TestMigrateMimeType(DirectoriesMixin, TestMigrations):
|
||||
def setUpBeforeMigration(self, apps):
|
||||
Document = apps.get_model("documents", "Document")
|
||||
doc = Document.objects.create(
|
||||
title="test", file_type="pdf", filename="file1.pdf"
|
||||
title="test",
|
||||
file_type="pdf",
|
||||
filename="file1.pdf",
|
||||
)
|
||||
self.doc_id = doc.id
|
||||
shutil.copy(
|
||||
@@ -55,7 +57,9 @@ class TestMigrateMimeType(DirectoriesMixin, TestMigrations):
|
||||
)
|
||||
|
||||
doc2 = Document.objects.create(
|
||||
checksum="B", file_type="pdf", storage_type=STORAGE_TYPE_GPG
|
||||
checksum="B",
|
||||
file_type="pdf",
|
||||
storage_type=STORAGE_TYPE_GPG,
|
||||
)
|
||||
self.doc2_id = doc2.id
|
||||
shutil.copy(
|
||||
@@ -88,7 +92,9 @@ class TestMigrateMimeTypeBackwards(DirectoriesMixin, TestMigrations):
|
||||
def setUpBeforeMigration(self, apps):
|
||||
Document = apps.get_model("documents", "Document")
|
||||
doc = Document.objects.create(
|
||||
title="test", mime_type="application/pdf", filename="file1.pdf"
|
||||
title="test",
|
||||
mime_type="application/pdf",
|
||||
filename="file1.pdf",
|
||||
)
|
||||
self.doc_id = doc.id
|
||||
shutil.copy(
|
||||
|
@@ -1,4 +1,5 @@
|
||||
from documents.tests.utils import DirectoriesMixin, TestMigrations
|
||||
from documents.tests.utils import DirectoriesMixin
|
||||
from documents.tests.utils import TestMigrations
|
||||
|
||||
|
||||
class TestMigrateNullCharacters(DirectoriesMixin, TestMigrations):
|
||||
|
@@ -1,4 +1,5 @@
|
||||
from documents.tests.utils import DirectoriesMixin, TestMigrations
|
||||
from documents.tests.utils import DirectoriesMixin
|
||||
from documents.tests.utils import TestMigrations
|
||||
|
||||
|
||||
class TestMigrateTagColor(DirectoriesMixin, TestMigrations):
|
||||
|
@@ -1,7 +1,9 @@
|
||||
from django.test import TestCase
|
||||
|
||||
from .factories import DocumentFactory, CorrespondentFactory
|
||||
from ..models import Document, Correspondent
|
||||
from ..models import Correspondent
|
||||
from ..models import Document
|
||||
from .factories import CorrespondentFactory
|
||||
from .factories import DocumentFactory
|
||||
|
||||
|
||||
class CorrespondentTestCase(TestCase):
|
||||
|
@@ -4,16 +4,14 @@ import tempfile
|
||||
from tempfile import TemporaryDirectory
|
||||
from unittest import mock
|
||||
|
||||
from django.test import TestCase, override_settings
|
||||
|
||||
from documents.parsers import (
|
||||
get_parser_class,
|
||||
get_supported_file_extensions,
|
||||
get_default_file_extension,
|
||||
get_parser_class_for_mime_type,
|
||||
DocumentParser,
|
||||
is_file_ext_supported,
|
||||
)
|
||||
from django.test import override_settings
|
||||
from django.test import TestCase
|
||||
from documents.parsers import DocumentParser
|
||||
from documents.parsers import get_default_file_extension
|
||||
from documents.parsers import get_parser_class
|
||||
from documents.parsers import get_parser_class_for_mime_type
|
||||
from documents.parsers import get_supported_file_extensions
|
||||
from documents.parsers import is_file_ext_supported
|
||||
from paperless_tesseract.parsers import RasterisedDocumentParser
|
||||
from paperless_text.parsers import TextDocumentParser
|
||||
|
||||
|
@@ -6,9 +6,9 @@ from pathlib import Path
|
||||
import filelock
|
||||
from django.conf import settings
|
||||
from django.test import TestCase
|
||||
|
||||
from documents.models import Document
|
||||
from documents.sanity_checker import check_sanity, SanityCheckMessages
|
||||
from documents.sanity_checker import check_sanity
|
||||
from documents.sanity_checker import SanityCheckMessages
|
||||
from documents.tests.utils import DirectoriesMixin
|
||||
|
||||
|
||||
@@ -23,7 +23,8 @@ class TestSanityCheckMessages(TestCase):
|
||||
self.assertEqual(len(capture.output), 1)
|
||||
self.assertEqual(capture.records[0].levelno, logging.INFO)
|
||||
self.assertEqual(
|
||||
capture.records[0].message, "Sanity checker detected no issues."
|
||||
capture.records[0].message,
|
||||
"Sanity checker detected no issues.",
|
||||
)
|
||||
|
||||
def test_info(self):
|
||||
|
@@ -2,8 +2,8 @@ import logging
|
||||
from unittest import mock
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from paperless.settings import default_task_workers, default_threads_per_worker
|
||||
from paperless.settings import default_task_workers
|
||||
from paperless.settings import default_threads_per_worker
|
||||
|
||||
|
||||
class TestSettings(TestCase):
|
||||
@@ -21,7 +21,7 @@ class TestSettings(TestCase):
|
||||
def test_workers_threads(self):
|
||||
for i in range(1, 64):
|
||||
with mock.patch(
|
||||
"paperless.settings.multiprocessing.cpu_count"
|
||||
"paperless.settings.multiprocessing.cpu_count",
|
||||
) as cpu_count:
|
||||
cpu_count.return_value = i
|
||||
|
||||
|
@@ -4,10 +4,13 @@ from unittest import mock
|
||||
from django.conf import settings
|
||||
from django.test import TestCase
|
||||
from django.utils import timezone
|
||||
|
||||
from documents import tasks
|
||||
from documents.models import Document, Tag, Correspondent, DocumentType
|
||||
from documents.sanity_checker import SanityCheckMessages, SanityCheckFailedException
|
||||
from documents.models import Correspondent
|
||||
from documents.models import Document
|
||||
from documents.models import DocumentType
|
||||
from documents.models import Tag
|
||||
from documents.sanity_checker import SanityCheckFailedException
|
||||
from documents.sanity_checker import SanityCheckMessages
|
||||
from documents.tests.utils import DirectoriesMixin
|
||||
|
||||
|
||||
@@ -106,7 +109,8 @@ class TestTasks(DirectoriesMixin, TestCase):
|
||||
messages.warning("Some warning")
|
||||
m.return_value = messages
|
||||
self.assertEqual(
|
||||
tasks.sanity_check(), "Sanity check exited with warnings. See log."
|
||||
tasks.sanity_check(),
|
||||
"Sanity check exited with warnings. See log.",
|
||||
)
|
||||
m.assert_called_once()
|
||||
|
||||
@@ -116,7 +120,8 @@ class TestTasks(DirectoriesMixin, TestCase):
|
||||
messages.info("Some info")
|
||||
m.return_value = messages
|
||||
self.assertEqual(
|
||||
tasks.sanity_check(), "Sanity check exited with infos. See log."
|
||||
tasks.sanity_check(),
|
||||
"Sanity check exited with infos. See log.",
|
||||
)
|
||||
m.assert_called_once()
|
||||
|
||||
|
@@ -25,7 +25,7 @@ class TestViews(TestCase):
|
||||
]:
|
||||
if language_given:
|
||||
self.client.cookies.load(
|
||||
{settings.LANGUAGE_COOKIE_NAME: language_given}
|
||||
{settings.LANGUAGE_COOKIE_NAME: language_given},
|
||||
)
|
||||
elif settings.LANGUAGE_COOKIE_NAME in self.client.cookies.keys():
|
||||
self.client.cookies.pop(settings.LANGUAGE_COOKIE_NAME)
|
||||
@@ -51,5 +51,6 @@ class TestViews(TestCase):
|
||||
f"frontend/{language_actual}/polyfills.js",
|
||||
)
|
||||
self.assertEqual(
|
||||
response.context_data["main_js"], f"frontend/{language_actual}/main.js"
|
||||
response.context_data["main_js"],
|
||||
f"frontend/{language_actual}/main.js",
|
||||
)
|
||||
|
@@ -7,7 +7,8 @@ from contextlib import contextmanager
|
||||
from django.apps import apps
|
||||
from django.db import connection
|
||||
from django.db.migrations.executor import MigrationExecutor
|
||||
from django.test import override_settings, TransactionTestCase
|
||||
from django.test import override_settings
|
||||
from django.test import TransactionTestCase
|
||||
|
||||
|
||||
def setup_directories():
|
||||
@@ -97,7 +98,7 @@ class TestMigrations(TransactionTestCase):
|
||||
assert (
|
||||
self.migrate_from and self.migrate_to
|
||||
), "TestCase '{}' must define migrate_from and migrate_to properties".format(
|
||||
type(self).__name__
|
||||
type(self).__name__,
|
||||
)
|
||||
self.migrate_from = [(self.app, self.migrate_from)]
|
||||
self.migrate_to = [(self.app, self.migrate_to)]
|
||||
|
Reference in New Issue
Block a user