Skip to content

Commit

Permalink
fix: cerate school
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Jan 9, 2025
1 parent bb76e3c commit b94296b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/api/serializers/school.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ class SchoolSerializer(_SchoolSerializer):

uk_county = serializers.ChoiceField( # type: ignore[assignment]
source="county",
required=False,
choices=[
"Aberdeen City",
"Aberdeenshire",
Expand Down Expand Up @@ -418,3 +419,13 @@ def validate(self, attrs):
)

return attrs

def create(self, validated_data):
school = School.objects.create(**validated_data)

user = self.request.non_school_teacher_user
user.teacher.school = school
user.teacher.is_admin = True
user.teacher.save(update_fields=["school", "is_admin"])

return school
33 changes: 31 additions & 2 deletions src/api/serializers/school_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
Created on 02/02/2024 at 15:38:51(+00:00).
"""

import typing as t

from codeforlife.tests import ModelSerializerTestCase
from codeforlife.user.models import School, User
from codeforlife.user.models import (
AdminSchoolTeacherUser,
NonSchoolTeacherUser,
School,
User,
)

from ..views.school import SchoolViewSet
from .school import SchoolSerializer
Expand All @@ -15,11 +22,15 @@

class TestSchoolSerializer(ModelSerializerTestCase[User, School]):
model_serializer_class = SchoolSerializer
fixtures = ["school_1"]
fixtures = ["school_1", "non_school_teacher"]

def setUp(self):
self.school_1 = School.objects.get(pk=2)

non_school_teacher_user = NonSchoolTeacherUser.objects.first()
assert non_school_teacher_user
self.non_school_teacher_user = non_school_teacher_user

def test_validate__country_ne_gb(self):
"""
Setting a UK county raises an error if the country does not equal GB.
Expand All @@ -44,3 +55,21 @@ def test_validate_name__name_not_unique(self):
value=self.school_1.name,
error_code="name_not_unique",
)

def test_create(self):
"""Can successfully create a school."""
user = self.non_school_teacher_user

self.assert_create(
validated_data={
"name": "Test School",
"country": "CY",
},
context={"request": self.request_factory.post(user=user)},
new_data={"county": None},
)

user = t.cast(AdminSchoolTeacherUser, user) # type: ignore[assignment]
user.teacher.refresh_from_db()
assert user.teacher.school
assert user.teacher.is_admin

0 comments on commit b94296b

Please sign in to comment.