forked from scythargon/model_mommy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtests.py
executable file
·129 lines (106 loc) · 4.32 KB
/
runtests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
import sys
import warnings
from optparse import OptionParser
from os.path import dirname, join
import django
def parse_args():
parser = OptionParser()
parser.add_option('--use-tz', dest='USE_TZ', action='store_true')
parser.add_option('--postgresql', dest='USE_POSTGRESQL', action='store_true')
parser.add_option('--postgis', dest='USE_POSTGIS', action='store_true')
return parser.parse_args()
def configure_settings(options):
from django.conf import settings
# If DJANGO_SETTINGS_MODULE envvar exists the settings will be
# configured by it. Otherwise it will use the parameters bellow.
if not settings.configured:
params = dict(
DATABASES={
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:',
}
},
INSTALLED_APPS=(
'test.generic',
'django.contrib.contenttypes',
'test_without_migrations',
'test.ambiguous',
'test.ambiguous2',
),
SITE_ID=1,
TEST_RUNNER='django.test.simple.DjangoTestSuiteRunner',
TEST_ROOT=join(dirname(__file__), 'test', 'generic', 'tests'),
MIDDLEWARE_CLASSES=(),
)
if getattr(options, 'USE_POSTGRESQL', False):
if getattr(options, 'USE_POSTGIS', False):
engine = 'django.contrib.gis.db.backends.postgis'
else:
engine = 'django.db.backends.postgresql_psycopg2'
params['DATABASES'] = {
'default': {
'ENGINE': engine,
'NAME': 'model_mommy',
'TEST_NAME': 'test_model_mommy',
'USER': '',
'PASSWORD': '',
}
}
# Force the use of timezone aware datetime and change Django's warning to
# be treated as errors.
if getattr(options, 'USE_TZ', False):
params.update(USE_TZ=True)
warnings.filterwarnings('error', r"DateTimeField received a naive datetime",
RuntimeWarning, r'django\.db\.models\.fields')
# Configure Django's settings
settings.configure(**params)
return settings
# We only need this if HstoreFields are a possibility
from django.db.backends.signals import connection_created
from django.test.runner import DiscoverRunner
def create_hstore(sender, **kwargs):
conn = kwargs.get('connection')
if conn is not None:
cursor = conn.cursor()
cursor.execute('CREATE EXTENSION IF NOT EXISTS HSTORE')
class PostgresRunner(DiscoverRunner):
"""Create HStore extension before test database is created"""
def setup_databases(self):
connection_created.connect(create_hstore)
result = super(PostgresRunner, self).setup_databases()
connection_created.disconnect(create_hstore)
return result
def get_runner(settings):
'''
Asks Django for the TestRunner defined in settings or the default one.
'''
from django.test.utils import get_runner
extra_apps = []
if getattr(options, 'USE_POSTGRESQL', False):
extra_apps.append('django.contrib.postgres')
if getattr(options, 'USE_POSTGIS', False):
extra_apps.append('django.contrib.gis')
extra_apps.append('django.contrib.auth')
setattr(settings, 'INSTALLED_APPS', extra_apps + list(getattr(settings, 'INSTALLED_APPS')))
from test_without_migrations.management.commands._base import DisableMigrations
setattr(settings, 'MIGRATION_MODULES', DisableMigrations())
TestRunner = get_runner(settings)
return TestRunner(verbosity=1, interactive=True, failfast=False)
def runtests(options=None, labels=None):
settings = configure_settings(options)
if getattr(options, 'USE_POSTGRESQL', False):
settings.TEST_RUNNER = 'runtests.PostgresRunner'
else:
settings.TEST_RUNNER = 'django.test.runner.DiscoverRunner'
if not labels:
labels = ['test.generic']
runner = get_runner(settings)
django.setup()
sys.exit(runner.run_tests(labels))
if __name__ == '__main__':
options, labels = parse_args()
if getattr(options, 'USE_POSTGIS', False):
setattr(options, 'USE_POSTGRESQL', True)
runtests(options, labels)