0.4.4: Django 3.0 support, actions.export_as_csv_action added.
[fnpdjango.git] / runtests.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8
3 # This file is part of FNPDjango, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See README.md for more information.
5 #
6 """
7 Creates a simple Django configuration and runs tests for fnpdjango.
8 """
9 from __future__ import unicode_literals
10
11 import sys
12 import os
13 from os.path import dirname, abspath
14 from optparse import OptionParser
15 from shutil import rmtree
16
17 from django.conf import settings
18 from fnpdjango.utils.settings import LazyUGettextLazy as _
19
20
21 # For convenience configure settings if they are not pre-configured or if we
22 # haven't been provided settings to use by environment variable.
23 if not settings.configured and not os.environ.get('DJANGO_SETTINGS_MODULE'):
24     import tempfile
25     media_root = tempfile.mkdtemp(prefix='djangotest_')
26
27     settings.configure(
28         DATABASES={
29             'default': {
30                 'ENGINE': 'django.db.backends.sqlite3',
31
32                 'NAME': 'test.db',
33             }
34         },
35         INSTALLED_APPS=[
36             'django.contrib.admin',
37             'django.contrib.auth',
38             'django.contrib.contenttypes',
39             'django.contrib.messages',
40             'django.contrib.sessions',
41             'django.contrib.sites',
42
43             'fnpdjango',
44             'tests',
45         ],
46         LANGUAGE_CODE='pl',
47         MEDIA_ROOT=media_root,
48         STATIC_URL='/static/',
49         MIDDLEWARE_CLASSES=[    # Django <= 1.9
50             'django.middleware.common.CommonMiddleware',
51             'fnpdjango.middleware.URLLocaleMiddleware',
52             'fnpdjango.middleware.SetRemoteAddrFromXRealIP',
53             'django.contrib.sessions.middleware.SessionMiddleware',
54             'django.contrib.auth.middleware.AuthenticationMiddleware',
55             'django.contrib.messages.middleware.MessageMiddleware',
56         ],
57         MIDDLEWARE=[
58             'django.middleware.common.CommonMiddleware',
59             'fnpdjango.middleware.URLLocaleMiddleware',
60             'fnpdjango.middleware.SetRemoteAddrFromXRealIP',
61             'django.contrib.sessions.middleware.SessionMiddleware',
62             'django.contrib.auth.middleware.AuthenticationMiddleware',
63             'django.contrib.messages.middleware.MessageMiddleware',
64         ],
65         FNPDJANGO_REALIP = True,
66         ROOT_URLCONF='tests.urls',
67         TEMPLATES=[
68             {
69                 'BACKEND': 'django.template.backends.django.DjangoTemplates',
70                 'APP_DIRS': True,
71                 'OPTIONS': {
72                     'context_processors': [
73                         'django.contrib.auth.context_processors.auth',
74                         'django.contrib.messages.context_processors.messages',
75                     ]
76                 }
77             },
78         ],
79         TEST_LAZY_UGETTEXT_LAZY=_("Lazy setting."),
80         USE_I18N=True,
81
82         SECRET_KEY='x',
83         DEBUG=True,
84         SITE_ID=1,
85     )
86 else:
87     media_root = None
88
89 try:
90     from django.test.runner import DiscoverRunner
91 except ImportError:
92     # Django < 1.6
93     from django.test.simple import DjangoTestSuiteRunner as DiscoverRunner
94
95
96 def runtests(*test_args, **kwargs):
97     """Actual test suite entry point."""
98     if not test_args:
99         test_args = ['tests']
100     parent = dirname(abspath(__file__))
101     sys.path.insert(0, parent)
102
103     # For Django 1.7+
104     try:
105         from django import setup
106     except ImportError:
107         pass
108     else:
109         setup()
110
111     test_runner = DiscoverRunner(
112         verbosity=kwargs.get('verbosity', 1),
113         interactive=kwargs.get('interactive', False),
114         failfast=kwargs.get('failfast'))
115     failures = test_runner.run_tests(test_args)
116     if media_root:
117         rmtree(media_root)
118     sys.exit(failures)
119
120 if __name__ == '__main__':
121     parser = OptionParser()
122     parser.add_option('--failfast', action='store_true',
123                       default=False, dest='failfast')
124
125     (options, args) = parser.parse_args()
126
127     runtests(failfast=options.failfast, *args)