Improve the document base test case for models and for views. Add document test mixin.
Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
@@ -1,2 +1,2 @@
|
||||
from .base import GenericDocumentViewTestCase # NOQA
|
||||
from .base import GenericDocumentTestCase, GenericDocumentViewTestCase # NOQA
|
||||
from .literals import * # NOQA
|
||||
|
||||
@@ -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"""
|
||||
|
||||
45
mayan/apps/documents/tests/mixins.py
Normal file
45
mayan/apps/documents/tests/mixins.py
Normal file
@@ -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()
|
||||
Reference in New Issue
Block a user