test_problems.py 3.5 KB
Newer Older
Adrià Labay's avatar
Adrià Labay committed
1
2
3
import pytest
from datetime import datetime

4
5
from zesje.database import db, Exam, ExamLayout, Problem, FeedbackOption,\
                           ProblemWidget, Student, Submission, Solution, Grader
Adrià Labay's avatar
Adrià Labay committed
6
7
8
9


@pytest.fixture
def add_test_data(app):
10
11
12
13
14
    for layout in ExamLayout:
        id = layout.value
        print(id)
        exam = Exam(id=id, name=f'exam {layout.name}', finalized=True, layout=layout)
        db.session.add(exam)
Adrià Labay's avatar
Adrià Labay committed
15

16
17
        problem = Problem(id=id, name=f'Problem {layout.name}', exam_id=id)
        db.session.add(problem)
Adrià Labay's avatar
Adrià Labay committed
18

19
20
21
22
        problem_widget = ProblemWidget(id=id, name=f'problem widget {layout.name}', problem_id=id, page=2,
                                       width=100, height=150, x=40, y=200, type='problem_widget')
        db.session.add(problem_widget)
        db.session.commit()
Adrià Labay's avatar
Adrià Labay committed
23

24
25
26
        feedback_option = FeedbackOption(id=id, problem_id=id, text='text', description='desc', score=1)
        db.session.add(feedback_option)
        db.session.commit()
Adrià Labay's avatar
Adrià Labay committed
27

28
    yield app
Adrià Labay's avatar
Adrià Labay committed
29
30
31
32


@pytest.mark.parametrize('id, status', [
    (1, 200),
33
    (2, 200),
Adrià Labay's avatar
Adrià Labay committed
34
    (42, 404)
35
], ids=['Exists Templated', 'Exists unstructured', 'Not exists'])
Adrià Labay's avatar
Adrià Labay committed
36
37
38
39
40
41
def test_get_problem(test_client, add_test_data, id, status):
    result = test_client.get(f'/api/problems/{id}')

    assert result.status_code == status


42
43
44
45
46
47
48
49
@pytest.mark.parametrize('exam_id, position, status', [
    (1, (-1, 4, 400, 200), 409),
    (1, (10, -5, 400, 200), 409),
    (1, (10, 10, 800, 200), 409),
    (1, (10, 700, 400, 500), 409),
    (2, (-24, 5, 10000, 200), 200)
], ids=['Exceeds left', 'Exceeds top', 'Exceeds right', 'Exceeds bottom', 'Allowed unstructured'])
def test_add_problem(test_client, add_test_data, exam_id, position, status):
Adrià Labay's avatar
Adrià Labay committed
50
    req_body = {
51
        'exam_id': exam_id,
Adrià Labay's avatar
Adrià Labay committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
        'x': position[0],
        'y': position[1],
        'width': position[2],
        'height': position[3],
        'page': 1,
        'name': 'Problem'
    }

    result = test_client.post('/api/problems', data=req_body)
    assert result.status_code == status


@pytest.mark.parametrize('id, status', [
    (1, 200),
66
    (2, 200),
Adrià Labay's avatar
Adrià Labay committed
67
    (42, 404),
68
], ids=['Allowed templated', 'Allowed unstructured', 'Not exists'])
Adrià Labay's avatar
Adrià Labay committed
69
70
71
72
73
74
75
76
77
78
79
def test_rename_problem(test_client, add_test_data, id, status):
    new_name = 'New'
    result = test_client.put(f'/api/problems/{id}', data={'name': new_name})

    assert result.status_code == status
    if status == 200:
        assert Problem.query.get(id).name == new_name


@pytest.mark.parametrize('id, status', [
    (1, 200),
80
    (2, 200),
Adrià Labay's avatar
Adrià Labay committed
81
    (42, 404),
82
], ids=['Allowed templated', 'Allowed unstructured', 'Not exists'])
Adrià Labay's avatar
Adrià Labay committed
83
84
85
86
87
88
89
def test_delete_problem(test_client, add_test_data, id, status):
    result = test_client.delete(f'/api/problems/{id}')

    assert result.status_code == status
    assert Problem.query.get(id) is None


90
91
92
93
94
@pytest.mark.parametrize('exam_id, problem_id', [
    (1, 1),
    (2, 2),
], ids=['Templated', 'Unstructured'])
def test_delete_problem_graded(test_client, add_test_data, exam_id, problem_id):
Adrià Labay's avatar
Adrià Labay committed
95
96
97
98
99
    student = Student(first_name='', last_name='')
    db.session.add(student)
    grader = Grader(name='Zesje')
    db.session.add(grader)
    db.session.commit()
100
    sub = Submission(student=student, exam_id=exam_id)
Adrià Labay's avatar
Adrià Labay committed
101
102
    db.session.add(sub)
    db.session.commit()
103
    sol = Solution(problem_id=problem_id, submission=sub, graded_by=grader, graded_at=datetime.now())
Adrià Labay's avatar
Adrià Labay committed
104
105
106
    db.session.add(sol)
    db.session.commit()

107
    result = test_client.delete(f'/api/problems/{problem_id}')
Adrià Labay's avatar
Adrià Labay committed
108
109

    assert result.status_code == 403
110
    assert Problem.query.get(problem_id) is not None