-
Notifications
You must be signed in to change notification settings - Fork 15
/
runtests.py
executable file
·120 lines (108 loc) · 3.8 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
#!/usr/bin/env python3
"""Utility script to setup Django and run tests against package"""
import sys
from os.path import dirname, join
try:
from django import setup
from django.apps import apps
from django.conf import settings
from django.core.management import execute_from_command_line
except ImportError:
print(
"Could not load Django.\n"
"Try running `flit install --symlink` before `./runtests.py`\n"
"or run `make test` (or `make tox`) for an all in one solution",
)
exit(-1)
try:
import improved_user # noqa: F401 pylint: disable=unused-import
except ImportError:
print(
"Could not load improved_user!\n"
"Try running `flit install --symlink` before `./runtests.py`\n"
"or run `make test` (or `make tox`) for an all in one solution",
)
exit(-1)
def configure_django():
"""Configure Django before tests"""
settings.configure(
SECRET_KEY="m-4Umd2!_nQQX.Ux6dYaiffmgRFpFxri!hqmffqBAhuAu*-!9n",
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",
"django.template.context_processors.request",
],
},
}
],
MIDDLEWARE=[
"django.contrib.sessions.middleware.SessionMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
],
)
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.questioner import (
NonInteractiveMigrationQuestioner as Questioner,
)
from django.db.migrations.state import ProjectState
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:])