From 35e43472997a1b71ce21d782250ea8f326311c11 Mon Sep 17 00:00:00 2001 From: Tybo Verslype Date: Sat, 2 Mar 2024 20:45:45 +0100 Subject: [PATCH 1/9] chore: add test for admins faculty --- backend/api/tests/test_admin.py | 84 +++++++++++++++++++++++++++++---- 1 file changed, 75 insertions(+), 9 deletions(-) diff --git a/backend/api/tests/test_admin.py b/backend/api/tests/test_admin.py index 5863c9d2..6bddb68b 100644 --- a/backend/api/tests/test_admin.py +++ b/backend/api/tests/test_admin.py @@ -5,21 +5,41 @@ from django.urls import reverse from ..models.admin import Admin +from authentication.models import Faculty +def create_faculty(name): + """ + Create a Faculty with the given arguments.""" + return Faculty.objects.create( + name=name + ) -def create_admin(id, first_name, last_name, email): +def create_admin(id, first_name, last_name, email, faculty=None): """ Create a Admin with the given arguments. """ username = f"{first_name}_{last_name}" - return Admin.objects.create( - id=id, - first_name=first_name, - last_name=last_name, - username=username, - email=email, - create_time=timezone.now(), - ) + if faculty is None: + return Admin.objects.create( + id=id, + first_name=first_name, + last_name=last_name, + username=username, + email=email, + create_time=timezone.now() + ) + else: + admin = Admin.objects.create( + id=id, + first_name=first_name, + last_name=last_name, + username=username, + email=email, + create_time=timezone.now(), + ) + for fac in faculty: + admin.faculties.add(fac) + return admin class AdminModelTests(TestCase): @@ -145,3 +165,49 @@ def test_admin_detail_view(self): self.assertEqual(content_json["first_name"], admin.first_name) self.assertEqual(content_json["last_name"], admin.last_name) self.assertEqual(content_json["email"], admin.email) + + def test_admin_faculty(self): + """ + Able to retrieve faculty details of a single admin. + """ + # Create an admin for testing with the name "Bob Peeters" + faculty = create_faculty(name="testing faculty") + admin = create_admin( + id=5, + first_name="Bob", + last_name="Peeters", + email="bob.peeters@example.com", + faculty=[faculty] + ) + + # Make a GET request to retrieve the admin details + response = self.client.get( + reverse("admin-detail", args=[str(admin.id)]), follow=True) + + # Check if the response was successful + self.assertEqual(response.status_code, 200) + + # Assert that the response is JSON + self.assertEqual(response.accepted_media_type, "application/json") + + # Parse the JSON content from the response + content_json = json.loads(response.content.decode("utf-8")) + + # Assert the details of the retrieved admin match the created admin + self.assertEqual(int(content_json["id"]), admin.id) + self.assertEqual(content_json["first_name"], admin.first_name) + self.assertEqual(content_json["last_name"], admin.last_name) + self.assertEqual(content_json["email"], admin.email) + print(content_json["faculties"]) + + response = self.client.get(content_json["faculties"][0], follow=True) + + # Check if the response was successful + self.assertEqual(response.status_code, 200) + + # Assert that the response is JSON + self.assertEqual(response.accepted_media_type, "application/json") + + # Parse the JSON content from the response + content_json = json.loads(response.content.decode("utf-8")) + self.assertEqual(content_json["name"], faculty.name) From 28fa50aea144a01502fcbdd0972b19f7a43dc02a Mon Sep 17 00:00:00 2001 From: Tybo Verslype Date: Sat, 2 Mar 2024 21:07:57 +0100 Subject: [PATCH 2/9] chore: assistant tests endpoints --- backend/api/tests/test_admin.py | 2 + backend/api/tests/test_assistant.py | 181 ++++++++++++++++++++++++++-- 2 files changed, 173 insertions(+), 10 deletions(-) diff --git a/backend/api/tests/test_admin.py b/backend/api/tests/test_admin.py index 6bddb68b..963db1cb 100644 --- a/backend/api/tests/test_admin.py +++ b/backend/api/tests/test_admin.py @@ -7,6 +7,7 @@ from ..models.admin import Admin from authentication.models import Faculty + def create_faculty(name): """ Create a Faculty with the given arguments.""" @@ -14,6 +15,7 @@ def create_faculty(name): name=name ) + def create_admin(id, first_name, last_name, email, faculty=None): """ Create a Admin with the given arguments. diff --git a/backend/api/tests/test_assistant.py b/backend/api/tests/test_assistant.py index 7e9e3701..11cc49e2 100644 --- a/backend/api/tests/test_assistant.py +++ b/backend/api/tests/test_assistant.py @@ -4,21 +4,62 @@ from django.utils import timezone from django.urls import reverse from api.models.assistant import Assistant +from api.models.course import Course +from authentication.models import Faculty -def create_assistant(id, first_name, last_name, email): - # Create an assistant with the given arguments. - username = f"{first_name}_{last_name}" - return Assistant.objects.create( - id=id, - first_name=first_name, - last_name=last_name, - username=username, - email=email, - create_time=timezone.now(), +def create_course(name, academic_startyear, description=None, + parent_course=None): + """ + Create a Course with the given arguments. + """ + return Course.objects.create( + name=name, + academic_startyear=academic_startyear, + description=description, + parent_course=parent_course + ) + + +def create_faculty(name): + """Create a Faculty with the given arguments.""" + return Faculty.objects.create( + name=name ) +def create_assistant( + id, + first_name, + last_name, + email, + faculty=None, + courses=None + ): + """ + Create a assistant with the given arguments. + """ + username = f"{first_name}_{last_name}" + assistant = Assistant.objects.create( + id=id, + first_name=first_name, + last_name=last_name, + username=username, + email=email, + create_time=timezone.now() + ) + + if faculty is not None: + for fac in faculty: + assistant.faculties.add(fac) + + if courses is not None: + for cours in courses: + assistant.courses.add(cours) + + return assistant + + class AssistantModelTests(TestCase): def test_no_assistant(self): """ @@ -151,3 +192,123 @@ def test_assistant_detail_view(self): self.assertEqual(content_json["first_name"], assistant.first_name) self.assertEqual(content_json["last_name"], assistant.last_name) self.assertEqual(content_json["email"], assistant.email) + + def test_assistant_faculty(self): + """ + Able to retrieve faculty details of a single assistant. + """ + # Create an assistant for testing with the name "Bob Peeters" + faculty = create_faculty(name="testing faculty") + assistant = create_assistant( + id=5, + first_name="Bob", + last_name="Peeters", + email="bob.peeters@example.com", + faculty=[faculty] + ) + + # Make a GET request to retrieve the assistant details + response = self.client.get( + reverse("assistant-detail", args=[str(assistant.id)]), follow=True) + + # Check if the response was successful + self.assertEqual(response.status_code, 200) + + # Assert that the response is JSON + self.assertEqual(response.accepted_media_type, "application/json") + + # Parse the JSON content from the response + content_json = json.loads(response.content.decode("utf-8")) + + # Assert the details of the retrieved assistant + # match the created assistant + self.assertEqual(int(content_json["id"]), assistant.id) + self.assertEqual(content_json["first_name"], assistant.first_name) + self.assertEqual(content_json["last_name"], assistant.last_name) + self.assertEqual(content_json["email"], assistant.email) + print(content_json["faculties"]) + + response = self.client.get(content_json["faculties"][0], follow=True) + + # Check if the response was successful + self.assertEqual(response.status_code, 200) + + # Assert that the response is JSON + self.assertEqual(response.accepted_media_type, "application/json") + + # Parse the JSON content from the response + content_json = json.loads(response.content.decode("utf-8")) + self.assertEqual(content_json["name"], faculty.name) + + def test_assistant_courses(self): + """ + Able to retrieve courses details of a single assistant. + """ + # Create an assistant for testing with the name "Bob Peeters" + course1 = create_course( + name="Introduction to Computer Science", + academic_startyear=2022, + description="An introductory course on computer science." + ) + course2 = create_course( + name="Intermediate to Computer Science", + academic_startyear=2023, + description="An second course on computer science." + ) + + assistant = create_assistant( + id=5, + first_name="Bob", + last_name="Peeters", + email="bob.peeters@example.com", + courses=[course1, course2] + ) + + # Make a GET request to retrieve the assistant details + response = self.client.get( + reverse("assistant-detail", args=[str(assistant.id)]), follow=True) + + # Check if the response was successful + self.assertEqual(response.status_code, 200) + + # Assert that the response is JSON + self.assertEqual(response.accepted_media_type, "application/json") + + # Parse the JSON content from the response + content_json = json.loads(response.content.decode("utf-8")) + + # Assert the details of the retrieved assistant + # match the created assistant + self.assertEqual(int(content_json["id"]), assistant.id) + self.assertEqual(content_json["first_name"], assistant.first_name) + self.assertEqual(content_json["last_name"], assistant.last_name) + self.assertEqual(content_json["email"], assistant.email) + print(content_json["faculties"]) + + response = self.client.get(content_json["courses"], follow=True) + + # Check if the response was successful + self.assertEqual(response.status_code, 200) + + # Assert that the response is JSON + self.assertEqual(response.accepted_media_type, "application/json") + + # Parse the JSON content from the response + content_json = json.loads(response.content.decode("utf-8")) + + # Assert that the parsed JSON is a list with multiple assistant + self.assertEqual(len(content_json), 2) + + content = content_json[0] + self.assertEqual(int(content["id"]), course1.id) + self.assertEqual(content["name"], course1.name) + self.assertEqual( + int(content["academic_startyear"]), course1.academic_startyear) + self.assertEqual(content["description"], course1.description) + + content = content_json[1] + self.assertEqual(int(content["id"]), course2.id) + self.assertEqual(content["name"], course2.name) + self.assertEqual( + int(content["academic_startyear"]), course2.academic_startyear) + self.assertEqual(content["description"], course2.description) From f461709576655fe4473c4540b0d5a2d6f01b71cf Mon Sep 17 00:00:00 2001 From: Tybo Verslype Date: Sat, 2 Mar 2024 21:19:27 +0100 Subject: [PATCH 3/9] chore: add student endpoint tests --- backend/api/tests/test_assistant.py | 3 - backend/api/tests/test_student.py | 180 ++++++++++++++++++++++++++-- 2 files changed, 169 insertions(+), 14 deletions(-) diff --git a/backend/api/tests/test_assistant.py b/backend/api/tests/test_assistant.py index 11cc49e2..4e8af6a9 100644 --- a/backend/api/tests/test_assistant.py +++ b/backend/api/tests/test_assistant.py @@ -67,7 +67,6 @@ def test_no_assistant(self): """ response_root = self.client.get(reverse("assistant-list"), follow=True) - # print(response.content) self.assertEqual(response_root.status_code, 200) # Assert that the response is JSON self.assertEqual(response_root.accepted_media_type, "application/json") @@ -226,7 +225,6 @@ def test_assistant_faculty(self): self.assertEqual(content_json["first_name"], assistant.first_name) self.assertEqual(content_json["last_name"], assistant.last_name) self.assertEqual(content_json["email"], assistant.email) - print(content_json["faculties"]) response = self.client.get(content_json["faculties"][0], follow=True) @@ -283,7 +281,6 @@ def test_assistant_courses(self): self.assertEqual(content_json["first_name"], assistant.first_name) self.assertEqual(content_json["last_name"], assistant.last_name) self.assertEqual(content_json["email"], assistant.email) - print(content_json["faculties"]) response = self.client.get(content_json["courses"], follow=True) diff --git a/backend/api/tests/test_student.py b/backend/api/tests/test_student.py index 01620891..df03625d 100644 --- a/backend/api/tests/test_student.py +++ b/backend/api/tests/test_student.py @@ -4,21 +4,62 @@ from django.utils import timezone from django.urls import reverse from ..models.student import Student +from ..models.course import Course +from authentication.models import Faculty -def create_student(id, first_name, last_name, email): - # Create an student with the given arguments. - username = f"{first_name}_{last_name}" - return Student.objects.create( - id=id, - first_name=first_name, - last_name=last_name, - username=username, - email=email, - create_time=timezone.now(), +def create_course(name, academic_startyear, description=None, + parent_course=None): + """ + Create a Course with the given arguments. + """ + return Course.objects.create( + name=name, + academic_startyear=academic_startyear, + description=description, + parent_course=parent_course + ) + + +def create_faculty(name): + """Create a Faculty with the given arguments.""" + return Faculty.objects.create( + name=name ) +def create_student( + id, + first_name, + last_name, + email, + faculty=None, + courses=None + ): + """ + Create a student with the given arguments. + """ + username = f"{first_name}_{last_name}" + student = Student.objects.create( + id=id, + first_name=first_name, + last_name=last_name, + username=username, + email=email, + create_time=timezone.now() + ) + + if faculty is not None: + for fac in faculty: + student.faculties.add(fac) + + if courses is not None: + for cours in courses: + student.courses.add(cours) + + return student + + class StudentModelTests(TestCase): def test_no_student(self): """ @@ -26,7 +67,6 @@ def test_no_student(self): """ response_root = self.client.get(reverse("student-list"), follow=True) - # print(response.content) self.assertEqual(response_root.status_code, 200) # Assert that the response is JSON self.assertEqual(response_root.accepted_media_type, "application/json") @@ -149,3 +189,121 @@ def test_student_detail_view(self): self.assertEqual(content_json["first_name"], student.first_name) self.assertEqual(content_json["last_name"], student.last_name) self.assertEqual(content_json["email"], student.email) + + def test_student_faculty(self): + """ + Able to retrieve faculty details of a single student. + """ + # Create an student for testing with the name "Bob Peeters" + faculty = create_faculty(name="testing faculty") + student = create_student( + id=5, + first_name="Bob", + last_name="Peeters", + email="bob.peeters@example.com", + faculty=[faculty] + ) + + # Make a GET request to retrieve the student details + response = self.client.get( + reverse("student-detail", args=[str(student.id)]), follow=True) + + # Check if the response was successful + self.assertEqual(response.status_code, 200) + + # Assert that the response is JSON + self.assertEqual(response.accepted_media_type, "application/json") + + # Parse the JSON content from the response + content_json = json.loads(response.content.decode("utf-8")) + + # Assert the details of the retrieved student + # match the created student + self.assertEqual(int(content_json["id"]), student.id) + self.assertEqual(content_json["first_name"], student.first_name) + self.assertEqual(content_json["last_name"], student.last_name) + self.assertEqual(content_json["email"], student.email) + + response = self.client.get(content_json["faculties"][0], follow=True) + + # Check if the response was successful + self.assertEqual(response.status_code, 200) + + # Assert that the response is JSON + self.assertEqual(response.accepted_media_type, "application/json") + + # Parse the JSON content from the response + content_json = json.loads(response.content.decode("utf-8")) + self.assertEqual(content_json["name"], faculty.name) + + def test_student_courses(self): + """ + Able to retrieve courses details of a single student. + """ + # Create an student for testing with the name "Bob Peeters" + course1 = create_course( + name="Introduction to Computer Science", + academic_startyear=2022, + description="An introductory course on computer science." + ) + course2 = create_course( + name="Intermediate to Computer Science", + academic_startyear=2023, + description="An second course on computer science." + ) + + student = create_student( + id=5, + first_name="Bob", + last_name="Peeters", + email="bob.peeters@example.com", + courses=[course1, course2] + ) + + # Make a GET request to retrieve the student details + response = self.client.get( + reverse("student-detail", args=[str(student.id)]), follow=True) + + # Check if the response was successful + self.assertEqual(response.status_code, 200) + + # Assert that the response is JSON + self.assertEqual(response.accepted_media_type, "application/json") + + # Parse the JSON content from the response + content_json = json.loads(response.content.decode("utf-8")) + + # Assert the details of the retrieved student + # match the created student + self.assertEqual(int(content_json["id"]), student.id) + self.assertEqual(content_json["first_name"], student.first_name) + self.assertEqual(content_json["last_name"], student.last_name) + self.assertEqual(content_json["email"], student.email) + + response = self.client.get(content_json["courses"], follow=True) + + # Check if the response was successful + self.assertEqual(response.status_code, 200) + + # Assert that the response is JSON + self.assertEqual(response.accepted_media_type, "application/json") + + # Parse the JSON content from the response + content_json = json.loads(response.content.decode("utf-8")) + + # Assert that the parsed JSON is a list with multiple student + self.assertEqual(len(content_json), 2) + + content = content_json[0] + self.assertEqual(int(content["id"]), course1.id) + self.assertEqual(content["name"], course1.name) + self.assertEqual( + int(content["academic_startyear"]), course1.academic_startyear) + self.assertEqual(content["description"], course1.description) + + content = content_json[1] + self.assertEqual(int(content["id"]), course2.id) + self.assertEqual(content["name"], course2.name) + self.assertEqual( + int(content["academic_startyear"]), course2.academic_startyear) + self.assertEqual(content["description"], course2.description) From e948ce1a440baaef74111b0d501aee1f7483c804 Mon Sep 17 00:00:00 2001 From: Tybo Verslype Date: Sat, 2 Mar 2024 21:23:04 +0100 Subject: [PATCH 4/9] chore: add teacher endpoint tests --- backend/api/tests/test_teacher.py | 179 ++++++++++++++++++++++++++++-- 1 file changed, 169 insertions(+), 10 deletions(-) diff --git a/backend/api/tests/test_teacher.py b/backend/api/tests/test_teacher.py index 7e2fdd88..5122ab0b 100644 --- a/backend/api/tests/test_teacher.py +++ b/backend/api/tests/test_teacher.py @@ -4,21 +4,62 @@ from django.utils import timezone from django.urls import reverse from ..models.teacher import Teacher +from ..models.course import Course +from authentication.models import Faculty -def create_teacher(id, first_name, last_name, email): - # Create an Teacher with the given arguments. - username = f"{first_name}_{last_name}" - return Teacher.objects.create( - id=id, - first_name=first_name, - last_name=last_name, - username=username, - email=email, - create_time=timezone.now(), +def create_course(name, academic_startyear, description=None, + parent_course=None): + """ + Create a Course with the given arguments. + """ + return Course.objects.create( + name=name, + academic_startyear=academic_startyear, + description=description, + parent_course=parent_course + ) + + +def create_faculty(name): + """Create a Faculty with the given arguments.""" + return Faculty.objects.create( + name=name ) +def create_teacher( + id, + first_name, + last_name, + email, + faculty=None, + courses=None + ): + """ + Create a teacher with the given arguments. + """ + username = f"{first_name}_{last_name}" + teacher = Teacher.objects.create( + id=id, + first_name=first_name, + last_name=last_name, + username=username, + email=email, + create_time=timezone.now() + ) + + if faculty is not None: + for fac in faculty: + teacher.faculties.add(fac) + + if courses is not None: + for cours in courses: + teacher.courses.add(cours) + + return teacher + + class TeacherModelTests(TestCase): def test_no_teacher(self): """ @@ -148,3 +189,121 @@ def test_teacher_detail_view(self): self.assertEqual(content_json["first_name"], teacher.first_name) self.assertEqual(content_json["last_name"], teacher.last_name) self.assertEqual(content_json["email"], teacher.email) + + def test_teacher_faculty(self): + """ + Able to retrieve faculty details of a single teacher. + """ + # Create an teacher for testing with the name "Bob Peeters" + faculty = create_faculty(name="testing faculty") + teacher = create_teacher( + id=5, + first_name="Bob", + last_name="Peeters", + email="bob.peeters@example.com", + faculty=[faculty] + ) + + # Make a GET request to retrieve the teacher details + response = self.client.get( + reverse("teacher-detail", args=[str(teacher.id)]), follow=True) + + # Check if the response was successful + self.assertEqual(response.status_code, 200) + + # Assert that the response is JSON + self.assertEqual(response.accepted_media_type, "application/json") + + # Parse the JSON content from the response + content_json = json.loads(response.content.decode("utf-8")) + + # Assert the details of the retrieved teacher + # match the created teacher + self.assertEqual(int(content_json["id"]), teacher.id) + self.assertEqual(content_json["first_name"], teacher.first_name) + self.assertEqual(content_json["last_name"], teacher.last_name) + self.assertEqual(content_json["email"], teacher.email) + + response = self.client.get(content_json["faculties"][0], follow=True) + + # Check if the response was successful + self.assertEqual(response.status_code, 200) + + # Assert that the response is JSON + self.assertEqual(response.accepted_media_type, "application/json") + + # Parse the JSON content from the response + content_json = json.loads(response.content.decode("utf-8")) + self.assertEqual(content_json["name"], faculty.name) + + def test_teacher_courses(self): + """ + Able to retrieve courses details of a single teacher. + """ + # Create an teacher for testing with the name "Bob Peeters" + course1 = create_course( + name="Introduction to Computer Science", + academic_startyear=2022, + description="An introductory course on computer science." + ) + course2 = create_course( + name="Intermediate to Computer Science", + academic_startyear=2023, + description="An second course on computer science." + ) + + teacher = create_teacher( + id=5, + first_name="Bob", + last_name="Peeters", + email="bob.peeters@example.com", + courses=[course1, course2] + ) + + # Make a GET request to retrieve the teacher details + response = self.client.get( + reverse("teacher-detail", args=[str(teacher.id)]), follow=True) + + # Check if the response was successful + self.assertEqual(response.status_code, 200) + + # Assert that the response is JSON + self.assertEqual(response.accepted_media_type, "application/json") + + # Parse the JSON content from the response + content_json = json.loads(response.content.decode("utf-8")) + + # Assert the details of the retrieved teacher + # match the created teacher + self.assertEqual(int(content_json["id"]), teacher.id) + self.assertEqual(content_json["first_name"], teacher.first_name) + self.assertEqual(content_json["last_name"], teacher.last_name) + self.assertEqual(content_json["email"], teacher.email) + + response = self.client.get(content_json["courses"], follow=True) + + # Check if the response was successful + self.assertEqual(response.status_code, 200) + + # Assert that the response is JSON + self.assertEqual(response.accepted_media_type, "application/json") + + # Parse the JSON content from the response + content_json = json.loads(response.content.decode("utf-8")) + + # Assert that the parsed JSON is a list with multiple teacher + self.assertEqual(len(content_json), 2) + + content = content_json[0] + self.assertEqual(int(content["id"]), course1.id) + self.assertEqual(content["name"], course1.name) + self.assertEqual( + int(content["academic_startyear"]), course1.academic_startyear) + self.assertEqual(content["description"], course1.description) + + content = content_json[1] + self.assertEqual(int(content["id"]), course2.id) + self.assertEqual(content["name"], course2.name) + self.assertEqual( + int(content["academic_startyear"]), course2.academic_startyear) + self.assertEqual(content["description"], course2.description) From 5a6bfec9e80890268e79186e955abd181d311605 Mon Sep 17 00:00:00 2001 From: Tybo Verslype Date: Sat, 2 Mar 2024 22:17:25 +0100 Subject: [PATCH 5/9] chore: add course endpoints tests --- backend/api/tests/test_course.py | 353 +++++++++++++++++++++++++++++++ 1 file changed, 353 insertions(+) diff --git a/backend/api/tests/test_course.py b/backend/api/tests/test_course.py index 21ff40b7..df05eb90 100644 --- a/backend/api/tests/test_course.py +++ b/backend/api/tests/test_course.py @@ -1,8 +1,97 @@ import json from django.test import TestCase +from django.utils import timezone from django.urls import reverse from ..models.course import Course +from ..models.teacher import Teacher +from ..models.assistant import Assistant +from ..models.student import Student +from ..models.project import Project + + +def create_project( + name, + description, + visible, + archived, + days, + course +): + """Create a Project with the given arguments.""" + deadline = timezone.now() + timezone.timedelta(days=days) + + return Project.objects.create( + name=name, + description=description, + visible=visible, + archived=archived, + deadline=deadline, + course=course + ) + + +def create_student( + id, + first_name, + last_name, + email + ): + """ + Create a student with the given arguments. + """ + username = f"{first_name}_{last_name}" + student = Student.objects.create( + id=id, + first_name=first_name, + last_name=last_name, + username=username, + email=email, + create_time=timezone.now() + ) + return student + + +def create_assistant( + id, + first_name, + last_name, + email + ): + """ + Create a assistant with the given arguments. + """ + username = f"{first_name}_{last_name}" + assistant = Assistant.objects.create( + id=id, + first_name=first_name, + last_name=last_name, + username=username, + email=email, + create_time=timezone.now() + ) + return assistant + + +def create_teacher( + id, + first_name, + last_name, + email + ): + """ + Create a teacher with the given arguments. + """ + username = f"{first_name}_{last_name}" + teacher = Teacher.objects.create( + id=id, + first_name=first_name, + last_name=last_name, + username=username, + email=email, + create_time=timezone.now() + ) + return teacher def create_course(name, academic_startyear, description=None, @@ -113,3 +202,267 @@ def test_course_detail_view(self): self.assertEqual( content_json["academic_startyear"], course.academic_startyear) self.assertEqual(content_json["description"], course.description) + + def test_course_teachers(self): + """ + Able to retrieve teachers details of a single course. + """ + teacher1 = create_teacher( + id=5, + first_name="Simon", + last_name="Mignolet", + email="simon.mignolet@ugent.be" + ) + + teacher2 = create_teacher( + id=6, + first_name="Ronny", + last_name="Deila", + email="ronny.deila@brugge.be" + ) + + course = create_course( + name="Chemistry 101", + academic_startyear=2022, + description="An introductory chemistry course." + ) + course.teachers.add(teacher1) + course.teachers.add(teacher2) + + response = self.client.get( + reverse("course-detail", args=[str(course.id)]), follow=True) + + self.assertEqual(response.status_code, 200) + self.assertEqual(response.accepted_media_type, "application/json") + + content_json = json.loads(response.content.decode("utf-8")) + + self.assertEqual(content_json["name"], course.name) + self.assertEqual( + content_json["academic_startyear"], course.academic_startyear) + self.assertEqual(content_json["description"], course.description) + + response = self.client.get(content_json["teachers"], follow=True) + + # Check if the response was successful + self.assertEqual(response.status_code, 200) + + # Assert that the response is JSON + self.assertEqual(response.accepted_media_type, "application/json") + + # Parse the JSON content from the response + content_json = json.loads(response.content.decode("utf-8")) + + # Assert that the parsed JSON is a list with multiple teachers + self.assertEqual(len(content_json), 2) + + content = content_json[0] + self.assertEqual(int(content["id"]), teacher1.id) + self.assertEqual(content["first_name"], teacher1.first_name) + self.assertEqual(content["last_name"], teacher1.last_name) + self.assertEqual(content["email"], teacher1.email) + + content = content_json[1] + self.assertEqual(int(content["id"]), teacher2.id) + self.assertEqual(content["first_name"], teacher2.first_name) + self.assertEqual(content["last_name"], teacher2.last_name) + self.assertEqual(content["email"], teacher2.email) + + def test_course_assistant(self): + """ + Able to retrieve assistant details of a single course. + """ + assistant1 = create_assistant( + id=5, + first_name="Simon", + last_name="Mignolet", + email="simon.mignolet@ugent.be" + ) + + assistant2 = create_assistant( + id=6, + first_name="Ronny", + last_name="Deila", + email="ronny.deila@brugge.be" + ) + + course = create_course( + name="Chemistry 101", + academic_startyear=2022, + description="An introductory chemistry course." + ) + course.assistants.add(assistant1) + course.assistants.add(assistant2) + + response = self.client.get( + reverse("course-detail", args=[str(course.id)]), follow=True) + + self.assertEqual(response.status_code, 200) + self.assertEqual(response.accepted_media_type, "application/json") + + content_json = json.loads(response.content.decode("utf-8")) + + self.assertEqual(content_json["name"], course.name) + self.assertEqual( + content_json["academic_startyear"], course.academic_startyear) + self.assertEqual(content_json["description"], course.description) + + response = self.client.get(content_json["assistants"], follow=True) + + # Check if the response was successful + self.assertEqual(response.status_code, 200) + + # Assert that the response is JSON + self.assertEqual(response.accepted_media_type, "application/json") + + # Parse the JSON content from the response + content_json = json.loads(response.content.decode("utf-8")) + + # Assert that the parsed JSON is a list with multiple teachers + self.assertEqual(len(content_json), 2) + + content = content_json[0] + self.assertEqual(int(content["id"]), assistant1.id) + self.assertEqual(content["first_name"], assistant1.first_name) + self.assertEqual(content["last_name"], assistant1.last_name) + self.assertEqual(content["email"], assistant1.email) + + content = content_json[1] + self.assertEqual(int(content["id"]), assistant2.id) + self.assertEqual(content["first_name"], assistant2.first_name) + self.assertEqual(content["last_name"], assistant2.last_name) + self.assertEqual(content["email"], assistant2.email) + + def test_course_student(self): + """ + Able to retrieve student details of a single course. + """ + student1 = create_student( + id=5, + first_name="Simon", + last_name="Mignolet", + email="simon.mignolet@ugent.be" + ) + + student2 = create_student( + id=6, + first_name="Ronny", + last_name="Deila", + email="ronny.deila@brugge.be" + ) + + course = create_course( + name="Chemistry 101", + academic_startyear=2022, + description="An introductory chemistry course." + ) + course.students.add(student1) + course.students.add(student2) + + response = self.client.get( + reverse("course-detail", args=[str(course.id)]), follow=True) + + self.assertEqual(response.status_code, 200) + self.assertEqual(response.accepted_media_type, "application/json") + + content_json = json.loads(response.content.decode("utf-8")) + + self.assertEqual(content_json["name"], course.name) + self.assertEqual( + content_json["academic_startyear"], course.academic_startyear) + self.assertEqual(content_json["description"], course.description) + + response = self.client.get(content_json["students"], follow=True) + + # Check if the response was successful + self.assertEqual(response.status_code, 200) + + # Assert that the response is JSON + self.assertEqual(response.accepted_media_type, "application/json") + + # Parse the JSON content from the response + content_json = json.loads(response.content.decode("utf-8")) + + # Assert that the parsed JSON is a list with multiple student + self.assertEqual(len(content_json), 2) + + content = content_json[0] + self.assertEqual(int(content["id"]), student1.id) + self.assertEqual(content["first_name"], student1.first_name) + self.assertEqual(content["last_name"], student1.last_name) + self.assertEqual(content["email"], student1.email) + + content = content_json[1] + self.assertEqual(int(content["id"]), student2.id) + self.assertEqual(content["first_name"], student2.first_name) + self.assertEqual(content["last_name"], student2.last_name) + self.assertEqual(content["email"], student2.email) + + def test_course_project(self): + """ + Able to retrieve project details of a single course. + """ + course = create_course( + name="Chemistry 101", + academic_startyear=2022, + description="An introductory chemistry course." + ) + + project1 = create_project( + name="become champions", + description="win the jpl", + visible=True, + archived=False, + days=50, + course=course + ) + + project2 = create_project( + name="become european champion", + description="win the cfl", + visible=True, + archived=False, + days=50, + course=course + ) + + response = self.client.get( + reverse("course-detail", args=[str(course.id)]), follow=True) + + self.assertEqual(response.status_code, 200) + self.assertEqual(response.accepted_media_type, "application/json") + + content_json = json.loads(response.content.decode("utf-8")) + + self.assertEqual(content_json["name"], course.name) + self.assertEqual( + content_json["academic_startyear"], course.academic_startyear) + self.assertEqual(content_json["description"], course.description) + + response = self.client.get(content_json["projects"], follow=True) + + # Check if the response was successful + self.assertEqual(response.status_code, 200) + + # Assert that the response is JSON + self.assertEqual(response.accepted_media_type, "application/json") + + # Parse the JSON content from the response + content_json = json.loads(response.content.decode("utf-8")) + + # Assert that the parsed JSON is a list with multiple projects + self.assertEqual(len(content_json), 2) + + content = content_json[0] + self.assertEqual(int(content["id"]), project1.id) + self.assertEqual(content["name"], project1.name) + self.assertEqual(content["description"], project1.description) + self.assertEqual(content["visible"], project1.visible) + self.assertEqual(content["archived"], project1.archived) + + content = content_json[1] + self.assertEqual(int(content["id"]), project2.id) + self.assertEqual(content["name"], project2.name) + self.assertEqual(content["description"], project2.description) + self.assertEqual(content["visible"], project2.visible) + self.assertEqual(content["archived"], project2.archived) From 84520f9a8fcfea66bf050738418c0ea2ee4c136a Mon Sep 17 00:00:00 2001 From: Tybo Verslype Date: Sat, 2 Mar 2024 22:30:09 +0100 Subject: [PATCH 6/9] chore: add endpoint tests submision --- backend/api/tests/test_submision.py | 60 +++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/backend/api/tests/test_submision.py b/backend/api/tests/test_submision.py index ea8e630f..07e904c6 100644 --- a/backend/api/tests/test_submision.py +++ b/backend/api/tests/test_submision.py @@ -226,3 +226,63 @@ def test_submission_detail_view(self): submission.submission_number ) self.assertEqual(retrieved_submission["group"], expected_group_url) + + def test_submission_group(self): + """ + Able to retrieve group of a single submission. + """ + course = create_course( + name="sel2", + academic_startyear=2023 + ) + project = create_project( + name="Project 1", + description="Description 1", + days=7, + course=course + ) + group = create_group(project=project, score=10) + submission = create_submission( + group=group, submission_number=1 + ) + + # Make a GET request to retrieve the submission + response = self.client.get( + reverse("submission-detail", args=[str(submission.id)]), + follow=True) + + # Check if the response was successful + self.assertEqual(response.status_code, 200) + + # Assert that the response is JSON + self.assertEqual(response.accepted_media_type, "application/json") + + # Parse the JSON content from the response + content_json = json.loads(response.content.decode("utf-8")) + + # Assert the details of the retrieved submission + # match the created submission + retrieved_submission = content_json + self.assertEqual(int(retrieved_submission["id"]), submission.id) + self.assertEqual( + int(retrieved_submission["submission_number"]), + submission.submission_number + ) + + response = self.client.get(content_json["group"], follow=True) + + # Check if the response was successful + self.assertEqual(response.status_code, 200) + + # Assert that the response is JSON + self.assertEqual(response.accepted_media_type, "application/json") + + # Parse the JSON content from the response + content_json = json.loads(response.content.decode("utf-8")) + + expected_project_url = "http://testserver" + reverse( + "project-detail", args=[str(project.id)]) + + self.assertEqual(int(content_json["id"]), group.id) + self.assertEqual(content_json["project"], expected_project_url) + self.assertEqual(content_json["score"], group.score) From 6d9a54eb0dbb9d333a295d37e69920c0185d59e8 Mon Sep 17 00:00:00 2001 From: Tybo Verslype Date: Sat, 2 Mar 2024 22:48:21 +0100 Subject: [PATCH 7/9] chore: add group api endpoint --- backend/api/tests/test_group.py | 115 ++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) diff --git a/backend/api/tests/test_group.py b/backend/api/tests/test_group.py index c8767ff2..e0320a44 100644 --- a/backend/api/tests/test_group.py +++ b/backend/api/tests/test_group.py @@ -190,3 +190,118 @@ def test_group_detail_view(self): self.assertEqual(int(content_json["id"]), group.id) self.assertEqual(content_json["project"], expected_project_url) self.assertEqual(content_json["score"], group.score) + + def test_group_project(self): + """Able to retrieve details of a single group.""" + course = create_course( + name="sel2", + academic_startyear=2023 + ) + + project = create_project( + name="Project 1", + description="Description 1", + days=7, course=course + ) + student = create_student( + id=5, + first_name="John", + last_name="Doe", + + email="john.doe@example.com") + + group = create_group(project=project, score=10) + group.students.add(student) + + response = self.client.get( + reverse("group-detail", args=[str(group.id)]), follow=True) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.accepted_media_type, "application/json") + + content_json = json.loads(response.content.decode("utf-8")) + + self.assertEqual(int(content_json["id"]), group.id) + self.assertEqual(content_json["score"], group.score) + + response = self.client.get(content_json["project"], follow=True) + + # Check if the response was successful + self.assertEqual(response.status_code, 200) + + # Assert that the response is JSON + self.assertEqual(response.accepted_media_type, "application/json") + + # Parse the JSON content from the response + content_json = json.loads(response.content.decode("utf-8")) + + expected_course_url = "http://testserver" + reverse( + "course-detail", args=[str(course.id)] + ) + + self.assertEqual(content_json["name"], project.name) + self.assertEqual(content_json["description"], project.description) + self.assertEqual(content_json["visible"], project.visible) + self.assertEqual(content_json["archived"], project.archived) + self.assertEqual(content_json["course"], expected_course_url) + + def test_group_students(self): + """Able to retrieve students details of a group.""" + course = create_course( + name="sel2", + academic_startyear=2023 + ) + + project = create_project( + name="Project 1", + description="Description 1", + days=7, course=course + ) + student1 = create_student( + id=5, + first_name="John", + last_name="Doe", + email="john.doe@example.com") + + student2 = create_student( + id=6, + first_name="kom", + last_name="mor_up", + email="kom.mor_up@example.com") + + group = create_group(project=project, score=10) + group.students.add(student1) + group.students.add(student2) + + response = self.client.get( + reverse("group-detail", args=[str(group.id)]), follow=True) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.accepted_media_type, "application/json") + + content_json = json.loads(response.content.decode("utf-8")) + + self.assertEqual(int(content_json["id"]), group.id) + self.assertEqual(content_json["score"], group.score) + + response = self.client.get(content_json["students"], follow=True) + + # Check if the response was successful + self.assertEqual(response.status_code, 200) + + # Assert that the response is JSON + self.assertEqual(response.accepted_media_type, "application/json") + + # Parse the JSON content from the response + content_json = json.loads(response.content.decode("utf-8")) + self.assertEqual(len(content_json), 2) + + content = content_json[0] + self.assertEqual(int(content["id"]), student1.id) + self.assertEqual(content["first_name"], student1.first_name) + self.assertEqual(content["last_name"], student1.last_name) + self.assertEqual(content["email"], student1.email) + + content = content_json[1] + self.assertEqual(int(content["id"]), student2.id) + self.assertEqual(content["first_name"], student2.first_name) + self.assertEqual(content["last_name"], student2.last_name) + self.assertEqual(content["email"], student2.email) From c09f5e0e15af40bd203c32652ad23d354574542b Mon Sep 17 00:00:00 2001 From: Tybo Verslype Date: Sat, 2 Mar 2024 23:11:20 +0100 Subject: [PATCH 8/9] chore: add api tests project endpoints --- backend/api/tests/test_project.py | 176 +++++++++++++++++++++++++++++- 1 file changed, 173 insertions(+), 3 deletions(-) diff --git a/backend/api/tests/test_project.py b/backend/api/tests/test_project.py index af7fb11a..b4e4058d 100644 --- a/backend/api/tests/test_project.py +++ b/backend/api/tests/test_project.py @@ -4,7 +4,7 @@ from django.urls import reverse from ..models.project import Project from ..models.course import Course -from ..models.checks import Checks +from ..models.checks import Checks, FileExtension def create_course(id, name, academic_startyear): @@ -15,9 +15,35 @@ def create_course(id, name, academic_startyear): id=id, name=name, academic_startyear=academic_startyear) -def create_checks(): +def create_fileExtension(id, extension): + """ + Create a FileExtension with the given arguments. + """ + return FileExtension.objects.create( + id=id, + extension=extension + ) + + +def create_checks( + id=None, + allowed_file_extensions=None, + forbidden_file_extensions=None): """Create a Checks with the given arguments.""" - return Checks.objects.create() + if id is None and allowed_file_extensions is None: + # extra if to make line shorter + if forbidden_file_extensions is None: + return Checks.objects.create() + + check = Checks.objects.create( + id=id, + ) + + for ext in allowed_file_extensions: + check.allowed_file_extensions.add(ext) + for ext in forbidden_file_extensions: + check.forbidden_file_extensions.add(ext) + return check def create_project( @@ -277,3 +303,147 @@ def test_multiple_project(self): self.assertEqual(retrieved_project["archived"], project2.archived) self.assertEqual(retrieved_project["checks"], expected_checks_url) self.assertEqual(retrieved_project["course"], expected_course_url) + + def test_project_course(self): + """ + Able to retrieve a course of a project after creating it. + """ + + course = create_course( + id=3, + name="test course", + academic_startyear=2024 + ) + checks = create_checks() + project = create_project( + name="test project", + description="test description", + visible=True, + archived=False, + days=7, + checks=checks, + course=course + ) + + response = self.client.get(reverse("project-list"), follow=True) + + self.assertEqual(response.status_code, 200) + self.assertEqual(response.accepted_media_type, "application/json") + + content_json = json.loads(response.content.decode("utf-8")) + + self.assertEqual(len(content_json), 1) + + retrieved_project = content_json[0] + + expected_checks_url = "http://testserver" + reverse( + "check-detail", args=[str(checks.id)] + ) + + self.assertEqual(retrieved_project["name"], project.name) + self.assertEqual(retrieved_project["description"], project.description) + self.assertEqual(retrieved_project["visible"], project.visible) + self.assertEqual(retrieved_project["archived"], project.archived) + self.assertEqual(retrieved_project["checks"], expected_checks_url) + + response = self.client.get(retrieved_project["course"], follow=True) + + # Check if the response was successful + self.assertEqual(response.status_code, 200) + + # Assert that the response is JSON + self.assertEqual(response.accepted_media_type, "application/json") + + # Parse the JSON content from the response + content_json = json.loads(response.content.decode("utf-8")) + + self.assertEqual(content_json["name"], course.name) + self.assertEqual( + content_json["academic_startyear"], course.academic_startyear) + self.assertEqual(content_json["description"], course.description) + + def test_project_checks(self): + """ + Able to retrieve a check of a project after creating it. + """ + + course = create_course( + id=3, + name="test course", + academic_startyear=2024 + ) + fileExtension1 = create_fileExtension(id=1, extension="jpg") + fileExtension2 = create_fileExtension(id=2, extension="png") + fileExtension3 = create_fileExtension(id=3, extension="tar") + fileExtension4 = create_fileExtension(id=4, extension="wfp") + checks = create_checks( + id=5, + allowed_file_extensions=[fileExtension1, fileExtension4], + forbidden_file_extensions=[fileExtension2, fileExtension3] + ) + project = create_project( + name="test project", + description="test description", + visible=True, + archived=False, + days=7, + checks=checks, + course=course + ) + + response = self.client.get(reverse("project-list"), follow=True) + + self.assertEqual(response.status_code, 200) + self.assertEqual(response.accepted_media_type, "application/json") + + content_json = json.loads(response.content.decode("utf-8")) + + self.assertEqual(len(content_json), 1) + + retrieved_project = content_json[0] + + expected_course_url = "http://testserver" + reverse( + "course-detail", args=[str(course.id)] + ) + + self.assertEqual(retrieved_project["name"], project.name) + self.assertEqual(retrieved_project["description"], project.description) + self.assertEqual(retrieved_project["visible"], project.visible) + self.assertEqual(retrieved_project["archived"], project.archived) + self.assertEqual(retrieved_project["course"], expected_course_url) + + response = self.client.get(retrieved_project["checks"], follow=True) + + # Check if the response was successful + self.assertEqual(response.status_code, 200) + + # Assert that the response is JSON + self.assertEqual(response.accepted_media_type, "application/json") + + # Parse the JSON content from the response + content_json = json.loads(response.content.decode("utf-8")) + + self.assertEqual(int(content_json["id"]), checks.id) + + # Assert the file extensions of the retrieved + # Checks match the created file extensions + retrieved_allowed_file_extensions = content_json[ + "allowed_file_extensions"] + + self.assertEqual(len(retrieved_allowed_file_extensions), 2) + self.assertEqual( + retrieved_allowed_file_extensions[0]["extension"], + fileExtension1.extension) + self.assertEqual( + retrieved_allowed_file_extensions[1]["extension"], + fileExtension4.extension) + + retrieved_forbidden_file_extensions = content_json[ + "forbidden_file_extensions"] + self.assertEqual(len(retrieved_forbidden_file_extensions), 2) + self.assertEqual( + retrieved_forbidden_file_extensions[0]["extension"], + fileExtension2.extension) + self.assertEqual( + retrieved_forbidden_file_extensions[1]["extension"], + fileExtension3.extension) From ebed9ed03903a62e79695d726fe686810e0f3475 Mon Sep 17 00:00:00 2001 From: Tybo Verslype Date: Sat, 2 Mar 2024 23:36:06 +0100 Subject: [PATCH 9/9] fix: removed prints --- backend/api/tests/test_admin.py | 1 - backend/api/tests/test_checks.py | 1 - backend/api/tests/test_submision.py | 1 - backend/api/tests/test_teacher.py | 1 - 4 files changed, 4 deletions(-) diff --git a/backend/api/tests/test_admin.py b/backend/api/tests/test_admin.py index 963db1cb..292e1f21 100644 --- a/backend/api/tests/test_admin.py +++ b/backend/api/tests/test_admin.py @@ -200,7 +200,6 @@ def test_admin_faculty(self): self.assertEqual(content_json["first_name"], admin.first_name) self.assertEqual(content_json["last_name"], admin.last_name) self.assertEqual(content_json["email"], admin.email) - print(content_json["faculties"]) response = self.client.get(content_json["faculties"][0], follow=True) diff --git a/backend/api/tests/test_checks.py b/backend/api/tests/test_checks.py index 1eb1ce75..e4247956 100644 --- a/backend/api/tests/test_checks.py +++ b/backend/api/tests/test_checks.py @@ -36,7 +36,6 @@ def test_no_fileExtension(self): """ response_root = self.client.get( reverse("fileExtension-list"), follow=True) - # print(response.content) self.assertEqual(response_root.status_code, 200) # Assert that the response is JSON self.assertEqual(response_root.accepted_media_type, "application/json") diff --git a/backend/api/tests/test_submision.py b/backend/api/tests/test_submision.py index 07e904c6..119415ef 100644 --- a/backend/api/tests/test_submision.py +++ b/backend/api/tests/test_submision.py @@ -63,7 +63,6 @@ def test_no_submission(self): response_root = self.client.get( reverse("submission-list"), follow=True) - # print(response.content) self.assertEqual(response_root.status_code, 200) # Assert that the response is JSON self.assertEqual(response_root.accepted_media_type, "application/json") diff --git a/backend/api/tests/test_teacher.py b/backend/api/tests/test_teacher.py index 5122ab0b..2e3d8c6e 100644 --- a/backend/api/tests/test_teacher.py +++ b/backend/api/tests/test_teacher.py @@ -67,7 +67,6 @@ def test_no_teacher(self): """ response_root = self.client.get(reverse("teacher-list"), follow=True) - # print(response.content) self.assertEqual(response_root.status_code, 200) # Assert that the response is JSON self.assertEqual(response_root.accepted_media_type, "application/json")