diff --git a/src/documents/tests/test_consumer.py b/src/documents/tests/test_consumer.py index 54d20ca8b..22e6afb61 100644 --- a/src/documents/tests/test_consumer.py +++ b/src/documents/tests/test_consumer.py @@ -212,6 +212,20 @@ def fake_magic_from_file(file, mime=False): @mock.patch("documents.consumer.magic.from_file", fake_magic_from_file) 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): 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. patcher = mock.patch("documents.consumer.Consumer._send_progress") - patcher.start() + self._send_progress = patcher.start() self.addCleanup(patcher.stop) self.consumer = Consumer() @@ -278,6 +292,8 @@ class TestConsumer(DirectoriesMixin, TestCase): self.assertFalse(os.path.isfile(filename)) + self._assert_first_last_send_progress() + def testOverrideFilename(self): filename = self.get_test_file() override_filename = "Statement for November.pdf" @@ -286,21 +302,26 @@ class TestConsumer(DirectoriesMixin, TestCase): self.assertEqual(document.title, "Statement for November") + self._assert_first_last_send_progress() + def testOverrideTitle(self): document = self.consumer.try_consume_file(self.get_test_file(), override_title="Override Title") self.assertEqual(document.title, "Override Title") + self._assert_first_last_send_progress() def testOverrideCorrespondent(self): c = Correspondent.objects.create(name="test") document = self.consumer.try_consume_file(self.get_test_file(), override_correspondent_id=c.pk) self.assertEqual(document.correspondent.id, c.id) + self._assert_first_last_send_progress() def testOverrideDocumentType(self): dt = DocumentType.objects.create(name="test") 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._assert_first_last_send_progress() def testOverrideTags(self): t1 = Tag.objects.create(name="t1") @@ -311,37 +332,42 @@ class TestConsumer(DirectoriesMixin, TestCase): self.assertIn(t1, document.tags.all()) self.assertNotIn(t2, document.tags.all()) self.assertIn(t3, document.tags.all()) + self._assert_first_last_send_progress() 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): self.consumer.try_consume_file(self.get_test_file()) - try: - self.consumer.try_consume_file(self.get_test_file()) - except ConsumerError as e: - self.assertTrue(str(e).endswith("It is a duplicate.")) - return + self.assertRaisesMessage( + ConsumerError, + "It is a duplicate", + self.consumer.try_consume_file, + self.get_test_file() + ) - self.fail("Should throw exception") + self._assert_first_last_send_progress(last_status="FAILED") def testDuplicates2(self): self.consumer.try_consume_file(self.get_test_file()) - try: - self.consumer.try_consume_file(self.get_test_archive_file()) - except ConsumerError as e: - self.assertTrue(str(e).endswith("It is a duplicate.")) - return + self.assertRaisesMessage( + ConsumerError, + "It is a duplicate", + self.consumer.try_consume_file, + self.get_test_archive_file() + ) - self.fail("Should throw exception") + self._assert_first_last_send_progress(last_status="FAILED") def testDuplicates3(self): self.consumer.try_consume_file(self.get_test_archive_file()) @@ -351,13 +377,15 @@ class TestConsumer(DirectoriesMixin, TestCase): def testNoParsers(self, m): m.return_value = [] - try: - self.consumer.try_consume_file(self.get_test_file()) - except ConsumerError as e: - self.assertEqual(str(e), "sample.pdf: Unsupported mime type application/pdf") - return + self.assertRaisesMessage( + ConsumerError, + "sample.pdf: Unsupported mime type application/pdf", + self.consumer.try_consume_file, + 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") def testFaultyParser(self, m): @@ -367,24 +395,28 @@ class TestConsumer(DirectoriesMixin, TestCase): "weight": 0 })] - try: - self.consumer.try_consume_file(self.get_test_file()) - except ConsumerError as e: - self.assertEqual(str(e), "sample.pdf: Error while consuming document sample.pdf: Does not compute.") - return + self.assertRaisesMessage( + ConsumerError, + "sample.pdf: Error while consuming document sample.pdf: Does not compute.", + self.consumer.try_consume_file, + self.get_test_file() + ) - self.fail("Should throw exception.") + self._assert_first_last_send_progress(last_status="FAILED") @mock.patch("documents.consumer.Consumer._write") def testPostSaveError(self, m): filename = self.get_test_file() m.side_effect = OSError("NO.") - try: - self.consumer.try_consume_file(filename) - except ConsumerError as e: - self.assertEqual(str(e), "sample.pdf: The following error occured while consuming sample.pdf: NO.") - else: - self.fail("Should raise exception") + + self.assertRaisesMessage( + ConsumerError, + "sample.pdf: The following error occured while consuming sample.pdf: NO.", + self.consumer.try_consume_file, + filename + ) + + self._assert_first_last_send_progress(last_status="FAILED") # file not deleted self.assertTrue(os.path.isfile(filename)) @@ -401,6 +433,8 @@ class TestConsumer(DirectoriesMixin, TestCase): self.assertEqual(document.title, "new docs") self.assertEqual(document.filename, "none/new docs.pdf") + self._assert_first_last_send_progress() + @override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{title}") @mock.patch("documents.signals.handlers.generate_unique_filename") def testFilenameHandlingUnstableFormat(self, m): @@ -424,6 +458,8 @@ class TestConsumer(DirectoriesMixin, TestCase): self.assertIsNotNone(os.path.isfile(document.title)) self.assertTrue(os.path.isfile(document.source_path)) + self._assert_first_last_send_progress() + @mock.patch("documents.consumer.DocumentClassifier") def testClassifyDocument(self, m): correspondent = Correspondent.objects.create(name="test") @@ -443,19 +479,26 @@ class TestConsumer(DirectoriesMixin, TestCase): self.assertIn(t1, document.tags.all()) self.assertNotIn(t2, document.tags.all()) + self._assert_first_last_send_progress() + @override_settings(CONSUMER_DELETE_DUPLICATES=True) def test_delete_duplicate(self): dst = self.get_test_file() self.assertTrue(os.path.isfile(dst)) doc = self.consumer.try_consume_file(dst) + self._assert_first_last_send_progress() + self.assertFalse(os.path.isfile(dst)) self.assertIsNotNone(doc) + self._send_progress.reset_mock() + dst = self.get_test_file() self.assertTrue(os.path.isfile(dst)) self.assertRaises(ConsumerError, self.consumer.try_consume_file, dst) self.assertFalse(os.path.isfile(dst)) + self._assert_first_last_send_progress(last_status="FAILED") @override_settings(CONSUMER_DELETE_DUPLICATES=False) def test_no_delete_duplicate(self): @@ -471,6 +514,8 @@ class TestConsumer(DirectoriesMixin, TestCase): self.assertRaises(ConsumerError, self.consumer.try_consume_file, dst) self.assertTrue(os.path.isfile(dst)) + self._assert_first_last_send_progress(last_status="FAILED") + class PreConsumeTestCase(TestCase):