A community-supported supercharged document management system: scan, index and archive all your documents
Python aiangulararchivingdjangodmsdocument-managementdocument-management-systemllmmachine-learningocroptical-character-recognitionpdf
refactored the test cases to use a mixin for setting up temporary directories.
jonaswinkler6834e563a880
diff --git a/src/documents/tests/test_api.py b/src/documents/tests/test_api.pyindex d9a2aac26..37f774891 100644--- a/src/documents/tests/test_api.py+++ b/src/documents/tests/test_api.py@@ -7,14 +7,13 @@ from pathvalidate import ValidationErrorfrom rest_framework.test import APITestCasefrom documents.models import Document, Correspondent, DocumentType, Tag-from documents.tests.utils import setup_directories, remove_dirs+from documents.tests.utils import DirectoriesMixin-class DocumentApiTest(APITestCase):+class DocumentApiTest(DirectoriesMixin, APITestCase):def setUp(self):- self.dirs = setup_directories()- self.addCleanup(remove_dirs, self.dirs)+ super(DocumentApiTest, self).setUp()user = User.objects.create_superuser(username="temp_admin")self.client.force_login(user=user)diff --git a/src/documents/tests/test_consumer.py b/src/documents/tests/test_consumer.pyindex ed835a467..d1cd0adf1 100644--- a/src/documents/tests/test_consumer.py+++ b/src/documents/tests/test_consumer.py@@ -6,7 +6,7 @@ from unittest.mock import MagicMockfrom django.test import TestCase, override_settings-from .utils import setup_directories, remove_dirs+from .utils import DirectoriesMixinfrom ..consumer import Consumer, ConsumerErrorfrom ..models import FileInfo, Tag, Correspondent, DocumentType, Documentfrom ..parsers import DocumentParser, ParseError@@ -408,7 +408,7 @@ def fake_magic_from_file(file, mime=False):@mock.patch("documents.consumer.magic.from_file", fake_magic_from_file)-class TestConsumer(TestCase):+class TestConsumer(DirectoriesMixin, TestCase):def make_dummy_parser(self, path, logging_group):return DummyParser(path, logging_group, self.dirs.scratch_dir)@@ -417,8 +417,7 @@ class TestConsumer(TestCase):return FaultyParser(path, logging_group, self.dirs.scratch_dir)def setUp(self):- self.dirs = setup_directories()- self.addCleanup(remove_dirs, self.dirs)+ super(TestConsumer, self).setUp()patcher = mock.patch("documents.parsers.document_consumer_declaration.send")m = patcher.start()diff --git a/src/documents/tests/test_management_consumer.py b/src/documents/tests/test_management_consumer.pyindex 2d45acd01..aed824926 100644--- a/src/documents/tests/test_management_consumer.py+++ b/src/documents/tests/test_management_consumer.py@@ -7,11 +7,11 @@ from unittest import mockfrom django.conf import settingsfrom django.core.management import call_command, CommandError-from django.test import TestCase, override_settings+from django.test import override_settings, TestCasefrom documents.consumer import ConsumerErrorfrom documents.management.commands import document_consumer-from documents.tests.utils import setup_directories, remove_dirs+from documents.tests.utils import DirectoriesMixinclass ConsumerThread(Thread):@@ -33,19 +33,17 @@ def chunked(size, source):yield source[i:i+size]-class TestConsumer(TestCase):+class TestConsumer(DirectoriesMixin, TestCase):sample_file = os.path.join(os.path.dirname(__file__), "samples", "simple.pdf")def setUp(self) -> None:+ super(TestConsumer, self).setUp()self.t = Nonepatcher = mock.patch("documents.management.commands.document_consumer.async_task")self.task_mock = patcher.start()self.addCleanup(patcher.stop)- self.dirs = setup_directories()- self.addCleanup(remove_dirs, self.dirs)-def t_start(self):self.t = ConsumerThread()self.t.start()@@ -59,7 +57,7 @@ class TestConsumer(TestCase):# wait for the consumer to exit.self.t.join()- remove_dirs(self.dirs)+ super(TestConsumer, self).tearDown()def wait_for_task_mock_call(self):n = 0diff --git a/src/documents/tests/utils.py b/src/documents/tests/utils.pyindex 7b0938ee3..83148e9c7 100644--- a/src/documents/tests/utils.py+++ b/src/documents/tests/utils.py@@ -39,3 +39,18 @@ def remove_dirs(dirs):shutil.rmtree(dirs.data_dir, ignore_errors=True)shutil.rmtree(dirs.scratch_dir, ignore_errors=True)shutil.rmtree(dirs.consumption_dir, ignore_errors=True)+++class DirectoriesMixin:++ def __init__(self, *args, **kwargs):+ super().__init__(*args, **kwargs)+ self.dirs = None++ def setUp(self) -> None:+ self.dirs = setup_directories()+ super(DirectoriesMixin, self).setUp()++ def tearDown(self) -> None:+ super(DirectoriesMixin, self).tearDown()+ remove_dirs(self.dirs)