AppTestCase   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
dl 0
loc 43
c 1
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
F setUpClass() 0 34 9
1
from __future__ import print_function
2
from django.conf import settings
3
from django.contrib.auth import get_user_model
4
from django.core.management import call_command
5
from django.contrib.sites.models import Site
6
from django.test import TestCase
7
from django.test.utils import override_settings
8
import os
9
from parler import appsettings
10
11
try:
12
    from importlib import import_module
13
except ImportError:
14
    from django.utils.importlib import import_module  # Python 2.6
15
16
try:
17
    User = get_user_model()
18
except ImportError:  # django < 1.5
19
    from django.contrib.auth.models import User
20
21
22
def clear_cache():
23
    """
24
    Clear internal cache of apps loading
25
    """
26
    import django
27
    if django.VERSION >= (1.7):
28
        try:
29
            from django.db.models import loading
30
            loading.cache.loaded = False
31
        except ImportError:  # Django >= 1.9
32
            pass
33
    else:
34
        from django.apps import apps
35
        apps.clear_cache()
36
37
38
class override_parler_settings(override_settings):
39
    """
40
    Make sure the parler.appsettings is also updated with override_settings()
41
    """
42
43
    def __init__(self, **kwargs):
44
        super(override_parler_settings, self).__init__(**kwargs)
45
        self.old_values = {}
46
47
    def enable(self):
48
        super(override_parler_settings, self).enable()
49
        for key, value in self.options.items():
50
            self.old_values[key] = getattr(appsettings, key)
51
            setattr(appsettings, key, value)
52
53
    def disable(self):
54
        super(override_parler_settings, self).disable()
55
        for key in self.options.keys():
56
            setattr(appsettings, key, self.old_values[key])
57
58
59
class AppTestCase(TestCase):
60
    """
61
    Tests for URL resolving.
62
    """
63
    user = None
64
    install_apps = (
65
        'parler.tests.testapp',
66
    )
67
68
    @classmethod
69
    def setUpClass(cls):
70
        super(AppTestCase, cls).setUpClass()
71
72
        from django.template.loaders import app_directories  # late import, for django 1.7
73
        if cls.install_apps:
74
            # When running this app via `./manage.py test fluent_pages`, auto install the test app + models.
75
            run_syncdb = False
76
            for appname in cls.install_apps:
77
                if appname not in settings.INSTALLED_APPS:
78
                    print('Adding {0} to INSTALLED_APPS'.format(appname))
79
                    settings.INSTALLED_APPS = (appname,) + tuple(settings.INSTALLED_APPS)
80
                    run_syncdb = True
81
82
                    # Flush caches
83
                    testapp = import_module(appname)
84
                    clear_cache()
85
                    app_directories.app_template_dirs += (
86
                        os.path.join(os.path.dirname(testapp.__file__), 'templates'),
87
                    )
88
89
            if run_syncdb:
90
                call_command('syncdb', verbosity=0)  # may run south's overlaid version
91
92
        # Create basic objects
93
        # 1.4 does not create site automatically with the defined SITE_ID, 1.3 does.
94
        Site.objects.get_or_create(id=settings.SITE_ID, defaults=dict(domain='django.localhost', name='django at localhost'))
95
        cls.user, _ = User.objects.get_or_create(is_superuser=True, is_staff=True, username="admin")
96
97
        # Be supportive for other project settings too.
98
        cls.conf_fallbacks = list(appsettings.PARLER_LANGUAGES['default']['fallbacks'] or ['en'])
99
        cls.conf_fallback = cls.conf_fallbacks[0]
100
        cls.other_lang1 = next(x for x, _ in settings.LANGUAGES if x not in cls.conf_fallbacks)  # "af"
101
        cls.other_lang2 = next(x for x, _ in settings.LANGUAGES if x not in cls.conf_fallbacks + [cls.other_lang1])  # "ar"
102