Skip to content

Commit

Permalink
testen model
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnoutAllaert committed Mar 21, 2024
1 parent d6ac1b7 commit 5482180
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 14 deletions.
18 changes: 12 additions & 6 deletions api/serializers/gebruiker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from api.models.gebruiker import Gebruiker
from api.models.vak import Vak


class GebruikerSerializer(serializers.ModelSerializer):
"""
Serializer voor het serialiseren en deserialiseren van Gebruiker objecten.
Expand All @@ -16,13 +17,13 @@ class GebruikerSerializer(serializers.ModelSerializer):
update(self, instance, validated_data): Werkt een bestaande gebruiker bij in de database.
"""

first_name = serializers.CharField(source='user.first_name', read_only=True)
last_name = serializers.CharField(source='user.last_name', read_only=True)
email = serializers.EmailField(source='user.email', read_only=True)
first_name = serializers.CharField(source="user.first_name", read_only=True)
last_name = serializers.CharField(source="user.last_name", read_only=True)
email = serializers.EmailField(source="user.email", read_only=True)

class Meta:
model = Gebruiker
fields = ['user', 'is_lesgever', 'first_name', 'last_name', 'email']
fields = ["user", "is_lesgever", "first_name", "last_name", "email"]

def create(self, validated_data):
"""
Expand Down Expand Up @@ -52,8 +53,13 @@ def update(self, instance, validated_data):
instance.save()
return instance


def validate_lesgever_change(instance):
if instance.is_lesgever and Vak.objects.filter(lesgevers=instance):
raise serializers.ValidationError(f"De lesgever {instance} moet eerst verwijderd worden als lesgever in zijn huidige vakken")
raise serializers.ValidationError(
f"De lesgever {instance} moet eerst verwijderd worden als lesgever in zijn huidige vakken"
)
elif not instance.is_lesgever and Vak.objects.filter(studenten=instance):
raise serializers.ValidationError(f"De student {instance} moet eerst verwijderd worden als student in zijn huidige vakken")
raise serializers.ValidationError(
f"De student {instance} moet eerst verwijderd worden als student in zijn huidige vakken"
)
5 changes: 5 additions & 0 deletions api/tests/factories/indiening.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class Meta:
fake.date_time_between(start_date="+1d", end_date="+30d")
)
)
status = factory.Faker("boolean")

indiening_bestanden = factory.RelatedFactory(
"api.tests.factories.indiening.IndieningBestandFactory", "indiening"
)


class IndieningBestandFactory(DjangoModelFactory):
Expand Down
7 changes: 7 additions & 0 deletions api/tests/factories/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@ class Meta:
fake.date_time_between(start_date="+1d", end_date="+30d")
)
)
extra_deadline = factory.LazyFunction(
lambda: timezone.make_aware(
fake.date_time_between(start_date="+30d", end_date="+40d")
)
)
max_score = factory.Faker("random_int", min=10, max=100)
zichtbaar = factory.Faker('boolean')
gearchiveerd = factory.Faker('boolean')
14 changes: 6 additions & 8 deletions api/tests/models/test_indiening.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ def test_groep(self):

def test_tijdstip(self):
self.assertIsNotNone(self.indiening.tijdstip)

def test_indiening_bestand(self):
self.assertEqual(self.indiening.indieningbestand_set.count(), 0)

def test_indiening_bestand_add(self):
IndieningBestandFactory.create(indiening=self.indiening)
self.assertEqual(self.indiening.indieningbestand_set.count(), 1)


def test_status(self):
self.assertIsNotNone(self.indiening.status)

def test_indiening_bestanden(self):
self.assertEqual(self.indiening.indiening_bestanden.count(), 1)

class IndieningBestandModelTest(TestCase):
def setUp(self):
Expand Down
15 changes: 15 additions & 0 deletions api/tests/models/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,18 @@ def test_project_vak(self):

def test_project_max_score(self):
self.assertTrue(10 <= self.project.max_score <= 100)

def test_project_zichtbaar(self):
self.assertIsNotNone(self.project.zichtbaar)

def test_project_gearchiveerd(self):
self.assertIsNotNone(self.project.gearchiveerd)

def test_project_deadline(self):
self.assertIsNotNone(self.project.deadline)

def test_project_extra_deadline(self):
self.assertIsNotNone(self.project.extra_deadline)

def test_project_opgave_bestand(self):
self.assertEqual(self.project.opgave_bestand.read(), b"file content")

0 comments on commit 5482180

Please sign in to comment.