Add settings class cache invalidation method. Update common.tests.base.BaseTestCase

class to invalidate settings cache. Update tests using Django's TestCase to
BaseTestCase.
This commit is contained in:
Roberto Rosario
2017-01-14 00:13:35 -04:00
parent 53d356d992
commit 4e7559d396
14 changed files with 105 additions and 50 deletions

View File

@@ -3,8 +3,9 @@ from __future__ import absolute_import, unicode_literals
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from django.core.exceptions import PermissionDenied
from django.test import TestCase, override_settings
from django.test import override_settings
from common.tests import BaseTestCase
from documents.models import Document, DocumentType
from documents.permissions import permission_document_view
from documents.tests import TEST_SMALL_DOCUMENT_PATH, TEST_DOCUMENT_TYPE
@@ -19,8 +20,9 @@ TEST_DOCUMENT_TYPE_2 = 'test document type 2'
@override_settings(OCR_AUTO_OCR=False)
class PermissionTestCase(TestCase):
class PermissionTestCase(BaseTestCase):
def setUp(self):
super(PermissionTestCase).setUp()
self.document_type_1 = DocumentType.objects.create(
label=TEST_DOCUMENT_TYPE
)
@@ -53,9 +55,9 @@ class PermissionTestCase(TestCase):
self.group.user_set.add(self.user)
self.role.groups.add(self.group)
Permission.invalidate_cache()
def tearDown(self):
super(PermissionTestCase).tearDown()
for document_type in DocumentType.objects.all():
document_type.delete()

View File

@@ -3,9 +3,10 @@ from __future__ import absolute_import, unicode_literals
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.urlresolvers import reverse
from django.test import TestCase, override_settings
from django.test.client import Client
from django.test import override_settings
from common.tests import BaseTestCase
from smart_settings.classes import Namespace
from user_management.tests.literals import (
TEST_ADMIN_EMAIL, TEST_ADMIN_PASSWORD, TEST_ADMIN_USERNAME
)
@@ -13,20 +14,22 @@ from user_management.tests.literals import (
from .literals import TEST_EMAIL_AUTHENTICATION_BACKEND
class UserLoginTestCase(TestCase):
class UserLoginTestCase(BaseTestCase):
"""
Test that users can login via the supported authentication methods
"""
def setUp(self):
super(UserLoginTestCase, self).setUp()
self.admin_user = get_user_model().objects.create_superuser(
username=TEST_ADMIN_USERNAME, email=TEST_ADMIN_EMAIL,
password=TEST_ADMIN_PASSWORD
)
self.client = Client()
Namespace.invalidate_cache_all()
@override_settings(AUTHENTICATION_LOGIN_METHOD='username')
def test_normal_behaviour(self):
def test_normal_behavior(self):
response = self.client.get(reverse('documents:document_list'))
self.assertRedirects(
response,

View File

@@ -4,7 +4,7 @@ import datetime
import time
from django.contrib.auth import get_user_model
from django.test import TestCase, override_settings
from django.test import override_settings
from django.utils.timezone import now
from common.tests import BaseTestCase
@@ -24,8 +24,9 @@ from ..models import DocumentCheckout, NewVersionBlock
@override_settings(OCR_AUTO_OCR=False)
class DocumentCheckoutTestCase(TestCase):
class DocumentCheckoutTestCase(BaseTestCase):
def setUp(self):
super(DocumentCheckoutTestCase).setUp()
self.admin_user = get_user_model().objects.create_superuser(
username=TEST_ADMIN_USERNAME, email=TEST_ADMIN_EMAIL,
password=TEST_ADMIN_PASSWORD
@@ -41,6 +42,7 @@ class DocumentCheckoutTestCase(TestCase):
)
def tearDown(self):
super(DocumentCheckoutTestCase).tearDown()
self.document_type.delete()
def test_document_checkout(self):

View File

@@ -1,11 +1,21 @@
from __future__ import unicode_literals
from __future__ import absolute_import, unicode_literals
from django.test import TestCase
from .mixins import ContentTypeCheckMixin, OpenFileCheckMixin, TempfileCheckMixin
from permissions.classes import Permission
from smart_settings.classes import Namespace
from .mixins import (
ContentTypeCheckMixin, OpenFileCheckMixin, TempfileCheckMixin
)
class BaseTestCase(ContentTypeCheckMixin, OpenFileCheckMixin, TempfileCheckMixin, TestCase):
"""
This is the most basic test case class any test in the project should use.
"""
def setUp(self):
super(BaseTestCase, self).setUp()
Namespace.invalidate_cache_all()
Permission.invalidate_cache()

View File

@@ -1,8 +1,9 @@
from __future__ import unicode_literals
from django.core.files.base import File
from django.test import TestCase, override_settings
from django.test import override_settings
from common.tests import BaseTestCase
from documents.models import DocumentType
from documents.tests import TEST_SMALL_DOCUMENT_PATH, TEST_DOCUMENT_TYPE
from metadata.models import MetadataType, DocumentTypeMetadataType
@@ -16,8 +17,9 @@ from .literals import (
@override_settings(OCR_AUTO_OCR=False)
class IndexTestCase(TestCase):
class IndexTestCase(BaseTestCase):
def setUp(self):
super(IndexTestCase).setUp()
self.document_type = DocumentType.objects.create(
label=TEST_DOCUMENT_TYPE
)
@@ -28,6 +30,7 @@ class IndexTestCase(TestCase):
)
def tearDown(self):
super(IndexTestCase).tearDown()
self.document_type.delete()
def test_indexing(self):

View File

@@ -4,8 +4,9 @@ import hashlib
import time
from django.core.files import File
from django.test import TestCase, override_settings
from django.test import override_settings
from common.tests import BaseTestCase
from django_gpg.models import Key
from django_gpg.tests.literals import TEST_KEY_DATA, TEST_KEY_PASSPHRASE
from documents.models import DocumentType, DocumentVersion
@@ -21,13 +22,15 @@ from .literals import (
@override_settings(OCR_AUTO_OCR=False)
class DocumentSignaturesTestCase(TestCase):
class DocumentSignaturesTestCase(BaseTestCase):
def setUp(self):
super(DocumentSignaturesTestCase).setUp()
self.document_type = DocumentType.objects.create(
label=TEST_DOCUMENT_TYPE
)
def tearDown(self):
super(DocumentSignaturesTestCase).tearDown()
self.document_type.delete()
def test_embedded_signature_no_key(self):
@@ -247,13 +250,17 @@ class DocumentSignaturesTestCase(TestCase):
@override_settings(OCR_AUTO_OCR=False)
class EmbeddedSignaturesTestCase(TestCase):
class EmbeddedSignaturesTestCase(BaseTestCase):
def setUp(self):
super(EmbeddedSignaturesTestCase).setUp()
self.document_type = DocumentType.objects.create(
label=TEST_DOCUMENT_TYPE
)
def tearDown(self):
super(EmbeddedSignaturesTestCase).tearDown()
self.document_type.delete()
def test_unsigned_document_version_method(self):

View File

@@ -1,7 +1,8 @@
from __future__ import unicode_literals
from django.test import TestCase, override_settings
from django.test import override_settings
from common.tests import BaseTestCase
from documents.models import DocumentType
from documents.tests import TEST_DOCUMENT_PATH, TEST_DOCUMENT_TYPE
@@ -11,8 +12,9 @@ from .literals import TEST_FOLDER_LABEL
@override_settings(OCR_AUTO_OCR=False)
class FolderTestCase(TestCase):
class FolderTestCase(BaseTestCase):
def setUp(self):
super(FolderTestCase).setUp()
self.document_type = DocumentType.objects.create(
label=TEST_DOCUMENT_TYPE
)
@@ -23,6 +25,8 @@ class FolderTestCase(TestCase):
)
def tearDown(self):
super(FolderTestCase).tearDown()
self.document_type.delete()
def test_folder_creation(self):

View File

@@ -3,8 +3,9 @@
from __future__ import unicode_literals
from django.contrib.auth import get_user_model
from django.test import TestCase, override_settings
from django.test import override_settings
from common.tests import BaseTestCase
from documents.models import DocumentType
from documents.tests import TEST_DOCUMENT_PATH, TEST_DOCUMENT_TYPE
from user_management.tests.literals import (
@@ -17,8 +18,9 @@ from .literals import TEST_SMART_LINK_LABEL, TEST_SMART_LINK_DYNAMIC_LABEL
@override_settings(OCR_AUTO_OCR=False)
class SmartLinkTestCase(TestCase):
class SmartLinkTestCase(BaseTestCase):
def setUp(self):
super(SmartLinkTestCase).setUp()
self.document_type = DocumentType.objects.create(
label=TEST_DOCUMENT_TYPE
)
@@ -34,6 +36,8 @@ class SmartLinkTestCase(TestCase):
)
def tearDown(self):
super(SmartLinkTestCase).tearDown()
self.document_type.delete()
def test_dynamic_label(self):

View File

@@ -2,8 +2,9 @@ from __future__ import unicode_literals
from django.core.files.base import File
from django.core.exceptions import ValidationError
from django.test import TestCase, override_settings
from django.test import override_settings
from common.tests import BaseTestCase
from documents.models import DocumentType
from documents.tests import TEST_SMALL_DOCUMENT_PATH, TEST_DOCUMENT_TYPE
@@ -18,8 +19,9 @@ from .literals import (
@override_settings(OCR_AUTO_OCR=False)
class MetadataTestCase(TestCase):
class MetadataTestCase(BaseTestCase):
def setUp(self):
super(MetadataTestCase).setUp()
self.document_type = DocumentType.objects.create(
label=TEST_DOCUMENT_TYPE
)
@@ -36,6 +38,8 @@ class MetadataTestCase(TestCase):
)
def tearDown(self):
super(MetadataTestCase).tearDown()
self.document_type.delete()
def test_no_default(self):

View File

@@ -16,14 +16,6 @@ logger = logging.getLogger(__name__)
class Namespace(object):
_registry = {}
@classmethod
def get_all(cls):
return cls._registry.values()
@classmethod
def get(cls, name):
return cls._registry[name]
@staticmethod
def initialize():
for app in apps.get_app_configs():
@@ -36,6 +28,19 @@ class Namespace(object):
'Imported settings.py file for app %s', app.name
)
@classmethod
def get_all(cls):
return cls._registry.values()
@classmethod
def get(cls, name):
return cls._registry[name]
@classmethod
def invalidate_cache_all(cls):
for namespace in cls.get_all():
namespace.invalidate_cache()
def __unicode__(self):
return unicode(self.label)
@@ -52,8 +57,16 @@ class Namespace(object):
def add_setting(self, **kwargs):
return Setting(namespace=self, **kwargs)
def invalidate_cache(self):
for setting in self.settings:
setting.invalidate_cache()
class Setting(object):
@staticmethod
def deserialize_value(value):
return yaml.safe_load(value)
@staticmethod
def serialize_value(value):
if isinstance(value, Promise):
@@ -61,10 +74,6 @@ class Setting(object):
return yaml.safe_dump(value, allow_unicode=True)
@staticmethod
def deserialize_value(value):
return yaml.safe_load(value)
def __init__(self, namespace, global_name, default, help_text=None, is_path=False):
self.global_name = global_name
self.default = default
@@ -75,6 +84,9 @@ class Setting(object):
def __unicode__(self):
return unicode(self.global_name)
def invalidate_cache(self):
self.loaded = False
@property
def serialized_value(self):
return self.yaml

View File

@@ -4,10 +4,11 @@ import shutil
from django.contrib.auth import get_user_model
from django.core.files.base import File
from django.test import TestCase, override_settings
from django.test import override_settings
from django.test.client import Client
from common.utils import mkdtemp
from common.tests import BaseTestCase
from documents.models import Document, DocumentType
from documents.tests import (
TEST_COMPRESSED_DOCUMENT_PATH, TEST_DOCUMENT_TYPE,
@@ -23,12 +24,13 @@ from ..models import WatchFolderSource, WebFormSource
@override_settings(OCR_AUTO_OCR=False)
class UploadDocumentTestCase(TestCase):
class UploadDocumentTestCase(BaseTestCase):
"""
Test creating documents
"""
def setUp(self):
super(UploadDocumentTestCase).setUp()
self.document_type = DocumentType.objects.create(
label=TEST_DOCUMENT_TYPE
)
@@ -40,6 +42,8 @@ class UploadDocumentTestCase(TestCase):
self.client = Client()
def tearDown(self):
super(UploadDocumentTestCase).tearDown()
self.document_type.delete()
self.admin_user.delete()

View File

@@ -6,10 +6,11 @@ import shutil
from django.contrib.auth import get_user_model
from django.core.urlresolvers import reverse
from django.test.client import Client
from django.test import TestCase, override_settings
from django.test import override_settings
from acls.models import AccessControlList
from checkouts.models import NewVersionBlock
from common.tests import BaseTestCase
from common.tests.test_views import GenericViewTestCase
from common.utils import fs_cleanup, mkdtemp
from documents.models import Document, DocumentType
@@ -118,8 +119,9 @@ class DocumentUploadTestCase(GenericDocumentViewTestCase):
@override_settings(OCR_AUTO_OCR=False)
class DocumentUploadIssueTestCase(TestCase):
class DocumentUploadIssueTestCase(BaseTestCase):
def setUp(self):
super(DocumentUploadIssueTestCase, self).setUp()
self.document_type = DocumentType.objects.create(
label=TEST_DOCUMENT_TYPE
)
@@ -131,6 +133,7 @@ class DocumentUploadIssueTestCase(TestCase):
self.client = Client()
def tearDown(self):
super(DocumentUploadIssueTestCase, self).tearDown()
self.document_type.delete()
def test_issue_25(self):

View File

@@ -1,8 +1,9 @@
from __future__ import unicode_literals
from django.core.files.base import File
from django.test import TestCase, override_settings
from django.test import override_settings
from common.tests import BaseTestCase
from documents.models import DocumentType
from documents.tests import TEST_DOCUMENT_TYPE, TEST_SMALL_DOCUMENT_PATH
@@ -12,8 +13,9 @@ from .literals import TEST_TAG_COLOR, TEST_TAG_LABEL
@override_settings(OCR_AUTO_OCR=False)
class TagTestCase(TestCase):
class TagTestCase(BaseTestCase):
def setUp(self):
super(TagTestCase).setUp()
self.document_type = DocumentType.objects.create(
label=TEST_DOCUMENT_TYPE
)
@@ -24,6 +26,7 @@ class TagTestCase(TestCase):
)
def tearDown(self):
super(TagTestCase).tearDown()
self.document.delete()
self.document_type.delete()

View File

@@ -186,11 +186,9 @@ class MetadataLookupIntegrationTestCase(GenericDocumentViewTestCase):
self.document_type.metadata.create(metadata_type=self.metadata_type)
def test_user_list_lookup_render(self):
self.login(
username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD
)
self.login_user()
def test_user_list_lookup_render(self):
self.metadata_type.lookup = '{{ users }}'
self.metadata_type.save()
self.document.metadata.create(metadata_type=self.metadata_type)
@@ -209,10 +207,6 @@ class MetadataLookupIntegrationTestCase(GenericDocumentViewTestCase):
)
def test_group_list_lookup_render(self):
self.login(
username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD
)
self.metadata_type.lookup = '{{ groups }}'
self.metadata_type.save()
self.document.metadata.create(metadata_type=self.metadata_type)