-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
329 lines (285 loc) · 12.5 KB
/
tests.py
File metadata and controls
329 lines (285 loc) · 12.5 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import os
import json
import unittest
import tempfile
from copy import deepcopy
from datetime import datetime
from dateutil.relativedelta import relativedelta
from fr_app import app
from fr_app.models import db, User, FeatureRequest, Client, ProductArea
from flask_fixtures import FixturesMixin
app.config.from_object('fr_app.settings.TestingConf')
class FeatureRequestTestCase(unittest.TestCase, FixturesMixin):
fixtures = ['clients.json', 'product_areas.json', 'users.json']
app, db = app, db
post_data = {
"user": 1,
"client": 1,
"product_area": 1,
"title": "First Feature Request",
"client_priority": 1,
"description": "First Feature Request description",
"target_date": str(datetime.utcnow().date())
}
def setUp(self):
self.db_fd, app.config['DATABASE'] = tempfile.mkstemp()
app.testing = True
self.app = app.test_client()
with app.app_context():
db.create_all()
def tearDown(self):
db.drop_all()
os.close(self.db_fd)
os.unlink(app.config['DATABASE'])
def test_product_area_data(self):
"""Test initial data for ProductArea model."""
product_areas = ProductArea.query.all()
assert len(product_areas) == ProductArea.query.count() == 4
def test_client_data(self):
"""Test initial data for Client model."""
clients = Client.query.all()
assert len(clients) == Client.query.count() == 3
def test_user_data(self):
"""Test initial data for User model."""
users = User.query.all()
assert len(users) == User.query.count() == 3
def test_feature_request_data(self):
"""Test initial data for FeatureRequest model."""
feature_requests = FeatureRequest.query.all()
assert len(feature_requests) == FeatureRequest.query.count() == 0
def test_creating_feature_request_no_data(self):
response = self.app.post('/api/feature_requests/add/', data=dict())
response_data = json.loads(response.get_data().decode('utf-8'))
assert response_data['message'] == 'No input data provided'
def test_creating_feature_request_valid_data(self):
response = self.app.post(
'/api/feature_requests/add/',
data=json.dumps(self.post_data),
content_type='application/json'
)
response_data = json.loads(response.get_data().decode('utf-8'))
assert response_data['message'] == 'Created new feature request.'
def test_creating_feature_request_invalid_title(self):
post_data = deepcopy(self.post_data)
post_data['title'] = 'less'
response = self.app.post(
'/api/feature_requests/add/',
data=json.dumps(post_data),
content_type='application/json'
)
response_data = json.loads(response.get_data().decode('utf-8'))
assert response_data['errors']['title'][0] == \
'Length must be between 6 and 255.'
def test_creating_feature_request_past_target_date(self):
post_data = deepcopy(self.post_data)
now = datetime.utcnow().date()
post_data['target_date'] = str(now - relativedelta(months=1))
response = self.app.post(
'/api/feature_requests/add/',
data=json.dumps(post_data),
content_type='application/json'
)
response_data = json.loads(response.get_data().decode('utf-8'))
assert response_data['errors']['target_date'][0] == \
'Target date must be in the future'
def test_creating_feature_request_negative_client_priority(self):
post_data = deepcopy(self.post_data)
post_data['client_priority'] = -1
response = self.app.post(
'/api/feature_requests/add/',
data=json.dumps(post_data),
content_type='application/json'
)
response_data = json.loads(response.get_data().decode('utf-8'))
assert response_data['errors']['client_priority'][0] == \
'Must be at least 1.'
def test_creating_feature_request_no_user_data(self):
post_data = deepcopy(self.post_data)
del post_data['user']
response = self.app.post(
'/api/feature_requests/add/',
data=json.dumps(post_data),
content_type='application/json'
)
response_data = json.loads(response.get_data().decode('utf-8'))
assert response_data['errors']['user'][0] == \
'Missing data for required field.'
def test_creating_feature_request_no_client_data(self):
post_data = deepcopy(self.post_data)
del post_data['client']
response = self.app.post(
'/api/feature_requests/add/',
data=json.dumps(post_data),
content_type='application/json'
)
response_data = json.loads(response.get_data().decode('utf-8'))
assert response_data['errors']['client'][0] == \
'Missing data for required field.'
def test_creating_feature_request_no_product_area_data(self):
post_data = deepcopy(self.post_data)
del post_data['product_area']
response = self.app.post(
'/api/feature_requests/add/',
data=json.dumps(post_data),
content_type='application/json'
)
response_data = json.loads(response.get_data().decode('utf-8'))
assert response_data['errors']['product_area'][0] == \
'Missing data for required field.'
def test_creating_feature_request_no_title_data(self):
post_data = deepcopy(self.post_data)
del post_data['title']
response = self.app.post(
'/api/feature_requests/add/',
data=json.dumps(post_data),
content_type='application/json'
)
response_data = json.loads(response.get_data().decode('utf-8'))
assert response_data['errors']['title'][0] == \
'Missing data for required field.'
def test_creating_feature_request_check_client_priority_reordering(self):
response = self.app.post(
'/api/feature_requests/add/',
data=json.dumps(self.post_data),
content_type='application/json'
)
response_data = json.loads(response.get_data().decode('utf-8'))
assert response_data['message'] == 'Created new feature request.'
first_id, first_client_priority = \
response_data['data'][0]['id'],\
response_data['data'][0]['client_priority']
assert first_id == 1
assert first_client_priority == 1
# sending same client_priority again will result in moving the first
# priority to 2
response = self.app.post(
'/api/feature_requests/add/',
data=json.dumps(self.post_data),
content_type='application/json'
)
response_data = json.loads(response.get_data().decode('utf-8'))
assert response_data['message'] == 'Created new feature request.'
second_id, second_client_priority = \
response_data['data'][0]['id'],\
response_data['data'][0]['client_priority']
first_fr = FeatureRequest.query.get(first_id)
assert first_fr.id == 1
# got reordered after adding another feature request
assert first_fr.client_priority == 2
assert second_id == 2
assert second_client_priority == 1
def test_creating_feature_request_check_client_priority_no_reordering(
self):
response = self.app.post(
'/api/feature_requests/add/',
data=json.dumps(self.post_data),
content_type='application/json'
)
response_data = json.loads(response.get_data().decode('utf-8'))
assert response_data['message'] == 'Created new feature request.'
first_id, first_client_priority = \
response_data['data'][0]['id'],\
response_data['data'][0]['client_priority']
assert first_id == 1
assert first_client_priority == 1
post_data = deepcopy(self.post_data)
post_data['client_priority'] = 2
response = self.app.post(
'/api/feature_requests/add/',
data=json.dumps(post_data),
content_type='application/json'
)
response_data = json.loads(response.get_data().decode('utf-8'))
assert response_data['message'] == 'Created new feature request.'
second_id, second_client_priority = \
response_data['data'][0]['id'],\
response_data['data'][0]['client_priority']
first_fr = FeatureRequest.query.get(first_id)
assert first_fr.id == 1
# got reordered after adding another feature request
assert first_fr.client_priority == 1
assert second_id == 2
assert second_client_priority == 2
def test_updating_feature_request_description(self):
response = self.app.post(
'/api/feature_requests/add/',
data=json.dumps(self.post_data),
content_type='application/json'
)
response_data = json.loads(response.get_data().decode('utf-8'))
assert response_data['message'] == 'Created new feature request.'
description = response_data['data'][0]['description']
assert description == self.post_data['description']
# update FR now
self.post_data['description'] = "I updated description"
response = self.app.post(
'/api/feature_requests/1/',
data=json.dumps(self.post_data),
content_type='application/json'
)
response_data = json.loads(response.get_data().decode('utf-8'))
assert response_data['data'][0]['description'] ==\
self.post_data['description']
def test_updating_feature_request_client_priority(self):
"""Create a feature with priority 1, and update it to 2"""
response = self.app.post(
'/api/feature_requests/add/',
data=json.dumps(self.post_data),
content_type='application/json'
)
response_data = json.loads(response.get_data().decode('utf-8'))
assert response_data['message'] == 'Created new feature request.'
client_priority = response_data['data'][0]['client_priority']
assert client_priority == self.post_data['client_priority']
# update FR now
self.post_data['client_priority'] = 2
response = self.app.post(
'/api/feature_requests/1/',
data=json.dumps(self.post_data),
content_type='application/json'
)
response_data = json.loads(response.get_data().decode('utf-8'))
assert response_data['data'][0]['client_priority'] ==\
self.post_data['client_priority']
def test_updating_feature_requests_client_priority(self):
"""
Create feature requests with priority 1, and 2.
Update the second one to be 3, check only 1 and 3 exist.
"""
for turn in range(1, 3):
self.post_data['client_priority'] = turn
response = self.app.post(
'/api/feature_requests/add/',
data=json.dumps(self.post_data),
content_type='application/json'
)
response_data = json.loads(response.get_data().decode('utf-8'))
assert response_data['message'] == 'Created new feature request.'
client_priority = response_data['data'][0]['client_priority']
assert client_priority == self.post_data['client_priority']
# update FR now
self.post_data['client_priority'] = 3
response = self.app.post(
'/api/feature_requests/2/',
data=json.dumps(self.post_data),
content_type='application/json'
)
response_data = json.loads(response.get_data().decode('utf-8'))
assert response_data['data'][0]['client_priority'] ==\
self.post_data['client_priority']
assert FeatureRequest.query.get(1).client_priority == 1
assert FeatureRequest.query.get(2).client_priority == 3
def test_creating_feature_request_invalid_target_date(self):
post_data = deepcopy(self.post_data)
# invalid date for November month
post_data['target_date'] = '2018-11-31'
response = self.app.post(
'/api/feature_requests/add/',
data=json.dumps(post_data),
content_type='application/json'
)
response_data = json.loads(response.get_data().decode('utf-8'))
assert response_data['errors']['target_date'][0] ==\
'Not a valid date.'
if __name__ == '__main__':
unittest.main()