Skip to content

Commit

Permalink
chore: check score is below max_score project
Browse files Browse the repository at this point in the history
  • Loading branch information
BramMeir committed Mar 9, 2024
1 parent d6dd675 commit b58501f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
13 changes: 11 additions & 2 deletions backend/api/serializers/group_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ class Meta:
model = Group
fields = ["id", "project", "students", "score"]

def validate(self, data):
# Make sure the score of the group is lower or equal to the maximum score
group: Group = self.instance

if "score" in data and data["score"] > group.project.max_score:
raise ValidationError(gettext("group.errors.score_exceeds_max"))

return data


class StudentJoinGroupSerializer(StudentIDSerializer):

Expand All @@ -34,15 +43,15 @@ def validate(self, data):

# Make sure the group is not already full
if group.is_full():
raise serializers.ValidationError(gettext("group.errors.full"))
raise ValidationError(gettext("group.errors.full"))

# Make sure the student is part of the course
if not group.project.course.students.filter(id=student.id).exists():
raise ValidationError(gettext("group.errors.not_in_course"))

# Make sure the student is not already in a group
if student.is_enrolled_in_group(group.project):
raise serializers.ValidationError(gettext("group.errors.already_in_group"))
raise ValidationError(gettext("group.errors.already_in_group"))

return data

Expand Down
19 changes: 16 additions & 3 deletions backend/api/tests/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ def create_course(name, academic_startyear, description=None, parent_course=None
)


def create_project(name, description, days, course, group_size=2):
def create_project(name, description, days, course, group_size=2, max_score=20):
"""Create a Project with the given arguments."""
deadline = timezone.now() + timedelta(days=days)
return Project.objects.create(
name=name, description=description, deadline=deadline, course=course, group_size=group_size
name=name, description=description, deadline=deadline, course=course, group_size=group_size, max_score=max_score
)


Expand Down Expand Up @@ -347,7 +347,7 @@ def test_update_score_of_group(self):
course = create_course(name="sel2", academic_startyear=2023)

project = create_project(
name="Project 1", description="Description 1", days=7, course=course
name="Project 1", description="Description 1", days=7, course=course, max_score=20
)

# Add this teacher to the course
Expand All @@ -368,6 +368,19 @@ def test_update_score_of_group(self):
group.refresh_from_db()
self.assertEqual(group.score, 20)

# Try to update the score of a group to a score higher than the maximum score
response = self.client.patch(
reverse("group-detail", args=[str(group.id)]),
{"score": 30},
follow=True,
)

self.assertEqual(response.status_code, 400)

# Make sure the score of the group is not updated
group.refresh_from_db()
self.assertEqual(group.score, 20)


class GroupModelTestsAsStudent(APITestCase):
def setUp(self) -> None:
Expand Down

0 comments on commit b58501f

Please sign in to comment.