diff --git a/mayan/apps/documents/tests/__init__.py b/mayan/apps/documents/tests/__init__.py index 68da177dbd..57c2836216 100644 --- a/mayan/apps/documents/tests/__init__.py +++ b/mayan/apps/documents/tests/__init__.py @@ -1,2 +1,2 @@ -from .base import GenericDocumentViewTestCase # NOQA +from .base import GenericDocumentTestCase, GenericDocumentViewTestCase # NOQA from .literals import * # NOQA diff --git a/mayan/apps/documents/tests/base.py b/mayan/apps/documents/tests/base.py index 99c8e1ebb5..4f9eecc7b7 100644 --- a/mayan/apps/documents/tests/base.py +++ b/mayan/apps/documents/tests/base.py @@ -2,77 +2,20 @@ from __future__ import unicode_literals -import os - -from django.conf import settings from django.test import override_settings from common.tests import BaseTestCase, GenericViewTestCase -from ..models import DocumentType - -from .literals import ( - TEST_DOCUMENT_TYPE_LABEL, TEST_SMALL_DOCUMENT_FILENAME, - TEST_SMALL_DOCUMENT_PATH, -) +from .mixins import DocumentTestMixin +@override_settings(DOCUMENT_PARSING_AUTO_PARSING=False) @override_settings(OCR_AUTO_OCR=False) -class GenericDocumentTestCase(BaseTestCase): - auto_create_document_type = True - auto_upload_document = True - test_document_filename = TEST_SMALL_DOCUMENT_FILENAME - - def create_document_type(self): - self.document_type = DocumentType.objects.create( - label=TEST_DOCUMENT_TYPE_LABEL - ) - - def upload_document(self): - with open(self.test_document_path) as file_object: - document = self.document_type.new_document( - file_object=file_object, label=self.test_document_filename - ) - return document - - def setUp(self): - super(GenericDocumentTestCase, self).setUp() - self.test_document_path = os.path.join( - settings.BASE_DIR, 'apps', 'documents', 'tests', 'contrib', - 'sample_documents', self.test_document_filename - ) - - if self.auto_create_document_type: - self.create_document_type() - - if self.auto_upload_document: - self.document = self.upload_document() - - def tearDown(self): - self.document_type.delete() - super(GenericDocumentTestCase, self).tearDown() +class GenericDocumentTestCase(DocumentTestMixin, BaseTestCase): + """Base test case when testing models or classes""" +@override_settings(DOCUMENT_PARSING_AUTO_PARSING=False) @override_settings(OCR_AUTO_OCR=False) -class GenericDocumentViewTestCase(GenericViewTestCase): - test_document_filename = TEST_SMALL_DOCUMENT_FILENAME - test_document_path = TEST_SMALL_DOCUMENT_PATH - - def upload_document(self): - with open(self.test_document_path) as file_object: - document = self.document_type.new_document( - file_object=file_object, label=self.test_document_filename - ) - return document - - def setUp(self): - super(GenericDocumentViewTestCase, self).setUp() - self.document_type = DocumentType.objects.create( - label=TEST_DOCUMENT_TYPE_LABEL - ) - self.document = self.upload_document() - - def tearDown(self): - if self.document_type.pk: - self.document_type.delete() - super(GenericDocumentViewTestCase, self).tearDown() +class GenericDocumentViewTestCase(DocumentTestMixin, GenericViewTestCase): + """Base test case when testing views""" diff --git a/mayan/apps/documents/tests/mixins.py b/mayan/apps/documents/tests/mixins.py new file mode 100644 index 0000000000..13e51c08c0 --- /dev/null +++ b/mayan/apps/documents/tests/mixins.py @@ -0,0 +1,45 @@ +from __future__ import unicode_literals + +import os + +from django.conf import settings + +from ..models import DocumentType + +from .literals import TEST_DOCUMENT_TYPE_LABEL, TEST_SMALL_DOCUMENT_FILENAME + + +class DocumentTestMixin(object): + auto_create_document_type = True + auto_upload_document = True + test_document_filename = TEST_SMALL_DOCUMENT_FILENAME + + def create_document_type(self): + self.document_type = DocumentType.objects.create( + label=TEST_DOCUMENT_TYPE_LABEL + ) + + def upload_document(self): + with open(self.test_document_path) as file_object: + document = self.document_type.new_document( + file_object=file_object, label=self.test_document_filename + ) + return document + + def setUp(self): + super(DocumentTestMixin, self).setUp() + self.test_document_path = os.path.join( + settings.BASE_DIR, 'apps', 'documents', 'tests', 'contrib', + 'sample_documents', self.test_document_filename + ) + + if self.auto_create_document_type: + self.create_document_type() + + if self.auto_upload_document: + self.document = self.upload_document() + + def tearDown(self): + for document_type in DocumentType.objects.all(): + document_type.delete() + super(DocumentTestMixin, self).tearDown()