Skip to content

Commit

Permalink
Add tests for quizzes
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonGrace2282 committed Jun 3, 2024
1 parent 141febe commit 9b4f1eb
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 9 deletions.
9 changes: 0 additions & 9 deletions tin/apps/assignments/tests/test_assignments.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,3 @@ def test_submit_assignment_with_file(client, assignment):

assert is_redirect(response)
assert assignment.submissions.count() == 1


@login("student")
def test_submit_quiz_as_assignment(client, quiz):
response = client.post(
reverse("assignments:submit", args=[quiz.id]), {"text": "print('I hate CSS')"}
)

assert response.status_code == 404
101 changes: 101 additions & 0 deletions tin/apps/assignments/tests/test_quiz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
from __future__ import annotations

import json

import pytest
from django.conf import settings
from django.urls import reverse

from tin.tests import is_redirect, login


@login("student")
def test_submit_quiz_as_assignment(client, quiz):
response = client.post(
reverse("assignments:submit", args=[quiz.id]), {"text": "print('I hate CSS')"}
)

assert response.status_code == 404


@login("student")
def test_submit_quiz(client, quiz):
response = client.post(
reverse("assignments:quiz", args=[quiz.id]), {"text": "print('I hate CSS')"}
)

assert is_redirect(response)
assert quiz.submissions.count() == 1


@login("student")
def test_submit_assigment_as_quiz(client, assignment):
response = client.post(
reverse("assignments:quiz", args=[assignment.id]), {"text": "print('I hate CSS')"}
)

assert response.status_code == 404


@login("student")
def test_quiz_ended_has_message(client, quiz):
response = client.post(reverse("assignments:quiz_end", args=[quiz.id]))
assert is_redirect(response)
all = tuple(msg for msg in quiz.log_messages.all())
assert len(all) == 1
(first,) = all
assert first.content == "Ended quiz"

response = client.post(
reverse("assignments:quiz", args=[quiz.id]), {"text": "print('I hate CSS')"}
)

assert response.status_code == 404


@login("student")
def test_quiz_data_basic(client, quiz):
response = client.get(reverse("assignments:report", args=[quiz.id]))
data = json.loads(response.content.decode("utf-8"))
assert data == {"action": "no action"}


@login("teacher")
@pytest.mark.parametrize(
("quiz_action", "action"),
(("1", "color"), ("2", "lock")),
)
def test_quiz_data_with_severity(client, quiz, quiz_action, action):
quiz.quiz_action = quiz_action
quiz.save()

response = client.get(
reverse("assignments:report", args=[quiz.id]),
{"severity": settings.QUIZ_ISSUE_THRESHOLD, "content": "hi"},
)
data = json.loads(response.content.decode("utf-8"))
assert data == {"action": action}

msgs = tuple(quiz.log_messages.all())
assert len(msgs) == 1
assert msgs[0].content == "hi"


@login("teacher")
def test_quiz_data_after_close(client, quiz):
# this should end the quiz
client.post(reverse("assignments:quiz_end", args=[quiz.id]))
response = client.get(
reverse("assignments:report", args=[quiz.id]),
{"severity": settings.QUIZ_ISSUE_THRESHOLD, "content": "hi"},
)

assert json.loads(response.content.decode("utf-8")) == {"action": "no action"}


@login("teacher")
def test_clear_quiz_messages(client, student, quiz):
quiz.log_messages.create(student=student, content="hi", severity=0)
response = client.post(reverse("assignments:clear", args=[quiz.id, student.id]))
assert is_redirect(response)
assert not quiz.log_messages.exists()

0 comments on commit 9b4f1eb

Please sign in to comment.