-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.py
More file actions
430 lines (367 loc) · 18.1 KB
/
test.py
File metadata and controls
430 lines (367 loc) · 18.1 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import json
import unittest
import uuid
from app import application, db
from app.models import UserSystemInfo, SuccessfulInstalls, FailedInstalls, Attempts
class TestCase(unittest.TestCase):
def setUp(self):
application.config['TESTING'] = True
application.config['WTF_CSRF_ENABLED'] = False
application.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/result_aggregation_test.db'
self.application = application.test_client()
db.create_all()
def tearDown(self):
db.session.remove()
db.drop_all()
# ===================================================================================#
# ======================== Initialization functions =================================#
def create_user_info(self):
data = {
"distribution_name": "Ubuntu",
"distribution_version": "15.10",
"system_verson": "#42-Ubuntu SMP Thu May 12 22:05:35 UTC 2016",
"system": "Linux",
"machine": "x86_64",
"system_platform": "Linux-4.2.0-36-generic-x86_64-with-Ubuntu-15.10-wily",
"python_version": "2.7.10"
}
return data
def create_failed_installs(self):
data = [
{
"name": "EasyMercurial",
"version": "2.5.0",
"error_description": "errors finding EasyMercurial version"
},
{
"name": "Nose (nosetests)",
"version": "4.0",
"error_description": "errors finding Nose (nosetests) version"
}
]
return data
def create_successful_installs(self):
data = [
{"name": "git", "version": "2.5.0"},
{"name": "make", "version": "4.0"}
]
return data
def create_database(self):
"""
This functions creates a database having 2 users by posting to '/installation_data/'.
Out of which one user has one attempt, while the other user has 2 attempts.
"""
data = {
"user_system_info": self.create_user_info(),
"failed_installs": self.create_failed_installs(),
"successful_installs": self.create_successful_installs(),
"unique_user_id": None
}
data['user_system_info']['workshop_id'] = "test_workshop_1"
response = self.application.post('/installation_data/', data=json.dumps(data),
headers={'Content-Type': 'application/json'})
self.assertEqual(response.status_code, 200)
key = json.loads(response.data.decode('utf-8'))['key']
data['unique_user_id'] = key
response = self.application.post('/installation_data/', data=json.dumps(data),
headers={'Content-Type': 'application/json'})
self.assertEqual(response.status_code, 200)
data['unique_user_id'] = None
data['user_system_info']['workshop_id'] = "test_workshop_2"
response = self.application.post('/installation_data/', data=json.dumps(data),
headers={'Content-Type': 'application/json'})
self.assertEqual(response.status_code, 200)
count = UserSystemInfo.query.count() # Count User rows
self.assertEqual(count, 2)
count = Attempts.query.count()
self.assertEqual(count, 3)
# ===========================================================================#
# ========================= Unit test cases =================================#
def test_insert_User_Info(self):
"""Add user info directly in the database"""
unique_user_id = str(uuid.uuid4())
u = UserSystemInfo(distribution_name="Ubuntu", distribution_version="15.10",
system_version="#42-Ubuntu SMP Thu May 12 22:05:35 UTC 2016",
system="Linux", machine="x86_64",
system_platform="Linux-4.2.0-36-generic-x86_64-with-Ubuntu-15.10-wily",
python_version="2.7.10", unique_user_id=unique_user_id)
db.session.add(u)
db.session.commit()
count = UserSystemInfo.query.count()
self.assertEqual(count, 1)
def test_post_data(self):
"""Post the data with all the information"""
data = {
"user_system_info": self.create_user_info(),
"failed_installs": self.create_failed_installs(),
"successful_installs": self.create_successful_installs()
}
response = self.application.post('/installation_data/', data=json.dumps(data),
headers={'Content-Type': 'application/json'})
self.assertEqual(response.status_code, 200)
def test_post_delete_User(self):
"""Create a user and the delete it"""
data = {
"user_system_info": self.create_user_info(),
"failed_installs": self.create_failed_installs(),
"successful_installs": self.create_successful_installs()
}
response = self.application.post('/installation_data/', data=json.dumps(data),
headers={'Content-Type': 'application/json'})
self.assertEqual(response.status_code, 200)
count = FailedInstalls.query.count()
self.assertEqual(count, 2)
user = UserSystemInfo.query.get(1)
db.session.delete(user)
db.session.commit()
count = UserSystemInfo.query.count() # Count User rows
self.assertEqual(count, 0)
count = FailedInstalls.query.count() # Count Failure rows
self.assertEqual(count, 0)
count = SuccessfulInstalls.query.count() # Count Success rows
self.assertEqual(count, 0)
def test_post_empty_failure(self):
"""Post the data with an empty list of failed_installs"""
data = {
"user_system_info": self.create_user_info(),
"failed_installs": [],
"successful_installs": self.create_successful_installs()
}
response = self.application.post('/installation_data/', data=json.dumps(data),
headers={'Content-Type': 'application/json'})
self.assertEqual(response.status_code, 200)
def test_post_empty_success(self):
"""Post the data with an empty list of successful_installs"""
data = {
"user_system_info": self.create_user_info(),
"failed_installs": self.create_failed_installs(),
"successful_installs": []
}
response = self.application.post('/installation_data/', data=json.dumps(data),
headers={'Content-Type': 'application/json'})
self.assertEqual(response.status_code, 200)
def test_post_empty_user_data(self):
"""Post the data without any user info"""
data = {
"user_system_info": {},
"failed_installs": self.create_failed_installs(),
"successful_installs": self.create_successful_installs()
}
response = self.application.post('/installation_data/', data=json.dumps(data),
headers={'Content-Type': 'application/json'})
self.assertEqual(response.status_code, 200)
def test_post_modifies_unique_id(self):
"""Post the data to installation_data endpoint with an accidently modified unique_user_id"""
data = {"user_system_info": self.create_user_info(),
"failed_installs": [],
"successful_installs": [],
"unique_user_id": "7dd03934-bed1-443e-a4f" # modified manually
}
response = self.application.post('/installation_data/', data=json.dumps(data),
headers={'Content-Type': 'application/json'})
self.assertEqual(response.status_code, 200)
message = json.loads(response.data.decode('utf-8'))['summary']['message']
self.assertEqual(message, "new id generated")
def test_post_none_unique_id(self):
data = {
"user_system_info": self.create_user_info(),
"failed_installs": [],
"successful_installs": [],
"unique_user_id": None
}
response = self.application.post('/installation_data/', data=json.dumps(data),
headers={'Content-Type': 'application/json'})
self.assertEqual(response.status_code, 200)
message = json.loads(response.data.decode('utf-8'))['summary']['message']
self.assertEqual(message, "new id generated")
def test_post_with_same_unique_id(self):
"""Post the data with same unique id (same user), and count the number
of rows of UserSystemInfo"""
data = {
"user_system_info": self.create_user_info(),
"failed_installs": [],
"successful_installs": [],
"unique_user_id": None
}
response = self.application.post('/installation_data/', data=json.dumps(data),
headers={'Content-Type': 'application/json'})
self.assertEqual(response.status_code, 200)
count = UserSystemInfo.query.count() # Count User rows
self.assertEqual(count, 1)
key = json.loads(response.data.decode('utf-8'))['key']
data = {
"user_system_info": self.create_user_info(),
"failed_installs": [],
"successful_installs": [],
"unique_user_id": key # using same unique key
}
response = self.application.post('/installation_data/', data=json.dumps(data),
headers={'Content-Type': 'application/json'})
self.assertEqual(response.status_code, 200)
"""The number of rows should remail 1 only"""
count = UserSystemInfo.query.count() # Count User rows
self.assertEqual(count, 1)
def test_post_no_email_id_on_second_request(self):
"""Post the data with email id and workshop id in the first attempt.
Now post again with the same unique id, but without email and workshop id.
"""
data = {
"user_system_info": self.create_user_info(),
"failed_installs": [],
"successful_installs": []
}
data['user_system_info']['email_id'] = "abc@pqr.com"
data['user_system_info']['workshop_id'] = "XYZ"
response = self.application.post('/installation_data/', data=json.dumps(data),
headers={'Content-Type': 'application/json'})
self.assertEqual(response.status_code, 200)
key = json.loads(response.data.decode('utf-8'))['key']
user = UserSystemInfo.query.first()
assert user.email_id is not None
assert user.workshop_id is not None
"""POST again with same unique_user_id, but without email id and workshop_id.
email_id and worskhop_id should not be rewritten to None"""
data = {
"user_system_info": self.create_user_info(),
"failed_installs": [],
"successful_installs": [],
"unique_user_id": key
}
response = self.application.post('/installation_data/', data=json.dumps(data),
headers={'Content-Type': 'application/json'})
self.assertEqual(response.status_code, 200)
user = UserSystemInfo.query.first()
assert user.email_id is not None
assert user.workshop_id is not None
def test_get_view_all(self):
"""Get request on 'view' endpoint"""
response = self.application.get('/view/')
self.assertEqual(response.status_code, 200)
def test_get_workshops(self):
"""Get request on '/view/' endpoint and check the count of workshops"""
self.create_database()
payload = {"export": "json"}
response = self.application.get('/view/', query_string=payload)
self.assertEqual(response.status_code, 200)
response_json = json.loads(response.data.decode('utf-8'))
workshops_count = len(response_json['workshops'])
self.assertEqual(workshops_count, 2)
def test_get_failed_installs_list(self):
"""Get request on '/view/' and count the failed_installs"""
self.create_database()
payload = {"export": "json"}
response = self.application.get('/view/', query_string=payload)
self.assertEqual(response.status_code, 200)
response_json = json.loads(response.data.decode('utf-8'))
workshops_count = len(response_json['most_failed_packages'])
self.assertEqual(workshops_count, 2)
def test_get_workshops_on_workshops_page(self):
"""Get request on '/view/test_workshop_1/' endpoint and check the count of workshops"""
self.create_database()
payload = {"export": "json"}
response = self.application.get('/view/test_workshop_1/', query_string=payload)
self.assertEqual(response.status_code, 200)
response_json = json.loads(response.data.decode('utf-8'))
workshops_count = len(response_json['workshops'])
self.assertEqual(workshops_count, 2)
def test_get_view_by_workshop(self):
"""
Get data from '/view/test_workshop/'
"""
unique_user_id = str(uuid.uuid4())
u = UserSystemInfo(distribution_name="Ubuntu", distribution_version="15.10",
system_version="#42-Ubuntu SMP Thu May 12 22:05:35 UTC 2016",
system="Linux", machine="x86_64",
system_platform="Linux-4.2.0-36-generic-x86_64-with-Ubuntu-15.10-wily",
python_version="2.7.10", unique_user_id=unique_user_id, workshop_id="test_workshop")
db.session.add(u)
db.session.commit()
count = UserSystemInfo.query.count()
self.assertEqual(count, 1)
response = self.application.get('/view/test_workshop/')
self.assertEqual(response.status_code, 200)
response = self.application.get('/view/test_workshop/?export=json')
self.assertEqual(response.status_code, 200)
message = json.loads(response.data.decode('utf-8'))
assert 'os_users' in message
assert 'most_failed_packages' in message
def test_get_view_all_attempts(self):
"""Get most_failed_packages from '/view/' for all_attempts"""
self.create_database()
payload = {"all_attempts": "1", "export": "json"}
response = self.application.get('/view/', query_string=payload)
self.assertEqual(response.status_code, 200)
response_json = json.loads(response.data.decode('utf-8'))
package_failed_count = response_json['most_failed_packages'][0]['count']
self.assertEqual(package_failed_count, 3)
def test_get_view_latest_attempt(self):
"""Get most_failed_packages from '/view/' for latest_attempt"""
self.create_database()
payload = {"export": "json"}
response = self.application.get('/view/', query_string=payload)
self.assertEqual(response.status_code, 200)
response_json = json.loads(response.data.decode('utf-8'))
package_failed_count = response_json['most_failed_packages'][0]['count']
self.assertEqual(package_failed_count, 2)
def test_detail_latest_attempts_all_workshops(self):
"""
Get details of a package for latest attempt and all workshops.
"""
self.create_database()
payload = {"package_one_name": "EasyMercurial", "package_one_version": "2.5.0", "export": "json"}
response = self.application.get('/view/detail/', query_string=payload)
self.assertEqual(response.status_code, 200)
response_json = json.loads(response.data.decode('utf-8'))
system_count = response_json['package_details'][0]['user_system_info']['system'][0][1] # format : [('Linux', 3), ('windows', 2)]
self.assertEqual(system_count, 2)
def test_detail_all_attempts_all_workshops(self):
"""
Get details of a package for all attempt and all workshops.
"""
self.create_database()
payload = {
"package_one_name": "EasyMercurial",
"package_one_version": "2.5.0",
"all_attempts": "1",
"export": "json"
}
response = self.application.get('/view/detail/', query_string=payload)
self.assertEqual(response.status_code, 200)
response_json = json.loads(response.data.decode('utf-8'))
system_count = response_json['package_details'][0]['user_system_info']['system'][0][1] # format : [('Linux', 3), ('windows', 2)]
self.assertEqual(system_count, 3)
def test_detail_latest_attempt_one_workshop(self):
"""
Get details of a package for latest attempt and one workshop.
"""
self.create_database()
payload = {
"package_one_name": "EasyMercurial",
"package_one_version": "2.5.0",
"workshop_id": "test_workshop_1",
"export": "json"
}
response = self.application.get('/view/detail/', query_string=payload)
self.assertEqual(response.status_code, 200)
response_json = json.loads(response.data.decode('utf-8'))
system_count = response_json['package_details'][0]['user_system_info']['system'][0][1] # format : [('Linux', 3), ('windows', 2)]
self.assertEqual(system_count, 1)
def test_detail_all_attempts_one_workshop(self):
"""
Get details of a package for all attempts and one workshop.
"""
self.create_database()
payload = {
"package_one_name": "EasyMercurial",
"package_one_version": "2.5.0",
"workshop_id": "test_workshop_1",
"all_attempts": "1",
"export": "json"
}
response = self.application.get('/view/detail/', query_string=payload)
self.assertEqual(response.status_code, 200)
response_json = json.loads(response.data.decode('utf-8'))
system_count = response_json['package_details'][0]['user_system_info']['system'][0][1] # format : [('Linux', 3), ('windows', 2)]
self.assertEqual(system_count, 2)
if __name__ == '__main__':
unittest.main()