def create(self, validated_data): request = self.context.get("request") name = validated_data.get("name") subject = request.data['subject'] subject_instance = Subject.objects.get(pk=subject['id']) exercises = request.data['exercises'] print('data================', exercises) # throughModel = exams.subject exam = Exam(name=name, subject=subject_instance) exam.save() for exercise in exercises: ex_name = exercise['name'] ex_description = exercise['description'] exercise_instance = Exercise(name=ex_name, description=ex_description, exam=exam, subject=subject_instance) exercise_instance.save() return exam
class UserTest(TestCase): def setUp(self): self.user = User() self.user.email = "*****@*****.**" self.user.password = "******" self.user.first_name = "TestUser" self.user.username = "******" self.user.score = 100 self.first_user = User() self.first_user.email = "*****@*****.**" self.first_user.password = "******" self.first_user.first_name = "TestUser" self.first_user.username = "******" self.first_user.score = 200 def test_user_get_short_name(self): User.objects.create_user(email=self.user.email, password=self.user.password, first_name=self.user.first_name, username=self.user.username) user = User.objects.get(email=self.user.email) self.assertEqual(self.user.email, user.get_short_name()) def test_user_get_full_name(self): User.objects.create_user(email=self.user.email, password=self.user.password, first_name=self.user.first_name, username=self.user.username) user = User.objects.get(email=self.user.email) self.assertEqual(self.user.email, user.get_full_name()) def test_get_current_ranking_position(self): self.user.save() position = self.user.get_position() self.assertEqual(1, position) def test_if_current_ranking_position_is_second(self): self.first_user.save() self.user.save() self.assertEqual(2, self.user.get_position()) def test_if_current_ranking_position_is_first(self): second_user = self.first_user second_user.score = 50 second_user.save() self.user.save() self.assertEqual(1, self.user.get_position()) def test_if_current_user_dont_do_correct_exercise(self): self.user.save() self.assertEqual(0, self.user.get_correct_exercises()) def test_if_current_user_has_one_exercise(self): self.exercise = Exercise() self.exercise.title = 'Basic Exercise' self.exercise.category = 2 self.exercise.statement_question = '<p>Text Basic Exercise.</p>' self.exercise.score = 10 self.exercise.deprecated = 0 self.exercise.save() self.user_exercise = UserExercise() self.user_exercise.scored = False self.user_exercise.code = """ #include <stdio.h> int main () { char c; scanf("%c", &c); printf("B"); return 0; } """ self.user.id = 1 self.user_exercise.user = self.user self.user_exercise.exercise = self.exercise self.user_exercise.status = True self.user.save() self.user_exercise.save() self.assertEqual(100, self.user.get_correct_exercises())