mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-11-03 19:17:13 -05:00 
			
		
		
		
	refactored the test cases to use a mixin for setting up temporary directories.
This commit is contained in:
		
							parent
							
								
									938499706c
								
							
						
					
					
						commit
						6834e563a8
					
				@ -7,14 +7,13 @@ from pathvalidate import ValidationError
 | 
				
			|||||||
from rest_framework.test import APITestCase
 | 
					from rest_framework.test import APITestCase
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from documents.models import Document, Correspondent, DocumentType, Tag
 | 
					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):
 | 
					    def setUp(self):
 | 
				
			||||||
        self.dirs = setup_directories()
 | 
					        super(DocumentApiTest, self).setUp()
 | 
				
			||||||
        self.addCleanup(remove_dirs, self.dirs)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        user = User.objects.create_superuser(username="temp_admin")
 | 
					        user = User.objects.create_superuser(username="temp_admin")
 | 
				
			||||||
        self.client.force_login(user=user)
 | 
					        self.client.force_login(user=user)
 | 
				
			||||||
 | 
				
			|||||||
@ -6,7 +6,7 @@ from unittest.mock import MagicMock
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
from django.test import TestCase, override_settings
 | 
					from django.test import TestCase, override_settings
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from .utils import setup_directories, remove_dirs
 | 
					from .utils import DirectoriesMixin
 | 
				
			||||||
from ..consumer import Consumer, ConsumerError
 | 
					from ..consumer import Consumer, ConsumerError
 | 
				
			||||||
from ..models import FileInfo, Tag, Correspondent, DocumentType, Document
 | 
					from ..models import FileInfo, Tag, Correspondent, DocumentType, Document
 | 
				
			||||||
from ..parsers import DocumentParser, ParseError
 | 
					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)
 | 
					@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):
 | 
					    def make_dummy_parser(self, path, logging_group):
 | 
				
			||||||
        return DummyParser(path, logging_group, self.dirs.scratch_dir)
 | 
					        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)
 | 
					        return FaultyParser(path, logging_group, self.dirs.scratch_dir)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def setUp(self):
 | 
					    def setUp(self):
 | 
				
			||||||
        self.dirs = setup_directories()
 | 
					        super(TestConsumer, self).setUp()
 | 
				
			||||||
        self.addCleanup(remove_dirs, self.dirs)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        patcher = mock.patch("documents.parsers.document_consumer_declaration.send")
 | 
					        patcher = mock.patch("documents.parsers.document_consumer_declaration.send")
 | 
				
			||||||
        m = patcher.start()
 | 
					        m = patcher.start()
 | 
				
			||||||
 | 
				
			|||||||
@ -7,11 +7,11 @@ from unittest import mock
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
from django.conf import settings
 | 
					from django.conf import settings
 | 
				
			||||||
from django.core.management import call_command, CommandError
 | 
					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.consumer import ConsumerError
 | 
				
			||||||
from documents.management.commands import document_consumer
 | 
					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):
 | 
					class ConsumerThread(Thread):
 | 
				
			||||||
@ -33,19 +33,17 @@ def chunked(size, source):
 | 
				
			|||||||
        yield source[i:i+size]
 | 
					        yield source[i:i+size]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class TestConsumer(TestCase):
 | 
					class TestConsumer(DirectoriesMixin, TestCase):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    sample_file = os.path.join(os.path.dirname(__file__), "samples", "simple.pdf")
 | 
					    sample_file = os.path.join(os.path.dirname(__file__), "samples", "simple.pdf")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def setUp(self) -> None:
 | 
					    def setUp(self) -> None:
 | 
				
			||||||
 | 
					        super(TestConsumer, self).setUp()
 | 
				
			||||||
        self.t = None
 | 
					        self.t = None
 | 
				
			||||||
        patcher = mock.patch("documents.management.commands.document_consumer.async_task")
 | 
					        patcher = mock.patch("documents.management.commands.document_consumer.async_task")
 | 
				
			||||||
        self.task_mock = patcher.start()
 | 
					        self.task_mock = patcher.start()
 | 
				
			||||||
        self.addCleanup(patcher.stop)
 | 
					        self.addCleanup(patcher.stop)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.dirs = setup_directories()
 | 
					 | 
				
			||||||
        self.addCleanup(remove_dirs, self.dirs)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def t_start(self):
 | 
					    def t_start(self):
 | 
				
			||||||
        self.t = ConsumerThread()
 | 
					        self.t = ConsumerThread()
 | 
				
			||||||
        self.t.start()
 | 
					        self.t.start()
 | 
				
			||||||
@ -59,7 +57,7 @@ class TestConsumer(TestCase):
 | 
				
			|||||||
            # wait for the consumer to exit.
 | 
					            # wait for the consumer to exit.
 | 
				
			||||||
            self.t.join()
 | 
					            self.t.join()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        remove_dirs(self.dirs)
 | 
					        super(TestConsumer, self).tearDown()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def wait_for_task_mock_call(self):
 | 
					    def wait_for_task_mock_call(self):
 | 
				
			||||||
        n = 0
 | 
					        n = 0
 | 
				
			||||||
 | 
				
			|||||||
@ -39,3 +39,18 @@ def remove_dirs(dirs):
 | 
				
			|||||||
    shutil.rmtree(dirs.data_dir, ignore_errors=True)
 | 
					    shutil.rmtree(dirs.data_dir, ignore_errors=True)
 | 
				
			||||||
    shutil.rmtree(dirs.scratch_dir, ignore_errors=True)
 | 
					    shutil.rmtree(dirs.scratch_dir, ignore_errors=True)
 | 
				
			||||||
    shutil.rmtree(dirs.consumption_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)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user