mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-02 13:45:10 -05:00
test cases
This commit is contained in:
parent
47189342e4
commit
94be8781e9
@ -212,6 +212,20 @@ def fake_magic_from_file(file, mime=False):
|
|||||||
@mock.patch("documents.consumer.magic.from_file", fake_magic_from_file)
|
@mock.patch("documents.consumer.magic.from_file", fake_magic_from_file)
|
||||||
class TestConsumer(DirectoriesMixin, TestCase):
|
class TestConsumer(DirectoriesMixin, TestCase):
|
||||||
|
|
||||||
|
def _assert_first_last_send_progress(self, first_status="STARTING", last_status="SUCCESS", first_progress=0, first_progress_max=100, last_progress=100, last_progress_max=100):
|
||||||
|
|
||||||
|
self._send_progress.assert_called()
|
||||||
|
|
||||||
|
args, kwargs = self._send_progress.call_args_list[0]
|
||||||
|
self.assertEqual(args[0], first_progress)
|
||||||
|
self.assertEqual(args[1], first_progress_max)
|
||||||
|
self.assertEqual(args[2], first_status)
|
||||||
|
|
||||||
|
args, kwargs = self._send_progress.call_args_list[len(self._send_progress.call_args_list) - 1]
|
||||||
|
self.assertEqual(args[0], last_progress)
|
||||||
|
self.assertEqual(args[1], last_progress_max)
|
||||||
|
self.assertEqual(args[2], last_status)
|
||||||
|
|
||||||
def make_dummy_parser(self, logging_group, progress_callback=None):
|
def make_dummy_parser(self, logging_group, progress_callback=None):
|
||||||
return DummyParser(logging_group, self.dirs.scratch_dir, self.get_test_archive_file())
|
return DummyParser(logging_group, self.dirs.scratch_dir, self.get_test_archive_file())
|
||||||
|
|
||||||
@ -232,7 +246,7 @@ class TestConsumer(DirectoriesMixin, TestCase):
|
|||||||
|
|
||||||
# this prevents websocket message reports during testing.
|
# this prevents websocket message reports during testing.
|
||||||
patcher = mock.patch("documents.consumer.Consumer._send_progress")
|
patcher = mock.patch("documents.consumer.Consumer._send_progress")
|
||||||
patcher.start()
|
self._send_progress = patcher.start()
|
||||||
self.addCleanup(patcher.stop)
|
self.addCleanup(patcher.stop)
|
||||||
|
|
||||||
self.consumer = Consumer()
|
self.consumer = Consumer()
|
||||||
@ -278,6 +292,8 @@ class TestConsumer(DirectoriesMixin, TestCase):
|
|||||||
|
|
||||||
self.assertFalse(os.path.isfile(filename))
|
self.assertFalse(os.path.isfile(filename))
|
||||||
|
|
||||||
|
self._assert_first_last_send_progress()
|
||||||
|
|
||||||
def testOverrideFilename(self):
|
def testOverrideFilename(self):
|
||||||
filename = self.get_test_file()
|
filename = self.get_test_file()
|
||||||
override_filename = "Statement for November.pdf"
|
override_filename = "Statement for November.pdf"
|
||||||
@ -286,21 +302,26 @@ class TestConsumer(DirectoriesMixin, TestCase):
|
|||||||
|
|
||||||
self.assertEqual(document.title, "Statement for November")
|
self.assertEqual(document.title, "Statement for November")
|
||||||
|
|
||||||
|
self._assert_first_last_send_progress()
|
||||||
|
|
||||||
def testOverrideTitle(self):
|
def testOverrideTitle(self):
|
||||||
document = self.consumer.try_consume_file(self.get_test_file(), override_title="Override Title")
|
document = self.consumer.try_consume_file(self.get_test_file(), override_title="Override Title")
|
||||||
self.assertEqual(document.title, "Override Title")
|
self.assertEqual(document.title, "Override Title")
|
||||||
|
self._assert_first_last_send_progress()
|
||||||
|
|
||||||
def testOverrideCorrespondent(self):
|
def testOverrideCorrespondent(self):
|
||||||
c = Correspondent.objects.create(name="test")
|
c = Correspondent.objects.create(name="test")
|
||||||
|
|
||||||
document = self.consumer.try_consume_file(self.get_test_file(), override_correspondent_id=c.pk)
|
document = self.consumer.try_consume_file(self.get_test_file(), override_correspondent_id=c.pk)
|
||||||
self.assertEqual(document.correspondent.id, c.id)
|
self.assertEqual(document.correspondent.id, c.id)
|
||||||
|
self._assert_first_last_send_progress()
|
||||||
|
|
||||||
def testOverrideDocumentType(self):
|
def testOverrideDocumentType(self):
|
||||||
dt = DocumentType.objects.create(name="test")
|
dt = DocumentType.objects.create(name="test")
|
||||||
|
|
||||||
document = self.consumer.try_consume_file(self.get_test_file(), override_document_type_id=dt.pk)
|
document = self.consumer.try_consume_file(self.get_test_file(), override_document_type_id=dt.pk)
|
||||||
self.assertEqual(document.document_type.id, dt.id)
|
self.assertEqual(document.document_type.id, dt.id)
|
||||||
|
self._assert_first_last_send_progress()
|
||||||
|
|
||||||
def testOverrideTags(self):
|
def testOverrideTags(self):
|
||||||
t1 = Tag.objects.create(name="t1")
|
t1 = Tag.objects.create(name="t1")
|
||||||
@ -311,37 +332,42 @@ class TestConsumer(DirectoriesMixin, TestCase):
|
|||||||
self.assertIn(t1, document.tags.all())
|
self.assertIn(t1, document.tags.all())
|
||||||
self.assertNotIn(t2, document.tags.all())
|
self.assertNotIn(t2, document.tags.all())
|
||||||
self.assertIn(t3, document.tags.all())
|
self.assertIn(t3, document.tags.all())
|
||||||
|
self._assert_first_last_send_progress()
|
||||||
|
|
||||||
def testNotAFile(self):
|
def testNotAFile(self):
|
||||||
try:
|
|
||||||
self.consumer.try_consume_file("non-existing-file")
|
|
||||||
except ConsumerError as e:
|
|
||||||
self.assertTrue(str(e).endswith('File not found.'))
|
|
||||||
return
|
|
||||||
|
|
||||||
self.fail("Should throw exception")
|
self.assertRaisesMessage(
|
||||||
|
ConsumerError,
|
||||||
|
"File not found",
|
||||||
|
self.consumer.try_consume_file,
|
||||||
|
"non-existing-file"
|
||||||
|
)
|
||||||
|
|
||||||
|
self._assert_first_last_send_progress(last_status="FAILED")
|
||||||
|
|
||||||
def testDuplicates1(self):
|
def testDuplicates1(self):
|
||||||
self.consumer.try_consume_file(self.get_test_file())
|
self.consumer.try_consume_file(self.get_test_file())
|
||||||
|
|
||||||
try:
|
self.assertRaisesMessage(
|
||||||
self.consumer.try_consume_file(self.get_test_file())
|
ConsumerError,
|
||||||
except ConsumerError as e:
|
"It is a duplicate",
|
||||||
self.assertTrue(str(e).endswith("It is a duplicate."))
|
self.consumer.try_consume_file,
|
||||||
return
|
self.get_test_file()
|
||||||
|
)
|
||||||
|
|
||||||
self.fail("Should throw exception")
|
self._assert_first_last_send_progress(last_status="FAILED")
|
||||||
|
|
||||||
def testDuplicates2(self):
|
def testDuplicates2(self):
|
||||||
self.consumer.try_consume_file(self.get_test_file())
|
self.consumer.try_consume_file(self.get_test_file())
|
||||||
|
|
||||||
try:
|
self.assertRaisesMessage(
|
||||||
self.consumer.try_consume_file(self.get_test_archive_file())
|
ConsumerError,
|
||||||
except ConsumerError as e:
|
"It is a duplicate",
|
||||||
self.assertTrue(str(e).endswith("It is a duplicate."))
|
self.consumer.try_consume_file,
|
||||||
return
|
self.get_test_archive_file()
|
||||||
|
)
|
||||||
|
|
||||||
self.fail("Should throw exception")
|
self._assert_first_last_send_progress(last_status="FAILED")
|
||||||
|
|
||||||
def testDuplicates3(self):
|
def testDuplicates3(self):
|
||||||
self.consumer.try_consume_file(self.get_test_archive_file())
|
self.consumer.try_consume_file(self.get_test_archive_file())
|
||||||
@ -351,13 +377,15 @@ class TestConsumer(DirectoriesMixin, TestCase):
|
|||||||
def testNoParsers(self, m):
|
def testNoParsers(self, m):
|
||||||
m.return_value = []
|
m.return_value = []
|
||||||
|
|
||||||
try:
|
self.assertRaisesMessage(
|
||||||
self.consumer.try_consume_file(self.get_test_file())
|
ConsumerError,
|
||||||
except ConsumerError as e:
|
"sample.pdf: Unsupported mime type application/pdf",
|
||||||
self.assertEqual(str(e), "sample.pdf: Unsupported mime type application/pdf")
|
self.consumer.try_consume_file,
|
||||||
return
|
self.get_test_file()
|
||||||
|
)
|
||||||
|
|
||||||
|
self._assert_first_last_send_progress(last_status="FAILED")
|
||||||
|
|
||||||
self.fail("Should throw exception")
|
|
||||||
|
|
||||||
@mock.patch("documents.parsers.document_consumer_declaration.send")
|
@mock.patch("documents.parsers.document_consumer_declaration.send")
|
||||||
def testFaultyParser(self, m):
|
def testFaultyParser(self, m):
|
||||||
@ -367,24 +395,28 @@ class TestConsumer(DirectoriesMixin, TestCase):
|
|||||||
"weight": 0
|
"weight": 0
|
||||||
})]
|
})]
|
||||||
|
|
||||||
try:
|
self.assertRaisesMessage(
|
||||||
self.consumer.try_consume_file(self.get_test_file())
|
ConsumerError,
|
||||||
except ConsumerError as e:
|
"sample.pdf: Error while consuming document sample.pdf: Does not compute.",
|
||||||
self.assertEqual(str(e), "sample.pdf: Error while consuming document sample.pdf: Does not compute.")
|
self.consumer.try_consume_file,
|
||||||
return
|
self.get_test_file()
|
||||||
|
)
|
||||||
|
|
||||||
self.fail("Should throw exception.")
|
self._assert_first_last_send_progress(last_status="FAILED")
|
||||||
|
|
||||||
@mock.patch("documents.consumer.Consumer._write")
|
@mock.patch("documents.consumer.Consumer._write")
|
||||||
def testPostSaveError(self, m):
|
def testPostSaveError(self, m):
|
||||||
filename = self.get_test_file()
|
filename = self.get_test_file()
|
||||||
m.side_effect = OSError("NO.")
|
m.side_effect = OSError("NO.")
|
||||||
try:
|
|
||||||
self.consumer.try_consume_file(filename)
|
self.assertRaisesMessage(
|
||||||
except ConsumerError as e:
|
ConsumerError,
|
||||||
self.assertEqual(str(e), "sample.pdf: The following error occured while consuming sample.pdf: NO.")
|
"sample.pdf: The following error occured while consuming sample.pdf: NO.",
|
||||||
else:
|
self.consumer.try_consume_file,
|
||||||
self.fail("Should raise exception")
|
filename
|
||||||
|
)
|
||||||
|
|
||||||
|
self._assert_first_last_send_progress(last_status="FAILED")
|
||||||
|
|
||||||
# file not deleted
|
# file not deleted
|
||||||
self.assertTrue(os.path.isfile(filename))
|
self.assertTrue(os.path.isfile(filename))
|
||||||
@ -401,6 +433,8 @@ class TestConsumer(DirectoriesMixin, TestCase):
|
|||||||
self.assertEqual(document.title, "new docs")
|
self.assertEqual(document.title, "new docs")
|
||||||
self.assertEqual(document.filename, "none/new docs.pdf")
|
self.assertEqual(document.filename, "none/new docs.pdf")
|
||||||
|
|
||||||
|
self._assert_first_last_send_progress()
|
||||||
|
|
||||||
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{title}")
|
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{title}")
|
||||||
@mock.patch("documents.signals.handlers.generate_unique_filename")
|
@mock.patch("documents.signals.handlers.generate_unique_filename")
|
||||||
def testFilenameHandlingUnstableFormat(self, m):
|
def testFilenameHandlingUnstableFormat(self, m):
|
||||||
@ -424,6 +458,8 @@ class TestConsumer(DirectoriesMixin, TestCase):
|
|||||||
self.assertIsNotNone(os.path.isfile(document.title))
|
self.assertIsNotNone(os.path.isfile(document.title))
|
||||||
self.assertTrue(os.path.isfile(document.source_path))
|
self.assertTrue(os.path.isfile(document.source_path))
|
||||||
|
|
||||||
|
self._assert_first_last_send_progress()
|
||||||
|
|
||||||
@mock.patch("documents.consumer.DocumentClassifier")
|
@mock.patch("documents.consumer.DocumentClassifier")
|
||||||
def testClassifyDocument(self, m):
|
def testClassifyDocument(self, m):
|
||||||
correspondent = Correspondent.objects.create(name="test")
|
correspondent = Correspondent.objects.create(name="test")
|
||||||
@ -443,19 +479,26 @@ class TestConsumer(DirectoriesMixin, TestCase):
|
|||||||
self.assertIn(t1, document.tags.all())
|
self.assertIn(t1, document.tags.all())
|
||||||
self.assertNotIn(t2, document.tags.all())
|
self.assertNotIn(t2, document.tags.all())
|
||||||
|
|
||||||
|
self._assert_first_last_send_progress()
|
||||||
|
|
||||||
@override_settings(CONSUMER_DELETE_DUPLICATES=True)
|
@override_settings(CONSUMER_DELETE_DUPLICATES=True)
|
||||||
def test_delete_duplicate(self):
|
def test_delete_duplicate(self):
|
||||||
dst = self.get_test_file()
|
dst = self.get_test_file()
|
||||||
self.assertTrue(os.path.isfile(dst))
|
self.assertTrue(os.path.isfile(dst))
|
||||||
doc = self.consumer.try_consume_file(dst)
|
doc = self.consumer.try_consume_file(dst)
|
||||||
|
|
||||||
|
self._assert_first_last_send_progress()
|
||||||
|
|
||||||
self.assertFalse(os.path.isfile(dst))
|
self.assertFalse(os.path.isfile(dst))
|
||||||
self.assertIsNotNone(doc)
|
self.assertIsNotNone(doc)
|
||||||
|
|
||||||
|
self._send_progress.reset_mock()
|
||||||
|
|
||||||
dst = self.get_test_file()
|
dst = self.get_test_file()
|
||||||
self.assertTrue(os.path.isfile(dst))
|
self.assertTrue(os.path.isfile(dst))
|
||||||
self.assertRaises(ConsumerError, self.consumer.try_consume_file, dst)
|
self.assertRaises(ConsumerError, self.consumer.try_consume_file, dst)
|
||||||
self.assertFalse(os.path.isfile(dst))
|
self.assertFalse(os.path.isfile(dst))
|
||||||
|
self._assert_first_last_send_progress(last_status="FAILED")
|
||||||
|
|
||||||
@override_settings(CONSUMER_DELETE_DUPLICATES=False)
|
@override_settings(CONSUMER_DELETE_DUPLICATES=False)
|
||||||
def test_no_delete_duplicate(self):
|
def test_no_delete_duplicate(self):
|
||||||
@ -471,6 +514,8 @@ class TestConsumer(DirectoriesMixin, TestCase):
|
|||||||
self.assertRaises(ConsumerError, self.consumer.try_consume_file, dst)
|
self.assertRaises(ConsumerError, self.consumer.try_consume_file, dst)
|
||||||
self.assertTrue(os.path.isfile(dst))
|
self.assertTrue(os.path.isfile(dst))
|
||||||
|
|
||||||
|
self._assert_first_last_send_progress(last_status="FAILED")
|
||||||
|
|
||||||
|
|
||||||
class PreConsumeTestCase(TestCase):
|
class PreConsumeTestCase(TestCase):
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user