fix longs tests

This commit is contained in:
Ross
2023-06-05 16:44:35 +01:00
parent e0723d4d3a
commit afdc8e95c7
6 changed files with 278 additions and 152 deletions
+146 -80
View File
@@ -1,3 +1,4 @@
from collections import defaultdict
import json
import os
from re import A
@@ -23,6 +24,7 @@ from longs.models import (
Long as Question,
LongSeries,
LongSeriesImage,
)
from generic.models import CidUser, CidUserGroup, UserUserGroup
@@ -31,6 +33,8 @@ from django.contrib.auth.models import User
from rad.test_helpers import AssertNotFound, create_cid_user_and_groups
APP_NAME = "longs"
@@ -223,18 +227,30 @@ def test_exams(db, client):
print("check question:", q)
# check redirects to the json file work
# TODO: sort out testing of the saved JSON files
question_json_res = client.get(
reverse(
f"longs:exam_question_json_cid",
kwargs={
"pk": exam.pk,
"sk": q,
"cid": cid_user.cid,
"passcode": cid_user.passcode,
},
),
follow=True,
)
try:
question_json_res = client.get(
reverse(
f"longs:exam_question_json_cid",
kwargs={
"pk": exam.pk,
"sk": q,
"cid": cid_user.cid,
"passcode": cid_user.passcode,
},
),
follow=True,
)
except AttributeError: # UserUser has no cid attribute
question_json_res = client.get(
reverse(
f"longs:exam_question_json",
kwargs={
"pk": exam.pk,
"sk": q,
},
),
follow=True,
)
# assert question_json_res.status_code == 200
assert len(question_json_res.redirect_chain) == 2
assert question_json_res.redirect_chain[0][1] == 302
@@ -242,18 +258,42 @@ def test_exams(db, client):
assert question_json_res.redirect_chain[1][0].endswith(f"{q}.json")
question_json_unbased_res = client.get(
reverse(
f"longs:exam_question_json_unbased_cid",
kwargs={
"pk": exam.pk,
"sk": q,
"cid": cid_user.cid,
"passcode": cid_user.passcode,
},
),
follow=True,
)
print(cid_user, testing_cid_user)
if testing_cid_user:
question_json_unbased_res = client.get(
reverse(
f"longs:exam_question_json_unbased_cid",
kwargs={
"pk": exam.pk,
"sk": q,
"cid": cid_user.cid,
"passcode": cid_user.passcode,
},
),
follow=True,
)
else:
test = reverse(
f"longs:exam_question_json_unbased",
kwargs={
"pk": exam.pk,
"sk": q,
},
)
print(test)
question_json_unbased_res = client.get(
reverse(
f"longs:exam_question_json_unbased",
kwargs={
"pk": exam.pk,
"sk": q,
},
),
follow=True,
)
assert question_json_unbased_res.status_code == 200
question_json = json.loads(question_json_unbased_res.content)
@@ -262,9 +302,7 @@ def test_exams(db, client):
# As we store references to the questions we have to check individually
assert q in exam_json["questions"].keys()
q_json = Question.objects.get(pk=q).get_question_json(
based=False
)
q_json = Question.objects.get(pk=q).get_question_json(based=False)
assert question_json == q_json
@@ -325,17 +363,20 @@ def test_exams(db, client):
# Long answers currently send a seperate object for each component
valid_answers = []
valid_answers_extra = []
valid_answers_by_question = defaultdict(dict)
sections = (
("1", "answer observations"),
("2", "answer interpretation"),
("3", "answer principle diagnosis"),
("4", "answer differential diagnosis"),
("5", "answer management"),
)
i = 0
for questions in exam_json["questions"]:
for qid in questions:
# Long answers submit 5 sections
sections = (
("1", "answer observations"),
("2", "answer interpretation"),
("3", "answer principle diagnosis"),
("4", "answer differential diagnosis"),
("5", "answer management"),
)
for n, name in sections:
post_data = {
"eid": exam_json["eid"],
@@ -348,6 +389,8 @@ def test_exams(db, client):
post_data["passcode"] = cid_user.passcode
valid_answers_extra.append(post_data)
valid_answers_by_question[i][n] = post_data
i = i + 1
post_json = {
"eid": exam_json["eid"],
@@ -469,21 +512,32 @@ def test_exams(db, client):
answer_lis = cid_exam_scores_soup.find(
"ul", {"class": "score-answer-list"}
).find_all("li")
).find_all("li", {"class": "user-answer-li"})
assert len(answer_lis) == len(valid_answers)
assert len(answer_lis) == len(valid_answers_by_question)
pprint(valid_answers_by_question)
print(cid_user)
for n in range(
len(valid_answers)
len(valid_answers_by_question)
): # Check no answers are visible if results not published
answer = valid_answers[n]
li = answer_lis[n]
lis = answer_lis[n]
#n = str(n + 1) # questions are not zero indexed (lists are)
assert answer["ans"] in str(li) # Check that the saved answer is shown
for qidn, section_title in sections:
print(n, qidn)
answer = valid_answers_by_question[n][qidn]
assert not li.find(
"span", {"class": "correct-answer"}
) # and that the correct answer isn't
li = lis.find("li", attrs={"data-qidn": qidn})
assert li["class"][0].lower() in section_title
assert answer["ans"] in str(li) # Check that the saved answer is shown
# assert not li.find(
# "span", {"class": "correct-answer"}
# ) # and that the correct answer isn't
assert (
cid_exam_scores_soup.find("div", {"class": "score-overview"}) is None
@@ -498,9 +552,10 @@ def test_exams(db, client):
answer_lis = cid_exam_scores_soup.find(
"ul", {"class": "score-answer-list"}
).find_all("li")
assert len(answer_lis) == len(valid_answers) + 1
).find_all("li", {"class": "user-answer-li"})
assert len(answer_lis) == len(valid_answers_by_question) + 1
assert "Not answered" in str(answer_lis[-1])
assert str(answer_lis[-1]).count("Not answered") == 5
# Publish the results!
exam.publish_results = True
@@ -516,51 +571,62 @@ def test_exams(db, client):
answer_lis = cid_exam_scores_soup.find(
"ul", {"class": "score-answer-list"}
).find_all("li")
).find_all("li", {"class": "user-answer-li"})
assert len(answer_lis) == len(valid_answers) + 1
assert len(answer_lis) == len(valid_answers_by_question) + 1
assert "Not answered" in str(answer_lis[-1])
assert str(answer_lis[-1]).count("Not answered") == 5
# Repetition....
for n in range(
len(valid_answers)
): # Check answers are visible if results published
answer = valid_answers[n]
li = answer_lis[n]
len(valid_answers_by_question)
): # Check no answers are visible if results not published
lis = answer_lis[n]
#n = str(n + 1) # questions are not zero indexed (lists are)
assert answer["ans"] in str(li) # Check that the saved answer is shown
for qidn, section_title in sections:
answer = valid_answers_by_question[n][qidn]
assert li.find("span", {"class": "correct-answer"}).string == "testanswer"
li = lis.find("li", attrs={"data-qidn": qidn})
if users_tested == 1:
# First user the answers will not have been saved
assert (
cid_exam_scores_soup.find("div", {"class": "score-overview"})
.find("span", {"id": "total-score"})
.string
== "0 (3 unmarked)"
)
else:
# Subsequent users should already have answers marked (apart from the last question which we have deleted)
assert (
cid_exam_scores_soup.find("div", {"class": "score-overview"})
.find("span", {"id": "total-score"})
.string
== "4 (1 unmarked)"
)
assert li["class"][0].lower() in section_title
assert answer["ans"] in str(li) # Check that the saved answer is shown
print(cid_exam_scores_soup.find("div", {"class": "score-overview"}))
# For long cases an unmarked answers is given a score of 4
# Each user needs marking individually
assert (
cid_exam_scores_soup.find("div", {"class": "score-overview"})
.find("span", {"id": "total-score"})
.string
== "4 (3 unmarked)"
)
# Mark the questions
# For longs this needs to be done for each user
question: Question
for question in exam.exam_questions.all():
print(question)
print(question.get_compare_answers())
try:
new_ans = Answer(
question=question,
answer=question.get_unmarked_user_answers().pop(),
status=Answer.MarkOptions.CORRECT,
)
new_ans.save()
except IndexError: # Final question does not have an answer
pass
answers = question.get_unmarked_user_answers()
print(answers)
for answer in answers:
answer.score = UserAnswer.ScoreOptions.EIGHT
answer.save()
#try:
# new_ans = Answer(
# question=question,
# answer=question.get_unmarked_user_answers().pop(),
# status=Answer.MarkOptions.CORRECT,
# )
# new_ans.save()
#except IndexError: # Final question does not have an answer
# pass
# TODO: test double marking
# Load the exam results page (again)
cid_exam_scores_res = client.get(exam_scores_url)
@@ -570,7 +636,7 @@ def test_exams(db, client):
cid_exam_scores_soup.find("div", {"class": "score-overview"})
.find("span", {"id": "total-score"})
.string
== "6"
== "28.0"
)
generated_questions.pop(-1).delete()