-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconftest.py
258 lines (198 loc) · 6.65 KB
/
conftest.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import pytest
import uuid
from donate.app import create_app
from donate.database import (
db as _db,
make_dependencies,
count_dependencies,
)
from donate.settings import TestConfig
from donate.models import (
User,
Account,
Project,
Transaction,
Currency,
StripeDonation,
StripeSubscription,
StripePlan,
)
@pytest.yield_fixture(scope='function')
def app():
""" an application for the tests """
_app = create_app(TestConfig)
with _app.app_context():
_db.create_all()
ctx = _app.test_request_context()
ctx.push()
yield _app
ctx.pop()
@pytest.fixture(scope='function')
def testapp(app):
""" A webtest app """
return app.test_client()
@pytest.yield_fixture(scope='function')
def db(app):
_db.app = app
with app.app_context():
_db.create_all()
yield _db
# Close db connection
_db.session.close()
_db.drop_all()
@pytest.fixture(scope='session')
def model_objects():
objects = {
'u1': User(id=1,
username="Matt",
email="[email protected]"),
'u2': User(id=2,
username="Melissa",
email="[email protected]"),
'u3': User(id=3,
username="Miley",
email="[email protected]"),
'u4': User(id=4,
username="Martha",
email="[email protected]"),
'usd': Currency(id=1, code="USD", name="US Dollar"),
'btc': Currency(id=2, code="BTC", name="Bitcoin"),
'acct1': Account(id=1, ccy_id=1, name="USD Credits"),
'acct2': Account(id=2, ccy_id=1, name="USD Debits"),
'acct3': Account(id=3, ccy_id=2, name="BTC Wallet"),
}
return objects
def data_dict_builder(data):
if ("label" not in data
or "keys" not in data
or "obj_data" not in data):
raise ValueError("Malformed test data dictionary passed to \
data_dict_builder")
label = data['label']
keys = data['keys']
obj_data = data['obj_data']
# {'label<n>': {key: value}}
# n is the nth entry in the passed dictionary, e.g. user1
objects = {"{}{}".format(label, _obj): {
keys[_key]: obj_data[_obj][_key]
for _key in range(len(keys))
}
for _obj in range(len(obj_data))}
return objects
@pytest.fixture(scope='session')
def user_data():
label = 'user'
keys = ["username", "email"]
user_data = [
("name1", "[email protected]"),
(None, "[email protected]"),
("name1", "[email protected]"),
("name1", None),
("name1", "[email protected]")
]
return data_dict_builder({'label': label,
'keys': keys,
'obj_data': user_data})
@pytest.fixture(scope="session")
def currency_data():
# should generate non-alphanumeric ccy codes to test that creation fails
label = "ccy"
keys = ["code", "name"]
account_data = [
("USD", "US Dollar"),
("BTC", "Bitcoin"),
("ETH", "Etherium"),
("EUR", "Euro")]
return data_dict_builder({"label": label,
"keys": keys,
"obj_data": account_data})
@pytest.fixture(scope='session')
def account_data(currency_data):
label = "acct"
keys = ['name', 'ccy']
account_data = [
('general', "USD"),
('noisetor', "BTC"),
('noisetor', "ETH"),
('sawstop', "USD")]
return data_dict_builder({"label": label,
"keys": keys,
"obj_data": account_data})
@pytest.fixture(scope='session')
def stripe_plan_data(account_data):
label = "stripe_plan"
keys = ["ccy_id", "name", "amount", "interval", "desc"]
# This needs to be built based on the number of accounts
# separate the selectino of account from pool created above with
# plan creation. These are just for basic tests.
obj_data = [
(1, "#$10/month", 10, "month", "10 bucks a month!"), # NOQA
(3, "#$20/month", 20, "month", "10 bucks a month!"), # NOQA
(2, "#$10/month", 10, "month", "10 bucks a month!"), # NOQA
(1, "#$5000/month", 500, "month", "500 bucks a month!"), # NOQA
(1, "#$10/month", 10, "month", "10 bucks a month!"), # NOQA
]
return data_dict_builder({"label": label,
"keys": keys,
"obj_data": obj_data})
@pytest.fixture(scope='session')
def stripe_subscription_data():
label = 'stripe_sub'
keys = ['plan_id', 'user']
obj_data = [
("$10 / month", "Frank"),
("$20 / week", "Rachel"),
("$5 / whenever", "Asad"),
]
return data_dict_builder({"label": label,
"keys": keys,
"obj_data": obj_data})
@pytest.fixture(scope='session')
def stripe_donation_data():
label = "stripe_donation"
keys = ['anonymous', 'type', 'card_id', 'charge_id', 'customer_id']
obj_data = [
(True, 'stripe_donation', "1234-5678-9101", uuid.uuid1().hex, '10'),
(False, 'stripe_donation', "0987-6543-2112", uuid.uuid1().hex, '11'),
(False, 'stripe_donation', "8888-9999-1111-2222", uuid.uuid1().hex, '12')]
return data_dict_builder({"label": label,
"keys": keys,
"obj_data": obj_data})
@pytest.fixture(scope='function')
def test_db_project(db):
proj_name = "Test Proj"
proj_desc = "A project for testing"
proj_goal = 100
ccy_name = "USD Dollar"
ccy_code = "USD"
acct_name = "{}_{}_acct".format(proj_name, ccy_code)
ccy = Currency(name=ccy_name, code=ccy_code)
account = Account(name=acct_name, ccy=ccy)
proj = Project(name=proj_name, desc=proj_desc, goal=proj_goal)
proj.accounts = [account]
db.session.add(proj)
db.session.commit()
@pytest.fixture(scope='function')
def test_form():
class form:
vals = {"donor[email]": "[email protected]",
"donor[name]": "Jimmy Hoffa",
"donor[stripe_token]": "abc123",
"charge[recurring]": True,
"donor[anonymous]": True,
"project_select": "test project"}
amt = 100
def getlist(self, x):
return [" ", self.amt, '']
def __getitem__(self, x):
return self.vals[x]
def __contains__(self, x):
return x in self.vals
def get(self, x, y):
if x in self.vals:
return self.vals[x]
else:
return y
def items(self):
return self.vals.items()
return form()