Further basic integration testing
This commit is contained in:
@@ -0,0 +1,344 @@
|
||||
import json
|
||||
import os
|
||||
from re import A
|
||||
from django.conf import settings
|
||||
import pytest
|
||||
|
||||
import tempfile
|
||||
|
||||
# from django.contrib.auth.models import User
|
||||
from django.urls import reverse
|
||||
|
||||
from rich.pretty import pprint
|
||||
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
from rapids.views import GenericExamViews as RapidExamViews
|
||||
|
||||
from rapids.models import Answer, Exam, Examination, Rapid as Question
|
||||
|
||||
from generic.models import CidUser, CidUserGroup
|
||||
|
||||
APP_NAME = "rapids"
|
||||
|
||||
|
||||
def create_cid_user_and_groups(db):
|
||||
group1 = CidUserGroup.objects.create(name="Group1")
|
||||
group2 = CidUserGroup.objects.create(name="Group2")
|
||||
|
||||
cid_user_1000 = CidUser.objects.create(cid=1000, passcode="ABCD", group=group1)
|
||||
cid_user_1001 = CidUser.objects.create(cid=1001, passcode="EFGH", group=group1)
|
||||
cid_user_2001 = CidUser.objects.create(cid=2001, passcode="EFGH", group=group2)
|
||||
|
||||
assert cid_user_1000.cid == 1000
|
||||
assert cid_user_1001.cid == 1001
|
||||
|
||||
|
||||
def create_exam(db):
|
||||
exam: Exam = RapidExamViews.Exam.objects.create(
|
||||
name="Cid Exam", exam_mode=True, active=True
|
||||
)
|
||||
|
||||
group1: CidUserGroup = CidUserGroup.objects.get(name="Group1")
|
||||
|
||||
exam.cid_user_groups.add(group1)
|
||||
|
||||
exam.valid_cid_users.add(*group1.ciduser_set.all())
|
||||
|
||||
assert exam in group1.rapids_cid_user_groups.all()
|
||||
|
||||
assert exam
|
||||
|
||||
return exam
|
||||
|
||||
|
||||
def create_examination(db):
|
||||
examination = Examination.objects.create(examination="test examination")
|
||||
examination.save()
|
||||
examination = Examination.objects.create(examination="test examination 2")
|
||||
examination.save()
|
||||
|
||||
|
||||
def create_rapid_image(db, rapid):
|
||||
rapid_image = RapidImage()
|
||||
|
||||
|
||||
def create_question(exam, answer=None):
|
||||
# Just assign random stuff
|
||||
|
||||
image = tempfile.NamedTemporaryFile(
|
||||
dir=settings.MEDIA_ROOT, suffix=".jpg", delete=False
|
||||
)
|
||||
|
||||
question: Question = Question.objects.create(
|
||||
laterality=Question.LATERALITY_CHOICES.NONE,
|
||||
examination=Examination.objects.get(pk=1),
|
||||
image=image.name,
|
||||
)
|
||||
question.exams.set([exam])
|
||||
|
||||
# Create an answer for the question
|
||||
if answer is None:
|
||||
answer_text = "testanswer"
|
||||
else:
|
||||
answer_text = answer
|
||||
answer = Answer(question=question, answer=answer_text, answer_compare=answer_text, status=Answer.MarkOptions.CORRECT)
|
||||
answer.save()
|
||||
|
||||
return question
|
||||
|
||||
|
||||
def test_exams(db, client):
|
||||
return
|
||||
create_cid_user_and_groups(db)
|
||||
exam = create_exam(db)
|
||||
|
||||
|
||||
create_structures(db)
|
||||
create_question_types(db)
|
||||
|
||||
question1 = create_question(exam)
|
||||
create_question(exam)
|
||||
create_question(exam)
|
||||
|
||||
active_exams = client.get(reverse(f"{APP_NAME}:active_exams"))
|
||||
assert active_exams.status_code == 200
|
||||
assert len(json.loads(active_exams.content)["exams"]) == 0
|
||||
|
||||
cid_user_1001 = CidUser.objects.get(cid=1001)
|
||||
|
||||
for cid_user in [cid_user_1001]:
|
||||
# Test active exams sections works as intended
|
||||
active_exams_cid = client.get(
|
||||
reverse(
|
||||
f"active_exams_cid",
|
||||
kwargs={"cid": cid_user.cid, "passcode": cid_user.passcode},
|
||||
)
|
||||
)
|
||||
|
||||
assert active_exams_cid.status_code == 200
|
||||
assert len(json.loads(active_exams_cid.content)["exams"]) == 1
|
||||
|
||||
active_exams_cid = client.get(
|
||||
reverse(f"active_exams_cid", kwargs={"cid": 1001, "passcode": "EFGH"})
|
||||
)
|
||||
|
||||
assert active_exams_cid.status_code == 200
|
||||
assert len(json.loads(active_exams_cid.content)["exams"]) == 1
|
||||
|
||||
no_active_exams_cid = client.get(
|
||||
reverse(f"active_exams_cid", kwargs={"cid": 2001, "passcode": "EGFH"})
|
||||
)
|
||||
|
||||
assert no_active_exams_cid.status_code == 401
|
||||
assert json.loads(no_active_exams_cid.content)["status"] == "invalid"
|
||||
|
||||
invalid_passcode = client.get(
|
||||
reverse(f"active_exams_cid", kwargs={"cid": 1001, "passcode": "ABCD"})
|
||||
)
|
||||
assert invalid_passcode.status_code == 401
|
||||
assert json.loads(invalid_passcode.content)["status"] == "invalid"
|
||||
|
||||
exam_metadata = json.loads(active_exams_cid.content)["exams"][0]
|
||||
|
||||
assert exam_metadata["name"] == "Cid Exam"
|
||||
assert exam_metadata["exam_active"] == True
|
||||
assert exam_metadata["exam_mode"] == True
|
||||
|
||||
res = client.get(exam_metadata["url"])
|
||||
assert res.status_code == 200
|
||||
exam_json = json.loads(res.content)
|
||||
|
||||
assert (
|
||||
question1.get_question_json(answers=False)
|
||||
in exam_json["questions"].values()
|
||||
)
|
||||
|
||||
assert len(exam_json["questions"]) == 3
|
||||
|
||||
exam.active = False
|
||||
exam.save()
|
||||
|
||||
res = client.get(exam_metadata["url"])
|
||||
assert res.status_code == 404
|
||||
|
||||
exam.active = True
|
||||
exam.save()
|
||||
|
||||
post_json = {
|
||||
"eid": f"anatomy/{exam.pk}",
|
||||
"cid": cid_user_1001.cid,
|
||||
"start_time": "1659618141.731",
|
||||
"answers": [[]],
|
||||
}
|
||||
|
||||
# Test submission
|
||||
# Start with 0 answers
|
||||
res = client.post(reverse("global_exam_answers_submit"), data=post_json)
|
||||
|
||||
json_res = json.loads(res.content)
|
||||
|
||||
assert json_res["success"] == True
|
||||
assert json_res["question_count"] == 0
|
||||
|
||||
post_json = {
|
||||
"eid": f"anatomy/{exam.pk+10}",
|
||||
"cid": cid_user_1001.cid,
|
||||
"start_time": "1659618141.731",
|
||||
"answers": [[]],
|
||||
}
|
||||
res = client.post(reverse("global_exam_answers_submit"), data=post_json)
|
||||
json_res = json.loads(res.content)
|
||||
assert json_res["success"] == False
|
||||
|
||||
# Test invalid cid
|
||||
post_json = {
|
||||
"eid": f"anatomy/{exam.pk+10}",
|
||||
"cid": 1,
|
||||
"start_time": "1659618141.731",
|
||||
"answers": [[]],
|
||||
}
|
||||
res = client.post(reverse("global_exam_answers_submit"), data=post_json)
|
||||
json_res = json.loads(res.content)
|
||||
assert json_res["success"] == False
|
||||
assert json_res["error"] == "Invalid data"
|
||||
|
||||
valid_answers = []
|
||||
|
||||
for questions in exam_json["questions"]:
|
||||
for qid in questions:
|
||||
valid_answers.append(
|
||||
{
|
||||
"eid": exam_json["eid"],
|
||||
"cid": cid_user_1001.cid,
|
||||
"passcode": cid_user_1001.passcode,
|
||||
"qid": qid,
|
||||
"qidn": "1",
|
||||
"ans": f"answer {qid}",
|
||||
}
|
||||
)
|
||||
|
||||
post_json = {
|
||||
"eid": exam_json["eid"],
|
||||
"cid": cid_user_1001.cid,
|
||||
"start_time": "1659618141.731",
|
||||
"answers": json.dumps(valid_answers),
|
||||
}
|
||||
|
||||
# Test submission
|
||||
# give three answers
|
||||
res = client.post(reverse("global_exam_answers_submit"), data=post_json)
|
||||
|
||||
json_res = json.loads(res.content)
|
||||
|
||||
assert json_res["success"] == True
|
||||
assert json_res["question_count"] == 3
|
||||
|
||||
# We can then check the answers have been submitted (and can be viewed by the user)
|
||||
|
||||
# Get overview scores page
|
||||
cid_scores_res = client.get(
|
||||
reverse(f"cid_scores", kwargs={"cid": 1001, "passcode": "EFGH"})
|
||||
)
|
||||
|
||||
cid_scores_soup = BeautifulSoup(cid_scores_res.content, "html.parser")
|
||||
|
||||
print(cid_scores_soup.find("div", {"id" : "exam-assigned"}))
|
||||
search_exam = cid_scores_soup.find("div", {"id" : "exam-assigned"}).find("ul", {"class" : exam.app_name}).find_all("li", attrs={"data-exam-id": exam.pk})
|
||||
assert search_exam
|
||||
assert exam.name in str(search_exam)
|
||||
assert "Active" in str(search_exam)
|
||||
#assert "Active" in assigned_exams # check it is active
|
||||
|
||||
invalid_exam = cid_scores_soup.find_all("li", attrs={"data-exam-id": exam.pk+5})
|
||||
assert not invalid_exam # Check we can't find an invalid exam
|
||||
|
||||
# Check that we have a results link
|
||||
results_exam = cid_scores_soup.find("div", {"id" : "exam-results"}).find("ul", {"class" : exam.app_name}).find_all("li", attrs={"data-exam-id": exam.pk})
|
||||
assert results_exam
|
||||
assert exam.name in str(results_exam)
|
||||
assert exam.name+" test" not in str(results_exam)
|
||||
assert "Results Published" not in str(results_exam) # It should not be published yet
|
||||
|
||||
|
||||
# Load the exam results page
|
||||
cid_exam_scores_res = client.get(
|
||||
reverse(f"{exam.app_name}:exam_scores_cid_user", kwargs={"pk": exam.pk, "cid": 1001, "passcode": "EFGH"})
|
||||
)
|
||||
cid_exam_scores_soup = BeautifulSoup(cid_exam_scores_res.content, "html.parser")
|
||||
|
||||
# Check that we have an alert that exams are not published
|
||||
alert = cid_exam_scores_soup.find("div", {"class": "alert"})
|
||||
assert "Results are not currently published." in str(alert)
|
||||
|
||||
|
||||
answer_lis = cid_exam_scores_soup.find("ul", {"class": "score-answer-list"}).find_all("li")
|
||||
|
||||
assert len(answer_lis) == len(valid_answers)
|
||||
|
||||
for n in range(len(valid_answers)): # Check no answers are visible if results not published
|
||||
answer = valid_answers[n]
|
||||
li = answer_lis[n]
|
||||
|
||||
assert answer["ans"] in str(li) # Check that the saved answer is shown
|
||||
|
||||
assert li.find("span", {"class": "correct-answer"}).string == "*****" # and that the correct answer isn't
|
||||
|
||||
assert cid_exam_scores_soup.find("div", {"class": "score-overview"}) is None # check we hide the score overview if not published
|
||||
|
||||
# Add another question to the exam
|
||||
create_question(exam)
|
||||
|
||||
# Load the exam results page (again)
|
||||
cid_exam_scores_res = client.get(
|
||||
reverse(f"{exam.app_name}:exam_scores_cid_user", kwargs={"pk": exam.pk, "cid": 1001, "passcode": "EFGH"})
|
||||
)
|
||||
cid_exam_scores_soup = BeautifulSoup(cid_exam_scores_res.content, "html.parser")
|
||||
|
||||
answer_lis = cid_exam_scores_soup.find("ul", {"class": "score-answer-list"}).find_all("li")
|
||||
assert len(answer_lis) == len(valid_answers) + 1
|
||||
assert "Not answered" in str(answer_lis[-1])
|
||||
|
||||
# Publish the results!
|
||||
exam.publish_results = True
|
||||
exam.save()
|
||||
|
||||
# Load the exam results page (again)
|
||||
cid_exam_scores_res = client.get(
|
||||
reverse(f"{exam.app_name}:exam_scores_cid_user", kwargs={"pk": exam.pk, "cid": 1001, "passcode": "EFGH"})
|
||||
)
|
||||
cid_exam_scores_soup = BeautifulSoup(cid_exam_scores_res.content, "html.parser")
|
||||
|
||||
assert cid_exam_scores_soup.find("div", {"class": "alert"}) is None # Check our warning banner is gone
|
||||
|
||||
answer_lis = cid_exam_scores_soup.find("ul", {"class": "score-answer-list"}).find_all("li")
|
||||
|
||||
assert len(answer_lis) == len(valid_answers) + 1
|
||||
assert "Not answered" in str(answer_lis[-1])
|
||||
|
||||
# Repetition....
|
||||
for n in range(len(valid_answers)): # Check answers are visible if results published
|
||||
answer = valid_answers[n]
|
||||
li = answer_lis[n]
|
||||
|
||||
assert answer["ans"] in str(li) # Check that the saved answer is shown
|
||||
|
||||
assert li.find("span", {"class": "correct-answer"}).string == "testanswer"
|
||||
|
||||
assert cid_exam_scores_soup.find("div", {"class": "score-overview"}).find("span", {"id": "total-score"}).string == "0 (3 unmarked)"
|
||||
|
||||
|
||||
for question in exam.exam_questions.all():
|
||||
try:
|
||||
new_ans = Answer(question=question, answer=question.get_unmarked_user_answers().pop(), status=Answer.MarkOptions.CORRECT)
|
||||
new_ans.save()
|
||||
except KeyError: # Final question does not have an answer
|
||||
pass
|
||||
|
||||
# Load the exam results page (again)
|
||||
cid_exam_scores_res = client.get(
|
||||
reverse(f"{exam.app_name}:exam_scores_cid_user", kwargs={"pk": exam.pk, "cid": 1001, "passcode": "EFGH"})
|
||||
)
|
||||
cid_exam_scores_soup = BeautifulSoup(cid_exam_scores_res.content, "html.parser")
|
||||
|
||||
assert cid_exam_scores_soup.find("div", {"class": "score-overview"}).find("span", {"id": "total-score"}).string == "6"
|
||||
Reference in New Issue
Block a user