forked from dj-stripe/dj-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtests.py
182 lines (164 loc) · 5.43 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import os
import sys
import django
from django.conf import settings
from coverage import coverage
from termcolor import colored
cov = coverage(config_file=True)
cov.erase()
cov.start()
TESTS_THRESHOLD = 100
TESTS_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
settings.configure(
TIME_ZONE='America/Los_Angeles',
DEBUG=True,
USE_TZ=True,
DATABASES={
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"NAME": "djstripe",
"USER": "",
"PASSWORD": "",
"HOST": "",
"PORT": "",
},
},
ROOT_URLCONF="tests.test_urls",
INSTALLED_APPS=[
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.sites",
"jsonfield",
"djstripe",
"tests",
"tests.apps.testapp"
],
MIDDLEWARE_CLASSES=(
"django.contrib.sessions.middleware.SessionMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware"
),
SITE_ID=1,
STRIPE_PUBLIC_KEY=os.environ.get("STRIPE_PUBLIC_KEY", ""),
STRIPE_SECRET_KEY=os.environ.get("STRIPE_SECRET_KEY", ""),
DJSTRIPE_PLANS={
"test0": {
"stripe_plan_id": "test_id_0",
"name": "Test Plan 0",
"description": "A test plan",
"price": 1000, # $10.00
"currency": "usd",
"interval": "month"
},
"test": {
"stripe_plan_id": "test_id",
"name": "Test Plan 1",
"description": "Another test plan",
"price": 2500, # $25.00
"currency": "usd",
"interval": "month"
},
"test2": {
"stripe_plan_id": "test_id_2",
"name": "Test Plan 2",
"description": "Yet Another test plan",
"price": 5000, # $50.00
"currency": "usd",
"interval": "month"
},
"test_deletion": {
"stripe_plan_id": "test_id_3",
"name": "Test Plan 3",
"description": "Test plan for deletion.",
"price": 5000, # $50.00
"currency": "usd",
"interval": "month"
},
"test_trial": {
"stripe_plan_id": "test_id_4",
"name": "Test Plan 4",
"description": "Test plan for trails.",
"price": 7000, # $70.00
"currency": "usd",
"interval": "month",
"trial_period_days": 7
},
"unidentified_test_plan": {
"name": "Unidentified Test Plan",
"description": "A test plan with no ID.",
"price": 2500, # $25.00
"currency": "usd",
"interval": "month"
}
},
DJSTRIPE_PLAN_HIERARCHY = {
"bronze": {
"level": 1,
"plans": [
"test0",
"test",
]
},
"silver": {
"level": 2,
"plans": [
"test2",
"test_deletion",
]
},
"gold": {
"level": 3,
"plans": [
"test_trial",
"unidentified_test_plan",
]
},
},
DJSTRIPE_SUBSCRIPTION_REQUIRED_EXCEPTION_URLS=(
"(admin)",
"test_url_name",
"testapp_namespaced:test_url_namespaced",
),
)
# Avoid AppRegistryNotReady exception
# http://stackoverflow.com/questions/24793351/django-appregistrynotready
if hasattr(django, "setup"):
django.setup()
# Announce the test suite
sys.stdout.write(colored(text="\nWelcome to the ", color="magenta", attrs=["bold"]))
sys.stdout.write(colored(text="dj-stripe", color="green", attrs=["bold"]))
sys.stdout.write(colored(text=" test suite.\n\n", color="magenta", attrs=["bold"]))
# Announce test run
sys.stdout.write(colored(text="Step 1: Running unit tests.\n\n", color="yellow", attrs=["bold"]))
from django_nose import NoseTestSuiteRunner
test_runner = NoseTestSuiteRunner(verbosity=1)
failures = test_runner.run_tests(["."])
if failures:
sys.exit(failures)
# Announce coverage run
sys.stdout.write(colored(text="\nStep 2: Generating coverage results.\n\n", color="yellow", attrs=["bold"]))
cov.stop()
percentage = round(cov.report(show_missing=True), 2)
cov.html_report(directory='cover')
cov.save()
if percentage < TESTS_THRESHOLD:
sys.stderr.write(colored(text="YOUR CHANGES HAVE CAUSED TEST COVERAGE TO DROP. " +
"WAS {old}%, IS NOW {new}%.\n\n".format(old=TESTS_THRESHOLD, new=percentage),
color="red", attrs=["bold"]))
sys.exit(1)
# Announce flake8 run
sys.stdout.write(colored(text="\nStep 3: Checking for pep8 errors.\n\n", color="yellow", attrs=["bold"]))
print("pep8 errors:")
print("----------------------------------------------------------------------")
from subprocess import call
flake_result = call(["flake8", ".", "--count"])
if flake_result != 0:
sys.stderr.write("pep8 errors detected.\n")
sys.stderr.write(colored(text="\nYOUR CHANGES HAVE INTRODUCED PEP8 ERRORS!\n\n", color="red", attrs=["bold"]))
sys.exit(flake_result)
else:
print("None")
# Announce success
sys.stdout.write(colored(text="\nTests completed successfully with no errors. Congrats!\n", color="green", attrs=["bold"]))