Skip to content

Commit

Permalink
update testen + Merge branch 'develop' into backend_testen
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnoutAllaert committed May 19, 2024
2 parents a61dfcf + 9766e1b commit 8001c05
Show file tree
Hide file tree
Showing 49 changed files with 10,859 additions and 122 deletions.
Binary file modified .coverage
Binary file not shown.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ uploads
/data/
cypress/screenshots
/package-lock.json
/frontend/package-lock.json
/frontend/frontend/package-lock.json
package-lock.json
2 changes: 2 additions & 0 deletions api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from api.models.indiening import Indiening
from api.models.score import Score
from api.models.restrictie import Restrictie
from api.models.template import Template

admin.site.register(Gebruiker)
admin.site.register(Vak)
Expand All @@ -14,3 +15,4 @@
admin.site.register(Indiening)
admin.site.register(Score)
admin.site.register(Restrictie)
admin.site.register(Template)
1 change: 0 additions & 1 deletion api/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class Project(models.Model):
deadline = models.DateTimeField(null=True, blank=True)
extra_deadline = models.DateTimeField(null=True, blank=True)
max_score = models.PositiveSmallIntegerField(default=20)
aantal_groepen = models.PositiveSmallIntegerField(default=10)
max_groep_grootte = models.PositiveSmallIntegerField(default=1)
student_groep = models.BooleanField(default=False, blank=True)
zichtbaar = models.BooleanField(default=True, blank=True)
Expand Down
7 changes: 3 additions & 4 deletions api/serializers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class Meta:
"deadline",
"extra_deadline",
"max_score",
"aantal_groepen",
"max_groep_grootte",
"student_groep",
"zichtbaar",
Expand Down Expand Up @@ -76,8 +75,6 @@ def update(self, instance, validated_data):
Project: Het bijgewerkte project.
"""
validated_data.pop("max_groep_grootte", instance.max_groep_grootte)
validated_data.pop("student_groep", instance.student_groep)
validated_data.pop("aantal_groepen", instance.aantal_groepen)

deadline = validated_data.pop("deadline", instance.deadline)
extra_deadline = validated_data.pop("extra_deadline", instance.extra_deadline)
Expand Down Expand Up @@ -105,7 +102,9 @@ def create_groepen(instance):
except Exception:
pass
else:
for _ in range(instance.aantal_groepen):
for _ in range(
len(instance.vak.studenten.all()) // instance.max_groep_grootte + 1
):
try:
serializer = GroepSerializer(
data={"studenten": [], "project": instance.project_id}
Expand Down
18 changes: 18 additions & 0 deletions api/serializers/vak.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from rest_framework import serializers
from api.models.vak import Vak
from api.models.project import Project
from api.models.groep import Groep
from api.serializers.groep import GroepSerializer


Expand Down Expand Up @@ -86,6 +87,23 @@ def add_students_to_group(instance):
except Exception:
pass

else:
groepen = Groep.objects.filter(project=project.project_id)
nieuwe_groepen = (
len(instance.studenten.all()) // project.max_groep_grootte
+ 1
- len(groepen)
)
for _ in range(nieuwe_groepen):
try:
serializer = GroepSerializer(
data={"studenten": [], "project": project.project_id}
)
if serializer.is_valid():
serializer.save()
except Exception:
pass


def validate_students_teachers(students_data, teachers_data):
"""
Expand Down
1 change: 0 additions & 1 deletion api/tests/factories/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class Meta:
)
)
max_score = factory.Faker("random_int", min=10, max=100)
aantal_groepen = factory.Faker("random_int", min=2, max=100)
max_groep_grootte = factory.Faker("random_int", min=1, max=5)
student_groep = factory.Faker("boolean")
zichtbaar = factory.Faker("boolean")
Expand Down
8 changes: 0 additions & 8 deletions api/tests/serializers/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def test_contains_expected_fields(self):
"deadline",
"extra_deadline",
"max_score",
"aantal_groepen",
"max_groep_grootte",
"student_groep",
"zichtbaar",
Expand Down Expand Up @@ -88,7 +87,6 @@ def test_validation_for_blank_items(self):
"deadline": "",
"extra_deadline": "",
"max_score": "",
"aantal_groepen": "",
"max_groep_grootte": "",
"student_groep": "",
"zichtbaar": "",
Expand All @@ -107,7 +105,6 @@ def test_create(self):
"deadline": timezone.now() + timedelta(days=1),
"extra_deadline": timezone.now() + timedelta(days=2),
"max_score": 20,
"aantal_groepen": 50,
"max_groep_grootte": 1,
"student_groep": False,
"zichtbaar": True,
Expand All @@ -128,7 +125,6 @@ def test_create_higher_grootte(self):
"deadline": timezone.now() + timedelta(days=1),
"extra_deadline": timezone.now() + timedelta(days=2),
"max_score": 20,
"aantal_groepen": 50,
"max_groep_grootte": 5,
"student_groep": False,
"zichtbaar": True,
Expand All @@ -149,7 +145,6 @@ def test_create_no_deadline(self):
"deadline": None,
"extra_deadline": None,
"max_score": 20,
"aantal_groepen": 50,
"max_groep_grootte": 1,
"student_groep": False,
"zichtbaar": True,
Expand All @@ -170,7 +165,6 @@ def test_create_invalid_deadline(self):
"deadline": datetime.now() - timedelta(days=1),
"extra_deadline": self.serializer.data["extra_deadline"],
"max_score": 20,
"aantal_groepen": 50,
"max_groep_grootte": 1,
"student_groep": False,
"zichtbaar": True,
Expand All @@ -190,7 +184,6 @@ def test_create_invalid_extra_deadline(self):
"deadline": self.serializer.data["deadline"],
"extra_deadline": datetime.now() - timedelta(days=1),
"max_score": 20,
"aantal_groepen": 50,
"max_groep_grootte": 1,
"student_groep": False,
"zichtbaar": True,
Expand All @@ -209,7 +202,6 @@ def test_update(self):
"deadline": self.serializer.data["deadline"],
"extra_deadline": self.serializer.data["extra_deadline"],
"max_score": 20,
"aantal_groepen": 50,
"max_groep_grootte": 1,
"student_groep": False,
"zichtbaar": True,
Expand Down
23 changes: 23 additions & 0 deletions api/tests/serializers/test_vak.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,26 @@ def test_add_invalid_students_to_group(self):
serializer = VakSerializer(instance=self.vak_data, data=data, partial=True)
self.assertTrue(serializer.is_valid())
vak = serializer.save()

def test_add_students_to_group_with_max_groep_grootte(self):
students_data = [
GebruikerFactory.create(is_lesgever=False).user.id for _ in range(3)
]
teachers_data = [
GebruikerFactory.create(is_lesgever=True).user.id for _ in range(3)
]
data = {
"naam": "test vak",
"studenten": students_data,
"lesgevers": teachers_data,
}
serializer = VakSerializer(instance=self.vak_data, data=data, partial=True)
self.assertTrue(serializer.is_valid())
vak = serializer.save()
project = ProjectFactory.create(
student_groep=False, max_groep_grootte=2, vak=vak
)
serializer = VakSerializer(instance=self.vak_data, data=data, partial=True)
self.assertTrue(serializer.is_valid())
vak = serializer.save()
self.assertEqual(project.vak, vak)
1 change: 0 additions & 1 deletion api/tests/views/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ def test_project_detail_put(self):
"deadline": self.project.deadline,
"extra_deadline": self.project.extra_deadline,
"max_score": self.project.max_score,
"aantal_groepen": self.project.aantal_groepen,
"max_groep_grootte": self.project.max_groep_grootte,
"zichtbaar": self.project.zichtbaar,
"gearchiveerd": self.project.gearchiveerd,
Expand Down
77 changes: 66 additions & 11 deletions api/tests/views/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from api.tests.factories.gebruiker import GebruikerFactory
from django.core.files.uploadedfile import SimpleUploadedFile
from rest_framework import status
from django.http import FileResponse


class TemplateListViewTest(APITestCase):
Expand Down Expand Up @@ -44,7 +45,7 @@ def test_template_list_post(self):
file = SimpleUploadedFile("template.txt", b"file_content")
data["bestand"] = file
response = self.client.post(self.template_list_url, data)
self.assertEqual(response.status_code, 201)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Template.objects.count(), 2)

def test_template_list_post_as_student(self):
Expand All @@ -55,15 +56,15 @@ def test_template_list_post_as_student(self):
file = SimpleUploadedFile("template.txt", b"file_content")
data["bestand"] = file
response = self.client.post(self.template_list_url, data)
self.assertEqual(response.status_code, 403)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assertEqual(Template.objects.count(), 1)

def test_template_list_post_with_invalid_data(self):
data = {
"user": self.template.user.id,
}
response = self.client.post(self.template_list_url, data)
self.assertEqual(response.status_code, 400)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(Template.objects.count(), 1)


Expand All @@ -80,17 +81,17 @@ def setUp(self):

def test_template_detail_get(self):
response = self.client.get(self.template_detail_url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["template_id"], self.template.template_id)

def test_template_detail_get_as_student(self):
self.client.force_login(self.student.user)
response = self.client.get(self.template_detail_url)
self.assertEqual(response.status_code, 403)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

def test_template_detail_get_invalid(self):
response = self.client.get(reverse("template_detail", kwargs={"id": 6969}))
self.assertEqual(response.status_code, 404)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

def test_template_detail_put(self):
data = {
Expand All @@ -99,13 +100,13 @@ def test_template_detail_put(self):
file = SimpleUploadedFile("template.txt", b"file_content")
data["bestand"] = file
response = self.client.put(self.template_detail_url, data)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["template_id"], self.template.template_id)

def test_template_detail_patch(self):
data = {"bestand": SimpleUploadedFile("template.txt", b"different_content")}
response = self.client.patch(self.template_detail_url, data)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["template_id"], self.template.template_id)

def test_template_detail_put_as_student(self):
Expand All @@ -116,16 +117,70 @@ def test_template_detail_put_as_student(self):
file = SimpleUploadedFile("template.txt", b"file_content")
data["bestand"] = file
response = self.client.put(self.template_detail_url, data)
self.assertEqual(response.status_code, 403)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

def test_template_detail_put_with_invalid_data(self):
data = {
"user": self.template.user.id,
}
response = self.client.put(self.template_detail_url, data)
self.assertEqual(response.status_code, 400)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_template_detail_delete(self):
response = self.client.delete(self.template_detail_url)
self.assertEqual(response.status_code, 204)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.assertEqual(Template.objects.count(), 0)


class TemplateDetailBestandViewTest(APITestCase):
def setUp(self):
self.client = APIClient()
self.template = TemplateFactory.create()
self.teacher = GebruikerFactory.create(is_lesgever=True)
self.student = GebruikerFactory.create(is_lesgever=False)
self.client.force_login(self.teacher.user)
self.template_detail_bestand_url = reverse(
"template_detail_bestand", kwargs={"id": self.template.template_id}
)

def test_template_detail_bestand_get(self):
response = self.client.get(self.template_detail_bestand_url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertTrue(isinstance(response, FileResponse))
response_content = b"".join(response.streaming_content)
self.template.bestand.open()
template_content = self.template.bestand.read()
self.template.bestand.close()
self.assertEqual(response_content, template_content)

def test_template_detail_bestand_get_as_student(self):
self.client.force_login(self.student.user)
response = self.client.get(self.template_detail_bestand_url)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

def test_template_detail_bestand_get_invalid(self):
response = self.client.get(
reverse("template_detail_bestand", kwargs={"id": 6969})
)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

def test_template_detail_bestand_get_content_true(self):
response = self.client.get(
self.template_detail_bestand_url, {"content": "true"}
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.template.bestand.open()
self.assertEqual(response.data["content"], self.template.bestand.read())
self.template.bestand.close()

def test_template_detail_bestand_get_content_false(self):
response = self.client.get(
self.template_detail_bestand_url, {"content": "false"}
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertTrue(isinstance(response, FileResponse))
response_content = b"".join(response.streaming_content)
self.template.bestand.open()
template_content = self.template.bestand.read()
self.template.bestand.close()
self.assertEqual(response_content, template_content)
7 changes: 6 additions & 1 deletion api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
)
from .views.score import score_list, score_detail
from .views.groep import groep_list, groep_detail
from .views.template import template_list, template_detail
from .views.template import template_list, template_detail, template_detail_bestand
from .views.restrictie import (
restrictie_list,
restrictie_detail,
Expand Down Expand Up @@ -84,6 +84,11 @@
),
path("api/templates/", template_list, name="template_list"),
path("api/templates/<int:id>/", template_detail, name="template_detail"),
path(
"api/templates/<int:id>/template/",
template_detail_bestand,
name="template_detail_bestand",
),
]

urlpatterns = format_suffix_patterns(urlpatterns)
1 change: 1 addition & 0 deletions api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"scores": "api/scores",
"projecten": "api/projecten",
"restricties": "api/restricties",
"templates": "api/templates",
}


Expand Down
Loading

0 comments on commit 8001c05

Please sign in to comment.