Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

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.py
index d9a2aac26..37f774891 100644
--- a/src/documents/tests/test_api.py
+++ b/src/documents/tests/test_api.py
@@ -7,14 +7,13 @@ from pathvalidate import ValidationError
from rest_framework.test import APITestCase
from 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.py
index 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 MagicMock
from django.test import TestCase, override_settings
-from .utils import setup_directories, remove_dirs
+from .utils import DirectoriesMixin
from ..consumer import Consumer, ConsumerError
from ..models import FileInfo, Tag, Correspondent, DocumentType, Document
from ..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.py
index 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 mock
from django.conf import settings
from django.core.management import call_command, CommandError
-from django.test import TestCase, override_settings
+from django.test import override_settings, TestCase
from documents.consumer import ConsumerError
from documents.management.commands import document_consumer
-from documents.tests.utils import setup_directories, remove_dirs
+from documents.tests.utils import DirectoriesMixin
class 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 = None
patcher = 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 = 0
diff --git a/src/documents/tests/utils.py b/src/documents/tests/utils.py
index 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)