From 88c35cb9e031a04f9ef2d3d1ce89ac691091ad48 Mon Sep 17 00:00:00 2001 From: ozer550 Date: Mon, 8 Jan 2024 12:22:23 +0530 Subject: [PATCH] add tests for get_questions --- kolibri/core/exams/models.py | 6 +++--- kolibri/core/exams/serializers.py | 2 +- kolibri/core/exams/test/test_exam_api.py | 24 ++++++++++++++++++++++ kolibri/plugins/coach/class_summary_api.py | 2 +- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/kolibri/core/exams/models.py b/kolibri/core/exams/models.py index 990f1c18ddd..5bec27c61f1 100644 --- a/kolibri/core/exams/models.py +++ b/kolibri/core/exams/models.py @@ -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 diff --git a/kolibri/core/exams/serializers.py b/kolibri/core/exams/serializers.py index b7d00d10c26..7ac49e0f95b 100644 --- a/kolibri/core/exams/serializers.py +++ b/kolibri/core/exams/serializers.py @@ -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 diff --git a/kolibri/core/exams/test/test_exam_api.py b/kolibri/core/exams/test/test_exam_api.py index 97ab450c2f3..e6db25e9fd1 100644 --- a/kolibri/core/exams/test/test_exam_api.py +++ b/kolibri/core/exams/test/test_exam_api.py @@ -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) diff --git a/kolibri/plugins/coach/class_summary_api.py b/kolibri/plugins/coach/class_summary_api.py index 1c2c6c3e140..7e281b13bc7 100644 --- a/kolibri/plugins/coach/class_summary_api.py +++ b/kolibri/plugins/coach/class_summary_api.py @@ -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", [])