Skip to content

Commit

Permalink
Merge pull request #87 from SELab-2/backend_changes
Browse files Browse the repository at this point in the history
update testen
  • Loading branch information
LGDTimtou authored Mar 23, 2024
2 parents b48b8f8 + 6b2650e commit d0496a8
Show file tree
Hide file tree
Showing 15 changed files with 145 additions and 117 deletions.
2 changes: 1 addition & 1 deletion api/models/indiening.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Indiening(models.Model):
indiening_id = models.AutoField(primary_key=True)
groep = models.ForeignKey("Groep", on_delete=models.CASCADE)
tijdstip = models.DateTimeField(auto_now_add=True)
status = models.BooleanField()
status = models.BooleanField(default=False)

def __str__(self):
return str(self.indiening_id)
Expand Down
2 changes: 1 addition & 1 deletion api/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Project(models.Model):
opgave_bestand = models.FileField(upload_to=upload_to)
vak = models.ForeignKey(Vak, on_delete=models.CASCADE)
deadline = models.DateTimeField(null=True, blank=True)
extra_deadline = models.DateTimeField(null=True, blank=True)
extra_deadline = models.DateTimeField(null=True, blank=True, default=None)
max_score = models.IntegerField(default=20)
zichtbaar = models.BooleanField(default=True, blank=True)
gearchiveerd = models.BooleanField(default=False, blank=True)
Expand Down
8 changes: 8 additions & 0 deletions api/serializers/groep.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from rest_framework import serializers
from api.models.groep import Groep
from collections import Counter


class GroepSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -74,6 +75,13 @@ def validate_students(students_data, project, current_group=None):
if current_group is not None:
groepen = groepen.exclude(groep_id=current_group.groep_id)

student_counts = Counter(students_data)
for student, count in student_counts.items():
if count > 1:
raise serializers.ValidationError(
f"Student {student} komt meerdere keren voor in de groep!"
)

for student in students_data:
if student.is_lesgever:
raise serializers.ValidationError(
Expand Down
2 changes: 1 addition & 1 deletion api/serializers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def validate_deadlines(deadline, extra_deadline):
if deadline <= timezone.now():
raise serializers.ValidationError("Deadline moet in de toekomst liggen")

if extra_deadline <= deadline:
if extra_deadline is not None and extra_deadline <= deadline:
raise serializers.ValidationError(
"Extra deadline moet na de eerste deadline liggen"
)
4 changes: 3 additions & 1 deletion api/tests/factories/groep.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ def studenten(self, create, extracted, **kwargs):
for student in extracted:
self.studenten.add(student)
else:
self.studenten.add(GebruikerFactory(is_lesgever=False))
student = GebruikerFactory(is_lesgever=False)
self.project.vak.studenten.add(student)
self.studenten.add(student)
4 changes: 1 addition & 3 deletions api/tests/factories/indiening.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ class IndieningFactory(DjangoModelFactory):
class Meta:
model = Indiening

indiening_id = factory.Sequence(lambda n: n)
groep = SubFactory(GroepFactory)
tijdstip = factory.LazyFunction(
lambda: timezone.make_aware(
fake.date_time_between(start_date="+1d", end_date="+30d")
)
)
status = factory.Faker("boolean")

indiening_bestanden = factory.RelatedFactory(
"api.tests.factories.indiening.IndieningBestandFactory", "indiening"
)
Expand All @@ -33,6 +32,5 @@ class IndieningBestandFactory(DjangoModelFactory):
class Meta:
model = IndieningBestand

indiening_bestand_id = factory.Sequence(lambda n: n)
indiening = SubFactory(IndieningFactory)
bestand = FileField(filename="test.txt", data=b"file content")
5 changes: 2 additions & 3 deletions api/tests/factories/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class ProjectFactory(DjangoModelFactory):
class Meta:
model = Project

project_id = factory.Sequence(lambda n: n)
titel = factory.Faker("word")
beschrijving = factory.Faker("paragraph")
opgave_bestand = factory.django.FileField(data=b"file content")
Expand All @@ -29,5 +28,5 @@ class Meta:
)
)
max_score = factory.Faker("random_int", min=10, max=100)
zichtbaar = factory.Faker('boolean')
gearchiveerd = factory.Faker('boolean')
zichtbaar = factory.Faker("boolean")
gearchiveerd = factory.Faker("boolean")
5 changes: 3 additions & 2 deletions api/tests/models/test_indiening.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ def test_groep(self):

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

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):
self.indiening_bestand = IndieningBestandFactory.create(
Expand Down
12 changes: 6 additions & 6 deletions api/tests/models/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +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")
self.assertEqual(self.project.opgave_bestand.read(), b"file content")
16 changes: 15 additions & 1 deletion api/tests/serializers/test_gebruiker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ def setUp(self):

def test_contains_expected_fields(self):
data = self.serializer.data
self.assertCountEqual(data.keys(), ["user", "is_lesgever"])
self.assertCountEqual(
data.keys(), ["user", "is_lesgever", "first_name", "last_name", "email"]
)

def test_user_field_content(self):
data = self.serializer.data
Expand All @@ -22,6 +24,18 @@ def test_is_lesgever_field_content(self):
data = self.serializer.data
self.assertEqual(data["is_lesgever"], self.gebruiker.is_lesgever)

def test_first_name_field_content(self):
data = self.serializer.data
self.assertEqual(data["first_name"], self.user.first_name)

def test_last_name_field_content(self):
data = self.serializer.data
self.assertEqual(data["last_name"], self.user.last_name)

def test_email_field_content(self):
data = self.serializer.data
self.assertEqual(data["email"], self.user.email)

def test_create(self):
data = {"user": UserFactory.create().id, "is_lesgever": False}
serializer = GebruikerSerializer(data=data)
Expand Down
40 changes: 35 additions & 5 deletions api/tests/serializers/test_groep.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ def test_studenten_field_content(self):
self.assertEqual(data["studenten"], students)

def test_create(self):
studenten = [
GebruikerFactory.create(is_lesgever=False).user.id for _ in range(3)
]
for student in studenten:
self.groep.project.vak.studenten.add(student)
data = {
"project": self.groep.project.project_id,
"studenten": [
GebruikerFactory.create(is_lesgever=False).user.id for _ in range(3)
],
"studenten": studenten,
}
serializer = GroepSerializer(data=data)
self.assertTrue(serializer.is_valid())
Expand All @@ -41,6 +44,7 @@ def test_create(self):

def test_create_invalid_user_already_in_a_group(self):
student = GebruikerFactory.create(is_lesgever=False).user.id
self.groep.project.vak.studenten.add(student)
data = {
"project": self.groep.project.project_id,
"studenten": [student],
Expand All @@ -61,6 +65,16 @@ def test_create_invalid_user_already_in_a_group(self):
self.assertTrue(newserializer.is_valid())
self.assertRaises(ValidationError, newserializer.save, raise_exception=True)

def test_create_invalid_user_not_in_vak(self):
student = GebruikerFactory.create(is_lesgever=False).user.id
data = {
"project": self.groep.project.project_id,
"studenten": [student],
}
serializer = GroepSerializer(data=data)
self.assertTrue(serializer.is_valid())
self.assertRaises(ValidationError, serializer.save, raise_exception=True)

def test_create_invalid_user_is_teacher(self):
data = {
"project": self.groep.project.project_id,
Expand All @@ -74,8 +88,9 @@ def test_create_invalid_user_is_teacher(self):

def test_update(self):
data = self.serializer.data
self.assertEqual(len(data["studenten"]), 1)
data["studenten"].append(GebruikerFactory.create(is_lesgever=False).user.id)
student = GebruikerFactory.create(is_lesgever=False).user.id
self.groep.project.vak.studenten.add(student)
data["studenten"].append(student)
serializer = GroepSerializer(instance=self.groep, data=data, partial=True)
self.assertTrue(serializer.is_valid())
groep = serializer.save()
Expand All @@ -87,6 +102,7 @@ def test_update_invalid_user_already_in_this_group(self):
data = self.serializer.data
self.assertEqual(len(data["studenten"]), 1)
student = GebruikerFactory.create(is_lesgever=False).user.id
self.groep.project.vak.studenten.add(student)
data["studenten"].append(student)
serializer = GroepSerializer(instance=self.groep, data=data, partial=True)
self.assertTrue(serializer.is_valid())
Expand All @@ -100,6 +116,20 @@ def test_update_invalid_user_already_in_this_group(self):
self.assertTrue(serializer.is_valid())
self.assertRaises(ValidationError, serializer.save, raise_exception=True)

def test_update_invalid_user_not_in_vak(self):
data = self.serializer.data
serializer = GroepSerializer(instance=self.groep, data=data, partial=True)
self.assertTrue(serializer.is_valid())
groep = serializer.save()
self.assertEqual(
[student.user.id for student in groep.studenten.all()], data["studenten"]
)
new_data = self.serializer.data
new_data["studenten"].append(GebruikerFactory.create(is_lesgever=False).user.id)
serializer = GroepSerializer(instance=groep, data=new_data, partial=True)
self.assertTrue(serializer.is_valid())
self.assertRaises(ValidationError, serializer.save, raise_exception=True)

def test_validation_for_blank_items(self):
serializer = GroepSerializer(data={"project": "", "studenten": []})
self.assertRaises(ValidationError, serializer.is_valid, raise_exception=True)
9 changes: 6 additions & 3 deletions api/tests/serializers/test_indiening.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ def setUp(self):

def test_indiening_serializer_fields(self):
data = self.serializer.data
self.assertEqual(set(data.keys()), set(["indiening_id", "groep", "tijdstip"]))
self.assertEqual(
set(data.keys()),
set(["indiening_id", "groep", "tijdstip", "status", "indiening_bestanden"]),
)

def test_indiening_serializer_create(self):
# can't check tijdstip because it's auto_now_add
groep = GroepFactory.create()
data = {"groep": groep.groep_id}
serializer = IndieningSerializer(data=data)
if not serializer.is_valid():
print(serializer.errors)
self.assertTrue(serializer.is_valid())
indiening = serializer.save()
self.assertEqual(indiening.groep, groep)

def test_indiening_serializer_update(self):
# can't check tijdstip because it's auto_now_add
new_data = {"groep": self.indiening.groep.groep_id}
serializer = IndieningSerializer(
instance=self.indiening, data=new_data, partial=True
Expand Down
44 changes: 44 additions & 0 deletions api/tests/serializers/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ def test_contains_expected_fields(self):
"opgave_bestand",
"vak",
"deadline",
"extra_deadline",
"max_score",
"zichtbaar",
"gearchiveerd",
],
)

Expand Down Expand Up @@ -55,6 +58,18 @@ def test_deadline_field_content(self):
data = self.serializer.data
self.assertEqual(parse(data["deadline"]), self.project.deadline)

def test_extra_deadline_field_content(self):
data = self.serializer.data
self.assertEqual(parse(data["extra_deadline"]), self.project.extra_deadline)

def test_zichtbaar_field_content(self):
data = self.serializer.data
self.assertEqual(data["zichtbaar"], self.project.zichtbaar)

def test_gearchiveerd_field_content(self):
data = self.serializer.data
self.assertEqual(data["gearchiveerd"], self.project.gearchiveerd)

def test_validation_for_blank_items(self):
serializer = ProjectSerializer(
data={
Expand All @@ -63,7 +78,10 @@ def test_validation_for_blank_items(self):
"opgave_bestand": "",
"vak": "",
"deadline": "",
"extra_deadline": "",
"max_score": "",
"zichtbaar": "",
"gearchiveerd": "",
}
)
self.assertRaises(ValidationError, serializer.is_valid, raise_exception=True)
Expand All @@ -76,7 +94,10 @@ def test_create(self):
"opgave_bestand": SimpleUploadedFile("file.txt", b"file_content"),
"vak": vak,
"deadline": self.serializer.data["deadline"],
"extra_deadline": self.serializer.data["extra_deadline"],
"max_score": 20,
"zichtbaar": True,
"gearchiveerd": False,
}
serializer = ProjectSerializer(data=data)
self.assertTrue(serializer.is_valid())
Expand All @@ -91,7 +112,27 @@ def test_create_invalid_deadline(self):
"opgave_bestand": SimpleUploadedFile("file.txt", b"file_content"),
"vak": vak,
"deadline": datetime.now() - timedelta(days=1),
"extra_deadline": self.serializer.data["extra_deadline"],
"max_score": 20,
"zichtbaar": True,
"gearchiveerd": False,
}
serializer = ProjectSerializer(data=data)
self.assertTrue(serializer.is_valid())
self.assertRaises(ValidationError, serializer.save, raise_exception=True)

def test_create_invalid_extra_deadline(self):
vak = VakFactory.create().vak_id
data = {
"titel": "test project",
"beschrijving": "Dit is een test project.",
"opgave_bestand": SimpleUploadedFile("file.txt", b"file_content"),
"vak": vak,
"deadline": self.serializer.data["deadline"],
"extra_deadline": datetime.now() - timedelta(days=1),
"max_score": 20,
"zichtbaar": True,
"gearchiveerd": False,
}
serializer = ProjectSerializer(data=data)
self.assertTrue(serializer.is_valid())
Expand All @@ -104,7 +145,10 @@ def test_update(self):
"opgave_bestand": SimpleUploadedFile("file.txt", b"file_content"),
"vak": self.serializer.data["vak"],
"deadline": self.serializer.data["deadline"],
"extra_deadline": self.serializer.data["extra_deadline"],
"max_score": 20,
"zichtbaar": True,
"gearchiveerd": False,
}
serializer = ProjectSerializer(instance=self.project, data=data, partial=True)
self.assertTrue(serializer.is_valid())
Expand Down
Loading

0 comments on commit d0496a8

Please sign in to comment.