-
Notifications
You must be signed in to change notification settings - Fork 47
/
controllers.py
425 lines (352 loc) · 14.5 KB
/
controllers.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
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
"""Maintain logic to perform CRUD operations on regression tests."""
from flask import (Blueprint, abort, flash, g, jsonify, redirect, request,
url_for)
from sqlalchemy import and_
from decorators import template_renderer
from mod_auth.controllers import check_access_rights, login_required
from mod_auth.models import Role
from mod_regression.forms import (AddCategoryForm, AddCorrectOutputForm,
AddTestForm, ConfirmationForm, EditTestForm,
RemoveCorrectOutputForm)
from mod_regression.models import (Category, InputType, OutputType,
RegressionTest, RegressionTestOutput,
RegressionTestOutputFiles)
from mod_sample.models import Sample, Tag
from mod_test.models import TestResultFile
from utility import serve_file_download
mod_regression = Blueprint('regression', __name__)
@mod_regression.before_app_request
def before_app_request() -> None:
"""Curate menu entries before app request."""
g.menu_entries['regression'] = {
'title': 'Regression tests',
'icon': 'industry',
'route': 'regression.index'
}
@mod_regression.route('/')
@template_renderer()
def index():
"""Display all regression tests."""
return {
'tests': RegressionTest.query.all(),
'categories': Category.query.order_by(Category.name.asc()).all(),
'tags': Tag.query.all()
}
@mod_regression.route('/sample/<sample_id>')
@template_renderer()
def by_sample(sample_id):
"""
Display regression tests based on the given sample.
:param sample_id: id of the sample
:type sample_id: int
:return: regression tests of the sample
:rtype: dict
"""
# Show all regression tests for sample
sample = Sample.query.filter(Sample.id == sample_id).first()
if sample is None:
g.log.error(f'requested sample with id: {sample_id} not found!')
abort(404)
return {
'sample': sample,
'tests': RegressionTest.query.filter(
RegressionTest.sample_id == sample.id).all()
}
@mod_regression.route('/test/<regression_id>/view')
@template_renderer()
def test_view(regression_id):
"""
Show a single regression test.
:param regression_id: id of the regression test
:type regression_id: int
:return: Regression test
:rtype: dict
"""
test = RegressionTest.query.filter(RegressionTest.id == regression_id).first()
if test is None:
g.log.error(f'requested regression test with id: {regression_id} not found!')
abort(404)
return {
'test': test
}
@mod_regression.route('/test/<regression_id>/delete', methods=['GET', 'POST'])
@template_renderer()
@login_required
@check_access_rights([Role.contributor, Role.admin])
def test_delete(regression_id):
"""
Delete the regression test.
Requires contributor or admin role.
:param regression_id: The ID of the Regression Test
:type regression_id: int
:return: Redirect
"""
# Show a Single Test
test = RegressionTest.query.filter(RegressionTest.id == regression_id).first()
if test is None:
g.log.error(f'requested regression test with id: {regression_id} not found!')
abort(404)
form = ConfirmationForm()
if form.validate_on_submit():
g.db.delete(test)
g.db.commit()
g.log.warning(f'regression test with id: {regression_id} deleted!')
flash('Regression Test Deleted')
return redirect(url_for('.index'))
return {
'form': form,
'regression_id': regression_id
}
@mod_regression.route('/test/<regression_id>/edit', methods=['GET', 'POST'])
@template_renderer()
@login_required
@check_access_rights([Role.contributor, Role.admin])
def test_edit(regression_id):
"""
Edit regression test.
Requires contributor or admin role.
param regression_id : The ID of the Regression Test
type regression_id : int
"""
test = RegressionTest.query.filter(RegressionTest.id == regression_id).first()
if test is None:
g.log.error(f'requested regression test with id: {regression_id} not found!')
abort(404)
form = EditTestForm(request.form)
form.sample_id.choices = [(sam.id, sam.sha) for sam in Sample.query.all()]
form.category_id.choices = [(cat.id, cat.name) for cat in Category.query.all()]
if form.validate_on_submit():
# Remove category for test and add again.
old_category = Category.query.filter(Category.id == test.categories[0].id).first()
old_category.regression_tests.remove(test)
new_category = Category.query.filter(Category.id == form.category_id.data).first()
new_category.regression_tests.append(test)
test.sample_id = form.sample_id.data
test.command = form.command.data
test.category_id = form.category_id.data
test.expected_rc = form.expected_rc.data
test.input_type = InputType.from_string(form.input_type.data)
test.output_type = OutputType.from_string(form.output_type.data)
test.description = form.description.data
g.db.commit()
g.log.info(f'regression test with id: {regression_id} updated!')
return redirect(url_for('.test_view', regression_id=regression_id))
if not form.is_submitted():
# Populate form with current set sample values
form.sample_id.data = test.sample_id
form.command.data = test.command
form.category_id.data = test.categories[0].id
form.expected_rc.data = test.expected_rc
form.input_type.data = test.input_type.value
form.output_type.data = test.output_type.value
form.description.data = test.description
return {'form': form, 'regression_id': regression_id}
@mod_regression.route('/test/<regression_id>/toggle')
@login_required
@check_access_rights([Role.contributor, Role.admin])
def toggle_active_status(regression_id):
"""
Change active status of the regression test.
:param regression_id: id of the regression test
:type regression_id: int
:return: response of status toggle
:rtype: dict
"""
regression_test = RegressionTest.query.filter(RegressionTest.id == regression_id).first()
if regression_test is None:
g.log.error(f'requested regression test with id: {regression_id} not found!')
abort(404)
regression_test.active = not regression_test.active
g.db.commit()
return jsonify({
"status": "success",
"active": str(regression_test.active)
})
@mod_regression.route('/test/<regression_test_output_id>/download', methods=['GET'])
def test_result_file(regression_test_output_id):
"""View the output files of the regression test."""
rto = RegressionTestOutput.query.filter(RegressionTestOutput.id == regression_test_output_id).first()
if rto is None:
g.log.error(f'requested regression test output with id: {regression_test_output_id} not found!')
abort(404)
return serve_file_download(rto.filename_correct, 'TestResults')
@mod_regression.route('/test/<regression_test_output_id>/download/variant', methods=['GET'])
def multiple_test_result_file(regression_test_output_id):
"""View the output files of the regression test (variants)."""
rtof = RegressionTestOutputFiles.query.filter(RegressionTestOutputFiles.id == regression_test_output_id).first()
if rtof is None:
g.log.error(f'requested regression test output file with id: {regression_test_output_id} not found!')
abort(404)
return serve_file_download(rtof.file_hashes + rtof.output.correct_extension, 'TestResults')
@mod_regression.route('/test/new', methods=['GET', 'POST'])
@template_renderer()
@login_required
@check_access_rights([Role.contributor, Role.admin])
def test_add():
"""
Add a regression test.
:return: form to add regression test
:rtype: dict
"""
form = AddTestForm(request.form)
form.sample_id.choices = [(sam.id, sam.sha) for sam in Sample.query.all()]
form.category_id.choices = [(cat.id, cat.name) for cat in Category.query.all()]
if form.validate_on_submit():
new_test = RegressionTest(
sample_id=form.sample_id.data,
command=form.command.data,
category_id=form.category_id.data,
expected_rc=form.expected_rc.data,
input_type=InputType.from_string(form.input_type.data),
output_type=OutputType.from_string(form.output_type.data),
description=form.description.data,
)
g.db.add(new_test)
category = Category.query.filter(Category.id == form.category_id.data).first()
category.regression_tests.append(new_test)
g.db.commit()
return redirect(url_for('.index'))
return {'form': form}
@mod_regression.route('/category/<category_id>/delete', methods=['GET', 'POST'])
@template_renderer()
@login_required
@check_access_rights([Role.contributor, Role.admin])
def category_delete(category_id):
"""
Delete the category.
:param category_id: The ID of the Category
:type category_id: int
:return: form and category
:rtype: dict
"""
category = Category.query.filter(Category.id == category_id).first()
if category is None:
g.log.error(f'requested category with id: {category_id} not found!')
abort(404)
form = ConfirmationForm()
if form.validate_on_submit():
g.db.delete(category)
g.db.commit()
g.log.warning(f'category with id: {category_id} deleted!')
return redirect(url_for('.index'))
return {
'form': form,
'category': category
}
@mod_regression.route('/category/<category_id>/edit', methods=['GET', 'POST'])
@template_renderer()
@login_required
@check_access_rights([Role.contributor, Role.admin])
def category_edit(category_id):
"""
Edit regression test category.
:param category_id: The ID of the Regression Test Category
:type category_id: int
:return: form and category id
:rtype: dict
"""
category = Category.query.filter(Category.id == category_id).first()
if category is None:
g.log.error(f'requested category with id: {category_id} not found!')
abort(404)
form = AddCategoryForm(request.form)
if form.validate():
category.name = form.category_name.data
category.description = form.category_description.data
g.db.commit()
g.log.info(f'category with id: {category_id} updated!')
flash('Category Updated')
return redirect(url_for('.index'))
if not form.is_submitted():
# Populate form with current set category values
form.category_name.data = category.name
form.category_description.data = category.description
return {'form': form, 'category_id': category_id}
@mod_regression.route('/category_add', methods=['GET', 'POST'])
@template_renderer()
@login_required
@check_access_rights([Role.contributor, Role.admin])
def category_add():
"""
Add a regression test category.
:return: form to add category
:rtype: dict
"""
form = AddCategoryForm(request.form)
if form.validate():
new_category = Category(
name=form.category_name.data, description=form.category_description.data)
g.db.add(new_category)
g.db.commit()
flash('New Category Added')
return redirect(url_for('.index'))
return {'form': form}
@mod_regression.route('/test/<regression_id>/output/new', methods=['GET', 'POST'])
@template_renderer()
@login_required
@check_access_rights([Role.contributor, Role.admin])
def output_add(regression_id):
"""
Add New Output.
Requires contributor or admin role.
param regression_id : The ID of the Regression Test
type regression_id : int
"""
test = RegressionTest.query.filter(RegressionTest.id == regression_id).first()
if test is None:
g.log.error(f'requested regression test with id: {regression_id} not found!')
abort(404)
form = AddCorrectOutputForm(request.form)
test_result = TestResultFile.query.filter(
and_(TestResultFile.regression_test_id == regression_id, TestResultFile.got.isnot(None))
).order_by(TestResultFile.test_id.desc()).limit(50).all()
check_doubles = {}
for result in test_result:
if result.got not in check_doubles:
append = True
for output_file in result.regression_test_output.multiple_files:
if result.got.strip() == output_file.file_hashes.strip():
append = False
break
if append:
check_doubles[result.got] = int(result.test_id)
form.output_file.choices = [(output.id, output.filename_correct + ' (original)') for output in test.output_files]
form.test_id.choices = [f'Test id {test_id} with output {got}' for got, test_id in check_doubles.items()]
if form.validate_on_submit():
test_data = form.test_id.data.strip().split()
new_output = RegressionTestOutputFiles(
regression_test_output_id=form.output_file.data,
file_hashes=test_data[5]
)
g.db.add(new_output)
g.db.commit()
g.log.warning(f'Output file for RegressionTestOutput id: {form.output_file.data} added!')
return redirect(url_for('.test_view', regression_id=regression_id))
return {'form': form, 'regression_id': regression_id}
@mod_regression.route('/test/<regression_id>/output/remove', methods=['GET', 'POST'])
@template_renderer()
@login_required
@check_access_rights([Role.contributor, Role.admin])
def output_remove(regression_id):
"""
Remove an Output File.
Requires contributor or admin role.
param regression_id : The ID of the Regression Test
type regression_id : int
"""
test = RegressionTest.query.filter(RegressionTest.id == regression_id).first()
if test is None:
g.log.error(f'requested regression test with id: {regression_id} not found!')
abort(404)
form = RemoveCorrectOutputForm(request.form)
form.output_file.choices = [(a.id, a.file_hashes + ' (variant)')
for r in test.output_files for a in r.multiple_files]
if form.validate_on_submit():
variant_file = RegressionTestOutputFiles.query.filter(
RegressionTestOutputFiles.id == form.output_file.data
).first()
g.db.delete(variant_file)
g.db.commit()
g.log.warning(f'Output file with id: {form.output_file.data} deleted!')
return redirect(url_for('.test_view', regression_id=regression_id))
return {'form': form, 'regression_id': regression_id}