Skip to content

Commit

Permalink
add tests for get_questions
Browse files Browse the repository at this point in the history
  • Loading branch information
ozer550 committed Jan 8, 2024
1 parent ffd1352 commit 88c35cb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
6 changes: 3 additions & 3 deletions kolibri/core/exams/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,13 @@ def get_questions(self):
Returns a list of all questions from all sections in the exam.
"""
questions = []
if self.data_model_version in {2, 1}:
for question in self.question_sources:
questions.append(question)
if self.data_model_version == 3:
for section in self.question_sources:
for question in section.get("questions", []):
questions.append(question)
else:
for question in self.question_sources:
questions.append(question)
return questions


Expand Down
2 changes: 1 addition & 1 deletion kolibri/core/exams/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def to_representation(self, instance):
data["question_sources"] = QuizSectionSerializer(
instance.question_sources, many=True
).data
if data["data_model_version"] in {1, 2}:
else:
data["question_sources"] = QuestionSourceSerializer(
instance.question_sources, many=True
).data
Expand Down
24 changes: 24 additions & 0 deletions kolibri/core/exams/test/test_exam_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,3 +525,27 @@ def test_post_exam_v2_model_fails(self):

response = self.post_new_exam(basic_exam)
self.assertEqual(response.status_code, 400)

def test_exam_model_get_questions_v3(self):
self.login_as_admin()
exam = self.make_basic_exam()
response = self.post_new_exam(exam)
exam_id = response.data["id"]
self.assertEqual(response.status_code, 201)
exam_model_instance = models.Exam.objects.get(id=exam_id)
self.assertEqual(len(exam_model_instance.get_questions()), 3)

def test_exam_model_get_questions_v2_v1(self):
self.login_as_admin()
self.exam.data_model_version = 2
self.exam.question_sources.append(
{
"exercise_id": uuid.uuid4().hex,
"question_id": uuid.uuid4().hex,
"title": "Title",
"counter_in_exercise": 0,
}
)

self.exam.save()
self.assertEqual(len(self.exam.get_questions()), 1)
2 changes: 1 addition & 1 deletion kolibri/plugins/coach/class_summary_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def _map_exam(item):
for question in question_source.get("questions", [])
if question.get("exercise_id") is not None
]
if data_model_version in {2, 1}:
else:
item["node_ids"] = [
question["exercise_id"]
for question in item.get("question_sources", [])
Expand Down

0 comments on commit 88c35cb

Please sign in to comment.