forked from jambonrose/django-improved-user
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntests.py
executable file
·123 lines (109 loc) · 3.85 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
#!/usr/bin/env python3
"""Utility script to setup Django and run tests against package"""
import sys
from os.path import dirname, join
from django import VERSION as DjangoVersion, setup
from django.apps import apps
from django.conf import settings
from django.core.management import execute_from_command_line
try:
import improved_user # noqa: F401 pylint: disable=unused-import
except ImportError:
print(
'Could not load improved_user!\n'
'Try running `./setup.py develop` before `./runtests.py`\n'
'or run `./setup.py test` for an all in one solution',
)
exit(-1)
def configure_django():
"""Configure Django before tests"""
middleware = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
]
if DjangoVersion >= (1, 10):
middleware_var_name = 'MIDDLEWARE'
else:
middleware_var_name = 'MIDDLEWARE_CLASSES'
middleware_kwargs = {
middleware_var_name: middleware,
}
settings.configure(
DATABASES={
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:',
},
},
INSTALLED_APPS=[
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.messages',
'django.contrib.sessions',
'django.contrib.sites',
'improved_user.apps.ImprovedUserConfig',
],
SITE_ID=1,
AUTH_USER_MODEL='improved_user.User',
FIXTURE_DIRS=(join(dirname(__file__), 'tests', 'fixtures'),),
TEMPLATES=[{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
}],
# TODO: when Dj1.8 dropped, use MIDDLEWARE directly
**middleware_kwargs # noqa: C815
)
setup()
def run_test_suite(*args):
"""Run the test suite"""
test_args = list(args) or []
execute_from_command_line(['manage.py', 'test'] + test_args)
def check_missing_migrations():
"""Check that user model and migration files are in sync"""
from django.db.migrations.autodetector import MigrationAutodetector
from django.db.migrations.loader import MigrationLoader
from django.db.migrations.state import ProjectState
try:
from django.db.migrations.questioner import (
NonInteractiveMigrationQuestioner as Questioner,
)
except ImportError:
# TODO: remove this once Dj1.8 dropped
from django.db.migrations.questioner import (
InteractiveMigrationQuestioner as Questioner,
)
loader = MigrationLoader(None, ignore_no_migrations=True)
conflicts = loader.detect_conflicts()
if conflicts:
raise Exception(
'Migration conflicts detected. Please fix your migrations.')
questioner = Questioner(dry_run=True, specified_apps=None)
autodetector = MigrationAutodetector(
loader.project_state(),
ProjectState.from_apps(apps),
questioner,
)
changes = autodetector.changes(
graph=loader.graph,
trim_to_apps=None,
convert_apps=None,
migration_name=None,
)
if changes:
raise Exception(
'Migration changes detected. '
'Please update or add to the migration file as appropriate')
print('Migration-checker detected no problems.')
if __name__ == '__main__':
configure_django()
check_missing_migrations()
run_test_suite(*sys.argv[1:])